reminder.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 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
  54. server.sendmail(EMAIL, user[1], BODY.encode('utf-8'))
  55. server.quit()
  56. else:
  57. BODY = string.join((
  58. "From: %s" % EMAIL,
  59. "To: %s" % user[1],
  60. "Subject: [Cavote] %s" % gettext(u"Vote reminder - Last days to modify your choice"),
  61. "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'),
  62. "Content-type: text/plain; charset=utf-8",
  63. "X-Mailer: %s" % VERSION,
  64. "",
  65. "%s %s : %s" % (gettext(u"A vote concerns you and is going to terminate on "), vote[2], vote[3]),
  66. gettext(u"You have already voted but you can still modify you choice"),
  67. "",
  68. gettext(u"This link will bring you to the form where you will be able to participate :"),
  69. link,
  70. "",
  71. gettext(u"If you think this mail is not for you, please ignore and delete it.")
  72. ), "\r\n")
  73. server = smtplib.SMTP(SMTP_SERVER)
  74. print EMAIL
  75. print user[1]
  76. print BODY
  77. server.sendmail(EMAIL, user[1], BODY.encode('utf-8'))
  78. server.quit()