SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 14ae4eff authored by Jorran de Wit's avatar Jorran de Wit
Browse files

Reduce lines of code draft_registration_inv

parent fc08930e
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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):
......
......@@ -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,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment