def register(self,params): print('REGISTRATION IN PROCESS...') errors = {} if not validations.validate_email(params.get('email')): errors['email'] = 'Invalid Email.' print('REGISTRATION ERRORS', errors) if not validations.validate_letters(params.get('first_name'),2): errors['first_name'] = 'Invalid First Name.' print('REGISTRATION ERRORS', errors) if not validations.validate_letters(params.get('last_name'),2): errors['last_name'] = 'Invalid Last Name.' print('REGISTRATION ERRORS', errors) if not validations.match_strings(params.get('pw1'),params.get('pw2')): errors['password'] = '******' print('REGISTRATION ERRORS', errors) if not validations.validate_length(params.get('pw1'),8) and not validations.validate_length(params.get('pw2'),8): errors['password'] = '******' print('REGISTRATION ERRORS', errors) if errors: print('-> FAILED REGISTRATION <-', errors) result = {'result': False, 'errors': errors} return {'result': False, 'errors': errors} else: print('USER INFO VALIDATED! CREATING USER...',params['pw1']) new_user = User.objects.create(first_name=params['first_name'],last_name=params['last_name'],email=params['email'],password=validations.gen_password(params['pw1'])) print('NEW USER:'******'result': True,'user':new_user}
def create_user(user_details): errors = [] user_details = json.loads(user_details) db = connect_mongodb() if not db: result['status_code'] = 400 result['message'] = "db connection failed" return result else: if 'username' in user_details: if not validate_username(user_details['username']): errors.append("username is invalid") else: mycol = db.users username = user_details['username'] x = list(mycol.find({'username': username})) if len(x) != 0: result['status_code'] = 203 result['message'] = "This username is in use. Try another" result['data'] = {} return result if 'first_name' in user_details and 'last_name' in user_details: if validate_name(user_details['first_name']) and validate_name(user_details['last_name']): user_details['name'] = { 'first' : user_details['first_name'], 'last' : user_details['last_name'] } del user_details['first_name'] del user_details['last_name'] else: errors.append('name is invalid. Max size is 50 characters') else: errors.append('first_name and last_name are required.') if 'email_id' in user_details and not validate_email(user_details['email_id']): errors.append('email_id is invalid') if 'contact_number' in user_details: if not validate_contact(user_details['contact_number']): errors.append('contact_number is invalid') else: errors.append('contact number is required') if 'dob' in user_details and not validate_dob(user_details['dob']): errors.append("invalid dob format. use example - Nov 25, 1997") user_details['is_deleted'] = 0 user_details['create_time'] = datetime.now() mycol = db.users x = mycol.insert_one(user_details) user_id = x.inserted_id if len(errors) > 0: result['status_code'] = 400 result['message'] = errors result['data'] = {"errors":errors} return result result['status_code'] = 200 result['message'] = "Signup Successful" result['data'] = {"user_id": user_id} return result
def register(self,params): print('REGISTRATION IN PROCESS...') errors = {} if not validations.validate_email(params.get('email')): errors['email'] = 'Invalid Email.' print('REGISTRATION ERRORS', errors) if not validations.validate_letters(params.get('first_name'),2): errors['first_name'] = 'Invalid First Name.' print('REGISTRATION ERRORS', errors) if not validations.validate_letters(params.get('last_name'),2): errors['last_name'] = 'Invalid Last Name.' print('REGISTRATION ERRORS', errors) if not validations.match_strings(params.get('pw1'),params.get('pw2')): errors['password'] = '******' print('REGISTRATION ERRORS', errors) if not validations.validate_letters(params.get('pw1'),8) and not validations.validate_letters(params.get('pw2'),8): errors['password'] = '******' print('REGISTRATION ERRORS', errors) if errors: print('-> FAILED REGISTRATION <-', errors) result = {'result': False, 'errors': errors} return {'result': False, 'errors': errors} else: print('USER INFO VALIDATED! CREATING USER...') result = {'result': True} return result
def login(self, email, password): # validate if validations.validate_email(email): # process errors = {} # find returns a list, can be empty try: u = User.objects.get(email=email) print('USER FOUND, CHECKING PASSWORD... ', u) # print('password: '******'should match: ', bcrypt.hashpw(password.encode(), bcrypt.gensalt())) if u: # compare hashed pw entered against hashed pw stored if True == True: # if bcrypt.hashpw(password.encode(),bcrypt.gensalt()) == u[0].password: return {'self': u} else: print('INVALID PASSWORD') return {'errors': 'Password incorrect!'} except: print('INVALID EMAIL') return {'errors': 'Email not found!'} else: return False
def update_user(user_details): errors = [] db = connect_mongodb() if not db: result['status_code'] = 400 result['message'] = "db connection failed" return result else: mycol = db.users user_details = json.loads(user_details) if 'user_id' in user_details: if not validate_user_id: errors.append("invalid user_id") else: x = list(mycol.find({'_id': ObjectId(user_details['user_id'])})) if len(x) == 0: result['status_code'] = 201 result['message'] = "no data found" result['data'] = {} return result find_query = {"_id": ObjectId(user_details['user_id'])} else: errors.append("user_id is required for updation") if 'first_name' in user_details and 'last_name' in user_details: if validate_name(user_details['first_name']) and validate_name(user_details['last_name']): user_details['name'] = { 'first': user_details['first_name'], 'last': user_details['last_name'] } del user_details['first_name'] del user_details['last_name'] else: errors.append('name is invalid. Max size is 50 characters') else: errors.append('first_name and last_name are required.') if 'email_id' in user_details and not validate_email(user_details['email_id']): errors.append('email_id is invalid') if 'contact_number' in user_details: if not validate_contact(user_details['contact_number']): errors.append('contact_number is invalid') if 'dob' in user_details and not validate_dob(user_details['dob']): errors.append("invalid dob format. use example - Nov 25, 1997") mycol.update(find_query, user_details) x = list(mycol.find({'_id': ObjectId(user_details['user_id'])}))[0] result['status_code'] = 200 result['message'] = "Updation Successful" result['data'] = x return result
def login(self,email,password): if validations.validate_email(email): # process errors = {} # .filter() returns a dict, can be empty # .get() will return an object, errors on not found. so... try: u = User.objects.get(email=email) print('USER FOUND, CHECKING PASSWORD... ',u) if bcrypt.hashpw(password.encode(),u.password) == u.password: return {'self': u} else: print('INVALID PASSWORD') return {'errors':'Password incorrect!'} except: print('INVALID EMAIL') return {'errors':'Email not found!'} else: return False
def login(self,email,password): print('LOGIN IN PROCESS....') if validations.validate_email(email): errors = {} # find returns a list, can be empty u = User.objects.find(email=email) if u[0]: print('USER FOUND. NOW MATCHING PASSWORDS.....') # compare hashed pw entered against hashed pw stored print(bcrypt.hashpw(password)) print(u[0].password) if bcrypt.hashpw(password) == u[0].password: return {'self': u[0]} else: print('PASSWORD NOT MATCH'') return False else: print('DID NOT FIND A USER TO LOGIN!!'') # ERROR INVALID EMAIL return False else: return False