Coverage for cookbook/helper/AllAuthCustomAdapter.py: 36%

28 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2023-12-29 00:47 +0100

1import datetime 

2from gettext import gettext as _ 

3 

4from allauth.account.adapter import DefaultAccountAdapter 

5from django.conf import settings 

6from django.contrib import messages 

7from django.core.cache import caches 

8 

9from cookbook.models import InviteLink 

10 

11 

12class AllAuthCustomAdapter(DefaultAccountAdapter): 

13 

14 def is_open_for_signup(self, request): 

15 """ 

16 Whether to allow sign-ups. 

17 """ 

18 signup_token = False 

19 if 'signup_token' in request.session and InviteLink.objects.filter( 

20 valid_until__gte=datetime.datetime.today(), used_by=None, uuid=request.session['signup_token']).exists(): 

21 signup_token = True 

22 

23 if request.resolver_match.view_name == 'account_signup' and not settings.ENABLE_SIGNUP and not signup_token: 

24 return False 

25 elif request.resolver_match.view_name == 'socialaccount_signup' and len(settings.SOCIAL_PROVIDERS) < 1: 

26 return False 

27 else: 

28 return super(AllAuthCustomAdapter, self).is_open_for_signup(request) 

29 

30 # disable password reset for now 

31 def send_mail(self, template_prefix, email, context): 

32 if settings.EMAIL_HOST != '': 

33 default = datetime.datetime.now() 

34 c = caches['default'].get_or_set(email, default, timeout=360) 

35 if c == default: 

36 try: 

37 super(AllAuthCustomAdapter, self).send_mail(template_prefix, email, context) 

38 except Exception: # dont fail signup just because confirmation mail could not be send 

39 pass 

40 else: 

41 messages.add_message(self.request, messages.ERROR, _('In order to prevent spam, the requested email was not send. Please wait a few minutes and try again.')) 

42 else: 

43 pass