66from google .appengine .api import users
77from google .appengine .runtime import DeadlineExceededError
88
9- from logic .logic import SymPyGamma , mathjax_latex
9+ from logic .logic import SymPyGamma
1010
1111import settings
1212import models
1919import datetime
2020import traceback
2121
22+ import logging
23+
24+
25+ ndb_client = models .ndb_client
26+
27+
2228LIVE_URL = '<a href="https://live.sympy.org">SymPy Live</a>'
2329LIVE_PROMOTION_MESSAGES = [
2430 'Need more control? Try ' + LIVE_URL + '.' ,
@@ -180,8 +186,9 @@ def index(request, user):
180186 form = SearchForm ()
181187
182188 if user :
183- history = models .Query .query (models .Query .user_id == user .user_id ())
184- history = history .order (- models .Query .date ).fetch (10 )
189+ with ndb_client .context ():
190+ history = models .Query .query (models .Query .user_id == user .user_id ())
191+ history = history .order (- models .Query .date ).fetch (10 )
185192 else :
186193 history = None
187194
@@ -193,9 +200,23 @@ def index(request, user):
193200 "examples" : EXAMPLES
194201 })
195202
203+
204+ def user_exists_and_input_not_present (user , input ):
205+ with ndb_client .context ():
206+ return (user and not models .Query .query (
207+ models .Query .text == input ,
208+ models .Query .user_id == user .user_id ()).get ())
209+
210+
211+ def input_exists (input ):
212+ with ndb_client .context ():
213+ return models .Query .query (models .Query .text == input ).get ()
214+
215+
196216@app_meta
197217@authenticate
198218def input (request , user ):
219+ logging .info ('Got the input from user' )
199220 if request .method == "GET" :
200221 form = SearchForm (request .GET )
201222 if form .is_valid ():
@@ -214,15 +235,18 @@ def input(request, user):
214235 "output" : "Can't handle the input."
215236 }]
216237
217- if (user and not models .Query .query (
218- models .Query .text == input ,
219- models .Query .user_id == user .user_id ()).get ()):
220- query = models .Query (text = input , user_id = user .user_id ())
221- query .put ()
222- elif not models .Query .query (models .Query .text == input ).get ():
223- query = models .Query (text = input , user_id = None )
224- query .put ()
225-
238+ if user_exists_and_input_not_present (user , input ):
239+ logging .info ('User exists and input not present' )
240+ with ndb_client .context ():
241+ query = models .Query (text = input , user_id = user .user_id ())
242+ logging .info ('query: %s' % query )
243+ query .put ()
244+ elif not input_exists (input ):
245+ logging .info ('Input does not exists' )
246+ with ndb_client .context ():
247+ query = models .Query (text = input , user_id = None )
248+ logging .info ('query: %s' % query )
249+ query .put ()
226250
227251 # For some reason the |random tag always returns the same result
228252 return ("result.html" , {
@@ -362,17 +386,25 @@ def get_card_full(request, card_name):
362386 return response
363387
364388
389+ def find_text_query (query ):
390+ with ndb_client .context ():
391+ return models .Query .query (models .Query .text == query .text )
392+
393+
365394def remove_query (request , qid ):
366395 user = users .get_current_user ()
367396
368397 if user :
369- query = models .ndb .Key (urlsafe = qid ).get ()
398+ with ndb_client .context ():
399+ query = models .ndb .Key (urlsafe = qid ).get ()
370400
371- if not models .Query .query (models .Query .text == query .text ):
372- query .user_id = None
373- query .put ()
401+ if not find_text_query (query ):
402+ with ndb_client .context ():
403+ query .user_id = None
404+ query .put ()
374405 else :
375- query .key .delete ()
406+ with ndb_client .context ():
407+ query .key .delete ()
376408
377409 response = {
378410 'result' : 'success' ,
0 commit comments