Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 181 lines (150 sloc) 5.869 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()))
32
33
34
35 class NewHandler(webapp.RequestHandler):
36 def get(self):
37 user = users.get_current_user()
38 if user:
39 logout_url = users.create_logout_url('/')
40 else:
41 self.redirect(users.create_login_url(self.request.uri))
42 return
43 option_one = "Yes"
44 option_two = "No"
45 self.response.out.write(template.render('templates/new.html', locals()))
46
47 def post(self):
48 user = users.get_current_user()
49 if not user:
50 self.redirect(users.create_login_url(self.request.uri))
51 return
52
53 duration_amount = int(self.request.get('duration_amount'))
54 multiplier = int(self.request.get('duration_multiplier'))
204f77f added encryption
unknown authored
55 hashcode = random_string()
9915517 @jonhull First commit
jonhull authored
56 issue = Issue(
57 title = cgi.escape(self.request.get('title')),
58 description = cgi.escape(self.request.get('description')),
59 duration = duration_amount * multiplier,
60 )
61 issue.put()
62 if self.request.get('option1'):
63 issue.add_choice(cgi.escape(self.request.get('option1')))
64 if self.request.get('option2'):
65 issue.add_choice(cgi.escape(self.request.get('option2')))
66 if self.request.get('option3'):
67 issue.add_choice(cgi.escape(self.request.get('option3')))
68 if self.request.get('option4'):
69 issue.add_choice(cgi.escape(self.request.get('option4')))
70 if self.request.get('option5'):
71 issue.add_choice(cgi.escape(self.request.get('option5')))
72
204f77f added encryption
unknown authored
73 #self.redirect('/issue/%s' % (issue.key().id()))
74 self.redirect('/issue/%s' % hashcode)
9915517 @jonhull First commit
jonhull authored
75
76 class EditHandler(webapp.RequestHandler):
77 def get(self,id):
78 user = users.get_current_user()
79 if user:
80 logout_url = users.create_logout_url('/')
81 else:
82 self.redirect(users.create_login_url(self.request.uri))
83 return
84 issue = Issue.get_by_id(int(id))
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
95 issue = Issue.get_by_id(int(id))
96
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
125 self.redirect('/issue/%s' % (id))
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
139 issue = Issue.get_by_id(int(id))
140 issue.update_status()
141
142 vote = issue.vote_for_member(user)
143
144 issueUrl = self.request.uri
145 self.response.out.write(template.render('templates/Issue.html', locals()))
146
147
148 def post(self,id):
149 user = users.get_current_user()
150 if not user: #don't want someone who is not authenticated to be able to vote
151 self.redirect(users.create_login_url(self.request.uri))
152 return
153
154 issue = Issue.get_by_id(int(id))
155 #vote = issue.vote_for_member()
156
157 new_choice = Choice.get_by_id(int(self.request.get('choice')))
158 was_updated = issue.register_vote(new_choice)
159
160 if was_updated:
161 self.redirect('/?success=updated')
162 else:
163 self.redirect('/?success=vote')
164
165
204f77f added encryption
unknown authored
166 def random_string():
167 hashbase = 'abcd1234'
168 return ''.join(random.sample(hashbase,len(hashbase)))
9915517 @jonhull First commit
jonhull authored
169
170 def main():
171 application = webapp.WSGIApplication([
172 ('/',MainPage),
173 ('/new',NewHandler),
174 ('/issue/(\d+).*',IssueHandler),
175 ('/edit/(\d+).*',EditHandler)],
176 debug=True)
177 util.run_wsgi_app(application)
178
179 if __name__ == '__main__':
180 main()
Something went wrong with that request. Please try again.