diff --git a/scipost/forms.py b/scipost/forms.py index 21c68fe51c5aa3a94289857a942539a620b7757d..047e3a0041438b5d14fedec3d4c01b5c13f1990b 100644 --- a/scipost/forms.py +++ b/scipost/forms.py @@ -133,6 +133,40 @@ class DraftInvitationForm(forms.ModelForm): 'cited_in_submission', 'cited_in_publication' ] + def __init__(self, *args, **kwargs): + ''' + This form has a required keyword argument `current_user` which is used for validation of + the form fields. + ''' + self.current_user = kwargs.pop('current_user') + super().__init__(*args, **kwargs) + + def clean_email(self): + email = self.cleaned_data['email'] + if RegistrationInvitation.objects.filter(email=email).exists(): + self.add_error('email', 'This email address has already been used for an invitation') + + if DraftInvitation.objects.filter(email=email).exists(): + self.add_error('email', ('This email address has already been' + ' used for a draft invitation')) + + if User.objects.filter(email=email).exists(): + self.add_error('email', 'This email address is already associated to a Contributor') + + return email + + def clean_invitation_type(self): + invitation_type = self.cleaned_data['invitation_type'] + if invitation_type == 'F' and not self.current_user.has_perm('scipost.can_invite_Fellows'): + self.add_error('invitation_type', ('You do not have the authorization' + ' to send a Fellow-type invitation.' + ' Consider Contributor, or cited (sub/pub).')) + if invitation_type == 'R': + self.add_error('invitation_type', ('Referee-type invitations must be made' + 'by the Editor-in-charge at the relevant' + ' Submission\'s Editorial Page.')) + return invitation_type + class RegistrationInvitationForm(forms.ModelForm): cited_in_submission = AutoCompleteSelectField('submissions_lookup', required=False) diff --git a/scipost/models.py b/scipost/models.py index dd2bc50535f6bef22f63975dbebd01b167c19593..886fe252800b6f70c7fd4dc4574a008a1a9b599d 100644 --- a/scipost/models.py +++ b/scipost/models.py @@ -205,8 +205,8 @@ class DraftInvitation(models.Model): Draft of an invitation, filled in by an officer. """ title = models.CharField(max_length=4, choices=TITLE_CHOICES) - first_name = models.CharField(max_length=30, default='') - last_name = models.CharField(max_length=30, default='') + first_name = models.CharField(max_length=30) + last_name = models.CharField(max_length=30) email = models.EmailField() invitation_type = models.CharField(max_length=2, choices=INVITATION_TYPE, default=INVITATION_CONTRIBUTOR) @@ -216,10 +216,9 @@ class DraftInvitation(models.Model): cited_in_publication = models.ForeignKey('journals.Publication', on_delete=models.CASCADE, blank=True, null=True) - drafted_by = models.ForeignKey(Contributor, - on_delete=models.CASCADE, + drafted_by = models.ForeignKey('scipost.Contributor', on_delete=models.CASCADE, blank=True, null=True) - date_drafted = models.DateTimeField(default=timezone.now) + date_drafted = models.DateTimeField(auto_now_add=True) processed = models.BooleanField(default=False) def __str__(self): diff --git a/scipost/views.py b/scipost/views.py index 793e442f16126d790ef3f314231df94731121af0..724f6c3274d25f1b5cef73927f2a0265054b6d4d 100644 --- a/scipost/views.py +++ b/scipost/views.py @@ -392,36 +392,13 @@ def draft_registration_invitation(request): This is similar to the registration_invitations method, which is used to complete the invitation process. """ - errormessage = '' - if request.method == 'POST': - draft_inv_form = DraftInvitationForm(request.POST) - Utils.load({'contributor': request.user.contributor, 'form': draft_inv_form}) - if draft_inv_form.is_valid(): - if Utils.email_already_invited(): - errormessage = ('DUPLICATE ERROR: ' - 'This email address has already been used for an invitation') - elif Utils.email_already_drafted(): - errormessage = ('DUPLICATE ERROR: ' - 'This email address has already been used for a draft invitation') - elif Utils.email_already_taken(): - errormessage = ('DUPLICATE ERROR: ' - 'This email address is already associated to a Contributor') - elif (draft_inv_form.cleaned_data['invitation_type'] == 'F' - and not request.user.has_perm('scipost.can_invite_Fellows')): - errormessage = ('You do not have the authorization to send a Fellow-type ' - 'invitation. Consider Contributor, or cited (sub/pub). ') - elif (draft_inv_form.cleaned_data['invitation_type'] == 'R'): - errormessage = ('Referee-type invitations must be made by the Editor-in-charge ' - 'at the relevant Submission\'s Editorial Page. ') - else: - Utils.create_draft_invitation() - messages.success(request, 'Draft invitation saved.') - return redirect(reverse('scipost:draft_registration_invitation')) - else: - errormessage = 'The form was not filled validly.' - - else: - draft_inv_form = DraftInvitationForm() + draft_inv_form = DraftInvitationForm(request.POST or None, current_user=request.user) + if draft_inv_form.is_valid(): + invitation = draft_inv_form.save(commit=False) + invitation.drafted_by = request.user.contributor + invitation.save() + messages.success(request, 'Draft invitation saved.') + return redirect(reverse('scipost:draft_registration_invitation')) sent_reg_inv = RegistrationInvitation.objects.filter(responded=False, declined=False) sent_reg_inv_fellows = sent_reg_inv.filter(invitation_type='F').order_by('last_name') @@ -445,7 +422,7 @@ def draft_registration_invitation(request): existing_drafts = DraftInvitation.objects.filter(processed=False).order_by('last_name') context = { - 'draft_inv_form': draft_inv_form, 'errormessage': errormessage, + 'draft_inv_form': draft_inv_form, 'sent_reg_inv_fellows': sent_reg_inv_fellows, 'sent_reg_inv_contrib': sent_reg_inv_contrib, 'sent_reg_inv_ref': sent_reg_inv_ref,