fix after pybabel compile
[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 from flaskext.babel import gettext
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').fetchall():
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 date_cursor = date_today
22 is_anniversary = False
23 while not is_anniversary and date_cursor <= date_end_vote:
24 date_cursor = date_cursor + timedelta(days=7)
25 print date_cursor
26 if date_cursor >= date_end_vote and date_cursor <= date_end_vote + timedelta(days=1):
27 print "Is anniversary"
28 is_anniversary = True
29
30 if date_today >= date_begin_reminder and date_today <= date_end_vote or is_anniversary:
31 voting_group = (vote[1],)
32 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():
33 userchoice_request = (user[0], vote[0],)
34 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()
35 print "Checking if user %s already voted %s" % (user[1].encode('utf-8'), vote[3].encode('utf-8'))
36 print userchoice
37 if userchoice is None:
38 #user didn't vote yet
39 link = VOTE_URL % vote[0]
40 BODY = string.join((
41 "From: %s" % EMAIL,
42 "To: %s" % user[1],
43 "Subject: [Cavote] %s" % gettext(u"Vote reminder - You didn't take part to it"),
44 "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'),
45 "Content-type: text/plain; charset=utf-8",
46 "X-Mailer: %s" % VERSION,
47 "",
48 "%s %s : %s" % (gettext(u"A vote concerns you and is going to terminate on"), vote[2], vote[3]),
49 gettext(u"You still didn't take part to it !"),
50 "",
51 gettext(u"This link will bring you to the form where you will be able to participate :"),
52 link,
53 "",
54 gettext(u"If you think this mail is not for you, please ignore and delete it.")
55 ), "\r\n")
56 server = smtplib.SMTP(SMTP_SERVER)
57 print EMAIL
58 print user[1]
59 print BODY.encode('utf-8')
60 server.sendmail(EMAIL, user[1], BODY.encode('utf-8'))
61 server.quit()
62 else:
63 link = VOTE_URL % vote[0]
64 BODY = string.join((
65 "From: %s" % EMAIL,
66 "To: %s" % user[1],
67 "Subject: [Cavote] %s" % gettext(u"Vote reminder - Last days to modify your choice"),
68 "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'),
69 "Content-type: text/plain; charset=utf-8",
70 "X-Mailer: %s" % VERSION,
71 "",
72 "%s %s : %s" % (gettext(u"A vote concerns you and is going to terminate on "), vote[2], vote[3]),
73 gettext(u"You have already voted but you can still modify your choice"),
74 "",
75 gettext(u"This link will bring you to the form where you will be able to participate :"),
76 link,
77 "",
78 gettext(u"If you think this mail is not for you, please ignore and delete it.")
79 ), "\r\n")
80 server = smtplib.SMTP(SMTP_SERVER)
81 print EMAIL
82 print user[1]
83 print BODY.encode('utf-8')
84 server.sendmail(EMAIL, user[1], BODY.encode('utf-8'))
85 server.quit()