Commits

Guilherme Gondim  committed 77d98bf

Improves subscription: if a user already exists in database, just update his
subscribed mailing lists.

  • Participants
  • Parent commits f691b6c

Comments (0)

Files changed (2)

File informativo/forms.py

     return mailing_lists
 
 
-class SubscribeForm(forms.ModelForm):
+class SubscribeForm(forms.Form):
+    first_name = forms.CharField(label=_('First name'), required=False)
+    last_name = forms.CharField(label=_('Last name'), required=False)
+    email = forms.EmailField(label=_('e-mail'), required=True)
     mailing_lists = forms.MultipleChoiceField(
         label=_('Mailing lists'),
         initial=[0],
         widget=forms.CheckboxSelectMultiple
     )
 
-    class Meta:
-        model = Contact
-
     def __init__(self, *args, **kwargs):
         super(SubscribeForm, self).__init__(*args, **kwargs)
         self.fields['mailing_lists'].choices = _get_mailinglists_choices()
 
-    def save(self, commit=True):
-        instance = super(SubscribeForm, self).save(commit=False)
+    def subscribe(self):
+        mailing_lists = self.cleaned_data['mailing_lists']
+        if '0' in mailing_lists:
+            subscribe_list = MailingList.objects.all()
+        else:
+            subscribe_list = MailingList.objects.filter(pk__in=mailing_lists)
 
-        pk_list = self.cleaned_data['mailing_lists']
-        if '0' in pk_list:
-            mailing_lists = MailingList.objects.all()
+        contact, created = Contact.objects.get_or_create(
+            email=self.cleaned_data['email'],
+            defaults={
+                'first_name': self.cleaned_data['first_name'],
+                'last_name': self.cleaned_data['first_name'],
+            }
+        )
+
+        if created:
+            contact.mailing_lists = subscribe_list
         else:
-            mailing_lists = MailingList.objects.filter(pk__in=pk_list)
+            contact.mailing_lists.add(*subscribe_list)
 
-        if commit:
-            instance.save()
-            instance.mailing_lists = mailing_lists
-
-        return instance
+        contact.save()
+        return contact
 
 
 class UnsubscribeForm(forms.Form):
         if '0' in mailing_lists:
             self._contact.mailing_lists.clear()
         else:
-            remove_list = MailingList.objects.exclude(pk__in=mailing_lists)
-            self._contact.mailing_lists.remove(*remove_list)
+            unsubscribe_list = MailingList.objects.exclude(pk__in=mailing_lists)
+            self._contact.mailing_lists.remove(*unsubscribe_list)
         return self._contact
  

File informativo/views.py

 from informativo import models
 
 
-class SubscribeFormView(generic.edit.CreateView):
+class SubscribeFormView(generic.edit.FormView):
     form_class = forms.SubscribeForm
     template_name = 'informativo/subscribe_form.html'
-    model = models.Contact
+
+    def form_valid(self, form):
+        response = super(SubscribeFormView, self).form_valid(form)
+        form.subscribe()
+        return response
 
     def get_context_data(self, **kwargs):
         context = super(SubscribeFormView, self).get_context_data(**kwargs)
 class UnsubscribeFormView(generic.edit.FormView):
     form_class = forms.UnsubscribeForm
     template_name = 'informativo/unsubscribe_form.html'
-    model = models.Contact
 
     def form_valid(self, form):
         response = super(UnsubscribeFormView, self).form_valid(form)