def post_list(self, request, **kwargs): """ Upon creation of a new idea tag, change the total count too""" # Get the document regExp = re.compile('.*/(?P<doc_id>[a-zA-Z0-9]{24})/.*') m = re.match(regExp, request.path).groupdict() if m: doc_id = m['doc_id'] else: return super(LikeResource, self).post_list(request) doc = documents.Idea.objects.get(id=doc_id) # Likes are only allowed on published content if doc.status != 'published': bundle = {"error": "User can only like published ideas."} return self.create_response(request, bundle, response_class=http.HttpBadRequest) else: # Get a list of who has liked or disliked users_already_liked = [vote['user'] for vote in doc['likes']] users_already_disliked = [vote['user'] for vote in doc['dislikes']] # Check this user actually exists in the db check_user = User.objects.get_by_natural_key(request.user.username) if not check_user: return #TODO: Return an error - like not incremented # Check whether the user has voted user_id = request.user.username # User has already disliked the idea - flip the vote if user_id in users_already_disliked: # Single transaction - decrement dislike, increment like, pull the previous vote documents.Idea.objects(id=doc_id).update( **{ 'inc__dislike_count': -1, 'pull__dislikes__user': user_id, 'inc__like_count': 1 }) # User has already liked the idea - block the duplicate attempt elif user_id not in users_already_liked: documents.Idea.objects(id=doc_id).update( **{'inc__like_count': 1}) # Having made any mods, now re-compute the score value current_idea = documents.Idea.objects.get(id=doc_id) score = vote_score(current_idea['like_count'], current_idea['dislike_count']) documents.Idea.objects(id=doc_id).update( **{'set__vote_score': score}) # User hasn't done anything up to now, so let him/her vote return super(LikeResource, self).post_list(request)
def post_list(self, request, **kwargs): """ Upon creation of a new idea tag, change the total count too""" # Get the document regExp = re.compile('.*/(?P<doc_id>[a-zA-Z0-9]{24})/.*') m = re.match(regExp, request.path).groupdict() if m: doc_id = m['doc_id'] else: return super(DislikeResource, self).post_list(request) doc = documents.Idea.objects.get(id=doc_id) # Likes are only allowed on published content if doc.status != 'published': bundle = {"error": "User can only dislike published ideas."} return self.create_response(request, bundle, response_class=http.HttpBadRequest) else: # Get a list of who has liked or disliked users_already_liked = [vote['user'] for vote in doc['likes']] users_already_disliked = [vote['user'] for vote in doc['dislikes']] # Check whether the user has voted user_id = request.user.username # FOR A DISLIKE ACTION... # User has already liked, so just let them switch their like to dislike # and keep track of the counters. if user_id in users_already_liked: # Single transaction - decrement like, increment dislike, pull the previous vote documents.Idea.objects(id=doc_id).update( **{ 'inc__like_count': -1, 'pull__likes__user': user_id, 'inc__dislike_count': 1 }) # User has already disliked, so block this attempted to dislike again elif user_id not in users_already_disliked: documents.Idea.objects(id=doc_id).update( **{'inc__dislike_count': 1}) # Having made any mods, now re-compute the score value current_idea = documents.Idea.objects.get(id=doc_id) score = vote_score(current_idea['like_count'], current_idea['dislike_count']) documents.Idea.objects(id=doc_id).update( **{'set__vote_score': score}) # User hasn't done anything up to now, so let them dislike. return super(DislikeResource, self).post_list(request)
def post_list(self, request, **kwargs): """ Upon creation of a new idea tag, change the total count too""" # Get the document regExp = re.compile(".*/(?P<doc_id>[a-zA-Z0-9]{24})/.*") m = re.match(regExp, request.path).groupdict() if m: doc_id = m["doc_id"] else: return super(LikeResource, self).post_list(request) doc = documents.Idea.objects.get(id=doc_id) # Likes are only allowed on published content if doc.status != "published": bundle = {"error": "User can only like published ideas."} return self.create_response(request, bundle, response_class=http.HttpBadRequest) else: # Get a list of who has liked or disliked users_already_liked = [vote["user"] for vote in doc["likes"]] users_already_disliked = [vote["user"] for vote in doc["dislikes"]] # Check this user actually exists in the db check_user = User.objects.get_by_natural_key(request.user.username) if not check_user: return # TODO: Return an error - like not incremented # Check whether the user has voted user_id = request.user.username # User has already disliked the idea - flip the vote if user_id in users_already_disliked: # Single transaction - decrement dislike, increment like, pull the previous vote documents.Idea.objects(id=doc_id).update( **{"inc__dislike_count": -1, "pull__dislikes__user": user_id, "inc__like_count": 1} ) # User has already liked the idea - block the duplicate attempt elif user_id not in users_already_liked: documents.Idea.objects(id=doc_id).update(**{"inc__like_count": 1}) # Having made any mods, now re-compute the score value current_idea = documents.Idea.objects.get(id=doc_id) score = vote_score(current_idea["like_count"], current_idea["dislike_count"]) documents.Idea.objects(id=doc_id).update(**{"set__vote_score": score}) # User hasn't done anything up to now, so let him/her vote return super(LikeResource, self).post_list(request)
def post_list(self, request, **kwargs): """ Upon creation of a new idea tag, change the total count too""" # Get the document regExp = re.compile(".*/(?P<doc_id>[a-zA-Z0-9]{24})/.*") m = re.match(regExp, request.path).groupdict() if m: doc_id = m["doc_id"] else: return super(DislikeResource, self).post_list(request) doc = documents.Idea.objects.get(id=doc_id) # Likes are only allowed on published content if doc.status != "published": bundle = {"error": "User can only dislike published ideas."} return self.create_response(request, bundle, response_class=http.HttpBadRequest) else: # Get a list of who has liked or disliked users_already_liked = [vote["user"] for vote in doc["likes"]] users_already_disliked = [vote["user"] for vote in doc["dislikes"]] # Check whether the user has voted user_id = request.user.username # FOR A DISLIKE ACTION... # User has already liked, so just let them switch their like to dislike # and keep track of the counters. if user_id in users_already_liked: # Single transaction - decrement like, increment dislike, pull the previous vote documents.Idea.objects(id=doc_id).update( **{"inc__like_count": -1, "pull__likes__user": user_id, "inc__dislike_count": 1} ) # User has already disliked, so block this attempted to dislike again elif user_id not in users_already_disliked: documents.Idea.objects(id=doc_id).update(**{"inc__dislike_count": 1}) # Having made any mods, now re-compute the score value current_idea = documents.Idea.objects.get(id=doc_id) score = vote_score(current_idea["like_count"], current_idea["dislike_count"]) documents.Idea.objects(id=doc_id).update(**{"set__vote_score": score}) # User hasn't done anything up to now, so let them dislike. return super(DislikeResource, self).post_list(request)
def obj_create(self, bundle, **kwargs): """ Modifies the content before submission """ # Add in the user bundle.data["user"] = bundle.request.user.username # These ensure that counts are present if ideas are created with tags/likes/dislikes and comments bundle = count_builder(bundle, "comments", "comment_count") bundle = count_builder(bundle, "likes", "like_count") bundle = count_builder(bundle, "dislikes", "dislike_count") bundle = count_builder(bundle, "tags", "tag_count") # Sanitize tags before they get submitted try: bundle.data["tags"] = cleanup_tags(bundle.data["tags"]) except: pass # Finally build the score bundle.data["vote_score"] = vote_score(bundle.data["like_count"], bundle.data["dislike_count"]) return super(IdeaResource, self).obj_create(bundle)
def obj_create(self, bundle, **kwargs): """ Modifies the content before submission """ # Add in the user bundle.data['user'] = bundle.request.user.username # These ensure that counts are present if ideas are created with tags/likes/dislikes and comments bundle = count_builder(bundle, 'comments', 'comment_count') bundle = count_builder(bundle, 'likes', 'like_count') bundle = count_builder(bundle, 'dislikes', 'dislike_count') bundle = count_builder(bundle, 'tags', 'tag_count') # Sanitize tags before they get submitted try: bundle.data['tags'] = cleanup_tags(bundle.data['tags']) except: pass # Finally build the score bundle.data['vote_score'] = vote_score(bundle.data['like_count'], bundle.data['dislike_count']) return super(IdeaResource, self).obj_create(bundle)