-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
118 lines (88 loc) · 3.18 KB
/
index.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from google.appengine.api import memcache, users
from google.appengine.ext.db import Key
from model import Opinion, Group
import webapp2
import cgi, jinja2, logging, os
jinja2_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user:
u_link = ('<a href=\"%s\">Sign out</a>' % users.create_logout_url('/'))
c = memcache.get('top')
if c == None:
opinions = Opinion.gql("WHERE archived = :1 ORDER BY vote DESC, created DESC", False)
template_values = { 'opinions' : opinions, 'u_link' : u_link }
template = jinja2_environment.get_template('html/index.html')
t = template.render(template_values)
memcache.set('top', t, time=3600)
self.response.out.write(t)
else:
self.response.out.write(c)
else:
u_link = ('<a href=\"%s\">sign in</a>' % users.create_login_url('/'))
self.response.out.write('<html><body>%s</body></html>' % u_link)
class OpinionPage(webapp2.RequestHandler):
def get(self):
g_key = ''
g_name = ''
if self.request.get('g') == None or self.request.get('g') == '':
opinions = Opinion.all()
else:
g = Group.get(self.request.get('g'))
opinions = Opinion.gql('WHERE group = :1', g)
g_key = g.key()
g_name = g.name
template_values = { 'opinions' : opinions, 'g_key' : g_key, 'g_name' : g_name }
template = jinja2_environment.get_template('html/opinion.html')
t = template.render(template_values)
self.response.out.write(t)
def _put(self):
o = Opinion.get(self.request.get('_k'))
if self.request.get('_t') == 'v':
o.vote += 1
o.put()
elif self.request.get('_t') == 'f':
o.archived = True
o.put()
def _delete(self):
if self.request.get('_t') == 'd':
o = Opinion.get(self.request.get('_k'))
o.delete()
def post(self):
if self.request.get('_m') == 'put':
self._put()
elif self.request.get('_m') == 'delete':
self._delete()
else:
o = Opinion(
title = cgi.escape(self.request.get('title')),
description = cgi.escape(self.request.get('description')),
author = users.get_current_user(),
group = Key(self.request.get('g'))
)
o.put()
memcache.delete('top')
self.redirect('/opinions?g=%s' % self.request.get('g'))
class GroupPage(webapp2.RequestHandler):
def get(self):
template = jinja2_environment.get_template('html/group.html')
gs = Group.all()
template_values = { 'groups' : gs }
t = template.render(template_values)
self.response.out.write(t)
def post(self):
g = Group(
name=cgi.escape(self.request.get('name')),
owner=users.get_current_user()
)
g.put()
self.redirect('/groups')
class TaskPage(webapp2.RequestHandler):
def get(self):
if (self.request.get('t') == 'update_schema'):
opinions = Opinion.all()
for o in opinions:
o.put()
app = webapp2.WSGIApplication([('/', MainPage), ('/opinions', OpinionPage), ('/groups', GroupPage), ('/tasks', TaskPage)],
debug=True)