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 test_create_friend_request_successful(self, db_controller_name_mock): db_controller_mock = db_controller_name_mock.return_value side_effects = [mock.Mock(), MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD), MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD), MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD), MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD), MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD)] db_controller_mock.get_mongodb_instance = mock.Mock(side_effect=side_effects) FriendRequestModel.friend_request_exists = mock.MagicMock(return_value= False) request = FriendRequestModel.create_friend_request(1, 5, 'hello', 'picId') self.assertEqual(request["user_id_sender"], friends_requests_creation_successful_mock["user_id_sender"]) self.assertEqual(request["user_id_rcv"], friends_requests_creation_successful_mock["user_id_rcv"]) self.assertEqual(request["message"], friends_requests_creation_successful_mock["message"]) self.assertEqual(request["picture"], friends_requests_creation_successful_mock["picture"])
def create_user_storie(body): HOURS_FAST_STORIES = 4 db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) storie_id = str(uuid.uuid4().hex) created_time = DateController.get_date_time() updated_time = "" rev = "" title = body['title'] desc = body["description"] if ("description" in body) else "" location = body['location'] visibility = body['visibility'] mult = body['multimedia'] story_type = body['story_type'] expired_time = DateController.get_date_time_inc_by_hours( HOURS_FAST_STORIES) if (story_type == "fast") else "" user_id = body['user_id'] storie = StorieModel.get_new_storie(storie_id, rev, user_id, created_time, updated_time, expired_time, title, desc, location, visibility, mult, story_type) db.stories.insert(storie) storie = StorieModel.format_storie_dates(storie) return storie
def update_storie(storie_id, body): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) act_storie = db.stories.find_one({'_id': storie_id}) if act_storie == None: raise NoStorieFoundException if act_storie['_rev'] != body.get('_rev'): raise DataVersionException rev = str(uuid.uuid4().hex) created_time = act_storie["created_time"] expired_time = act_storie["expired_time"] updated_time = DateController.get_date_time() title = body["title"] desc = body["description"] if ("description" in body) else "" location = body["location"] visibility = body["visibility"] mult = body["multimedia"] story_type = body["story_type"] user_id = body["user_id"] storie = StorieModel.get_new_storie(storie_id, rev, user_id, created_time, updated_time, expired_time, title, desc, location, visibility, mult, story_type) del storie['_id'] res = db.stories.find_and_modify({'_id': storie_id}, {'$set': storie}) res = db.stories.find_one({'_id': storie_id}) res = StorieModel.format_storie_dates(res) return res
def update_comment(comment_id, body): db = MongoController.get_mongodb_instance(MONGODB_USER,MONGODB_PASSWD) comment = db.storie_comments.find_one({"_id": comment_id}) if comment == None: raise NoCommentFoundException if comment["_rev"] != body.get("_rev"): raise DataVersionException comment_id = body['_id'] storie_id = body["storie_id"] user_id = body["user_id"] UserDataModel.exist_user(user_id) rev = str(uuid.uuid4().hex) comment_date = DateController.get_date_time() message = body["message"] comment = CommentModel.get_new_comment(comment_id,storie_id,user_id,rev,comment_date,message) del comment['_id'] comment = db.storie_comments.find_and_modify({'_id': comment_id},{'$set': comment}) comment = db.storie_comments.find_one({'_id': comment_id}) comment["date"] = DateController.get_date_time_with_format(comment["date"]) return comment
def update_user_data_by_user_id(user_id, body): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) user = db.users.find_one({"_id": user_id}) if user == None: raise NoUserDataFoundException if (user['_rev'] != body.get('_rev')): raise DataVersionException user_id = body["_id"] rev = str(uuid.uuid4().hex) user_name = user["user_name"] last_name = body["last_name"] name = body["name"] birthday = body["birthday"] if ("birthday" in body) else "" gender = body["gender"] if ("gender" in body) else "" email = body["email"] pic = body["picture"] user = UserDataModel.get_new_user(user_id, rev, user_name, last_name, name, birthday, gender, email, pic) del user['_id'] user = db.users.find_and_modify({"_id": user_id}, {'$set': user}) user = db.users.find_one({"_id": user_id}) return user
def storie_exists(storie_id): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) storie = db.stories.find_one({"_id": storie_id}) if storie == None: return False return True
def exist_user(user_id): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) user = db.users.find_one({"_id": user_id}) if user == None: raise NoUserDataFoundException return user
def get_friend_request(request_id): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) friend_req = db.friends_request.find_one({'_id': request_id}) if friend_req == None: raise NoFriendRequestFoundException friend_req["date"] = DateController.get_date_time_with_format(friend_req["date"]) return friend_req
def log_user_login_activity(user_id): login_activity = { "resource": "LOGIN", "date": DateController.get_date_time(), "user_id": user_id } db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) db.user_activities.insert(login_activity)
def log_storie_activity(user_id, storie_id, op): storie_activity = { "resource": "STORIE", "date": DateController.get_date_time(), "user_id": user_id, "storie_id": storie_id, "operation": op } db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) db.user_activities.insert(storie_activity)
def log_friend_activity(user_id, friend_id, op): friend_activity = { "resource": "FRIEND", "date": DateController.get_date_time(), "user_id": user_id, "friend_id": friend_id, "operation": op } db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) db.user_activities.insert(friend_activity)
def reaction_exists(storie_id, user_id): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) react = db.storie_reactions.find_one({ 'storie_id': storie_id, 'user_id': user_id }) if react == None: return False return True
def get_last_storie_comment(storie_id): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) response = [] comment = db.storie_comments.find_one({'storie_id': storie_id},sort=[("date", -1)]) if comment != None: user_comment = CommentModel.get_comment_with_user_data(comment) user_comment["date"] = DateController.get_date_time_with_format(user_comment["date"]) response.append(user_comment) return response
def count_today_comments(): db = MongoController.get_mongodb_instance(MONGODB_USER,MONGODB_PASSWD) date_from = DateController.today() date_to = DateController.tomorrow() count = db.storie_comments.find({ "$and" : [ { "date" : {'$gte': date_from} }, { "date" : {'$lt': date_to} } ] }).count() return count
def friend_request_exists(user_id_sender, user_id_rcv): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) opt1 = {'user_id_sender': user_id_sender, 'user_id_rcv': user_id_rcv} opt2 = {'user_id_sender': user_id_rcv, 'user_id_rcv': user_id_sender} friendship = db.friends_request.find_one({ "$or": [ opt1, opt2 ]}) if friendship == None: return False return True
def remove_reaction(reaction_id): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) reaction = db.storie_reactions.find_one({"_id": reaction_id}) if reaction == None: raise NoReactionFoundException db.storie_reactions.remove({"_id": reaction_id}) reaction["date"] = DateController.get_date_time_with_format( reaction["date"]) return reaction
def remove_comment(comment_id): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) comment = db.storie_comments.find_one({"_id": comment_id}) if comment == None: raise NoCommentFoundException db.storie_comments.remove({"_id": comment_id}) comment["date"] = DateController.get_date_time_with_format(comment["date"]) return comment
def get_storie_comments(storie_id): response = [] db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) comments = db.storie_comments.find({'storie_id': storie_id}) for com in comments: user_comment = CommentModel.get_comment_with_user_data(com) user_comment["date"] = DateController.get_date_time_with_format(user_comment["date"]) response.append(user_comment) return response
def _find_requests(fromHour, toHour): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) date = DateController.get_date() andArgs = [{"date": date}] if (fromHour is not None): andArgs.append({"hour": {'$gte': int(fromHour)}}) if (toHour is not None): andArgs.append({"hour": {'$lte': int(toHour)}}) server_requests = db.server_requests.find({"$and": andArgs}) return server_requests
def get_friends_requests_sent_by_user_id(user_id): data = [] db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) friends_requests_rcv = db.friends_request.find({'user_id_sender': user_id}) for fr in friends_requests_rcv: friend_request = FriendRequestModel.get_friend_request_with_user_data(fr, "user_id_rcv") friend_request["date"] = DateController.get_date_time_with_format(friend_request["date"]) friend_request["user_id"] = friend_request.pop("user_id_rcv") friend_request.pop("user_id_sender") data.append(friend_request) return data
def create_reaction(body): from models.storie import StorieModel db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) storie_id = body["storie_id"] if StorieModel.storie_exists(storie_id) == False: raise NoStorieFoundException user_id = body["user_id"] reaction = body["reaction"] if (reaction != ""): if ReactionModel.reaction_exists(storie_id, user_id) == True: #raise StorieReactionAlreadyFoundException db.storie_reactions.remove({ "user_id": user_id, "storie_id": storie_id }) reaction_id = str(uuid.uuid4().hex) rev = "" reaction_date = DateController.get_date_time() reaction = body["reaction"] reaction = ReactionModel.get_new_reaction(reaction_id, storie_id, user_id, rev, reaction_date, reaction) db.storie_reactions.insert(reaction) else: reaction = db.storie_reactions.find_one({ "user_id": user_id, "storie_id": storie_id }) if reaction == None: raise NoReactionFoundException db.storie_reactions.remove({ "user_id": user_id, "storie_id": storie_id }) reaction["date"] = DateController.get_date_time_with_format( reaction["date"]) return reaction
def create_structure(): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) date = DateController.get_date() request = db.server_requests.find_one({"date": date}) if (request != None): return date = DateController.get_date() hour = 0 data = {'date': date, '_id': 0, 'hour': 0, 'count': 0} for hour in range(0, 24): data['_id'] = date + ' ' + str(hour) data['hour'] = hour db.server_requests.insert(data)
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 insert_user(user_id, user_name): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) rev = "" last_name = "lastName " + user_name name = "name " + user_name birthday = "" gender = "" email = "email " + user_name pic = "" user = UserDataModel.get_new_user(str(user_id), rev, user_name, last_name, name, birthday, gender, email, pic) db.users.insert(user) return user
def get_size_friends_by_user_id(user_id, date_from, date_to): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) count = db.user_activities.find({ "$and": [{ "date": { '$gte': date_from } }, { "date": { '$lt': date_to } }, { "resource": "FRIEND" }, { "user_id": user_id }] }).count() return count
def get_user_reduced_data_by_user_id(user_id): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) user = db.users.find_one({'_id': user_id}) if user == None: raise NoUserDataFoundException user_id = user["_id"] user_name = user["user_name"] last_name = user["last_name"] name = user["name"] pic = user["picture"] reduced_user_data = UserDataModel.get_reduced_data_user( user_id, user_name, last_name, name, pic) return reduced_user_data
def get_all_users_except(user_id): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) UserDataModel.exist_user(user_id) users = db.users.find({"_id": { "$ne": user_id }}) response = [] for user in users: user_id = user["_id"] last_name = user["last_name"] user_name = user["user_name"] name = user["name"] pic = user["picture"] response.append( UserDataModel.get_reduced_data_user(user_id, user_name, last_name, name, pic)) return response
def get_storie_resume_reactions(storie_id, user_id, reaction): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) counter = db.storie_reactions.find({ 'storie_id': storie_id, 'reaction': reaction }).count() react = db.storie_reactions.find_one( { 'storie_id': storie_id, 'user_id': user_id, 'reaction': reaction }, { "date": 1, "_id": 0 }) if (react != None): react["date"] = DateController.get_date_time_with_format( react["date"]) return {"count": counter, "react": react}
def create_friend_request(user_id_sender, user_id_rcv, msg, picture): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) friend_request_exists = FriendRequestModel.friend_request_exists(user_id_sender, user_id_rcv) if (friend_request_exists == True): raise FriendRequestAlreadyExistsException friendship_exists = FriendModel.friendship_exists(user_id_sender, user_id_rcv) if friendship_exists == True: raise FriendshipAlreadyExistsException friend_req_id = str(uuid.uuid4().hex) date = DateController.get_date_time() friend_request = FriendRequestModel.get_new_friend_request(friend_req_id, user_id_sender, user_id_rcv, msg, date, picture) db.friends_request.insert(friend_request) friend_request["date"] = DateController.get_date_time_with_format(friend_request["date"]) return friend_request