-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (40 loc) · 1.42 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
import os
import webapp2
import jinja2
from google.appengine.ext import db
template_dir = os.path.join(os.path.dirname(__file__), '')
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir),
autoescape=True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.write(*a, **kw)
def render_str(self,template,**params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class Art(db.Model):
title = db.StringProperty(required=True)
art = db.TextProperty(required=True)
created = db.DateTimeProperty(auto_now_add = True)
class MainPage(Handler):
def render_front(self, title="", artwork="", error=""):
arts = db.GqlQuery("SELECT * FROM Art ORDER BY created DESC")
self.render("main.html", title=title, artwork=artwork, error=error, arts=arts)
def get(self):
# self.render("front.html")
self.render_front()
def post(self):
title = self.request.get('title')
art = self.request.get('art')
if title and art:
# self.write('Thanks!')
# Create an instance 'a' of art object
a = Art(title=title, art=art)
# Store our new art object instance in the database
a.put()
self.redirect('/')
else:
error = "We need both, a title and an artwork!"
self.render_front(title=title, artwork=art, error=error)
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)