From b11078e1bb3c9b6a648bf676c3cdb3e783650109 Mon Sep 17 00:00:00 2001 From: Jorran de Wit <jorrandewit@outlook.com> Date: Fri, 23 Jun 2017 13:40:33 +0200 Subject: [PATCH] First working version of upgrade prospect --- partners/forms.py | 46 +++++++++++++++++-- .../partners/promote_prospartner.html | 8 ---- partners/views.py | 12 +++-- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/partners/forms.py b/partners/forms.py index 4d6431b53..1e2291c44 100644 --- a/partners/forms.py +++ b/partners/forms.py @@ -1,4 +1,6 @@ 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): diff --git a/partners/templates/partners/promote_prospartner.html b/partners/templates/partners/promote_prospartner.html index c7aca96ad..437ad8ca2 100644 --- a/partners/templates/partners/promote_prospartner.html +++ b/partners/templates/partners/promote_prospartner.html @@ -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> · {{error}} - </div> - </div> - {% endfor %} - <input class="btn btn-primary" type="submit" value="Submit"/> </form> </div> diff --git a/partners/views.py b/partners/views.py index 1d863549c..26c5eb3d0 100644 --- a/partners/views.py +++ b/partners/views.py @@ -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) -- GitLab