reminder.py 4.2 KB

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