Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 179 lines (145 sloc) 5.689 kB
9915517 @jonhull First commit
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
6 import logging, urllib, os
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'))
55 issue = Issue(
56 title = cgi.escape(self.request.get('title')),
57 description = cgi.escape(self.request.get('description')),
58 duration = duration_amount * multiplier,
59 )
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
72 self.redirect('/issue/%s' % (issue.key().id()))
73
74
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
166
167
168 def main():
169 application = webapp.WSGIApplication([
170 ('/',MainPage),
171 ('/new',NewHandler),
172 ('/issue/(\d+).*',IssueHandler),
173 ('/edit/(\d+).*',EditHandler)],
174 debug=True)
175 util.run_wsgi_app(application)
176
177 if __name__ == '__main__':
178 main()
Something went wrong with that request. Please try again.