def post(self): tool_user, db_user = ADayThere.tool_user() if not tool_user: self.response.status = 401 return data = json.loads(self.request.body) day = Day.query_user_title(db_user.user_id, data["title"]).get() KeywordsDayList.delete_keywords(day) day.full_locality = data["full_locality"] day.title = data["title"] day.description = data["description"] if isinstance(data["keywords"], basestring): if "," in data["keywords"]: day.keywords = data["keywords"].split(",") else: day.keywords = data["keywords"].split(" ") else: day.keywords = data["keywords"] for keyword in day.keywords: Keywords.add_if_missing(keyword) day.places = [] for place in data["places"]: p = Place() p.name = place["name"] p.comment = place["comment"] p.location = Location() p.location.latitude = str(place["location"]["latitude"]) p.location.longitude = str(place["location"]["longitude"]) p.location.vicinity = place["vicinity"] day.places.append(p) day.photos = [] for photo in data["photos"]: day_photo = DayPhoto() day_photo.title = photo["title"] day_photo.description = photo["description"] day.photos.append(day_photo) photo_query = Photos.query_photo(db_user.user_id, photo["title"]) pq = photo_query.get() cnt = pq.used_by.count(day.title) if cnt == 0: pq.used_by.append(day.title) day.put() KeywordsDayList.add_keywords(day) Words.update_words(day.title, day.description, day.key, day.full_locality) self.response.status = 200
def delete(self): tool_user, db_user = ADayThere.tool_user() if not tool_user: self.response.status = 401 return title = self.request.get("title") day = Day.query_user_title(db_user.user_id, title).get() for photo in day.photos: photo_query = Photos.query_photo(db_user.user_id, photo.title) pq = photo_query.get() try: index = pq.index(day.title) day.title.pop(index) except: pass pq.key.delete() day.key.delete() KeywordsDayList.delete_keywords(day) Words.delete_words(day.key) self.response.status = 200
def put (self): logged_in_user, commenters_id = ADayThere.tool_user () if not logged_in_user: self.response.status = 401 return data = json.loads (self.request.body) print "DATA", data userid = data.get ('userid', None) title = data.get ('title', None) review = data.get ('rating', 1) if title is None or userid is None: self.response.status = 401 return prev_comment = UserComment.query_previous_comment (commenters_id.name, userid, title).get () if prev_comment is not None: self.response.status = 409 self.response.write (json.dumps ({'rating': prev_comment.rating, 'title': prev_comment.title})) return day = Day.query_user_title (userid, title).get () if day is None: self.response.status = 403 return new_comment = UserComment () new_comment.commenters_name = commenters_id.name new_comment.userid = userid new_comment.title = title new_comment.text = data.get ('text', None) new_comment.rating = review new_comment.put () if day.numberOfReviews == None or day.numberOfReviews == 0: day.numberOfReviews = 1; day.averageReview = review; else: rev = (day.numberOfReviews * day.averageReview) + review day.numberOfReviews = day.numberOfReviews + 1 day.averageReview = rev / day.numberOfReviews day.put () self.response.write (json.dumps ({'numberOfReviews': day.numberOfReviews, 'averageReview': day.averageReview, 'review': new_comment.to_dict ()})); self.response.status = 200
def get (self): tool_user, db_user = ADayThere.tool_user () if not ADayThere.admin_user (db_user): self.response.status = 401 self.response.write ("Unauthorized") return query = Day.query () if 'name' in self.request.GET.keys (): name = self.request.GET['name'] if name != '': query = query.filter (Day.name == name) if 'title' in self.request.GET.keys (): title = self.request.GET['title'] if title != '': query = query.filter (Day.title == title) if 'locale' in self.request.GET.keys (): locale = self.request.GET['locale'] if locale != '': query = query.filter (Day.full_locality == locale) limit = self.request.get ('limit', None) if limit is None: limit = DaysHandler.max_limit cursor = ndb.Cursor (urlsafe=self.request.get ('cursor')) days, cursor, more = query.fetch_page (int (limit), start_cursor=cursor) days_arr = [] for each in days: d = self.__build_day (each) days_arr.append (d) safe_cursor = '' if cursor: safe_cursor = cursor.urlsafe () resp_obj = { "days": days_arr, "cursor": safe_cursor, "more": more } resp = json.dumps (resp_obj) self.response.write (resp)
def get(self): tool_user, db_user = ADayThere.tool_user() if not tool_user: self.response.status = 401 return days = [] data = Day.query_user(db_user.user_id).fetch() for each in data: days.append(json.dumps(each.to_dict())) self.response.write(json.dumps(days))
def post (self): tool_user, db_user = ADayThere.tool_user () if not ADayThere.admin_user (db_user): self.response.status = 401 self.response.write ("Unauthorized") return sent_day = json.loads (self.request.body) day_query = Day.query_user_title (sent_day['userid'], sent_day['title']) day = day_query.get () if day is None: self.response.status = 404 self.response.write ("Not Found") return day.key.delete () self.response.status = 200 self.response.write ("OK")
def get(self): rand = self.request.get("random", None) if rand is not None: days = Day.query_random(20) self.return_days(days, None, False) return userid = self.request.get("user_id", None) if userid is not None: title = self.request.get("title", None) if title is not None: day = Day.query_user_title(userid, title).get() days = [] days.append(day) else: days = Day.query_user(userid).fetch() self.return_days(days, None, False) return wordstr = self.request.get("words", None) words = None if wordstr is not None: if wordstr.find(",") != -1: words = wordstr.split(",") else: words = wordstr.split(" ") args = {} args["limit"] = self.request.get("limit", 20) if self.request.get("full_locality", None) is not None: args["full_locality"] = self.request.get("full_locality") if self.request.get("cursor", None) is not None: args["cursor"] = Cursor(urlsafe=self.request.get("cursor")) if self.request.get("user_id", None) is not None: args["user_id"] = self.request.get("user_id") if self.request.get("minimum_rating", None) is not None: args["minimum_rating"] = int(self.request.get("minimum_rating")) if self.request.get("all_words", None) is not None: args["all_words"] = self.request.get("all_words") keywords = self.request.get("keywords", None) keyhash = None if keywords is not None: word_list = sorted(map(lambda x: x.strip(), keywords.split(","))) keyhash = hashlib.sha256(str(word_list)).hexdigest() if keyhash is None: if words is None: days, cursor, more = Day.query_days(args) else: days, cursor, more = Day.query_word_days(args, words) else: days, cursor, more = Day.query_keyword_days(keyhash, args, words) if cursor is not None: cursor = cursor.urlsafe() self.return_days(days, cursor, more)