From d2e694038237c0a7670894d1dcdf31cf6a42654e Mon Sep 17 00:00:00 2001 From: Christophe Siraut Date: Sat, 11 May 2013 16:19:57 +0200 Subject: [PATCH] Add templates and plumbing for notifications. --- .gitignore | 2 +- README | 14 +++++++++++++- accounts/forms.py | 2 +- accounts/models.py | 2 +- accounts/views.py | 17 +++++++++++++++++ sondage/views.py | 23 +++++++++++++---------- templates/form.html | 18 ++++++++++++++++++ templates/index.html | 1 + templates/registration/login.html | 2 +- urls.py | 1 + 10 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 templates/form.html diff --git a/.gitignore b/.gitignore index 116a32c..b39ce4a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ *.sqlite *.pyc *~ -fix +fix* settings.py staticroot diff --git a/README b/README index 86400eb..db3ebfe 100644 --- a/README +++ b/README @@ -97,7 +97,7 @@ Fix permission for apache user sudo chown -R www-data /usr/local/lib/nuages -Set domain name: login to the /admin/ url of your installation, modify the "Site" entry from "example.com" to your domain name. +Set domain name: login to /admin/sites/site/1/ and modify the entry from "example.com" to your domain name. Email Service ============= @@ -113,3 +113,15 @@ Another option is to add attributes in settings.py, this make email work only wh EMAIL_HOST_PASSWORD = 'password' EMAIL_USE_TLS = False +Migrations +========== + +When models change, we need to perform some manual steps to keep existing data: + + # mkdir fixture + # ./manage.py dumpdata sondage --indent=2 > fixture/sondage.json + # ./manage.py dumpdata accounts --indent=2 > fixture/accounts.json + # git pull + # ./manage.py reset sondage accounts + # ./manage.py syncdb + # ./manage.py loaddata < fixture/*.json diff --git a/accounts/forms.py b/accounts/forms.py index bcba967..ad570a2 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -4,5 +4,5 @@ from accounts.models import UserProfile class UserProfileForm(ModelForm): class Meta: model = UserProfile - fields = ('email_notification',) + fields = ('email_notifications',) diff --git a/accounts/models.py b/accounts/models.py index ae993af..412a8ce 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -4,7 +4,7 @@ from django.contrib.auth.signals import user_logged_in class UserProfile(models.Model): user = models.OneToOneField(User) - email_notification = models.BooleanField() + email_notifications = models.BooleanField() def login_handler(user, **kwargs): try: diff --git a/accounts/views.py b/accounts/views.py index 60bf1ba..76d2737 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -1,6 +1,10 @@ from django.conf import settings +from django.shortcuts import render, HttpResponseRedirect +from django.core.urlresolvers import reverse from django.contrib.auth.models import User +from django.contrib.auth.decorators import login_required from django.utils.translation import ugettext_lazy +from accounts.forms import UserProfileForm def _(string): return unicode(ugettext_lazy(string)) @@ -16,3 +20,16 @@ def email_notify(poll, voter): for choice in poll.choice_set.all(): message += "%s: %i\n" % (choice.choice, choice.votecount) poll.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL) + +@login_required +def profile(request): + if request.method == 'POST': + form = UserProfileForm(request.POST) + if form.is_valid(): + for k,v in form.cleaned_data.iteritems(): + setattr(request.user.userprofile, k, v) + request.user.userprofile.save() + return HttpResponseRedirect(reverse('home')) + form = UserProfileForm() + return render(request, "form.html", {'form': form}) + diff --git a/sondage/views.py b/sondage/views.py index 2f15f8e..af7122f 100644 --- a/sondage/views.py +++ b/sondage/views.py @@ -11,7 +11,7 @@ from sondage.models import Poll, Choice, Vote, Bulletin from sondage.forms import PollForm, ChoiceForm, VoteForm, BulletinForm from django.views.generic.create_update import update_object from django.contrib.auth.decorators import login_required -#from django.contrib.sites.models import Site +from django.utils.translation import ugettext_lazy as _ from django.conf import settings from accounts.views import email_notify from accounts.forms import UserProfileForm @@ -119,7 +119,7 @@ def editchoices(request, poll_id): return HttpResponseRedirect(redir) else: #vforms=OrderedItemFormset(request.POST, instance=poll) - error_message = "There are some errors in the form you posted." + error_message = _("There are some errors in the form you posted.") vforms=instances else: @@ -220,16 +220,19 @@ def vote(request, poll_id): old.voice = vorm.cleaned_data['voice'] old.comment = vorm.cleaned_data['comment'] old.save() - error_message = "Your vote has been updated, thank you." - if has_voted: - if poll.user: - email_notify(poll, voter) + error_message = _("Your vote has been updated, thank you.") + if has_voted and poll.user: + try: + if poll.user.userprofile.email_notifications: + email_notify(poll, voter) + except: + pass else: - error_message = 'Did you forget to provide your name?' + error_message = _('Did you forget to provide your name?') else: #error_message = form.errors - error_message = 'Did you forget to provide your name?' - voter = 'your name' + error_message = _('Did you forget to provide your name?') + voter = _('your name') else: # request.method = 'GET' @@ -258,7 +261,7 @@ def vote(request, poll_id): pass else: voter = '' - error_message = "Login let you modify your vote anytime." + error_message = _("Login let you modify your vote anytime.") key = 'has_voted-' + poll.id if request.session.get(key, False): has_voted = True # Used to show "Forget me" diff --git a/templates/form.html b/templates/form.html new file mode 100644 index 0000000..ffcd3e4 --- /dev/null +++ b/templates/form.html @@ -0,0 +1,18 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block content %} +
+{% csrf_token %} +{% for field in form %} +
+ {{ field.errors }} + {{ field.label_tag }}: {{ field }} {{ field.help_text }} +
+{% endfor %} +
+
+

+

+
+{% endblock %} diff --git a/templates/index.html b/templates/index.html index 0e5e851..b6d4c0a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -49,6 +49,7 @@

{% trans "My account" %}