Commit 4a168dd6 authored by Christophe Siraut's avatar Christophe Siraut

Merge branch 'emailnotify'

parents a3d9d19a 88c43c09
*.sqlite
*.pyc
*~
fix
fix*
settings.py
staticroot
local_settings.py
......@@ -98,6 +98,8 @@ Fix permission for apache user
sudo chown -R www-data /usr/local/lib/nuages
Set domain name: login to /admin/sites/site/1/ and modify the entry from "example.com" to your domain name.
Email Service
=============
......@@ -112,3 +114,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
from models import *
from django.contrib import admin
admin.site.register(UserProfile)
from django.forms import ModelForm
from accounts.models import UserProfile
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ('email_notifications',)
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.signals import user_logged_in
class UserProfile(models.Model):
user = models.OneToOneField(User)
email_notifications = models.BooleanField()
def login_handler(user, **kwargs):
try:
profile = UserProfile.objects.get(user=user)
except:
profile = UserProfile(user=user)
profile.save()
return
user_logged_in.connect(login_handler)
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
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))
def email_notify(poll, voter):
subject = _("Nuages email notification")
message = _("User %s has voted.") % voter
message += "\n\n"
message += _("The link to your poll: %s") % poll.link
message += "\n\n"
message += _("Current results:")
message += "\n\n"
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(instance=request.user.userprofile)
return render(request, "form.html", {'form': form})
......@@ -128,7 +128,8 @@ INSTALLED_APPS = (
'django.contrib.admindocs',
'sondage',
'request',
'registration'
'registration',
'accounts'
)
try:
......
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
import os
from binascii import hexlify
from django.db import models
......@@ -15,10 +17,15 @@ class Poll(models.Model):
upd_date = models.DateField(auto_now=True)
author = models.CharField(max_length=40)
description = models.CharField(max_length=300)
user = models.ForeignKey(User,blank=True,null=True)
def __unicode__(self):
return self.title
@property
def link(self):
return 'http://%s/%s/' % (Site.objects.get_current(), self.id)
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.DateTimeField()
......
......@@ -11,18 +11,22 @@ 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
def new(request):
if request.method == 'POST': # If the form has been submitted...
instance = Poll(author=str(request.user))
if request.user.is_authenticated():
instance = Poll(author=str(request.user), user=request.user)
else:
instance = Poll(author=str(request.user))
form = PollForm(request.POST, instance=instance) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
poll_id = form.cleaned_data.get('title') # Process the data in form.cleaned_data
new_poll = form.save()
#if not request.user.is_authenticated(): # Anonymous wants to create a poll...
key = 'is_' + new_poll.id + '_author'
request.session[key] = True # This writes cookie
......@@ -115,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:
......@@ -216,13 +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."
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'
......@@ -251,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"
......
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<form action="." method="post">
{% csrf_token %}
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }} {{ field.help_text }}
</div>
{% endfor %}
<br />
<div class="form-actions">
<p><input type="submit" value={% trans 'Send' %} /><p>
</div>
</form>
{% endblock %}
......@@ -26,37 +26,35 @@
<br /><div style="display:block;clear:both"></div> <br /><br />
{% block userspecific %}
{% if user.is_authenticated %}
<h2>{% trans "My polls" %}</h2>
{% if object_list %}
<ul>
{% for object in object_list %}
{% ifequal object.author user.username %}
<li><a href="{{ object.id }}/">{{ object.title }}</a> :
<a href="{{ object.id }}/edit/">{% trans "edit" %}</a> -
<a href="{{ object.id }}/delete/" onclick="return confirm('{% trans "Are you sure you want to delete this poll?" %}');">{% trans "delete" %}</a></li>
{% endifequal %}
{% with user.poll_set.all as polls %}
{% if polls %}
<ul>
{% for poll in polls %}
<li><a href="{{ poll.link }}">{{ poll.title }}</a> :
<a href="{{ poll.link }}edit/">{% trans "edit" %}</a> -
<a href="{{ poll.link }}delete/" onclick="return confirm('{% trans "Are you sure you want to delete this poll?" %}');">{% trans "delete" %}</a></li>
{% endfor %}
</ul>
{% else %}
<p>{% trans "No polls are available." %}</p>
<p>{% trans "No poll created yet." %}</p>
{% endif %}
{% endwith %}
<br />
<h2>{% trans "My account" %}</h2>
<ul>
<li>{% trans "Username" %}: {{user}}</li>
<li><a href="{% url 'profile' %}">{% trans 'Email notifications' %}</a>: {{ user.userprofile.email_notifications }}</li>
<li><a href="{% url 'auth_password_change' %}">{% trans 'Change password' %}</a></li>
<!--<li><a href="/user/email/change/">{% trans 'Change email address' %}</a></li>-->
<li><a href="{% url 'auth_logout' %}">{% trans 'Logout' %}</a></li>
</ul>
{% else %}
<h2>{% trans "Examples" %}</h2>
<ul>
......@@ -64,6 +62,7 @@
</ul>
{% endif %}
{% endblock %}
<br /><br /><br /><br />
......
......@@ -9,7 +9,7 @@
<input type="submit" value="{% trans 'Log in' %}" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
<br />
<p>{% trans "Forgot password" %}? <a href="{% url auth_password_reset %}">{% trans "Reset it" %}</a>!</p>
<p>{% trans "Not member" %}? <a href="{% url registration_register %}">{% trans "Register" %}</a>!</p>
{% endblock %}
......@@ -19,12 +19,8 @@ poll_dict = {
}
urlpatterns = patterns('',
# Example:
# (r'^nuage/', include('nuage.foo.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^$', 'django.views.generic.list_detail.object_list', dict(info_dict, template_name='index.html'), name='home'),
url(r'^nuages$', 'django.views.generic.simple.direct_to_template', {'template': 'nuages.html'}, name='documentation'),
url(r'^(?P<lang_id>\w{2})/nuages$', 'sondage.views.translation'),
......@@ -35,18 +31,9 @@ urlpatterns = patterns('',
url(r'^(?P<poll_id>\w+)/edit/choices/$', 'sondage.views.editchoices'),
url(r'^(?P<poll_id>\w{4})/vote/$', 'sondage.views.vote'),
url(r'^(?P<poll_id>\w+)/csv/$', 'sondage.views.exp_csv'),
url(r'^accounts/profile/$', 'accounts.views.profile', name='profile'),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^(?P<poll_id>\w{4})/clear/', 'sondage.userviews.clear_cookie'),
)
'''
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('',
(r'^%s/(?P<path>.*)$' % settings.MEDIA_URL[1:-1],
'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),
)
'''
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