def get_profile_stories_by_user_id(user_id): data = [] db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) opt1 = {"expired_time": ""} opt2 = {"expired_time": {"$gte": DateController.get_date_time()}} stories = db.stories.find({ "$and": [{ "$or": [opt1, opt2] }, { 'user_id': user_id }] }).sort('created_time', pymongo.DESCENDING) for storie in stories: storie_id = storie["_id"] storie = StorieModel.format_storie_dates(storie) storie["comments"] = CommentModel.get_last_storie_comment( storie_id) storie["reactions"] = ReactionModel.get_storie_reactions( storie_id, user_id) storie_with_user_data = StorieModel.get_storie_with_user_data( storie) data.append(storie_with_user_data) return data
def post(self): try: self.parser.add_argument('reaction', required=True, help="Field reaction is mandatory") self.parser.add_argument('user_id', required=True, help="Field user_id is mandatory") self.parser.add_argument('storie_id', required=True, help="Field storie_id is mandatory") args = self.parser.parse_args() validate_sender(args.get('user_id')) reaction = ReactionModel.create_reaction(args) UserActivityModel.log_reaction_activity(reaction["user_id"], reaction["storie_id"], reaction["reaction"], "ADD") return reaction except BadRequest as ex: return ErrorHandler.create_error_response( "Fields reaction, user_id and storie_id are mandatory", 400) except NoStorieFoundException as e: return ErrorHandler.create_error_response(str(e), 400) except UserMismatchException as e: return ErrorHandler.create_error_response(str(e), 409) except StorieReactionAlreadyFoundException as e: return ErrorHandler.create_error_response(str(e), 400) except DBConnectionError as e: return ErrorHandler.create_error_response(str(e), 500)
def delete_storie(storie_id, storie_user_id): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) storie = db.stories.find_one({ '_id': storie_id, 'user_id': storie_user_id }) if storie == None: raise NoStorieFoundException CommentModel.remove_comment_by_storie_id(storie_id) ReactionModel.remove_reaction_by_storie_id(storie_id) db.stories.remove({'_id': storie_id}) storie = StorieModel.format_storie_dates(storie) return storie
def delete(self, reaction_id): try: self._validate_author(reaction_id) reaction = ReactionModel.remove_reaction(reaction_id) UserActivityModel.log_reaction_activity(reaction["user_id"], reaction["storie_id"], reaction["reaction"], "DELETE") return self._get_reactions_response(reaction) except NoReactionFoundException as e: return ErrorHandler.create_error_response(str(e), 404) except UserMismatchException as e: return ErrorHandler.create_error_response(str(e), 409) except DBConnectionError as e: return ErrorHandler.create_error_response(str(e), 500)
def test_create_reaction(self, mock_db): mock_db_instance = mock.MagicMock() mock_db.return_value.get_mongodb_instance.return_value = mock_db_instance storie_id = '5ae66a31d4ef925dac59a95b' user_id = "10" result = ReactionModel.create_reaction({ "storie_id": storie_id, "user_id": user_id, "reaction": "LIKE" }) self.assertEqual(user_id, result["user_id"]) self.assertEqual(storie_id, result["storie_id"]) self.assertEqual("LIKE", result["reaction"])
def _validate_author(self, reaction_id): reaction = ReactionModel.get_reaction_with_id(reaction_id) author_id = reaction.get('user_id') validate_sender(author_id)
def get_stories(user_id, story_type='normal'): data = [] sp_list = [] stories_list = {} users_activity = {} db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) UserDataModel.exist_user(user_id) friends_id = FriendModel.get_friends_array_by_user_id(user_id) friends_id.append(user_id) opt1 = {"expired_time": ""} opt2 = {"expired_time": {"$gte": DateController.get_date_time()}} stories = db.stories.find({ "$or": [{ "$and": [{ "story_type": story_type }, { "$or": [opt1, opt2] }, { "user_id": { "$in": friends_id } }] }, { "$and": [{ "story_type": story_type }, { "$or": [opt1, opt2] }, { "user_id": { "$nin": friends_id } }, { "visibility": "public" }] }] }).sort("created_time", pymongo.DESCENDING) for storie in stories: storie_user_id = storie["user_id"] storie_id = storie["_id"] storie = StorieModel.format_storie_dates(storie) storie = StorieModel.get_storie_with_user_data(storie) storie["comments"] = CommentModel.get_last_storie_comment( storie_id) storie["reactions"] = ReactionModel.get_storie_reactions( storie_id, user_id) storie_data = StorieModel.get_storie_resume(storie) USER_ACT_PAST_DAYS = 10 if (storie_user_id not in users_activity): users_activity[ storie_user_id] = UserActivityModel.log_user_activity_resume( storie_user_id, USER_ACT_PAST_DAYS) storie_priority_data = StoriePriorityData( storie_id, storie_data["past"], storie_data["num_comments"], storie_data["num_reactions"], users_activity[storie_user_id]["num_friends"], users_activity[storie_user_id]["num_stories"]) sp_list.append(storie_priority_data) RulesMachine.process_data(storie_priority_data) stories_list[storie_id] = storie sp_list.sort(key=lambda x: x.get_priority(), reverse=True) for sp in sp_list: data.append(stories_list[sp.get_storie_id()]) return data