<commit_before>from django.core.exceptions import ObjectDoesNotExist
from django.utils.functional import SimpleLazyObject
from django.conf import settings

from partner.models import PartnerMember


def get_partner_object(request):
    partner_id = request.META.get('HTTP_PARTNER_ID', None)
    partner = None
    partner_member = None

    if request.user.is_authenticated():
        if partner_id:
            partner_member = PartnerMember.objects.filter(user=request.user, partner_id=partner_id).first()

        # TODO: remove when we finish whole logic for http headers (like: HTTP_ACTIVE_PARTNER)
        if not partner_member and settings.IS_DEV:
            partner_member = request.user.partner_members.first()

    if partner_member:
        partner = partner_member.partner

    return partner, partner_member


class ActivePartnerMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        request.active_partner, request.partner_member = SimpleLazyObject(lambda: get_partner_object(request))
        response = self.get_response(request)
        return response


def get_office_member_object(request):
    if request.user.is_authenticated():
        agency_members = request.user.agency_members.first()
        if not agency_members:
            return None

        office_id = request.META.get('HTTP_AGENCY_OFFICE_ID', None)

        if office_id:
            try:
                return request.user.agency_members.filter(office_id=office_id)
            except ObjectDoesNotExist:
                return None

        if settings.IS_DEV:
            # TODO: Remove once header is added on the frontend
            return request.user.agency_members.first()
    return None


class ActiveAgencyOfficeMiddleware(object):

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        request.agency_member = SimpleLazyObject(lambda: get_office_member_object(request))
        response = self.get_response(request)
        return response<commit_msg>Remove dev fallbacks, fix office retrieval<commit_after>from django.core.exceptions import ObjectDoesNotExist
from django.utils.functional import SimpleLazyObject
from django.conf import settings

from partner.models import PartnerMember


def get_partner_object(request):
    partner_id = request.META.get('HTTP_PARTNER_ID', None)
    partner = None
    partner_member = None

    if request.user.is_authenticated():
        if partner_id:
            partner_member = PartnerMember.objects.filter(user=request.user, partner_id=partner_id).first()

    if partner_member:
        partner = partner_member.partner

    return partner, partner_member


class ActivePartnerMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        request.active_partner, request.partner_member = SimpleLazyObject(lambda: get_partner_object(request))
        response = self.get_response(request)
        return response


def get_office_member_object(request):
    if request.user.is_authenticated():
        agency_members = request.user.agency_members.first()
        if not agency_members:
            return None

        office_id = request.META.get('HTTP_AGENCY_OFFICE_ID', None)

        if office_id:
            try:
                return request.user.agency_members.get(office_id=office_id)
            except ObjectDoesNotExist:
                return None

    return None


class ActiveAgencyOfficeMiddleware(object):

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        request.agency_member = SimpleLazyObject(lambda: get_office_member_object(request))
        response = self.get_response(request)
        return response
{'fraction_non_alphanumeric': 0.07091284167096441, 'fraction_numerical': 0.0, 'mean_word_length': 3.2532894736842106, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2482029', 'n_tokens_mistral': 1100, 'n_tokens_neox': 1060, 'n_words': 234}