Skip to content
Newer
Older
100644 183 lines (154 sloc) 5.95 KB
9915517 @jonhull First commit
jonhull authored
1 import cgi
2 from google.appengine.ext import webapp, db
3 from google.appengine.ext.webapp import util, template
4 from google.appengine.api import urlfetch, memcache, users, mail
5
9848664 fixed a bug - adding random lib
unknown authored
6 import logging, urllib, os, random
9915517 @jonhull First commit
jonhull authored
7 from datetime import datetime, timedelta
8
9 from models import Issue, Choice, Vote
10
11
12 class MainPage(webapp.RequestHandler):
13 def get(self):
14 user = users.get_current_user()
15 if user:
16 logout_url = users.create_logout_url('/')
17 else:
18 login_url = users.create_login_url('/')
19 issues = Issue.all().order('creation_date').fetch(30)
20 success_type = self.request.get('success')
21 success_msg = None
22 if success_type == 'vote':
23 success_msg = 'Your vote was successfully cast!'
24 if success_type == 'updated':
25 success_msg = 'Your vote was successfully updated!'
26 created_by = Issue.issues_created_by(member=user,limit=20)
27 voted_on = Issue.issues_voted_on(member=user,limit=20)
28 #recent_results = [issue for issue in voted_on if issue.has_results]
29 recent_voted = [issue for issue in voted_on if issue.is_active()]
30 recent_results = Issue.recent_results(limit=20)
31 self.response.out.write(template.render('templates/overview.html', locals()))
7bc3dd0 encrypted the html page sources, but pages are not displayed properly
unknown authored
32
9915517 @jonhull First commit
jonhull authored
33
34 class NewHandler(webapp.RequestHandler):
35 def get(self):
36 user = users.get_current_user()
37 if user:
38 logout_url = users.create_logout_url('/')
39 else:
40 self.redirect(users.create_login_url(self.request.uri))
41 return
42 option_one = "Yes"
43 option_two = "No"
44 self.response.out.write(template.render('templates/new.html', locals()))
45
46 def post(self):
47 user = users.get_current_user()
48 if not user:
49 self.redirect(users.create_login_url(self.request.uri))
50 return
51
52 duration_amount = int(self.request.get('duration_amount'))
53 multiplier = int(self.request.get('duration_multiplier'))
204f77f added encryption
unknown authored
54 hashcode = random_string()
9915517 @jonhull First commit
jonhull authored
55 issue = Issue(
56 title = cgi.escape(self.request.get('title')),
57 description = cgi.escape(self.request.get('description')),
58 duration = duration_amount * multiplier,
7bc3dd0 encrypted the html page sources, but pages are not displayed properly
unknown authored
59 urlcode = hashcode)
9915517 @jonhull First commit
jonhull authored
60 issue.put()
61 if self.request.get('option1'):
62 issue.add_choice(cgi.escape(self.request.get('option1')))
63 if self.request.get('option2'):
64 issue.add_choice(cgi.escape(self.request.get('option2')))
65 if self.request.get('option3'):
66 issue.add_choice(cgi.escape(self.request.get('option3')))
67 if self.request.get('option4'):
68 issue.add_choice(cgi.escape(self.request.get('option4')))
69 if self.request.get('option5'):
70 issue.add_choice(cgi.escape(self.request.get('option5')))
71
204f77f added encryption
unknown authored
72 #self.redirect('/issue/%s' % (issue.key().id()))
7bc3dd0 encrypted the html page sources, but pages are not displayed properly
unknown authored
73 self.redirect('/issue/%s' % issue.urlcode)
9915517 @jonhull First commit
jonhull authored
74
75 class EditHandler(webapp.RequestHandler):
76 def get(self,id):
77 user = users.get_current_user()
78 if user:
79 logout_url = users.create_logout_url('/')
80 else:
81 self.redirect(users.create_login_url(self.request.uri))
82 return
7bc3dd0 encrypted the html page sources, but pages are not displayed properly
unknown authored
83 #issue = Issue.get_by_id(int(id))
84 issue = Issue.get_by_urlcode(urlcode)
9915517 @jonhull First commit
jonhull authored
85 choices = issue.choices
86 self.response.out.write(template.render('templates/edit.html', locals()))
87
88 def post(self,id):
89 user = users.get_current_user()
90 if user:
91 logout_url = users.create_logout_url('/')
92 else:
93 self.redirect(users.create_login_url(self.request.uri))
94 return
7bc3dd0 encrypted the html page sources, but pages are not displayed properly
unknown authored
95 #issue = Issue.get_by_id(int(id))
96 issue = Issue.get_by_urlcode(urlcode)
9915517 @jonhull First commit
jonhull authored
97
98 if self.request.get('extend'):#if extending vote
99 choices = issue.choices
100 extend_amount = int(self.request.get('extend_amount')) * int(self.request.get('extend_multiplier'))
101 issue.extend_duration(extend_amount)
102 self.response.out.write(template.render('templates/edit.html', locals()))
103
104 else:#otherwise we are saving changes
105 duration_amount = int(self.request.get('duration_amount'))
106 multiplier = int(self.request.get('duration_multiplier'))
107 issue.duration = duration_amount * multiplier
108 if self.request.get('title'):
109 issue.title = cgi.escape(self.request.get('title'))
110 if self.request.get('description'):
111 issue.description = cgi.escape(self.request.get('description'))
112 if self.request.get('option1') and self.request.get('option2'):
113 choices = issue.choices
114 db.delete(choices)
115 issue.add_choice(cgi.escape(self.request.get('option1')))
116 issue.add_choice(cgi.escape(self.request.get('option2')))
117 if self.request.get('option3'):
118 issue.add_choice(cgi.escape(self.request.get('option3')))
119 if self.request.get('option4'):
120 issue.add_choice(cgi.escape(self.request.get('option4')))
121 if self.request.get('option5'):
122 issue.add_choice(cgi.escape(self.request.get('option5')))
123 issue.put()
124 #choices = issue.choices
7bc3dd0 encrypted the html page sources, but pages are not displayed properly
unknown authored
125 self.redirect('/issue/%s' % issue.urlcode)
9915517 @jonhull First commit
jonhull authored
126 #self.response.out.write(template.render('templates/edit.html', locals()))
127
128
129
130 class IssueHandler(webapp.RequestHandler):
131 def get(self,id):
132 user = users.get_current_user()
133 if user:
134 logout_url = users.create_logout_url('/')
135 else:
136 self.redirect(users.create_login_url(self.request.uri))
137 return
138
7bc3dd0 encrypted the html page sources, but pages are not displayed properly
unknown authored
139 #issue = Issue.get_by_id(int(id))
140 issue = Issue.get_by_urlcode(urlcode)
9915517 @jonhull First commit
jonhull authored
141 issue.update_status()
142
143 vote = issue.vote_for_member(user)
144
145 issueUrl = self.request.uri
146 self.response.out.write(template.render('templates/Issue.html', locals()))
147
148
149 def post(self,id):
150 user = users.get_current_user()
151 if not user: #don't want someone who is not authenticated to be able to vote
152 self.redirect(users.create_login_url(self.request.uri))
153 return
154
7bc3dd0 encrypted the html page sources, but pages are not displayed properly
unknown authored
155 #issue = Issue.get_by_id(int(id))
156 issue = Issue.get_by_url(urlcode)
9915517 @jonhull First commit
jonhull authored
157 #vote = issue.vote_for_member()
158
159 new_choice = Choice.get_by_id(int(self.request.get('choice')))
160 was_updated = issue.register_vote(new_choice)
161
162 if was_updated:
163 self.redirect('/?success=updated')
164 else:
165 self.redirect('/?success=vote')
166
167
204f77f added encryption
unknown authored
168 def random_string():
169 hashbase = 'abcd1234'
170 return ''.join(random.sample(hashbase,len(hashbase)))
9915517 @jonhull First commit
jonhull authored
171
172 def main():
173 application = webapp.WSGIApplication([
174 ('/',MainPage),
175 ('/new',NewHandler),
176 ('/issue/(\d+).*',IssueHandler),
177 ('/edit/(\d+).*',EditHandler)],
178 debug=True)
179 util.run_wsgi_app(application)
180
181 if __name__ == '__main__':
182 main()
Something went wrong with that request. Please try again.