<commit_before>from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import pre_save
from django.utils.functional import curry
from awx.main.models.activity_stream import ActivityStream


class ActivityStreamMiddleware(object):

    def process_request(self, request):
        if hasattr(request, 'user') and hasattr(request.user, 'is_authenticated') and request.user.is_authenticated():
            user = request.user
        else:
            user = None

        set_actor = curry(self.set_actor, user)
        pre_save.connect(set_actor, sender=ActivityStream, dispatch_uid=(self.__class__, request), weak=False)

    def process_response(self, request, response):
        pre_save.disconnect(dispatch_uid=(self.__class__, request))
        return response

    def set_actor(self, user, sender, instance, **kwargs):
        if sender == ActivityStream and isinstance(user, User) and instance.user is None:
            instance.user = user<commit_msg>Use process_response to attach user information later in the middleware request processing<commit_after>from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import pre_save
from django.utils.functional import curry
from awx.main.models.activity_stream import ActivityStream
import json
import urllib2

class ActivityStreamMiddleware(object):

    def process_request(self, request):
        self.isActivityStreamEvent = False
        if hasattr(request, 'user') and hasattr(request.user, 'is_authenticated') and request.user.is_authenticated():
            user = request.user
        else:
            user = None

        set_actor = curry(self.set_actor, user)
        pre_save.connect(set_actor, sender=ActivityStream, dispatch_uid=(self.__class__, request), weak=False)

    def process_response(self, request, response):
        pre_save.disconnect(dispatch_uid=(self.__class__, request))
        if self.isActivityStreamEvent:
            if "current_user" in request.COOKIES and "id" in request.COOKIES["current_user"]:
                userInfo = json.loads(urllib2.unquote(request.COOKIES['current_user']).decode('utf8'))
                userActual = User.objects.get(id=int(userInfo['id']))
                self.instance.user = userActual
                self.instance.save()
        return response

    def set_actor(self, user, sender, instance, **kwargs):
        if sender == ActivityStream:
            if isinstance(user, User) and instance.user is None:
                instance.user = user
            else:
                self.isActivityStreamEvent = True
                self.instance = instance
        else:
                self.isActivityStreamEvent = False
{'fraction_non_alphanumeric': 0.08816029143897997, 'fraction_numerical': 0.001092896174863388, 'mean_word_length': 3.1606060606060606, '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': '3356469', 'n_tokens_mistral': 738, 'n_tokens_neox': 713, 'n_words': 185}