def recover_password(user_id): logging.info('[SERVICE]: Recovering password' + user_id) logging.info('[DB]: QUERY') user = UserService.get_user(user_id=user_id) if not user: raise UserNotFound(message='User with id ' + user_id + ' does not exist') password = ''.join( random.choices(string.ascii_uppercase + string.digits, k=20)) user.password = user.set_password(password=password) try: logging.info('[DB]: ADD') db.session.add(user) db.session.commit() try: email = EmailService.send_html_email( recipients=[user.email], html='<p>User: '******'</p><p>Password: '******'</p>', subject='[trends.earth] Recover password') except EmailError as error: raise error except Exception as error: raise error return user
def authenticate_user(user_id, password): logging.info('[SERVICE]: Authenticate user '+user_id) user = UserService.get_user(user_id=user_id) if not user: raise UserNotFound(message='User with email '+user_id+' does not exist') if not user.check_password(password): raise AuthError(message='User or password not valid') # to serialize id with jwt user.id = user.id.hex return user
def delete_user(user_id): logging.info('[SERVICE]: Deleting user'+user_id) user = UserService.get_user(user_id=user_id) if not user: raise UserNotFound(message='User with email '+user_id+' does not exist') try: logging.info('[DB]: DELETE') db.session.delete(user) db.session.commit() except Exception as error: raise error return user
def get_user(user_id): logging.info('[SERVICE]: Getting user '+user_id) logging.info('[DB]: QUERY') try: val = UUID(user_id, version=4) user = User.query.get(user_id) except ValueError: user = User.query.filter_by(email=user_id).first() except Exception as error: raise error if not user: raise UserNotFound(message='User with id '+user_id+' does not exist') return user
def update_user(user, user_id): logging.info('[SERVICE]: Updating user') current_user = UserService.get_user(user_id=user_id) if not current_user: raise UserNotFound(message='User with id '+user_id+' does not exist') if 'role' in user: role = user.get('role') if user.get('role') in ROLES else current_user.role current_user.role = role current_user.name = user.get('name', current_user.name) current_user.country = user.get('country', current_user.country) current_user.institution = user.get('institution', current_user.institution) current_user.updated_at = datetime.datetime.utcnow() try: logging.info('[DB]: ADD') db.session.add(current_user) db.session.commit() except Exception as error: raise error return current_user