Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from django.contrib.auth.decorators import user_passes_test 

2from django.shortcuts import get_object_or_404 

3from discuss_data.ddusers.models import User 

4from django.contrib import messages 

5from django.http import HttpResponseRedirect 

6 

7 

8def dd_tou_accepted(function=None): 

9 """ 

10 Decorator for views that checks that the user has accepted the Discuss Data TOU, 

11 redirecting to the first-log-in page if not. 

12 

13 Implementation follows `@login_required` 

14 (https://docs.djangoproject.com/en/2.2/_modules/django/contrib/auth/decorators/#login_required). 

15 

16 Usage: 

17 :: 

18 @dd_tou_accepted 

19 def myview(request): 

20 ... 

21 """ 

22 actual_decorator = user_passes_test( 

23 lambda u: getattr(u, "accepted_dd_tou", False), 

24 login_url="/users/terms-of-use/", 

25 redirect_field_name="next", 

26 ) 

27 if function: 

28 return actual_decorator(function) 

29 return actual_decorator 

30 

31 

32def dd_profile_accessible(function): 

33 def wrap(request, *args, **kwargs): 

34 user = get_object_or_404(User, uuid=kwargs["us_uuid"]) 

35 if (user.profile_accessibility == "PUB") or ( 

36 user.profile_accessibility == "NET" and request.user.is_authenticated 

37 ): 

38 return function(request, *args, **kwargs) 

39 else: 

40 messages.error( 

41 request, "The user has chosen not to share his profile details." 

42 ) 

43 return HttpResponseRedirect( 

44 "/users/search/" 

45 ) # better: redirect to last page 

46 

47 wrap.__doc__ = function.__doc__ 

48 wrap.__name__ = function.__name__ 

49 return wrap