reminder.py 3.7 KB

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