reminder.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_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], vote[3])
  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
  43. to terminate on"), vote[2], vote[3].encode('utf-8')),
  44. gettext(u"You still didn't take part to it !"),
  45. "",
  46. gettext(u"This link will bring you to the form where you will be able to participate :"),
  47. link,
  48. "",
  49. gettext(u"If you think this mail is not for you, please ignore and delete it.")
  50. ), "\r\n")
  51. server = smtplib.SMTP(SMTP_SERVER)
  52. print EMAIL
  53. print user[1]
  54. print BODY
  55. server.sendmail(EMAIL, user[1], BODY.encode('utf-8'))
  56. server.quit()
  57. else:
  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
  67. to terminate on "), vote[2], vote[3].encode('utf-8')),
  68. gettext(u"You have already voted but you can still modify
  69. your choice"),
  70. "",
  71. gettext(u"This link will bring you to the form where you will be able to participate :"),
  72. link,
  73. "",
  74. gettext(u"If you think this mail is not for you, please ignore and delete it.")
  75. ), "\r\n")
  76. server = smtplib.SMTP(SMTP_SERVER)
  77. print EMAIL
  78. print user[1]
  79. print BODY
  80. server.sendmail(EMAIL, user[1], BODY.encode('utf-8'))
  81. server.quit()