-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
98 lines (88 loc) · 3.48 KB
/
main.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
95
96
97
98
#!/usr/bin/env python
import re
import os
import logging
from datetime import datetime, timedelta
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.api import memcache
from google.appengine.api import mail
from models import Message
import settings
HTML_ELEMENTS = re.compile(r'<(/?).*?>')
class Index(webapp.RequestHandler):
"""
This is just the demo application and isn't needed if you
are creating your own applications
"""
def get(self):
"Displays the home page, which lists the latest messages"
# lets have some basic cachine just in case
output = memcache.get("index")
if output is None:
# we're only showing messages from within the last five days
# this is for demonstration purposes only and stops
# the demo getting swamped with messages
yesterday = datetime.today() - timedelta(5)
# get all messages posted in the last day and order them
# by the date posted
messages = Message.all()
messages.order('-date')
messages.filter('date >', yesterday)
# prepare the context for the template
context = {
'messages': messages,
'debug': settings.DEBUG,
}
# calculate the template path
path = os.path.join(os.path.dirname(__file__), 'templates',
'index.html')
# render the template with the provided context
output = template.render(path, context)
memcache.add("index", output, settings.CACHE_TIME)
self.response.out.write(output)
class IMified(webapp.RequestHandler):
"This is the endpoint for the message from IMified"
def post(self):
"We receive post data from IMified"
userkey = self.request.get('userkey')
network = self.request.get('network')
# remove any markup from messages
msg = HTML_ELEMENTS.sub('', self.request.get('msg'))
step = self.request.get('step')
try:
# we try and create the message
message = Message(
userkey = userkey,
network = network,
msg = msg,
step = int(step)
)
message.put()
# send an email with the message on
mail.send_mail(sender="[email protected]",
to="Gareth Rushgrove <[email protected]>",
subject="New message posted on IMified demo",
body=msg)
# we just added a message so we need to clear our cache
memcache.delete("index")
# simple logging
logging.info("Saved new message")
# the response is send as an IM message to the sender
self.response.out.write('Message saved')
except:
# we'll log the error if it occurs
# and report we couldn't save the message to the sender
logging.error("Error occured creating new message")
self.response.out.write('An error ocured, message not saved')
# wire up the views
application = webapp.WSGIApplication([
('/', Index),
('/endpoint', IMified),
], debug=True)
def main():
"Run the application"
run_wsgi_app(application)
if __name__ == '__main__':
main()