-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·56 lines (43 loc) · 1.4 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
#!/usr/bin/env python
import os
import wsgiref.handlers
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.ext import db
# Email specific imports
import email
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
# Markdown2
import markdown2
def render(template_name, values):
path = os.path.join('templates', template_name)
return template.render(path, values)
class Post(db.Model):
title = db.StringProperty()
body = db.TextProperty()
added = db.DateTimeProperty(auto_now_add=True)
author = db.StringProperty()
class EmailHandler(InboundMailHandler):
def receive(self, mail_message):
post = Post()
post.title = mail_message.subject
for content_type, body in mail_message.bodies():
post.body = markdown2.markdown(body.decode())
post.author = 'John Smith'
post.put()
class Index(webapp.RequestHandler):
def get(self):
posts = Post.all().order('-added').fetch(10)
values = {
'posts': posts,
}
self.response.out.write(render('index.html', values))
application = webapp.WSGIApplication([
('/', Index),
EmailHandler.mapping()
], debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()