def get (self): tool_user, db_user = ADayThere.tool_user () if not tool_user: self.response.status = 401 return action = self.request.get ('action'); if action is None: self.response.status = 401 return if (action == 'count'): count = 0 query = Photos.query_user_id (db_user.user_id) if query is not None: count = query.count () res = json.dumps ({ "count": count }) self.response.write (res) return elif (action == 'img'): title = self.request.get ('title', None) if title is None: self.response.status = 401 return photo_query = Photos.query_photo (db_user.user_id, title) photo = photo_query.get () if photo is None: self.response.status = 404 return self.response.headers['Content-Type'] = 'image/png' self.response.write (photo.photo) return elif (action == 'list'): list_query = Photos.query_user_id (db_user.user_id) if list_query is None: self.response.status = 404 return titles = [] photos = list_query.fetch () for photo in photos: d = {} d['title'] = photo.title; d['used_by'] = photo.used_by; titles.append (d) self.response.write (json.dumps (titles)) return
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 delete (self): tool_user, db_user = ADayThere.tool_user () if not tool_user: self.response.status = 401 return titlestr = self.request.get ("titles", None) used_by = self.request.get ("used_by", None) if titlestr is None: self.response.status = 401 return titles = titlestr.split (",") if used_by is not None: photos = Photos.query (Photos.user_id == db_user.user_id, Photos.title.IN (titles), Photos.used_by == used_by).fetch (keys_only=True) else: photos = Photos.query (Photos.user_id == db_user.user_id, Photos.title.IN (titles)).fetch (keys_only=True) ndb.delete_multi (photos)
def put (self): tool_user, db_user = ADayThere.tool_user () if not tool_user: self.response.status = 401 return data = self.request.body lst = json.loads (data) entities = [] for p in lst: photo = Photos () photo.user_id = db_user.user_id photo.title = p['title'] decoded = p['url'].decode ('base64') photo.photo = decoded entities.append (photo) ndb.put_multi (entities) self.response.status = 200