Commit 6881bb9c authored by fred's avatar fred

add model, views and forms for writing newsletters

parent 74608485
from django import forms from django import forms
from .models import Subscriber from .models import Subscriber, Newsletter
class SubscribeForm(forms.ModelForm): class SubscribeForm(forms.ModelForm):
class Meta: class Meta:
model = Subscriber model = Subscriber
exclude = ('inscription_date', 'is_validated', 'is_registered', 'password') exclude = ('inscription_date', 'is_validated', 'is_registered', 'password')
class NewsletterForm(forms.ModelForm):
class Meta:
model = Newsletter
exclude = ('expedition_datetime', )
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'Subscriber'
db.create_table(u'newsletter_subscriber', (
(u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('email', self.gf('django.db.models.fields.EmailField')(unique=True, max_length=75)),
('inscription_date', self.gf('django.db.models.fields.DateField')(auto_now_add=True, blank=True)),
('is_validated', self.gf('django.db.models.fields.NullBooleanField')(null=True, blank=True)),
('is_registered', self.gf('django.db.models.fields.NullBooleanField')(null=True, blank=True)),
('password', self.gf('django.db.models.fields.CharField')(max_length=100)),
))
db.send_create_signal(u'newsletter', ['Subscriber'])
def backwards(self, orm):
# Deleting model 'Subscriber'
db.delete_table(u'newsletter_subscriber')
models = {
u'newsletter.subscriber': {
'Meta': {'object_name': 'Subscriber'},
'email': ('django.db.models.fields.EmailField', [], {'unique': 'True', 'max_length': '75'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'inscription_date': ('django.db.models.fields.DateField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'is_registered': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}),
'is_validated': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '100'})
}
}
complete_apps = ['newsletter']
\ No newline at end of file
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'Newsletter'
db.create_table(u'newsletter_newsletter', (
(u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('subject', self.gf('django.db.models.fields.CharField')(max_length=50)),
('date', self.gf('django.db.models.fields.DateField')()),
('text', self.gf('ckeditor.fields.RichTextField')(null=True, blank=True)),
('expedition_date', self.gf('django.db.models.fields.DateField')(null=True, blank=True)),
))
db.send_create_signal(u'newsletter', ['Newsletter'])
def backwards(self, orm):
# Deleting model 'Newsletter'
db.delete_table(u'newsletter_newsletter')
models = {
u'newsletter.newsletter': {
'Meta': {'ordering': "['date']", 'object_name': 'Newsletter'},
'date': ('django.db.models.fields.DateField', [], {}),
'expedition_date': ('django.db.models.fields.DateField', [], {'null': 'True', 'blank': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'subject': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'text': ('ckeditor.fields.RichTextField', [], {'null': 'True', 'blank': 'True'})
},
u'newsletter.subscriber': {
'Meta': {'object_name': 'Subscriber'},
'email': ('django.db.models.fields.EmailField', [], {'unique': 'True', 'max_length': '75'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'inscription_date': ('django.db.models.fields.DateField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'is_registered': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}),
'is_validated': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '100'})
}
}
complete_apps = ['newsletter']
\ No newline at end of file
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Deleting field 'Newsletter.expedition_date'
db.delete_column(u'newsletter_newsletter', 'expedition_date')
# Adding field 'Newsletter.expedition_datetime'
db.add_column(u'newsletter_newsletter', 'expedition_datetime',
self.gf('django.db.models.fields.DateTimeField')(null=True, blank=True),
keep_default=False)
def backwards(self, orm):
# Adding field 'Newsletter.expedition_date'
db.add_column(u'newsletter_newsletter', 'expedition_date',
self.gf('django.db.models.fields.DateField')(null=True, blank=True),
keep_default=False)
# Deleting field 'Newsletter.expedition_datetime'
db.delete_column(u'newsletter_newsletter', 'expedition_datetime')
models = {
u'newsletter.newsletter': {
'Meta': {'ordering': "['date']", 'object_name': 'Newsletter'},
'date': ('django.db.models.fields.DateField', [], {}),
'expedition_datetime': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'subject': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'text': ('ckeditor.fields.RichTextField', [], {'null': 'True', 'blank': 'True'})
},
u'newsletter.subscriber': {
'Meta': {'object_name': 'Subscriber'},
'email': ('django.db.models.fields.EmailField', [], {'unique': 'True', 'max_length': '75'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'inscription_date': ('django.db.models.fields.DateField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'is_registered': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}),
'is_validated': ('django.db.models.fields.NullBooleanField', [], {'null': 'True', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '100'})
}
}
complete_apps = ['newsletter']
\ No newline at end of file
...@@ -9,9 +9,12 @@ from django.db import models ...@@ -9,9 +9,12 @@ from django.db import models
from django.template import loader, Context from django.template import loader, Context
from django.conf import settings from django.conf import settings
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail from django.core.mail import send_mail
# rajout d'un commentaire inutile
from ckeditor.fields import RichTextField
class Subscriber(models.Model) : class Subscriber(models.Model) :
email = models.EmailField(unique = True) # TODO : informer si déjà inscrit ? Que faire dans ce cas. email = models.EmailField(unique = True) # TODO : informer si déjà inscrit ? Que faire dans ce cas.
...@@ -44,3 +47,17 @@ class Subscriber(models.Model) : ...@@ -44,3 +47,17 @@ class Subscriber(models.Model) :
return return
self.is_registered = True self.is_registered = True
self.save() self.save()
class Newsletter(models.Model):
class Meta:
verbose_name = _('Newsletter')
verbose_name_plural = _('Newsletters')
ordering = ['date']
subject = models.CharField(_('Title'), max_length=50)
date = models.DateField(_('Date'))
text = RichTextField(_('Text'), null=True, blank=True)
expedition_datetime = models.DateTimeField(_('Expedition Date/time'), null=True, blank=True)
{% extends "base.html" %}
{% block appbar %}
<h2>{{ object.date }}</h2>
{% endblock %}
{% block content %}
{% if perms.newsletter.add_newsletter %}
<p>
<a class="big-friendly-button" href="{% url 'newsletter-edit' pk=object.id %}">Modifier</a>
<a class="big-friendly-button" href="#">Envoyer</a>
</p>
{% endif %}
{% if newsletter.text %}
{{ newsletter.text|safe }}
{% endif %}
{% endblock %}
{% extends "base.html" %}
{% block extrascripts %}
<script src="{{ STATIC_URL }}ckeditor/ckeditor/ckeditor.js">
</script>
{% endblock %}
{% block appbar %}
{% if object %}
<h2>{{ object.date }}</h2>
{% else %}
<h2>Nouvelle newsletter</h2>
{% endif %}
{% endblock %}
{% block content %}
<form method="post">
<div id="form-content">
{% csrf_token %}
{{ form.as_p }}
</div>
{% block buttons %}
<button class="enable-on-change">Enregistrer</button>
{% endblock %}
</form>
{% endblock %}
{% extends "base.html" %}
{% block appbar %}
<h2>Newsletters</h2>
</span>
{% endblock %}
{% block content %}
{% if perms.newsletter.add_newsletter %}
<p>
<a class="big-friendly-button" href="{% url 'newsletter-create' %}">Nouvelle</a>
</p>
{% endif %}
<ul class="emission-list">
{% for newsletter in object_list %}
<li><a href="{% url 'newsletter-view' pk=newsletter.id %}/">{{ newsletter.date }}</a></li>
{% endfor %}
</ul>
{% endblock %}
...@@ -8,3 +8,10 @@ urlpatterns = patterns('', ...@@ -8,3 +8,10 @@ urlpatterns = patterns('',
url(r'^(?P<token>[0-9a-f]{40})$', 'newsletter.views.confirmation', name='newsletter-confirmation'), url(r'^(?P<token>[0-9a-f]{40})$', 'newsletter.views.confirmation', name='newsletter-confirmation'),
url(r'^unsubscription/$', unsubscription), url(r'^unsubscription/$', unsubscription),
) )
management_patterns = patterns('newsletter.views',
url(r'^$', 'newsletter_list', name='newsletter-list'),
url(r'^add$', 'newsletter_create', name='newsletter-create'),
url(r'^(?P<pk>\d+)/$', 'newsletter_view', name='newsletter-view'),
url(r'^(?P<pk>\d+)/edit$', 'newsletter_update', name='newsletter-edit'),
)
...@@ -6,18 +6,24 @@ import socket ...@@ -6,18 +6,24 @@ import socket
import urllib import urllib
import urllib2 import urllib2
import cookielib import cookielib
import datetime
from smtplib import SMTPException from smtplib import SMTPException
from django.conf import settings from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.db import models, IntegrityError from django.db import models, IntegrityError
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView
from .forms import SubscribeForm from .forms import SubscribeForm, NewsletterForm
from .models import Subscriber from .models import Subscriber, Newsletter
class SubscribeView(CreateView): class SubscribeView(CreateView):
...@@ -79,3 +85,45 @@ def unsubscription(request) : ...@@ -79,3 +85,45 @@ def unsubscription(request) :
except Subscriber.DoesNotExist : except Subscriber.DoesNotExist :
error_message = _("The entered email address does not exist. Please try another one.") error_message = _("The entered email address does not exist. Please try another one.")
return render(request, 'unsubscription_form.html', {'form' : form, 'message' : error_message}) return render(request, 'unsubscription_form.html', {'form' : form, 'message' : error_message})
class NewsletterCreateView(CreateView):
form_class = NewsletterForm
model = Newsletter
success_url = reverse_lazy('newsletter-list')
def get_form(self, *args, **kwargs):
if not self.request.user.has_perm('newsletter.add_newsletter'):
raise PermissionDenied()
return super(NewsletterCreateView, self).get_form(*args, **kwargs)
def get_initial(self):
initial = super(NewsletterCreateView, self).get_initial()
initial['date'] = datetime.datetime.today()
return initial
newsletter_create = NewsletterCreateView.as_view()
class NewsletterListView(ListView):
model = Newsletter
newsletter_list = NewsletterListView.as_view()
class NewsletterDetailView(DetailView):
model = Newsletter
newsletter_view = NewsletterDetailView.as_view()
class NewsletterUpdateView(UpdateView):
form_class = NewsletterForm
model = Newsletter
def get_form(self, *args, **kwargs):
if not self.request.user.has_perm('newsletter.add_newsletter'):
raise PermissionDenied()
return super(NewsletterUpdateView, self).get_form(*args, **kwargs)
newsletter_update = NewsletterUpdateView.as_view()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment