Coverage for sites/ptf_tools/ptf_tools/signals.py: 84%

73 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-19 19:20 +0000

1from itertools import chain 

2 

3from allauth.account.signals import user_signed_up 

4 

5from django.contrib.auth.models import Group 

6from django.contrib.auth.models import User 

7from django.dispatch import receiver 

8from django.http import HttpRequest 

9from django.urls import reverse_lazy 

10 

11from comments_moderation.models import CommentModerator 

12from comments_moderation.rights import ModeratorUserRights 

13from comments_moderation.utils import update_moderation_right 

14from ptf.models import Collection 

15from ptf.utils import send_email_from_template 

16 

17from .models import Invitation 

18from .models import InvitationExtraData 

19 

20 

21@receiver(user_signed_up) 

22def update_user_from_invite( 

23 sender, request: HttpRequest | None = None, user: User | None = None, **kwargs 

24): 

25 """ 

26 Update extra information on the user signing up from the attached invitation. 

27 The extra information is either basic data (first name, last name) 

28 or app specific extra information (eg. for comment moderation). 

29 """ 

30 

31 if not isinstance(user, User): 31 ↛ 32line 31 didn't jump to line 32, because the condition on line 31 was never true

32 return 

33 

34 try: 

35 invite: Invitation | None = Invitation.objects.get(email__iexact=user.email) 

36 # TODO: Log if no invitation attached to the user 

37 # This should not happened since signup is closed outside of invitation system 

38 except Invitation.DoesNotExist: 

39 invite = None 

40 

41 if invite is None: 41 ↛ 42line 41 didn't jump to line 42, because the condition on line 41 was never true

42 return 

43 

44 if invite.first_name: 44 ↛ 46line 44 didn't jump to line 46, because the condition on line 44 was never false

45 user.first_name = invite.first_name 

46 if invite.last_name: 46 ↛ 49line 46 didn't jump to line 49, because the condition on line 46 was never false

47 user.last_name = invite.last_name 

48 

49 extra_data = InvitationExtraData(**invite.extra_data) 

50 

51 # Handle comment moderation related data 

52 if extra_data.moderator: 52 ↛ 119line 52 didn't jump to line 119, because the condition on line 52 was never false

53 # The list of users that "invited" the signing up user as a comment moderator. 

54 # We will send an e-mail to notify them of the account creation. 

55 users_to_mail = [] 

56 # Mark the user as a comment moderator 

57 try: 

58 comment_moderator = CommentModerator.objects.get(user=user) 

59 except CommentModerator.DoesNotExist: 

60 comment_moderator = CommentModerator(user=user) 

61 

62 comment_moderator.is_moderator = True 

63 comment_moderator.save() 

64 

65 # Staff moderators - Fill the selected collections 

66 if extra_data.moderator.collections: 

67 pids = chain.from_iterable(c.pid for c in extra_data.moderator.collections) 

68 collections_id = Collection.objects.filter(pid__in=set(pids)).values_list( 

69 "pk", flat=True 

70 ) 

71 comment_moderator.collections.add(*collections_id) 

72 

73 users_to_mail.extend(c.user_id for c in extra_data.moderator.collections) 

74 

75 # Base moderatos - Create a moderaton right entry associated to 

76 # the selected comments 

77 if extra_data.moderator.comments: 

78 processed_comments = [] 

79 for comment in extra_data.moderator.comments: 

80 try: 

81 users_to_mail.append(comment.user_id) 

82 if comment.id in processed_comments: 82 ↛ 83line 82 didn't jump to line 83, because the condition on line 82 was never true

83 continue 

84 right_creator = User.objects.get(pk=comment.user_id) 

85 rights = ModeratorUserRights(right_creator) 

86 error, _ = update_moderation_right(comment.id, user.pk, rights) 

87 # TODO: Log if the the POST request is unsuccessful ? 

88 processed_comments.append(comment.id) 

89 # Catch any exception and continue the workflow. 

90 # We don't want this additional processing to result in the sign up 

91 # failing. 

92 except Exception: 

93 continue 

94 # Send notification e-mail 

95 if users_to_mail: 

96 recipient_list = User.objects.filter(pk__in=set(users_to_mail)).values( 

97 "first_name", "last_name", "email" 

98 ) 

99 if recipient_list: 99 ↛ 119line 99 didn't jump to line 119, because the condition on line 99 was never false

100 for r in recipient_list: 

101 context_data = { 

102 "full_name": f"{r['first_name']} {r['last_name']}", 

103 "sign_up_name": f"{user.first_name} {user.last_name}", 

104 "sign_up_email": user.email, 

105 "comment_moderator_url": request.build_absolute_uri( 

106 reverse_lazy("comment_moderators") 

107 ), 

108 "invitation_date": invite.created, 

109 "email_signature": "The editorial team", 

110 } 

111 send_email_from_template( 

112 "mail/comment_moderator_account_created.html", 

113 context_data, 

114 "[TRAMMEL] Comment moderator sign up", 

115 to=[r["email"]], 

116 ) 

117 

118 # Add the newly created user to the given user groups. 

119 if extra_data.user_groups: 

120 group_pks = [] 

121 for group_pk in extra_data.user_groups: 

122 try: 

123 group_pks.append(int(group_pk)) 

124 except ValueError: 

125 pass 

126 if group_pks: 126 ↛ 131line 126 didn't jump to line 131, because the condition on line 126 was never false

127 user_groups = Group.objects.filter(pk__in=group_pks) 

128 if user_groups: 128 ↛ 131line 128 didn't jump to line 131, because the condition on line 128 was never false

129 user.groups.add(*user_groups) 

130 

131 user.save()