From 97401ee5a6068e425d73c1bb4ff616cd9361905c Mon Sep 17 00:00:00 2001 From: Sylvain Le Bon <sylvain@startinblox.com> Date: Thu, 12 Oct 2023 11:46:44 +0200 Subject: [PATCH] bugfix: CurrentUserMiddleware --- README.md | 10 ++----- djangoldp_notification/check_integrity.py | 2 -- djangoldp_notification/middlewares.py | 36 +++++------------------ djangoldp_notification/models.py | 4 +-- 4 files changed, 12 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index da49646..0a82674 100644 --- a/README.md +++ b/README.md @@ -44,17 +44,13 @@ You can automatically create required subscriptions based on your settings.py wi # Middlewares -There is a `CurrentUserMiddleware` that catches the connected user of the last performed HTTP request and adds -to every model before it is saved. This is useful if you need to get the connected user that performed -the last HTTP request in a `pre_saved` signal. You can get it by using the following line : +There is a `CurrentUserMiddleware` that catches the connected user of the current HTTP request and makes it available through the `get_current_user` function : ```python -getattr(instance, MODEL_MODIFICATION_USER_FIELD, "Unknown user") +from djangoldp_notification.middlewares import get_current_user +get_current_user() ``` -`MODEL_MODIFICATION_USER_FIELD` is a constant that lies in `djangoldp_notification.middlewares` and -`instance` is the instance of your model before save in DB. - # Signals ## Create notification on subscribed objects diff --git a/djangoldp_notification/check_integrity.py b/djangoldp_notification/check_integrity.py index 77839b4..b4f779e 100644 --- a/djangoldp_notification/check_integrity.py +++ b/djangoldp_notification/check_integrity.py @@ -3,7 +3,6 @@ from django.conf import settings from django.db import models from djangoldp.models import Model from djangoldp_notification.models import send_request, Subscription -from djangoldp_notification.middlewares import MODEL_MODIFICATION_USER_FIELD class technical_user: urlid = settings.BASE_URL @@ -71,7 +70,6 @@ def check_integrity(options): continue except ObjectDoesNotExist: continue - setattr(resource, MODEL_MODIFICATION_USER_FIELD, technical_user) try: send_request(subscription.inbox, url_resource, resource, False) sent+=1 diff --git a/djangoldp_notification/middlewares.py b/djangoldp_notification/middlewares.py index 19ef251..1f0505f 100644 --- a/djangoldp_notification/middlewares.py +++ b/djangoldp_notification/middlewares.py @@ -1,34 +1,12 @@ -from django.db.models import signals - - -MODEL_MODIFICATION_USER_FIELD = 'modification_user' - +from threading import local +_thread_locals = local() class CurrentUserMiddleware: - def __init__(self, get_response=None): + def __init__(self, get_response): self.get_response = get_response - def __call__(self, request): - self.process_request(request) - response = self.get_response(request) - signals.pre_save.disconnect(dispatch_uid=request) - signals.pre_delete.disconnect(dispatch_uid=request) - return response - - def process_request(self, request): - if request.method in ('GET', 'HEAD', 'OPTION'): - # this request shouldn't update anything - # so no signal handler should be attached - return - - if hasattr(request, 'user') and request.user.is_authenticated: - user = request.user - else: - user = None - - def _update_users(sender, instance, **kwargs): - if(type(instance).__name__ != "ScheduledActivity" and type(instance).__name__ != "LogEntry" and type(instance).__name__ != "Activity"): - setattr(instance, MODEL_MODIFICATION_USER_FIELD, user) + _thread_locals._current_user = getattr(request, 'user', None) + return self.get_response(request) - signals.pre_save.connect(_update_users, dispatch_uid=request, weak=False) - signals.pre_delete.connect(_update_users, dispatch_uid=request, weak=False) +def get_current_user(): + return getattr(_thread_locals, '_current_user', None) \ No newline at end of file diff --git a/djangoldp_notification/models.py b/djangoldp_notification/models.py index cac5c4f..544634a 100644 --- a/djangoldp_notification/models.py +++ b/djangoldp_notification/models.py @@ -13,7 +13,7 @@ from djangoldp.fields import LDPUrlField from djangoldp.models import Model from djangoldp.permissions import CreateOnly, AuthenticatedOnly, ReadAndCreate, OwnerPermissions from djangoldp.activities.services import ActivityQueueService, activity_sending_finished -from djangoldp_notification.middlewares import MODEL_MODIFICATION_USER_FIELD +from djangoldp_notification.middlewares import get_current_user from djangoldp_notification.views import LDPNotificationsViewSet import logging @@ -193,7 +193,7 @@ def send_notifications(instance, request_type): def send_request(target, object_iri, instance, request_type): - author = getattr(getattr(instance, MODEL_MODIFICATION_USER_FIELD, None), "urlid", str(_("Auteur inconnu"))) + author = get_current_user().urlid # local inbox if target.startswith(settings.SITE_URL): user = Model.resolve_parent(target.replace(settings.SITE_URL, '')) -- GitLab