Coverage for website/events/decorators.py: 65.52%

23 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2025-08-14 10:31 +0000

1from django.core.exceptions import PermissionDenied 

2 

3from events import services 

4from events.models import Event 

5 

6 

7def organiser_only(view_function): 

8 """See OrganiserOnly.""" 

9 return OrganiserOnly(view_function) 

10 

11 

12class OrganiserOnly: 

13 """Decorator that denies access to the page under certain conditions. 

14 

15 Under these conditions access is denied: 

16 1. There is no `pk` or `registration` in the request 

17 2. The specified event does not exist 

18 3. The user is no organiser of the specified event 

19 """ 

20 

21 def __init__(self, view_function): 

22 self.view_function = view_function 

23 

24 def __call__(self, request, *args, **kwargs): 

25 event = None 

26 

27 if "pk" in kwargs: 27 ↛ 32line 27 didn't jump to line 32 because the condition on line 27 was always true

28 try: 

29 event = Event.objects.get(pk=kwargs.get("pk")) 

30 except Event.DoesNotExist: 

31 pass 

32 elif "registration" in kwargs: 

33 try: 

34 event = Event.objects.get( 

35 eventregistration__pk=kwargs.get("registration") 

36 ) 

37 except Event.DoesNotExist: 

38 pass 

39 

40 if event and services.is_organiser(request.member, event): 

41 return self.view_function(request, *args, **kwargs) 

42 

43 raise PermissionDenied