Mail notifications during last days of a vote.
[cavote.git] / reminder.py
1 import os
2 import hashlib
3 import smtplib
4 import string
5 from datetime import date, datetime, timedelta
6 import time
7
8 from settings import *
9
10
11 import sqlite3
12
13 conn = sqlite3.connect(DATABASE)
14 c = conn.cursor()
15
16 for vote in c.execute('select id, id_group, date_end, title from votes where is_open=1 and is_terminated=0'):
17 date_end_vote = datetime.strptime(vote[2], "%Y-%m-%d")
18 date_today = datetime.today()
19 date_begin_reminder = date_end_vote + timedelta(days=-3)
20
21 if date_today >= date_begin_reminder and date_today <= date_end_vote:
22 voting_group = (vote[1],)
23 for user in c.execute('select users.id, users.email, users.name from users join user_group on users.id=user_group.id_user where user_group.id_group = ?', voting_group).fetchall():
24 userchoice_request = (user[0], vote[0],)
25 userchoice = c.execute('select * from user_choice join choices on user_choice.id_choice = choices.id where id_user = ? and id_vote = ?', userchoice_request).fetchone()
26 print "Checking if user %s already voted %s" % (user[1], vote[3])
27 print userchoice
28 if userchoice is None:
29 #user didn't vote yet
30 link = "http://vote.ffdn.org/vote/%d" % vote[0]
31 BODY = string.join((
32 "From: %s" % EMAIL,
33 "To: %s" % user[1],
34 "Subject: [Cavote] Vote reminder - You didn't take part to it",
35 "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'),
36 "X-Mailer: %s" % VERSION,
37 "",
38 "A vote concerns you and is going to terminate on %s : %s" % (vote[2], vote[3]),
39 "You still didn't take part to it !",
40 "",
41 "This link will bring you to the form where you will be able to participate :",
42 link,
43 "",
44 "If you think this mail is not for you, please ignore and delete it."
45 ), "\r\n")
46 server = smtplib.SMTP(SMTP_SERVER)
47 print EMAIL
48 print user[1]
49 print BODY
50 server.sendmail(EMAIL, user[1], BODY)
51 server.quit()
52 else:
53 BODY = string.join((
54 "From: %s" % EMAIL,
55 "To: %s" % user[1],
56 "Subject: [Cavote] Vote reminder - Last days to modify your choice",
57 "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'),
58 "X-Mailer: %s" % VERSION,
59 "",
60 "A vote concerns you and is going to terminate on %s : %s" % (vote[2], vote[3]),
61 "You have already voted by can still modify you choice",
62 "",
63 "This link will bring you to the form where you will be able to participate :",
64 link,
65 "",
66 "If you think this mail is not for you, please ignore and delete it."
67 ), "\r\n")
68 server = smtplib.SMTP(SMTP_SERVER)
69 print EMAIL
70 print user[1]
71 print BODY
72 server.sendmail(EMAIL, user[1], BODY)
73 server.quit()