Guilherme Gondim avatar Guilherme Gondim committed 78a4315

conclude unsubscribe support

Comments (0)

Files changed (3)

informativo/forms.py

 from django import forms
 from django.utils.translation import ugettext_lazy as _
 
-from informativo.models import Contact
+from informativo.models import Contact, MailingList
+
+
+def _get_mailinglists_choices():
+    mailing_lists = [(m.pk, m.name) for m in MailingList.objects.all()]
+    mailing_lists.insert(0, (0, _('All')))
+    return mailing_lists
 
 
 class SubscribeForm(forms.ModelForm):
+    mailing_lists = forms.MultipleChoiceField(
+        label=_('Mailing lists'),
+        initial=[0],
+        widget=forms.CheckboxSelectMultiple
+    )
 
     class Meta:
         model = Contact
-        widgets = {'mailing_lists': forms.CheckboxSelectMultiple}
 
+    def __init__(self, *args, **kwargs):
+        super(SubscribeForm, self).__init__(*args, **kwargs)
+        self.fields['mailing_lists'].choices = _get_mailinglists_choices()
 
-def get_mailinglists_choices(self):
-    return ('all', _('All'))
+    def save(self, commit=True):
+        instance = super(SubscribeForm, self).save(commit=False)
+
+        pk_list = self.cleaned_data['mailing_lists']
+        if '0' in pk_list:
+            mailing_lists = MailingList.objects.all()
+        else:
+            mailing_lists = MailingList.objects.filter(pk__in=pk_list)
+
+        if commit:
+            instance.save()
+            instance.mailing_lists = mailing_lists
+
+        return instance
+
 
 class UnsubscribeForm(forms.Form):
     email = forms.EmailField(label=_('E-mail'), max_length=100)
     mailing_lists = forms.MultipleChoiceField(
         label=_('Mailing lists'),
-        choices=get_mailinglists_choices,
-        default='all',
+        initial=[0],
         widget=forms.CheckboxSelectMultiple
     )
 
+    def __init__(self, *args, **kwargs):
+        super(UnsubscribeForm, self).__init__(*args, **kwargs)
+        self.fields['mailing_lists'].choices = _get_mailinglists_choices()
+
     def clean_email(self):
         data = self.cleaned_data.get('email')
         try:
-            Contact.objects.get(email=data)
+            self._contact = Contact.objects.get(email=data)
         except:
             msg = _('This e-mail does not exist in our database.')
             raise forms.ValidationError(msg)
         return data
+
+    def unsubscribe(self):
+        email = self.cleaned_data['mailing_lists']
+        mailing_lists = self.cleaned_data['mailing_lists']
+        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)
+        return self._contact
+ 

informativo/urls.py

 
 
 urlpatterns = patterns('',
-    url('^$', views.SubscribeFormView.as_view(), name='informativo-subscribe'),
-    url('^subscribed/$', views.SubscribedView.as_view(), name='informativo-subscribed'),
-    url('^unsubscribed/$', views.UnsubscribedView.as_view(), name='informativo-unsubscribed'),
+    url('^subscribe/$', views.SubscribeFormView.as_view(), name='informativo-subscribe'),
+    url('^subscribe/success/$', views.SubscribedView.as_view(), name='informativo-subscribed'),
+    url('^unsubscribe/$', views.UnsubscribeFormView.as_view(), name='informativo-unsubscribe'),
+    url('^unsubscribe/success/$', views.UnsubscribedView.as_view(), name='informativo-unsubscribed'),
 )

informativo/views.py

     template_name = 'informativo/unsubscribe_form.html'
     model = models.Contact
 
-    def form_valid(form):
+    def form_valid(self, form):
         response = super(UnsubscribeFormView, self).form_valid(form)
-        self.unsubscribe(form.data)
+        contact = form.unsubscribe()
+        if not contact.mailing_lists.all():
+            contact.delete()
         return response
 
     def get_success_url(self):
         return reverse('informativo-unsubscribed')
 
-    def unsubscribe(form_data):
-        pass #TODO
-
 
 class UnsubscribedView(generic.base.TemplateView):
     template_name = 'informativo/unsubscribed.html'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.