def likes_query(self, offset): q = Like.all() q.filter("listener", self.station) q.filter("created <", offset) q.order("-created") likes = q.fetch(10) return likes
def add_to_likes(self, track): # Check if the like hasn't been stored yet q = Like.all() q.filter("listener", self.station.key()) q.filter("track", track.key()) existing_like = q.get() if(existing_like): logging.info("Track already liked by this listener") else: like = Like( track = track.key(), listener = self.station.key(), ) like.put() logging.info("Like saved into datastore") self.increment_likes_counter() logging.info("Listener likes counter incremented") Track.increment_likes_counter(track.key().id()) logging.info("Track likes counter incremented")
def delete_from_likes(self, track): q = Like.all() q.filter("listener", self.station.key()) q.filter("track", track.key()) like = q.get() if like is None: logging.info("This track has never been liked by this listener") else: like.delete() logging.info("Like deleted from datastore") self.decrement_likes_counter() logging.info("Listener Likes counter decremented") Track.decrement_likes_counter(track.key().id()) logging.info("Track likes counter decremented")
def delete(self, id): track = Track.get_by_id(int(id)) response = False if(track): # Queriying datastore for associated Like query = Like.all() query.filter("track", track) query.filter("listener", self.user_proxy.user.profile) like = query.get() if like: logging.info("Like associated to track_id %s and listener %s retrieved from datastore."%(id, self.user_proxy.profile["name"])) like.delete() response = True logging.info("Like is now deleted from datstore") else: logging.info("Like associated to track_id %s and listener %s does not exist in datastore."%(id, self.user_proxy.profile["name"])) self.response.out.write(json.dumps({ "response": response }))
def get_likes(self, offset): likes = self.likes_query(offset) extended_likes = Like.get_extended_likes(likes) return extended_likes
def delete(self): cursor = self.request.get("cursor") track_id = self.request.get("track_id") typeModel = self.request.get("type") if(track_id): track = Track.get_by_id(int(track_id)) logging.info("Getting from datastore track with track_id = " + track_id) if(track): logging.info("Track found") if(typeModel == "broadcast"): # Deleting broadcasts associated to track query = Broadcast.all() query.filter("track", track) if(cursor): logging.info("Cursor found") query.with_cursor(start_cursor = cursor) broadcasts = query.fetch(100) logging.info("Fetched : %d Broadcast(s) from datastore."%(len(broadcasts))) if(len(broadcasts)>0): db.delete(broadcasts) logging.info("Deleted : %d Broadcast(s) from datastore."%(len(broadcasts))) task = Task( method = 'DELETE', url = "/taskqueue/deletetrack", params = { "cursor": cursor, "track_id": track_id, "type": typeModel, } ) task.add(queue_name="worker-queue") else: task = Task( method = 'DELETE', url = "/taskqueue/deletetrack", params = { "track_id": track_id, "type": "like", } ) task.add(queue_name="worker-queue") elif (typeModel == "like"): # Deleting likes associated to track query = Like.all() query.filter("track", track) if(cursor): logging.info("Cursor found") query.with_cursor(start_cursor = cursor) like = query.get() if like is None: logging.info("No like for this track, deleting track.") track.delete() else: try: listener_proxy = StationApi(like.listener.shortname) listener_proxy.decrement_likes_counter() logging.info("Listener Like counter decremented") except: logging.info("User has not created a station (likes used to be linked to user to stations)") Track.decrement_likes_counter(track.key().id()) logging.info("Track likes counter decremented") like.delete() logging.info("Like deleted from datastore") cursor = query.cursor() task = Task( method = 'DELETE', url = "/taskqueue/deletetrack", params = { "cursor": cursor, "track_id": track_id, "type": typeModel, } ) task.add(queue_name="worker-queue") response = True else: response = False self.response.out.write(json.dumps({ "response": response }))