diff --git a/partners/forms.py b/partners/forms.py index 0f2e4d0c75f5a28fbc4342bdf799ab8ccf9194f6..6454802bb98667250a90b45b6a7ff141ce98d0de 100644 --- a/partners/forms.py +++ b/partners/forms.py @@ -171,7 +171,7 @@ class ProcessRequestContactForm(RequestContactForm): model = ContactRequest fields = RequestContactForm.Meta.fields + ('partner',) - def process_request(self, current_contact): + def process_request(self, current_user): if self.cleaned_data['decision'] == 'accept': self.instance.status = REQUEST_PROCESSED self.instance.save() @@ -183,19 +183,19 @@ class ProcessRequestContactForm(RequestContactForm): 'kind': self.cleaned_data['kind'], }, partner=self.cleaned_data['partner']) contactForm.is_valid() - contactForm.save(current_contact=current_contact) + contactForm.save(current_user=current_user) elif self.cleaned_data['decision'] == 'decline': self.instance.status = REQUEST_DECLINED self.instance.save() class RequestContactFormSet(forms.BaseModelFormSet): - def process_requests(self, current_contact): + def process_requests(self, current_user): """ Process all requests if status is eithter accept or decline. """ for form in self.forms: - form.process_request(current_contact=current_contact) + form.process_request(current_user=current_user) class ContactForm(forms.ModelForm): @@ -247,7 +247,7 @@ class NewContactForm(ContactForm): return email @transaction.atomic - def save(self, current_contact, commit=True): + def save(self, current_user, commit=True): """ If existing user is found, add it to the Partner. """ @@ -286,7 +286,7 @@ class NewContactForm(ContactForm): # Send email for activation PartnerUtils.load({'contact': contact}) - PartnerUtils.email_contact_new_for_activation(current_contact=current_contact) + PartnerUtils.email_contact_new_for_activation(current_user=current_user) return contact @@ -355,8 +355,8 @@ class PromoteToContactForm(forms.ModelForm): """ This form is used to create a new `partners.Contact` """ - contact_types = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, - choices=CONTACT_TYPES, required=False) + kind = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, + label='Contact types', choices=CONTACT_TYPES, required=False) class Meta: model = ProspectiveContact @@ -377,7 +377,7 @@ class PromoteToContactForm(forms.ModelForm): return email @transaction.atomic - def promote_contact(self, partner): + def promote_contact(self, partner, current_user): """ 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. @@ -388,22 +388,11 @@ class PromoteToContactForm(forms.ModelForm): 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'], - is_active=False, - ) - 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 + contact_form = NewContactForm(self.cleaned_data, partner=partner) + if contact_form.is_valid(): + return contact_form.save(current_user=current_user) + r = contact_form.errors + raise forms.ValidationError('NewContactForm invalid. Please contact Admin.') class PromoteToContactFormset(forms.BaseModelFormSet): @@ -415,13 +404,14 @@ class PromoteToContactFormset(forms.BaseModelFormSet): raise DeprecationWarning(("This formset is not meant to used with the default" " `save` method. User the `promote_contacts` instead.")) - def promote_contacts(self, partner): + @transaction.atomic + def promote_contacts(self, partner, current_user): """ Promote ProspectiveContact's to Contact's related to a certain Partner. """ contacts = [] for form in self.forms: - contacts.append(form.promote_contact(partner)) + contacts.append(form.promote_contact(partner, current_user)) partner.main_contact = contacts[0] partner.save() return contacts diff --git a/partners/urls.py b/partners/urls.py index f7149593c311fb36930813d9764d0a3a38986cab..f376c8de27594f1f53fa970b3d4b201c62518d90 100644 --- a/partners/urls.py +++ b/partners/urls.py @@ -18,8 +18,6 @@ urlpatterns = [ views.add_prospartner_contact, name='add_prospartner_contact'), url(r'^prospects/(?P<prospartner_id>[0-9]+)/promote$', views.promote_prospartner, name='promote_prospartner'), - url(r'^prospects/(?P<prospartner_id>[0-9]+)/events/add$', - views.add_prospartner_contact, name='add_prospartner_contact'), url(r'^prospects/(?P<prospartner_id>[0-9]+)/email_generic', views.email_prospartner_generic, name='email_prospartner_generic'), url(r'^prospects/(?P<prospartner_id>[0-9]+)/events/add$', diff --git a/partners/utils.py b/partners/utils.py index 3b49cd1ff482ed81fc196dfaa740f505567fa189..2d5241c44c89bcd779f7f0b0e992a634b06c4b9a 100644 --- a/partners/utils.py +++ b/partners/utils.py @@ -27,7 +27,7 @@ class PartnerUtils(BaseMailUtil): cls._context['email_subject']) @classmethod - def email_contact_new_for_activation(cls, current_contact): + def email_contact_new_for_activation(cls, current_user): """ Email a generic address for a Contact. @@ -36,4 +36,4 @@ class PartnerUtils(BaseMailUtil): cls._send_mail(cls, 'email_contact_new_for_activation', [cls._context['contact'].user.email], 'Welcome to the SciPost Supporting Partner Board', - extra_context={'created_by': current_contact}) + extra_context={'sent_by': current_user}) diff --git a/partners/views.py b/partners/views.py index 6874ef818afd57f1aeb474da42a65abdefa7fe51..a2567022163ce186524217a19a4967ab759d76e0 100644 --- a/partners/views.py +++ b/partners/views.py @@ -41,11 +41,13 @@ def dashboard(request): This page is meant as a personal page for Partners, where they will for example be able to read their personal data and agreements. ''' - personal_agreements = (MembershipAgreement.objects.open_to_partner() - .filter(partner__contact=request.user.partner_contact)) - context = { - 'personal_agreements': personal_agreements - } + context = {} + try: + context['personal_agreements'] = (MembershipAgreement.objects.open_to_partner() + .filter(partner__contact=request.user.partner_contact)) + except Contact.DoesNotExist: + pass + if request.user.has_perm('scipost.can_manage_SPB'): context['contact_requests_count'] = ContactRequest.objects.awaiting_processing().count() context['inactivate_contacts_count'] = Contact.objects.filter(user__is_active=False).count() @@ -102,10 +104,7 @@ def promote_prospartner(request, prospartner_id): queryset=prospartner.prospective_contacts.all()) if form.is_valid() and contact_formset.is_valid(): partner, institution = form.promote_to_partner(request.user) - contacts = contact_formset.promote_contacts(partner) - - # partner.send_mail() - # contacts.send_mail() + contacts = contact_formset.promote_contacts(partner, request.user) messages.success(request, ('<h3>Upgraded Partner %s</h3>' '%i contacts have received a validation mail.') % (str(partner), len(contacts))) @@ -165,10 +164,9 @@ def partner_add_contact(request, partner_id): partner = get_object_or_404(Partner, id=partner_id) form = NewContactForm(request.POST or None, partner=partner) if form.is_valid(): - contact = form.save(current_contact=request.user.partner_contact) + contact = form.save(current_user=request.user) messages.success(request, '<h3>Created contact: %s</h3>Email has been sent.' % str(contact)) - # raise return redirect(reverse('partners:dashboard')) context = { 'partner': partner, @@ -204,7 +202,7 @@ def process_contact_requests(request): formset = RequestContactModelFormSet(request.POST or None, queryset=ContactRequest.objects.awaiting_processing()) if formset.is_valid(): - formset.process_requests(current_contact=request.user.partner_contact) + formset.process_requests(current_user=request.user) messages.success(request, 'Processing completed') return redirect(reverse('partners:process_contact_requests')) context = { @@ -404,7 +402,7 @@ def activate_account(request, activation_key): form = ActivationForm(request.POST or None, instance=contact.user) if form.is_valid(): form.activate_user() - messages.success(request, '<h3>Thank you for registration</h3>.') + messages.success(request, '<h3>Thank you for registration</h3>') return redirect(reverse('partners:dashboard')) context = { 'contact': contact, diff --git a/templates/email/email_contact_new_for_activation.html b/templates/email/email_contact_new_for_activation.html index bc3c690cd1566f256f8c5d545b0ec6574eea2353..16bfbdb90ad5d4f5ff43e0e13cb041f6c38ef3b6 100644 --- a/templates/email/email_contact_new_for_activation.html +++ b/templates/email/email_contact_new_for_activation.html @@ -20,7 +20,7 @@ </p> <p> Sincerely,<br><br> - By {{sent_by.get_title_display}} {{sent_by.user.last_name}} + By {% if sent_by.partner_contact %}{{sent_by.partner_contact.get_title_display}}{% elif sent_by.contributor %}{{sent_by.contributor.get_title_display}}{% else %}{{sent_by.first_name}}{% endif %} {{ sent_by.last_name }} on behalf of SciPost and its Supporting Partners Board </p> diff --git a/templates/email/email_contact_new_for_activation.txt b/templates/email/email_contact_new_for_activation.txt index 00d10a6a5b059174af9b4869035c122d90f994c9..ee83ea8c1d84a58292906826ce439b5cd063e135 100644 --- a/templates/email/email_contact_new_for_activation.txt +++ b/templates/email/email_contact_new_for_activation.txt @@ -18,5 +18,5 @@ We are very pleased to welcome you on Board, and will be happy to answer any que Sincerely,\n\n -By {{sent_by.get_title_display}} {{sent_by.user.last_name}}\n +By {% if sent_by.partner_contact %}{{sent_by.partner_contact.get_title_display}}{% elif sent_by.contributor %}{{sent_by.contributor.get_title_display}}{% else %}{{sent_by.first_name}}{% endif %} {{ sent_by.last_name }}\n on behalf of SciPost and its Supporting Partners Board