Coverage for tweets/helpers/tweet_logic.py : 73%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
|
TweetNotFound)
''' Class to have all tweet functions, Pymongo db instance as a class variable. ''' # self.r_cache = settings.R_CACHE # self.jwt_secret = settings.JWT_SECRET
''' Params: user_id, tweet_text Allows user with _id user_id to create tweet with text tweet_text ''' "_id": str(uuid.uuid4()), "user_id": user_id, "tweet_text": tweet_text[:140], "created_at": datetime.datetime.utcnow(), "fav_count": 0, "fav_list": [], "retweet_count": 0, "retweet_list": [], "retweet_user_list": [], "is_retweet": False, "before_retweet_user": None, "before_tweet_id": None, "is_reply": False, "replies": [], "replies_count": 0, "is_threaded": False, "thread_id": None, } except Exception as e: res['message'] = {"Error": "Some intenal error occured try again"} res['code'] = 500
''' Params: user_id, tweet_id Allows user with _id user_id to delete tweet with id tweet_id '''
raise NotAuthorized # we need to change the original also original_query = {"_id": to_delete_tweet["before_tweet_id"]} original_update = { "$inc": { "retweet_count": -1 }, "$pull": { "retweet_user_list": user_id, "retweet_list": tweet_id } } tweet_ori_update = self.db.tweets.update_one(original_query, original_update) deleted_tweet = self.db.tweets.delete_one({"_id": tweet_id}) ## we need to change the original also original_query = {"_id": to_delete_tweet["before_tweet_id"]} original_update = { "$pull": { "replies": tweet_id }, "$inc": { "replies_count": -1 } } tweet_ori_update = self.db.tweets.update_one(original_query, original_update) deleted_tweet = self.db.tweets.delete_one({"_id": tweet_id}) else: except NotAuthorized: res['message'] = {"Error": "The user can only delete their own tweets/replies"} res['code'] = 401 except Exception as e: res['message'] = {"Error": "Some intenal error occured try again"} res['code'] = 500
''' Params: user_id, tweet_id Allows user with _id user_id to view tweet with id tweet_id ''' except Exception as e: res['message'] = {"Error": "Some intenal error occured try again"} res['code'] = 500
''' Params: user_id, tweet_id Allows user with _id user_id to like/unlike tweet with id tweet_id ''' # here we check, if user has already liked the tweet, if yes he now will unlike the tweet if i == user_id: tolike = 0 elif tolike == 0: update_query = { "$pull": { "fav_list": user_id }, "$inc": { "fav_count": -1 } } update_tweet = self.db.tweets.update_one(tweet_query, update_query, upsert=False) res['message'] = {"Success": "Tweet with id " + tweet_id + " unliked succesfully by user with id " + user_id } res['code'] = 200
except Exception as e: res['message'] = {"Error": "Some intenal error occured try again"} res['code'] = 500
''' Params: user_id, tweet_id Allows user with _id user_id to retweet tweet with id tweet_id ''' "_id": str(uuid.uuid4()), "user_id": user_id, "tweet_text": original_tweet['tweet_text'], "created_at": datetime.datetime.utcnow(), "fav_count": 0, "fav_list": [], "retweet_count": 0, "retweet_list": [], "retweet_user_list": [], "is_retweet": True, "before_retweet_user": original_tweet['user_id'], "before_tweet_id": original_tweet["_id"], "is_reply": False, "replies": [], "is_threaded": False, "thread_id": None, } "$push": { "retweet_user_list": user_id, "retweet_list": new_tweet.inserted_id } } update_orig_tweet = self.db.tweets.update_one(tweet_query, update_query, upsert=False) success_msg = str("Retweet with id " + retweet_tweet["_id"] + " created for tweet with id " + tweet_id + " by user with id " + user_id) res['message'] = {"Success": success_msg, "retweet_id": retweet_tweet["_id"] } res['code'] = 200
''' Params: user_id, tweet_id, reply_text Allows user with _id user_id to reply to tweet with id tweet_id with reply with text reply_text ''' raise TweetNotFound "_id": str(uuid.uuid4()), "user_id": user_id, "tweet_text": reply_text[:140], "created_at": datetime.datetime.utcnow(), "fav_count": 0, "fav_list": [], "retweet_count": 0, "retweet_list": [], "retweet_user_list": [], "is_retweet": False, "before_retweet_user": None, "before_tweet_id": original_tweet["_id"], "is_reply": True, "replies": [], "replies_count": 0, "is_threaded": False, "thread_id": None, } "$inc": { "replies_count": 1 } }
except TweetNotFound: res['message'] = {"Error": "No tweet found with given tweet id, cannot add reply"} res['code'] = 400 except Exception as e: res['message'] = {"Error": "Some intenal error occured try again"} res['code'] = 500
''' Params: user_id, thread_texts Allows user with _id user_id to create thread list of tweets with text as text in tweet_texts ''' raise EmptyException("Any tweet text") else: "_id": str(uuid.uuid4()), "user_id": user_id, "tweet_text": single_tweet[:140], "created_at": thread_time, "fav_count": 0, "fav_list": [], "retweet_count": 0, "retweet_list": [], "retweet_user_list": [], "is_retweet": False, "before_retweet_user": None, "before_tweet_id": None, "is_reply": False, "replies": [], "replies_count": 0, "is_threaded": True, "thread_id": thread_id, } except Exception as e: res['message'] = {"Error": "Some intenal error occured try again"} res['code'] = 500
''' Params: tweet_id Allows to view thread with id thread_id ''' except Exception as e: res['message'] = {"Error": "Some intenal error occured try again"} res['code'] = 500 |