-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskhandlers.py
94 lines (80 loc) · 3.17 KB
/
taskhandlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#-------------------------------------------------------------------------------
# Name: taskhandler
# Purpose: handlers for tasks
#
# Author: Eric
#
# Created: 13/02/2014
# Copyright: (c) Eric 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
import webapp2
#from google.appengine.api import taskqueue
from google.appengine.ext import ndb
import models
from basehandler import BaseHandler
from google.appengine.api import mail
import datetime
class InviteStudentHandler(BaseHandler):
def get(self):
""" send invite email to confirmed students
validate header? should contain X-AppEngine-Cron: true
get students with parental consent received and confirmed
check that they haven't been sent the invite email
send the invite email
update their datastore record
???
profit!
"""
self.response.headers['Content-Type'] = 'text/plain'
# note: we may end up customizing this for each student
# we should probably also implement some email templating
sender_address = 'Call to Code Support <[email protected]>'
subject = 'Welcome to Call to Code!'
body = """
Thank you for your interest in Call to Code!
Your parental consent has been confirmed, and you are ready to activate your account!
Please login at the link below:
http://www.calltocode.ie/
Thank you and good luck in the competition!
The Call to Code Support Team
"""
students = models.Student.get_invite_email_pending()
self.response.write('emails to send: %s\n' % students.count())
for student in students:
mail.send_mail(sender_address, student.email, subject, body)
student.invitation_email_sent = True
student.invitation_email_sent_date = datetime.datetime.now()
student.put()
self.response.write('Invitation email sent to %s.\n' % student.email)
#self.response.write(body)
# note to self: this will probably go away, cause i'll use
# cron jobs (scheduled tasks) rather than queues
##class AddInviteStudentTask(BaseHandler):
##
## def get(self):
## # render a template with a button that posts back here
## params = {}
## self.render_template('add-invite-task.html', **params)
##
## def post(self):
## # add the task to the queue
## # do i need to worry about the task already being in the queue? (yes!)
## try:
## #https://developers.google.com/appengine/docs/python/taskqueue/functions#add
## taskqueue.add(queue_name='invite-email',
## name='send-student-invite-email',
## url='/tasks/invite-student',
## method='POST')
## message = 'Task added successfully.'
## self.add_message(message, 'success')
##
## except taskqueue.TombstonedTaskError, e:
## message = 'oops!'
## self.add_message(message, 'danger')
##
## except taskqueue.TaskAlreadyExistsError, e:
## message = 'Task already exists.'
## self.add_message(message, 'danger')
##
## return self.get()