def get_user_details(self, get_id): try: with db_connection.database_conn() as collection: if collection["mif_user_dt"].find( { "$and": [{ "_id": get_id }, { 'status': 1 }, { 'admin_status': 1 }] }, { '_id': 1 }).count() > 0: return collection["mif_user_dt"].find({ "_id": get_id }, { 'remark': 0, 'status': 0, 'update_fields': 0, 'account_created': 0, 'update_on': 0, "admin_status": 0 }).__getitem__(0) else: return {"msg": "User & Password Not Exist", "msg_code": 11} except Exception as e: return { "msg": "Parent Exception", "msg_code": 0, "msg_dec": str(e) }
def create_comm_on_user_feed(data): try: data = json.loads(data) data['comment_dt'] = datetime.strptime( (datetime.now().strftime("%Y-%m-%d %H:%M:%S")), "%Y-%m-%d %H:%M:%S") data['comment_id'] = randint(1, 199) post_id = data['post_id'] del data['post_id'] with db_connection.database_conn() as collection: ins_msg = collection['mif_feeds'].update_one( { '_id': post_id }, { '$push': { 'comment_dec': data } }, upsert=False).modified_count if ins_msg == 1: return {"msg": "Success", "msg_code": 1} else: return {"msg": "Fail", "msg_code": 0} except Exception as e: return { "msg": "Service Parent Exception", "msg_code": 0, "msg_dec": e }
def frgt_user_psw(self, data): try: data = json.loads(data) upt_msg = 0 # Get _id from database using verify_user method get_data = (user_service().verify_user(str(data['verify_id']))) # check user exist in DB or not user_id == False if it empty. if '_id' in get_data: userid = get_data['_id'] del data['verify_id'] data['update_on'] = datetime.strptime( (datetime.now().strftime("%Y-%m-%d %H:%M:%S")), "%Y-%m-%d %H:%M:%S") with db_connection.database_conn() as collection: if 'status' in data: upt_msg = collection['mif_user_dt'].update_one( { '_id': userid }, { '$push': { 'update_fields': data }, '$set': { 'password': data['password'], 'status': data['status'] } }).modified_count else: upt_msg = collection['mif_user_dt'].update_one( { '_id': userid }, { '$push': { 'update_fields': data }, '$set': { 'password': data['password'] } }).modified_count if upt_msg == 1: return {"msg": "Success", "msg_code": 1} else: return { "msg": "Password or Status Not Update", "msg_code": 0 } elif '_id' not in get_data and 'msg_code' in get_data: return get_data except ValueError as e: return {"msg": "Value Error", "msg_code": 86, "msg_dec": str(e)} except PyMongoError as e: return {"msg": "pymongo Error", "msg_code": 86, "msg_dec": str(e)} except InvalidStringData: return {"msg": "Invalid Data", "msg_code": 91} except Exception as e: return {"msg": str(e), "msg_code": 0} finally: client = None
def advisor_login_auth(self, data): try: data = json.loads(data) advisor_id = data['adv_id'] advisor_paswrd = data['adv_pass'] with db_connection.database_conn() as collection: if collection["mif_advisor_dt"].find( { "$and": [{ "_id": advisor_id }, { "password": advisor_paswrd }] }, { '_id': 1 }).count() > 0: get_data = collection["mif_advisor_dt"].find( { "$and": [{ "_id": advisor_id }, { "password": advisor_paswrd }] }, { '_id': 1, 'status': 1, 'admin_status': 1 }).__getitem__(0) else: return {"msg": "User & Password Not Match", "msg_code": 11} if get_data['admin_status'] == 0: return { "msg": "Sorry Your Account Deactivated By Administrator", "msg_code": 3 } elif get_data['status'] == 0 and get_data['admin_status'] == 1: return { "msg": "Account Deactivated,You Will Have To Activate Your Account", "msg_code": 5 } else: return {"msg": "Success", "msg_code": 1} except ValueError as e: return {"msg": "Value Error", "msg_code": 86} except Exception as e: return { "msg": "Parent Exception", "msg_code": 0, "msg_dec": str(e) }
def verify_user(self, get_id): try: if get_id == "": return { "msg": "Fail Password Forget - Fields Can not be Empty", "msg_code": 13 } elif get_id != '': with db_connection.database_conn() as collection: # check user exist in DB if collection["mif_user_dt"].find( { '$or': [{ "_id": str(get_id) }, { "email": str(get_id) }] }, { '_id': 1 }).count() > 0: self.user_dt = collection["mif_user_dt"].find( { '$or': [{ "_id": str(get_id) }, { "email": str(get_id) }] }, { 'mob_no': 1, 'status': 1, 'admin_status': 1 }).__getitem__(0) else: return {"msg": "User Does Not Exist", "msg_code": 11} if self.user_dt['admin_status'] == 0: return { "msg": "Sorry Your Account Deactivated By Administrator", "msg_code": 3 } else: del self.user_dt['admin_status'] return self.user_dt except Exception as e: return { "msg": "Parent Exception", "msg_code": 0, "msg_dec": str(e) }
def upt_user_cate_flw(self, data): try: data = json.loads(data) user_id = str(data['_id']) del data['_id'] ''' # bulk = collection["mif_category_dt"].initialize_unordered_bulk_op() for key, value in data.items(): bulk.find({'_id': key}).update_one({'$inc': {"cate_flw_cnt": value}}) if data[key] == -1: data[key] = 0 upt_msg = bulk.execute()''' with db_connection.database_conn() as collection: upt_msg = 0 # update mif categories follow unfollow count for key, value in data.items(): upt_msg = collection['mif_category_dt'].update_one( { '_id': key }, { '$inc': { "cate_flw_cnt": value } }).modified_count if data[key] == -1: data[key] = 0 # update user category follow unfollow collection usr_cate_upt = collection['user_cate_flw'].update_one( { '_id': user_id }, { '$set': data }, upsert=False).modified_count if (upt_msg and usr_cate_upt) == 1: return {"msg": "Success", "msg_code": 1} else: return {"msg": "Fail", "msg_code": 0} except PyMongoError as e: return {"msg": "MongoDB Error", "msg_code": 0, "msg_dec": str(e)} except Exception as e: return { "msg": "Parent Exception", "msg_code": 0, "msg_dec": str(e) }
def upt_user_prf(self, get_id, data): try: data = json.loads(data) if ('password' in data) or ('admin_status' in data) or ('status' in data): return {"msg": "Can't Update fields Directly", "msg_code": 14} with db_connection.database_conn() as collection: data['update_on'] = datetime.strptime( (datetime.now().strftime("%Y-%m-%d %H:%M:%S")), "%Y-%m-%d %H:%M:%S") upt_msg = collection["mif_user_dt"].update_one({ '_id': get_id }, { '$push': { 'update_fields': data }, '$set': data }).modified_count if upt_msg == 0: if collection["mif_user_dt"].find( { "$and": [{ "_id": get_id }, { 'status': 1 }, { 'admin_status': 1 }] }, { '_id': 1 }).count() == 0: return {"msg": "User Does Not Exist", "msg_code": 11} return {"msg": "Fail", "msg_code": 0} if upt_msg == 1: return {"msg": "Success", "msg_code": 1} except ValueError as e: return {"msg": "Value Error", "msg_code": 86, "msg_dec": str(e)} except PyMongoError: return {"msg": "DB Error", "msg_code": 83} except Exception as e: return { "msg": "Profile Updation Fail", "msg_code": 0, "msg_dec": str(e) }
def get_cate(self): try: self.data = {} cate_list = list() with db_connection.database_conn() as collection: self.data = collection['mif_category_dt'].find( {'status': { '$eq': 1 }}) for GetAllCategories in self.data: cate_list.append(GetAllCategories) return {"category_dt": cate_list} except Exception as e: return { "msg": "Parent Exception", "msg_code": 0, "msg_dec": str(e) }
def create_usr_logs(self, data): try: data = json.loads(data) user_id = str(data['user_id']) if user_id == "" or user_id is None: return {"msg": "User ID Can't be Null", "msg_code": 13} else: log_in_tm = (datetime.strptime((data['log_in']), "%Y-%m-%d %H:%M:%S")) log_out_tm = (datetime.strptime((data['log_out']), "%Y-%m-%d %H:%M:%S")) log_dur = str(log_out_tm - log_in_tm) data = { 'user_id': user_id, 'log_in': log_in_tm, 'log_out': log_out_tm, # '_id': str(str(data['user_id'])+ str(datetime.now().strftime("%Y%m%d%H%M%S.f"))), 'login_duration': log_dur } with db_connection.database_conn() as collection: upt_msg = collection["user_login_logs"].insert_one( data).inserted_id if upt_msg: return {"msg": "Success", "msg_code": 1} else: return {"msg": "Fail", "msg_code": 0} except ValueError as e: return {"msg": "Value Error", "msg_code": 86, "msg_dec": str(e)} except PyMongoError as e: return {"msg": "pymongo Error", "msg_code": 86, "msg_dec": str(e)} except DuplicateKeyError: return {"msg": "User Name Already Exist", "msg_code": 83} except InvalidStringData: return {"msg": "Invalid Data", "msg_code": 91} except Exception as e: return { "msg": "Parent Exception", "msg_code": 0, "msg_dec": str(e) } finally: client = None
def get_feeds_by_user(user_id): try: feed_list = list() with db_connection.database_conn() as collection: data = collection['mif_feeds'].find({ 'user_id': user_id }).sort('feed_dt', -1) # data = collection['mif_feeds'].find({'user_id': user_id},{"comment_dec":{"$elemMatch":{"comment_status":1}}}).sort('feeds',-1) # data = collection['mif_feeds'].find({'user_id': user_id, 'status': 1, "comment_dec": {"$elemMatch":{"comment_status":1}}}).sort('feeds',-1) for GetAllfeeds in data: feed_list.append(GetAllfeeds) return {"feeds_dt": feed_list} except Exception as e: return {{ "msg": "Service Parent Exception", "msg_code": 0, "msg_dec": e }}
def get_feeds_by_cate_id(cate_name): try: feed_list = list() cate_name = str(cate_name).upper() print("cate_name - ", cate_name) with db_connection.database_conn() as collection: data = collection['mif_feeds'].find({ 'cate_name': cate_name, 'post_type': 1 }).sort('feed_dt', -1) for GetAllfeeds in data: feed_list.append(GetAllfeeds) return {"feeds_dt": feed_list} except Exception as e: return {{ "msg": "Service Parent Exception", "msg_code": 0, "msg_dec": e }}
def get_user_cate_flw(self, user_id): try: with db_connection.database_conn() as collection: if collection["user_cate_flw"].find({ "_id": str(user_id) }, { '_id': 1 }).count() > 0: data = collection["user_cate_flw"].find({ "_id": str(user_id) }).__getitem__(0) else: data = {"msg": "User Does Not Exist", "msg_code": 11} return data except Exception as e: return { "msg": "Parent Exception", "msg_code": 0, "msg_dec": str(e) }
def upt_feeds_by_user(post_id, like): try: print("like is -", like) with db_connection.database_conn() as collection: upt_msg = collection['mif_feeds'].update_one({ '_id': post_id }, { '$inc': { "likes": int(like) } }).modified_count if upt_msg >= 1: return {"msg": "Success", "msg_code": 1} else: return {"msg": "Fail", "msg_code": 0} except Exception as e: return {{ "msg": "Service Parent Exception", "msg_code": 0, "msg_dec": e }}
def get_cate_by_user(self, user_id): try: self.data1 = {} cate_list = list() with db_connection.database_conn() as collection: if collection["user_cate_flw"].find({ "_id": str(user_id) }, { '_id': 1 }).count() > 0: data1 = collection["user_cate_flw"].find( { "_id": str(user_id) }, { '_id': 0 }).__getitem__(0) self.data = collection['mif_category_dt'].find( {'status': { '$eq': 1 }}, { '_id': 1, 'cate_name': 1, 'cate_image': 1, 'cate_flw_cnt': 1 }) for GetAllCategories in self.data: GetAllCategories['user_cate_flw_status'] = data1[ GetAllCategories['_id']] cate_list.append(GetAllCategories) return {"category_dt": cate_list} else: return {"msg": "User Does Not Exist", "msg_code": 11} except Exception as e: return { "msg": "Parent Exception", "msg_code": 0, "msg_dec": str(e) }
def res_usr_psw(self, data): try: data = json.loads(data) self.upt_msg = 0 user_id = (data['_id']) cur_date_time = datetime.strptime( (datetime.now().strftime("%Y-%m-%d %H:%M:%S")), "%Y-%m-%d %H:%M:%S") del data['_id'] if user_id == "" or data['password'] == "" or data[ 'new_password'] == "": return { "msg": "Fail Password Reset - Fields Can not be Empty", "msg_code": 13 } else: data['update_on'] = datetime.strptime( (datetime.now().strftime("%Y-%m-%d %H:%M:%S")), "%Y-%m-%d %H:%M:%S") data['upt_dec'] = "Password Reset by User" with db_connection.database_conn() as collection: self.upt_msg = collection['mif_user_dt'].update_one( { "$and": [{ "_id": user_id }, { "password": data['password'] }, { 'status': 1 }] }, { '$push': { 'update_fields': data }, '$set': { 'password': data['new_password'], 'update_on': cur_date_time } }).modified_count if self.upt_msg == 0: if collection['mif_user_dt'].find( { "$and": [{ "_id": user_id }, { "password": str(data["password"]) }] }, { '_id': 1 }).count() <= 0: return { "msg": "Current Password Not Match with ID", "msg_code": 11 } return {"msg": "Password Reset Fail", "msg_code": 0} if self.upt_msg == 1: return {"msg": "Success", "msg_code": 1} except ValueError as e: return {"msg": "Value Error", "msg_code": 86} except PyMongoError as e: return {"msg": "pymongo Error", "msg_code": 86, "msg_dec": str(e)} except Exception as e: return { "msg": "Password Reset Fail", "msg_code": 0, "msg_dec": str(e) }
def create_nw_user(self, data): try: data = json.loads(data) user_id = str(data['_id']).lower() data = { '_id': user_id, 'password': str(data['password']), 'user_name': str(data['user_name']).lower(), 'mob_no': str(data['mob_no']), 'email': str(data['email']), 'gender': str(data['gender']).lower(), 'age': int(data['age']), 'image': '/media/images/mif_users/' + user_id + '.png', 'balance': float(0), 'account_created': datetime.strptime( (datetime.now().strftime("%Y-%m-%d %H:%M:%S")), "%Y-%m-%d %H:%M:%S"), 'status': 1, 'admin_status': 1, 'remark': '' } cate_id_dt = {} cate_id_dt['_id'] = user_id with db_connection.database_conn() as collection: upt_msg = collection["mif_user_dt"].insert_one( data).inserted_id # create user details document data2 = collection["mif_category_dt"].find({ 'status': 1 }, { '_id': 1 }).sort( '_id' ) # find all categories which exist in cate table have to insert on user_cate_flw. for get_cate_id in data2: cate_id_dt.setdefault(get_cate_id['_id'], 0) upt_msg_cate = collection["user_cate_flw"].insert_one( cate_id_dt ).inserted_id # create user categories follow unfollow document against user_id. if upt_msg and upt_msg_cate == user_id: return {"msg": "Success", "msg_code": 1} else: return {"msg": "Fail", "msg_code": 0} except ValueError as e: return {"msg": "Value Error", "msg_code": 86, "msg_dec": str(e)} except DuplicateKeyError: return {"msg": "User ID Already Exist", "msg_code": 83} except PyMongoError as e: return {"msg": "pymongo Error", "msg_code": 86, "msg_dec": str(e)} except InvalidStringData: return {"msg": "Invalid Data", "msg_code": 91} except Exception as e: return { "msg": "User creation Fail", "msg_code": 0, "msg_dec": str(e) }
def post_feeds_by_user(data): try: data = json.loads(data) '''data = { 'user_id': data['user_id'], 'feeds': data['feeds'], 'cate_id': data['cate'], 'feed_dt': datetime.strptime((datetime.now().strftime("%Y-%m-%d %H:%M:%S")), "%Y-%m-%d %H:%M:%S"), }''' data['feed_dt'] = datetime.strptime( (datetime.now().strftime("%Y-%m-%d %H:%M:%S")), "%Y-%m-%d %H:%M:%S") data['likes'] = 0 data['post_type'] = int( data['post_type'] ) # if post_type = 0 then it is private post || if post_type = 1 then it is public post data['comment_dec'] = [] with db_connection.database_conn() as collection: # Check if User is valid or not if collection["mif_user_dt"].find( { "$and": [{ "_id": str(data['user_id']) }, { "admin_status": 1 }, { "status": 1 }] }, { '_id': 1 }).count() > 0: if collection['mif_feeds'].find({}, { '_id': 1 }).sort('_id', -1).limit(1).count() > 0: get_last_inserted_id = collection['mif_feeds'].find( {}, { '_id': 1 }).sort('_id', -1).limit(1).__getitem__(0) data['_id'] = str( int(get_last_inserted_id['_id']) + 1) # increment _id by 1 else: data['_id'] = "101" # First Default _id (post_id) 101 ins_msg = collection['mif_feeds'].insert_one( data).acknowledged if ins_msg: return {"msg": "Success", "msg_code": 1} else: return {"msg": "Fail", "msg_code": 0} else: return { "msg": "Sorry User Does Not Exist OR Account Deactivated", "msg_code": 3 } except PyMongoError as e: return {{ "msg": "Service PyMongo Exception", "msg_code": 0, "msg_dec": e }} except Exception as e: return {{ "msg": "Service Parent Exception", "msg_code": 0, "msg_dec": e }}
def top_trending(): try: all_data = dict() get_all_data = list() from_date = datetime.today() - timedelta(days=31) print("from_Date- ", from_date) with db_connection.database_conn() as collection: top_cate = collection['mif_feeds'].aggregate([{ '$match': { 'feed_dt': { '$gte': from_date } } }, { '$group': { '_id': "$cate_name", 'count': { '$sum': 1 } } }, { '$sort': { 'count': -1 } }, { '$limit': 5 }]) for Top_categories in top_cate: get_all_data.append(Top_categories) all_data['top_categories'] = get_all_data top_users = collection['mif_feeds'].aggregate([{ '$match': { 'comment_dec.comment_dt': { '$gte': from_date } } }, { '$project': { '_id': 0, 'comment_dec': { 'user_id': 1 } } }, { '$unwind': "$comment_dec" }, { '$group': { '_id': "$comment_dec.user_id", 'count': { '$sum': 1 } } }, { '$sort': { 'count': -1 } }, { '$limit': 10 }]) get_all_data = [] for Top_users in top_users: get_all_data.append(Top_users) all_data['top_users'] = get_all_data del get_all_data return all_data except Exception as e: return { "msg": "Service Parent Exception", "msg_code": 0, "msg_dec": e }
def del_feeds_by_user(data): try: data = json.loads(data) upt_msg = 0 post_id = data['post_id'] user_id = data['user_id'] print("your data is ", data) with db_connection.database_conn() as collection: # Check that data exist only comment_id or not if yes then it must be delete any comment. if 'post_id' in data and 'comment_id' not in data: if collection['mif_feeds'].find( { "$and": [{ '_id': post_id }, { "user_id": user_id }] }, { '_id': 1 }).count() >= 1: upt_msg = collection['mif_feeds'].delete_one({ '_id': post_id }).deleted_count else: return { "msg": "Sorry You are not Authorize User", "msg_code": 8 } elif 'comment_id' in data and 'post_id' in data: # check comment_id which will delete that comment only delete by who create that comment OR who create that particular post if collection['mif_feeds'].find( { "$and": [{ 'comment_dec.comment_id': data['comment_id'] }, { '$or': [{ 'comment_dec.user_id': user_id }, { "user_id": user_id }] }] }, { '_id': 1 }).count() >= 1: upt_msg = collection['mif_feeds'].update_one( { '_id': post_id }, { '$pull': { 'comment_dec': { 'comment_id': data['comment_id'] } } }).modified_count else: return { "msg": "Sorry You are not Authorize User", "msg_code": 8 } if upt_msg >= 1: return {"msg": "Success", "msg_code": 1} else: return {"msg": "Fail", "msg_code": 0} except Exception as e: return {{ "msg": "Service Parent Exception", "msg_code": 0, "msg_dec": e }}