<commit_before>from .util import get_user, logout


class AuthenticationMiddleware(object):
    def process_request(self, request):
        # @@@ should not be persisting plaintext pw in session, but
        #     currently API doesn't allow any other technique
        userid = request.session.get("userid")
        password = request.session.get("password")

        request.auth = None
        request.user = None
        if userid and password:
            user = get_user(userid, password)
            if user:
                request.user = user
                request.auth = user.auth
            else:
                logout(request)
<commit_msg>Made request.user and request.auth lazy.<commit_after>from .util import get_user, logout



def get_user_from_request(request):
    # @@@ should not be persisting plaintext pw in session, but
    #     currently API doesn't allow any other technique
    userid = request.session.get("userid")
    password = request.session.get("password")

    user = None
    if userid and password:
        user = get_user(userid, password)
        if not user:
            logout(request)
    return user



class LazyUser(object):
    def __get__(self, request, obj_type=None):
        if not hasattr(request, '_cached_user'):
            request._cached_user = get_user_from_request(request)
        return request._cached_user



class LazyAuth(object):
    def __get__(self, request, obj_type=None):
        if not hasattr(request, '_cached_auth'):
            request._cached_auth=None
            if request.user:
                request._cached_auth = request.user.auth
        return request._cached_auth



class AuthenticationMiddleware(object):
    def process_request(self, request):
        request.__class__.user = LazyUser()
        request.__class__.auth = LazyAuth()

{'fraction_non_alphanumeric': 0.08378970427163199, 'fraction_numerical': 0.0, 'mean_word_length': 2.4933078393881454, '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': '6512794', 'n_tokens_mistral': 505, 'n_tokens_neox': 488, 'n_words': 135}