def username_must_not_exist_in_db(val): num_res = db.count_doc(constants.collectionName.users.name, { constants.misc_webargs.USERNAME.name: {'$regex': generateExactMatchPattern(val), '$options': 'i'}}) if num_res != 0: raise ValidationError('Username is already taken.') else: return None
def checkPassword(username, password): expr = {constants.misc_webargs.USERNAME.name: {'$regex': generateExactMatchPattern(username), '$options': 'i'}} cursor = db.find_docs(constants.collectionName.users.name, expr) for val in cursor: # first object and return hash = val[constants.misc_webargs.PASSWORD.name] return utils.verify_hashedPassword(password, hash) return False
def editUserInfo(args): expr = {constants.misc_webargs.USERNAME.name: { '$regex': generateExactMatchPattern(args[constants.misc_webargs.REFERRER_USERNAME.name]), '$options': 'i'}} cursor = db.find_docs(constants.collectionName.users.name, expr) updt = {} role = None for val in cursor: role = val[constants.misc_webargs.ROLE.name] if role == constants.roles.ADMIN.name: if constants.misc_webargs.CREDIT_SCORE.name in args and \ args[constants.misc_webargs.CREDIT_SCORE.name] is not None: updt["constants.misc_webargs.CREDIT_SCORE.name"] = args[constants.misc_webargs.CREDIT_SCORE.name] if constants.misc_webargs.DOCUMENT1_VER_STATUS.name in args and \ args[constants.misc_webargs.DOCUMENT1_VER_STATUS.name] is not None: updt["constants.misc_webargs.DOCUMENT1_VER_STATUS.name"] = args[ constants.misc_webargs.DOCUMENT1_VER_STATUS.name] if constants.misc_webargs.DOCUMENT2_VER_STATUS.name in args and \ args[constants.misc_webargs.DOCUMENT2_VER_STATUS.name] is not None: updt["constants.misc_webargs.DOCUMENT2_VER_STATUS.name"] = args[ constants.misc_webargs.DOCUMENT2_VER_STATUS.name] elif role == constants.roles.AGENT.name: if constants.misc_webargs.TIMEZONE.name in args and args[constants.misc_webargs.TIMEZONE.name] is not None: updt["constants.misc_webargs.TIMEZONE.name"] = args[constants.misc_webargs.TIMEZONE.name] else: return False try: db.find_and_modify(constants.collectionName.users.name, expr, updt, False) except: return False return True
def checkToken(username, token): query = {constants.misc_webargs.USERNAME.name: {'$regex': generateExactMatchPattern(username), '$options': 'i'}, constants.misc_webargs.TOKEN.name: token} val = db.find_docs_count(constants.collectionName.users.name, query) if val == 1: return True else: return False
def listCustomers(username): expr = {constants.misc_webargs.AGENT_NAME.name: {'$regex': generateExactMatchPattern(username), '$options': 'i'}} cursor = db.find_docs(constants.collectionName.relations.name, expr) listData = [] for val in cursor: customer = val[constants.misc_webargs.CUSTOMER_NAME.name] listData.append(customer) return listData
def getListOfUsers(username): expr = {constants.misc_webargs.USERNAME.name: {'$regex': generateExactMatchPattern(username), '$options': 'i'}} cursor = db.find_docs(constants.collectionName.users.name, expr) role = None for val in cursor: role = val[constants.misc_webargs.ROLE.name] if role == constants.roles.ADMIN.name: adminList = listOtherUsers(constants.roles.ADMIN.name, username) agentList = listOtherUsers(constants.roles.AGENT.name, username) customerList = listOtherUsers(constants.roles.CUSTOMER.name, username) return utils.generate_response(1, {constants.roles.ADMIN.name: adminList, constants.roles.AGENT.name: agentList, constants.roles.CUSTOMER.name: customerList}) elif role == constants.roles.AGENT.name: adminList = listOtherUsers(constants.roles.ADMIN.name, username) agentList = listOtherUsers(constants.roles.AGENT.name, username) customerList = listCustomers(username) return utils.generate_response(1, {constants.roles.ADMIN.name: adminList, constants.roles.AGENT.name: agentList, constants.roles.CUSTOMER.name: customerList}) else: return utils.generate_response(0, "FAILURE")
def viewLoansRequest(username): expr = {constants.misc_webargs.USERNAME.name: {'$regex': generateExactMatchPattern(username), '$options': 'i'}} cursor = db.find_docs(constants.collectionName.users.name, expr) role = None for val in cursor: role = val[constants.misc_webargs.ROLE.name] if role == constants.roles.ADMIN.name: expr = [{"$lookup": {"from": "loan_customer", "localField": "INSERTED_ID", "foreignField": "_id", "as": "R"}}, {"$project":{"R._id":0, "INSERTED_ID":0,"_id":0,"R.TIMESTAMP":0,"LOAN_CUST_ID":0,"USERNAME":0}},{"$unwind": "$R"}] elif role == constants.roles.AGENT.name: expr = [{"$lookup": {"from": "loan_customer", "localField": "INSERTED_ID", "foreignField": "_id", "as": "R"}}, {"$project":{"R._id":0, "INSERTED_ID":0,"_id":0,"R.TIMESTAMP":0,"LOAN_CUST_ID":0,"USERNAME":0}},{"$match":{"R.REFERRER_USERNAME":username}},{"$unwind": "$R"}] else: expr = [{"$lookup": {"from": "loan_customer", "localField": "INSERTED_ID", "foreignField": "_id", "as": "R"}}, {"$project":{"R._id":0, "INSERTED_ID":0,"_id":0,"R.TIMESTAMP":0,"LOAN_CUST_ID":0,"USERNAME":0}},{"$match":{"R.CUSTOMER_NAME":username}},{"$unwind": "$R"}] listData = [] req_coll = db.aggregate_db(constants.collectionName.loan_customer_counter.name, expr) for val in list(req_coll): listData.append(val) return utils.generate_response(1, listData)
def addToken(username, token): filter = {constants.misc_webargs.USERNAME.name: {'$regex': generateExactMatchPattern(username), '$options': 'i'}} update = {'$set': {constants.misc_webargs.TOKEN.name: token}} if db.edit_single_doc(constants.collectionName.users.name, filter, update) > 0: return True return False