def log_user_activity_resume(user_id, days): date_from = DateController.get_date_time_dec_by_days(days) date_to = DateController.get_date_time() user_resume = { "from": DateController.get_date_time_with_format(date_from), "to": DateController.get_date_time_with_format(date_to), "user_id": user_id, "num_stories": UserActivityModel.get_size_stories_by_user_id( user_id, date_from, date_to), "num_comments": UserActivityModel.get_size_comments_by_user_id( user_id, date_from, date_to), "num_reactions": UserActivityModel.get_size_reactions_by_user_id( user_id, date_from, date_to), "num_friends": UserActivityModel.get_size_friends_by_user_id( user_id, date_from, date_to) } return user_resume
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 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 format_storie_dates(storie): storie['created_time'] = DateController.get_date_time_with_format( storie['created_time']) storie['updated_time'] = DateController.get_date_time_with_format( storie['updated_time']) storie['expired_time'] = DateController.get_date_time_with_format( storie['expired_time']) return storie
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 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 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 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 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 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 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 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 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 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 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 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 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
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 count_today_stories(story_type='normal'): db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) date_from = DateController.today() date_to = DateController.tomorrow() count = db.stories.find({ "$and": [ { "story_type": story_type }, { "created_time": { '$gte': date_from } }, { "created_time": { '$lt': date_to } }, ] }).count() return count
def inc_requests(): date = DateController.get_date() hour = int(DateController.get_hour()) response = {} db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) data = {'date': date, 'hour': hour} request = db.server_requests.find_one(data) if (request == None): RequestCounterModel.create_structure() request = db.server_requests.find_one(data) response = db.server_requests.find_one_and_update( { 'date': date, 'hour': hour }, {'$inc': { 'count': 1 }}, return_document=RETURN_DOCUMENT_AFTER) #response["date"] = DateController.get_date_time_with_format(response["date"]) 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_comment(body): from models.storie import StorieModel db = MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD) storie_id = body["storie_id"] if not StorieModel.storie_exists(storie_id): raise NoStorieFoundException comment_id = str(uuid.uuid4().hex) user_id = body["user_id"] UserDataModel.exist_user(user_id) rev = "" comment_date = DateController.get_date_time() message = body["message"] comment = CommentModel.get_new_comment(comment_id,storie_id,user_id,rev,comment_date,message) db.storie_comments.insert(comment) comment["date"] = DateController.get_date_time_with_format(comment["date"]) return comment
def get_storie_resume(storie): storie_resume = { "storie_id": storie["_id"], "past": DateController.get_past_days(storie["created_time"]), "num_comments": CommentModel.count_storie_comments(storie["_id"]), "num_reactions": (storie["reactions"]["LIKE"]["count"] + storie["reactions"]["NOTLIKE"]["count"] + storie["reactions"]["GETBORED"]["count"] + storie["reactions"]["ENJOY"]["count"]) } return storie_resume
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 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