Commits

Guilherme Gondim committed 2981fc5

add initial unsubscribe form support

  • Participants
  • Parent commits e367064

Comments (0)

Files changed (4)

informativo/forms.py

 from django import forms
+from django.utils.translation import ugettext_lazy as _
 
 from informativo.models import Contact
 
     class Meta:
         model = Contact
         widgets = {'mailing_lists': forms.CheckboxSelectMultiple}
+
+
+def get_mailinglists_choices(self):
+    return ('all', _('All'))
+
+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',
+        widget=forms.CheckboxSelectMultiple
+    )
+
+    def clean_email(self):
+        data = self.cleaned_data.get('email')
+        try:
+            Contact.objects.get(email=data)
+        except:
+            msg = _('This e-mail does not exist in our database.')
+            raise forms.ValidationError(msg)
+        return data

informativo/models.py

 )
 
 
-def get_default_mainlist():
+def get_default_mailinglist():
     ids = MailingList.objects.values_list('id', flat=True)
     if len(ids) == 1:
         return ids
         'informativo.MailingList',
         verbose_name=_('mailing lists'),
         related_name='subscribers',
-        default=get_default_mainlist
+        default=get_default_mailinglist
     )
 
     class Meta:

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'),
 )

informativo/views.py

     template_name = 'informativo/subscribe_form.html'
     model = models.Contact
 
+    def get_context_data(self, **kwargs):
+        context = super(SubscribeFormView, self).get_context_data(**kwargs)
+        context['unsubscribe_form'] = forms.UnsubscribeForm
+        return context
+
     def get_success_url(self):
         return reverse('informativo-subscribed')
 
 
 class SubscribedView(generic.base.TemplateView):
     template_name = 'informativo/subscribed.html'
+
+
+class UnsubscribeFormView(generic.edit.FormView):
+    form_class = forms.UnsubscribeForm
+    template_name = 'informativo/unsubscribe_form.html'
+    model = models.Contact
+
+    def form_valid(form):
+        response = super(UnsubscribeFormView, self).form_valid(form)
+        self.unsubscribe(form.data)
+        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'