-
Notifications
You must be signed in to change notification settings - Fork 0
/
question.py
280 lines (226 loc) · 8.77 KB
/
question.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import os
import urllib
import cgi
import time
from Model import *
from google.appengine.api import users
from google.appengine.ext import ndb
from google.appengine.datastore.datastore_query import Cursor
from sets import Set
# from jinja2.filters import *
import jinja2
import webapp2
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)+ '/view/'),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
#DEFAULT_GUESTBOOK_NAME = 'default_guestbook'
# We set a parent key on the 'Greetings' to ensure that they are all in the same
# entity group. Queries across the single entity group will be consistent.
# However, the write rate should be limited to ~1/second.
def get_login_URL(self):
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
return url,url_linktext
class MainPage(webapp2.RequestHandler):
def get(self):
# if users.get_current_user():
# url = users.create_logout_url(self.request.uri)
# url_linktext = 'Logout'
# else:
# url = users.create_login_url(self.request.uri)
# url_linktext = 'Login'
# template_values = {
# 'current_user':users.get_current_user(),
# 'url': url,
# 'url_linktext': url_linktext
# }
url,url_linktext = get_login_URL(self)
#every page display 2 quesions
pagesize=5
curs=Cursor(urlsafe=self.request.get('cursor'))
questions,next_page_url = show_page(curs,pagesize)
template_values = {
'current_user':users.get_current_user(),
'questions': questions,
'url': url,
'url_linktext': url_linktext,
'next_page_url':next_page_url,
}
template = JINJA_ENVIRONMENT.get_template('main.html')
self.response.write(template.render(template_values))
class AddQuestion(webapp2.RequestHandler):
def post(self):
if not users.get_current_user():
url = users.create_login_url(self.request.uri)
self.redirect(url)
return
question_id=self.request.get("question_id")
if question_id:
question_key=ndb.Key(Question,int(question_id))
question=question_key.get()
question.content=self.request.get('content')
question.put()
time.sleep(0.1)
query_params = {'question_id': question_id}
self.redirect('/answer?' + urllib.urlencode(query_params))
else:
question = Question()
question.author = users.get_current_user()
question.content = self.request.get('content')
question.title = self.request.get('title')
if self.request.get('tag'):
question.tags = strip_tags(self.request.get('tag'))
question.put()
time.sleep(0.1)
self.redirect('/')
def get(self):
# question = Question()
# question.author = users.get_current_user()
# question.content = self.request.get('content')
# question.title = self.request.get('title')
# question.put()
# time.sleep(0.1)
self.redirect('/')
class listAnswer(webapp2.RequestHandler):
def get(self):
url,url_linktext = get_login_URL(self)
question_id = self.request.get('question_id')
question_key = ndb.Key('Question', int(question_id))
question=question_key.get()
#answers=Answer.query(ancestor=question_key).order(-Answer.margin).fetch()
pagesize=4
curs=Cursor(urlsafe=self.request.get('cursor'))
answers,next_page_url = show_answer_page(curs,pagesize,question_key,question_id)
template_values = {
'next_page_url':next_page_url,
'question': question,
'answers': answers,
'current_user':users.get_current_user(),
'url': url,
'url_linktext': url_linktext,
}
template = JINJA_ENVIRONMENT.get_template('answers.html')
self.response.write(template.render(template_values))
class AddAnswer(webapp2.RequestHandler):
def post(self):
if not users.get_current_user():
url = users.create_login_url(self.request.uri)
self.redirect(url)
return
question_id=self.request.get('question_id')
question_key = ndb.Key('Question', int(question_id))
answer_id=self.request.get('answer_id')
#edit an existing answer
if answer_id:
answer_key = ndb.Key(Question,int(question_id),Answer,int(answer_id))
answer=answer_key.get()
answer.content=self.request.get('content')
#initiallize instance answer
else:
answer = Answer(parent=question_key)
answer.parent_id = int(question_id)
answer.question_title = question_key.get().title
answer.author = users.get_current_user()
answer.content = self.request.get('content')
# answer.up=0
# answer.down=0
answer.put()
time.sleep(0.1)
query_params = {'question_id': question_id}
self.redirect('/answer?' + urllib.urlencode(query_params))
def get(self):
self.redirect('/')
class Vote(webapp2.RequestHandler):
def goToQuestionPage(self,question_id):
query_params = {'question_id': question_id}
self.redirect('/answer?' + urllib.urlencode(query_params))
def updateVote(self,vote,QorA,question_id,user_id):
if vote=="up":
if (user_id not in QorA.upList):
QorA.upList.append(user_id)
else:
self.goToQuestionPage(question_id)
if vote=="down":
if (user_id not in QorA.downList):
QorA.downList.append(user_id)
else:
self.goToQuestionPage(question_id)
QorA.put()
time.sleep(0.1)
self.goToQuestionPage(question_id)
def post(self):
current_user = users.get_current_user()
if not current_user:
url = users.create_login_url(self.request.uri)
self.redirect(url)
return
user_id=current_user.user_id()
question_id=self.request.get('question_id')
question_key=ndb.Key(Question,int(question_id))
question=question_key.get()
vote = self.request.get('name')
answer_id=self.request.get('answer_id')
#update question vote
if not answer_id:
self.updateVote(vote,question,question_id,user_id)
#update answer vote
if answer_id:
answer_key = ndb.Key(Question, int(question_id), Answer, int(answer_id))
answer=answer_key.get()
self.updateVote(vote,answer,question_id,user_id)
def get(self):
self.redirect('/')
class WWarning(webapp2.RequestHandler):
def post(self):
question_id=self.request.get("question_id")
template_values = {
'question_id': question_id
}
template = JINJA_ENVIRONMENT.get_template('warning.html')
self.response.write(template.render(template_values))
class TagHandler(webapp2.RequestHandler):
def get(self):
url,url_linktext = get_login_URL(self)
#every page display 2 quesions
pagesize=5
tag=self.request.get('tag')
curs=Cursor(urlsafe=self.request.get('cursor'))
questions,next_page_url = show_tag_page(curs,pagesize,tag)
template_values = {
'current_user':users.get_current_user(),
'questions': questions,
'url': url,
'url_linktext': url_linktext,
'next_page_url':next_page_url,
'tag':tag
}
template = JINJA_ENVIRONMENT.get_template('tag.html')
self.response.write(template.render(template_values))
class RSSHandler(webapp2.RequestHandler):
def get(self):
question_id=self.request.get("question_id")
question_key = ndb.Key(Question,int(question_id))
question = question_key.get()
answers = Answer.query(ancestor=question_key).order(-Answer.margin).fetch()
template_value = {
'question' : question,
'answers' : answers,
}
self.response.headers['Content-Type'] = 'text/xml'
template = JINJA_ENVIRONMENT.get_template('rss.xml')
self.response.write(template.render(template_value))
application = webapp2.WSGIApplication([
('/', MainPage),
('/add', AddQuestion),
('/answer',listAnswer),
('/addA', AddAnswer),
('/vote', Vote),
('/warning',WWarning),
('/tag',TagHandler),
('/rss',RSSHandler)
], debug=True)