SciPost Code Repository

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

First working version of upgrade prospect

parent ff846a4d
No related branches found
No related tags found
No related merge requests found
from django import forms
from django.contrib.auth.models import User
from django.db.models import Q
from captcha.fields import ReCaptchaField
from django_countries import countries
......@@ -62,11 +64,41 @@ class PromoteToContactForm(forms.ModelForm):
'email',
)
def promote_contacts(self, partner):
def clean_email(self):
"""
Check if email address is already used.
"""
email = self.cleaned_data['email']
if User.objects.filter(Q(email=email) | Q(username=email)).exists():
self.add_error('email', 'This emailadres has already been used.')
return email
def promote_contact(self, partner):
"""
Promote ProspectiveContact's to Contact's related to a certain Partner.
The status update after promotion is handled outside this method, in the Partner model.
"""
raise NotImplemented
# How to handle empty instances?
if self.errors:
return forms.ValidationError # Is this a valid exception?
# Create a new User and Contact linked to the partner given
user = User(
first_name=self.cleaned_data['first_name'],
last_name=self.cleaned_data['last_name'],
email=self.cleaned_data['email'],
username=self.cleaned_data['email']
)
user.save()
contact = Contact(
user=user,
title=self.cleaned_data['title'],
kind=self.cleaned_data['contact_types']
)
contact.save()
contact.partners.add(partner)
return contact
class PromoteToContactFormset(forms.BaseModelFormSet):
......@@ -97,7 +129,8 @@ class PromoteToContactFormset(forms.BaseModelFormSet):
if contact_type_keys:
# Add error to all forms if not all CONTACT_TYPES are assigned
for form in self.forms:
form.add_error('contact_types', "Not all contact types have been selected yet.")
form.add_error('contact_types', ("Not all contact types have been"
" divided over the contacts yet."))
def save(self, *args, **kwargs):
raise DeprecationWarning(("This formset is not meant to used with the default"
......@@ -107,7 +140,12 @@ class PromoteToContactFormset(forms.BaseModelFormSet):
"""
Promote ProspectiveContact's to Contact's related to a certain Partner.
"""
raise NotImplemented
contacts = []
for form in self.forms:
contacts.append(form.promote_contact(partner))
partner.main_contact = contacts[0]
partner.save()
return contacts
class ProspectivePartnerForm(forms.ModelForm):
......
......@@ -29,14 +29,6 @@
{{ form|bootstrap }}
{% endfor %}
{% for error in contact_formset.non_form_errors %}
<div class="form-group row">
<div class="alert alert-danger show">
<strong>Form error</strong> &middot; {{error}}
</div>
</div>
{% endfor %}
<input class="btn btn-primary" type="submit" value="Submit"/>
</form>
</div>
......
......@@ -93,13 +93,19 @@ def promote_prospartner(request, prospartner_id):
pk=prospartner_id)
form = PromoteToPartnerForm(request.POST or None, instance=prospartner)
ContactModelFormset = modelformset_factory(ProspectiveContact, PromoteToContactForm,
formset=PromoteToContactFormset)
formset=PromoteToContactFormset, extra=0)
contact_formset = ContactModelFormset(request.POST or None,
queryset=prospartner.prospective_contacts.all())
if form.is_valid() and contact_formset.is_valid():
partner, institution = form.promote_to_partner()
contact_formset.promote_contacts(partner)
raise NotImplemented
contacts = contact_formset.promote_contacts(partner)
# partner.send_mail()
# contacts.send_mail()
messages.success(request, ('<h3>Upgraded Partner %s</h3>'
'%i contacts have received a validation mail.') %
(str(partner), len(contacts)))
return redirect(reverse('partners:dashboard'))
context = {'form': form, 'contact_formset': contact_formset}
return render(request, 'partners/promote_prospartner.html', context)
......
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