reminder.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. if date_today >= date_begin_reminder and date_today <= date_end_vote:
  16. voting_group = (vote[1],)
  17. 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():
  18. userchoice_request = (user[0], vote[0],)
  19. 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()
  20. print "Checking if user %s already voted %s" % (user[1], vote[3])
  21. print userchoice
  22. if userchoice is None:
  23. #user didn't vote yet
  24. link = "http://vote.ffdn.org/vote/%d" % vote[0]
  25. BODY = string.join((
  26. "From: %s" % EMAIL,
  27. "To: %s" % user[1],
  28. "Subject: [Cavote] Vote reminder - You didn't take part to it",
  29. "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'),
  30. "X-Mailer: %s" % VERSION,
  31. "",
  32. "A vote concerns you and is going to terminate on %s : %s" % (vote[2], vote[3]),
  33. "You still didn't take part to it !",
  34. "",
  35. "This link will bring you to the form where you will be able to participate :",
  36. link,
  37. "",
  38. "If you think this mail is not for you, please ignore and delete it."
  39. ), "\r\n")
  40. server = smtplib.SMTP(SMTP_SERVER)
  41. print EMAIL
  42. print user[1]
  43. print BODY
  44. server.sendmail(EMAIL, user[1], BODY)
  45. server.quit()
  46. else:
  47. BODY = string.join((
  48. "From: %s" % EMAIL,
  49. "To: %s" % user[1],
  50. "Subject: [Cavote] Vote reminder - Last days to modify your choice",
  51. "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'),
  52. "X-Mailer: %s" % VERSION,
  53. "",
  54. "A vote concerns you and is going to terminate on %s : %s" % (vote[2], vote[3]),
  55. "You have already voted by can still modify you choice",
  56. "",
  57. "This link will bring you to the form where you will be able to participate :",
  58. link,
  59. "",
  60. "If you think this mail is not for you, please ignore and delete it."
  61. ), "\r\n")
  62. server = smtplib.SMTP(SMTP_SERVER)
  63. print EMAIL
  64. print user[1]
  65. print BODY
  66. server.sendmail(EMAIL, user[1], BODY)
  67. server.quit()