<commit_before>'''
Session-related middleware
'''

from datetime import datetime
from django.conf import settings
from cog.models import update_user_projects, update_user_tags, isUserRemote
from cog.views.utils import set_openid_cookie
from django.core.exceptions import ObjectDoesNotExist

class SessionMiddleware(object):
    
    def process_request(self, request):
        '''
        Method called before processing of the view.
        Used to periodically update the user's projects.
        '''
        
        # must not use shared anonymous session
        try:
            if request.user.is_authenticated() and request.user.profile.openid() is not None:
            
                s = request.session
                last_accessed_seconds = s.get('LAST_ACCESSED', 0) # defaults to Unix Epoch
                now_seconds = int(datetime.now().strftime("%s"))
                
                if now_seconds - last_accessed_seconds > settings.MY_PROJECTS_REFRESH_SECONDS:
                    
                    # update the user's projects across the federation                
                    update_user_projects(request.user)
                    
                    # update the user tags from their home site
                    if isUserRemote(request.user):
                        update_user_tags(request.user)
                    
                    # refresh session stamp
                    s['LAST_ACCESSED'] = now_seconds
                    s.save()
                    
        except ObjectDoesNotExist:
            pass # no profile
                
        # keep on processing this request
        return None<commit_msg>Updating session stamp before querying user projects.<commit_after>'''
Session-related middleware
'''

from datetime import datetime
from django.conf import settings
from cog.models import update_user_projects, update_user_tags, isUserRemote
from cog.views.utils import set_openid_cookie
from django.core.exceptions import ObjectDoesNotExist

class SessionMiddleware(object):
    
    def process_request(self, request):
        '''
        Method called before processing of the view.
        Used to periodically update the user's projects.
        '''
        
        # must not use shared anonymous session
        try:
            if request.user.is_authenticated() and request.user.profile.openid() is not None:
            
                s = request.session
                last_accessed_seconds = s.get('LAST_ACCESSED', 0) # defaults to Unix Epoch
                now_seconds = int(datetime.now().strftime("%s"))
                
                if now_seconds - last_accessed_seconds > settings.MY_PROJECTS_REFRESH_SECONDS:
                    
                    # update session stamp BEFORE querying remote sites
                    s['LAST_ACCESSED'] = now_seconds
                    s.save()
                    
                    # update the user's projects across the federation                
                    update_user_projects(request.user)
                    
                    # update the user tags from their home site
                    if isUserRemote(request.user):
                        update_user_tags(request.user)
                    
        except ObjectDoesNotExist:
            pass # no profile
                
        # keep on processing this request
        return None
{'fraction_non_alphanumeric': 0.05752212389380531, 'fraction_numerical': 0.0005899705014749262, 'mean_word_length': 1.6246130030959753, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2747047', 'n_tokens_mistral': 815, 'n_tokens_neox': 766, 'n_words': 229}