1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-02 09:51:09 +03:00

Make sure we are handling multiple activitystream instances from the middleware

This commit is contained in:
Matthew Jones 2013-11-13 13:35:48 -05:00
parent e2b657c72c
commit 78bdc7ae8b

View File

@ -1,9 +1,10 @@
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import pre_save
from django.db.models.signals import pre_save, post_save
from django.utils.functional import curry
from awx.main.models.activity_stream import ActivityStream
import json
import uuid
import urllib2
class ActivityStreamMiddleware(object):
@ -15,25 +16,34 @@ class ActivityStreamMiddleware(object):
else:
user = None
self.instances = []
set_actor = curry(self.set_actor, user)
pre_save.connect(set_actor, sender=ActivityStream, dispatch_uid=(self.__class__, request), weak=False)
self.disp_uid = str(uuid.uuid1())
self.finished = False
post_save.connect(set_actor, sender=ActivityStream, dispatch_uid=self.disp_uid, weak=False)
def process_response(self, request, response):
pre_save.disconnect(dispatch_uid=(self.__class__, request))
post_save.disconnect(dispatch_uid=self.disp_uid)
self.finished = True
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()
for instance in self.instances:
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']))
instance.user = userActual
instance.save()
else:
obj1_type_actual = instance.object1_type.split(".")[-1]
if obj1_type_actual in ("InventoryUpdate", "ProjectUpdate", "JobEvent") and instance.id is not None:
instance.delete()
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:
elif not self.finished:
self.isActivityStreamEvent = True
self.instance = instance
self.instances.append(instance)
else:
self.isActivityStreamEvent = False