def get(self): cookie = self.request.get('cookie') self.response.headers['Content-Type'] = 'application/json; charset=utf-8' if not cookie: error = json_error('failure', 'cookie') self.response.write(json.dumps(error)) else: session = Session.query(Session.cookie == cookie).get() if not session: error = json_error('failure', 'session') self.response.write(error) else: name = self.request.get('name') if not name: error = json_error('failure', 'name') self.response.write(json.dumps(error)) else: submissions_number = 40 submissions = Submission.query(Submission.lowerName == name.lower()).fetch(submissions_number) if not submissions: error = json_error('failure', 'no result') self.response.write(json.dumps(error)) else: submissions_array = [] if(submissions_number <= len(submissions)): for i in range(0, submissions_number): submission = submissions[i] json_submission = json_string(submission.key.urlsafe(), submission.name, submission.image, submission.rating) submissions_array.append(json_submission) else: for i in range(0, len(submissions)): submission = submissions[i] json_submission = json_string(submission.key.urlsafe(), submission.name, submission.image, submission.rating) submissions_array.append(json_submission) response = json.dumps(submissions_array) self.response.write(response)
def get(self): self.response.headers['Content-Type'] = 'application/json; charset=utf-8' name = self.request.get('name') if not name: error = json_error('delete submission', 'failure', 'name') self.response.write(json.dumps(error)) else: submission = Submission.query(Submission.name == name).get() if not submission: error = json_error('delete submission', 'failure', 'no such submission') self.response.write(json.dumps(error)) else: submission.key.delete() response = {'delete submission': {'status': 'ok'}} self.response.write(json.dumps(response))
def get(self): self.response.headers['Content-Type'] = 'application/json; charset=utf-8' category = self.request.get('category') if not category: error = json_error('delete category', 'failure', 'category') self.response.write(json.dumps(error)) else: submissions = Submission.query(Submission.category == category).fetch() if not submissions: error = json_error('delete category', 'failure', 'no such submission') self.response.write(json.dumps(error)) else: for i in range(0, len(submissions)): submissions[i].key.delete() response = {'delete category': {'status': 'ok'}} self.response.write(json.dumps(response))
def get(self): cookie = self.request.get('cookie') self.response.headers['Content-Type'] = 'application/json; charset=utf-8' if not cookie: error = json_error('retrieve', 'failure', 'cookie') self.response.write(json.dumps(error)) else: flag = self.request.get('flag') if not flag: error = json_error('retrieve', 'failure', 'no flag') self.response.write(json.dumps(error)) else: session = Session.query(Session.cookie == cookie).get() if not session: error = json_error('retrieve', 'failure', 'session') self.response.write(error) else: # flag = 1 means a single request if flag == '1': try: id = self.request.get('id'); key = ndb.Key(urlsafe=id) if not id: self.response.write(json.dumps(json_error('single request', 'failure', 'id'))) else: submission = key.get() if not submission: error = json_error('single request', 'failure', 'no corresponding submission') self.response.write(json.dumps(error)) else: last_vote = Vote.query(ndb.AND(Vote.user == session.user, Vote.sub_id == submission.key.urlsafe())).get() if not last_vote: vote_val = 0 else: vote_val = last_vote.value string_submission = json_string(submission.key.urlsafe(), submission.name, submission.category, submission.description, submission.location, submission.image, submission.keywords, submission.submitter, submission.tfrom, submission.tto, submission.rating, vote_val) response = json.dumps(string_submission) self.response.write(response) except: self.response.write(json.dumps(json_error('single request', 'failure', 'id'))) # flag = 2 means that we are requesting submissions to display in what's new elif flag == '2': time_range = 1447786800 date = datetime.datetime.fromtimestamp(time_range/1e3) submissions_number = 20 submissions = Submission.query(Submission.submitted >= date).fetch(submissions_number) if not submissions: error = json_error('what is new', 'failure', 'nothing in range') self.response.write(json.dumps(error)) else: submissions_array = [] if(submissions_number <= len(submissions)): for i in range(0, submissions_number): submission = submissions[i] # what's new used for around you too. Server return location so that the app # can use it to display submissions near user on the map json_submission = json_array_with_location(submission.key.urlsafe(), submission.name,submission.image, submission.rating, submission.location) submissions_array.append(json_submission) else: for i in range(0, len(submissions)): submission = submissions[i] json_submission = json_array_with_location(submission.key.urlsafe(), submission.name, submission.image, submission.rating, submission.location) submissions_array.append(json_submission) response = json.dumps(submissions_array) self.response.write(response) # flag = 4 means that we are requesting submissions for a specific category elif flag == '4': category = self.request.get('category') submissions_number = 20 if not category: error = json_error('retrieve category', 'failure', 'no category') self.response.write(json.dumps(error)) else: submissions = Submission.query(Submission.category == category).fetch(submissions_number) if not submissions: error = json_error('retrieve category', 'failure', 'empty category') self.response.write(json.dumps(error)) else: submissions_array = [] if(submissions_number <= len(submissions)): for i in range(0, submissions_number): submission = submissions[i] json_submission = json_array(submission.key.urlsafe(), submission.name,submission.image, submission.rating) submissions_array.append(json_submission) else: for i in range(0, len(submissions)): submission = submissions[i] json_submission = json_array(submission.key.urlsafe(), submission.name,submission.image, submission.rating) submissions_array.append(json_submission) response = json.dumps(submissions_array) self.response.write(response) # every other flag generate an error else: error = json_error('retrieve' ,'failure', 'flag') self.response.write(json.dumps(error))