1. Guilherme Gondim
  2. django-informativo

Commits

Guilherme Gondim  committed 70a9046

add forms and views form subscription

  • Participants
  • Parent commits e50a535
  • Branches default

Comments (0)

Files changed (4)

File informativo/forms.py

View file
  • Ignore whitespace
+from django import forms
+
+from informativo.models import Contact
+
+
+class SubscribeForm(forms.ModelForm):
+
+    class Meta:
+        model = Contact
+        widgets = {'mailing_lists': forms.CheckboxSelectMultiple}

File informativo/models.py

View file
  • Ignore whitespace
 )
 
 
+def get_default_mainlist():
+    ids = MailingList.objects.values_list('id', flat=True)
+    if len(ids) == 1:
+        return ids
+    return []
+
 class Contact(models.Model):
-    email = models.EmailField(_('e-mail'), unique=True)
     first_name = models.CharField(_('first name'), max_length=64, blank=True)
     last_name = models.CharField(_('last name'), max_length=64, blank=True)
+    email = models.EmailField(_('e-mail'), unique=True)
     mailing_lists = models.ManyToManyField(
         'informativo.MailingList',
         verbose_name=_('mailing lists'),
-        related_name='subscribers'
+        related_name='subscribers',
+        default=get_default_mainlist
     )
 
     class Meta:

File informativo/urls.py

View file
  • Ignore whitespace
+# -*- coding: utf-8 -*-
+
+from django.conf.urls import patterns, url
+
+from informativo import views
+
+
+urlpatterns = patterns('',
+    url('^$', views.SubscribeFormView.as_view(), name='informativo-subscribe'),
+    url('^subscribed/$', views.SubscribedView.as_view(), name='informativo-subscribed'),
+)

File informativo/views.py

View file
  • Ignore whitespace
-# Create your views here.
+from django.core.urlresolvers import reverse
+from django.views import generic
+
+from informativo import forms
+from informativo import models
+
+
+class SubscribeFormView(generic.edit.CreateView):
+    form_class = forms.SubscribeForm
+    template_name = 'informativo/subscribe_form.html'
+    model = models.Contact
+
+    def get_success_url(self):
+        return reverse('informativo-subscribed')
+
+
+class SubscribedView(generic.base.TemplateView):
+    template_name = 'informativo/subscribed.html'