Commit df083e8e authored by Christophe Siraut's avatar Christophe Siraut
Browse files

Notification functional.

parent b4d02956
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy
def email_notify(author, voter): def _(string):
#try: return unicode(ugettext_lazy(string))
print 'a'
user = User.objects.get(username=author) def email_notify(poll, voter):
subject = "Django email notification" subject = _("Nuages email notification")
message = "User %s has voted" % voter message = _("User %s has voted") % voter
user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL) message += "\n\n"
print author, user, voter message += _("The link to your poll: %s") % poll.link
#except: message += "\n\n"
#pass message += _("Current results:")
message += "\n\n"
for choice in poll.choice_set.all():
message += "%s: %i" % (choice.choice, choice.votecount)
message += "\n\n"
poll.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.sites.models import Site
import os import os
from binascii import hexlify from binascii import hexlify
from django.db import models from django.db import models
...@@ -16,12 +17,15 @@ class Poll(models.Model): ...@@ -16,12 +17,15 @@ class Poll(models.Model):
upd_date = models.DateField(auto_now=True) upd_date = models.DateField(auto_now=True)
author = models.CharField(max_length=40) author = models.CharField(max_length=40)
description = models.CharField(max_length=300) description = models.CharField(max_length=300)
username = models.OneToOneField(User) user = models.OneToOneField(User,blank=True,null=True)
m django.contrib.auth.models import User
def __unicode__(self): def __unicode__(self):
return self.title return self.title
@property
def link(self):
return 'http://%s/%s/' % (Site.objects.get_current(), self.id)
class Choice(models.Model): class Choice(models.Model):
poll = models.ForeignKey(Poll) poll = models.ForeignKey(Poll)
choice = models.DateTimeField() choice = models.DateTimeField()
......
...@@ -13,16 +13,19 @@ from django.views.generic.create_update import update_object ...@@ -13,16 +13,19 @@ from django.views.generic.create_update import update_object
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
#from django.contrib.sites.models import Site #from django.contrib.sites.models import Site
from django.conf import settings from django.conf import settings
from accounts.views import email_notify
def new(request): def new(request):
if request.method == 'POST': # If the form has been submitted... 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 form = PollForm(request.POST, instance=instance) # A form bound to the POST data
if form.is_valid(): # All validation rules pass if form.is_valid(): # All validation rules pass
poll_id = form.cleaned_data.get('title') # Process the data in form.cleaned_data poll_id = form.cleaned_data.get('title') # Process the data in form.cleaned_data
new_poll = form.save() new_poll = form.save()
#if not request.user.is_authenticated(): # Anonymous wants to create a poll...
key = 'is_' + new_poll.id + '_author' key = 'is_' + new_poll.id + '_author'
request.session[key] = True # This writes cookie request.session[key] = True # This writes cookie
...@@ -218,8 +221,8 @@ def vote(request, poll_id): ...@@ -218,8 +221,8 @@ def vote(request, poll_id):
old.save() old.save()
error_message = "Your vote has been updated, thank you." error_message = "Your vote has been updated, thank you."
if has_voted: if has_voted:
if poll.username: if poll.user:
email_notify(poll.username, voter) email_notify(poll, voter)
else: else:
error_message = 'Did you forget to provide your name?' error_message = 'Did you forget to provide your name?'
else: else:
......
Supports Markdown
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