def patch_current_user(): username = get_jwt_identity() data = request.get_json() auth_token = None user = User.query.filter_by(username=username).first() if data.get('company'): user.company = data.get('company') if data.get('email'): # check if email already used existing_user = User.query.filter( and_(User.id != user.id, User.email == data.get('email'))).first() if not existing_user: user.email = data.get('email') else: return errors.bad_request(message='email_already_used') if data.get('first_name'): user.first_name = data.get('first_name') if data.get('last_name'): user.last_name = data.get('last_name') try: if data.get('role'): user.roles = data.get('role') except: pass db.session.add(user) db.session.commit() return { 'auth_token': auth_token if auth_token else None, 'user': user.to_dictionary(), }
def register_user(): # get the post data post_data = request.get_json() # check if user already exists user = User.query.filter( or_( User.username == post_data.get('username'), User.email == post_data.get('email') ) ).first() if not user: try: user = User( username=post_data.get('username'), password=post_data.get('password'), email=post_data.get('email'), first_name=post_data.get('first_name'), last_name=post_data.get('last_name'), admin=post_data.get('admin') ) if app.config['MAIL_USERNAME'] and app.config['MAIL_PASSWORD']: send_confirmation_email(app, user.email) else: user.confirmed = True user.confirmed_on = datetime.now() # insert the user db.session.add(user) db.session.commit() return jsonify({ 'status': 'success', 'data': { 'user': user.to_dictionary(), } }), 201 except Exception as e: errors.unauthorized(e) else: message = 'user_already_exists' if user.username == post_data.get('username'): message = 'username_already_used' elif user.email == post_data.get('email'): message = 'email_already_used' return errors.bad_request(message=message)
def admin_patch_user(user_id): data = request.get_json() user = User.query.get_or_404(user_id) if data.get('company') is not None: user.company = data.get('company') if data.get('email') is not None: # check if email already used existing_user = User.query.filter( and_(User.id != user_id, User.email == data.get('email'))).first() if not existing_user: user.email = data.get('email') else: return errors.bad_request(message='email_already_used') if data.get('first_name') is not None: user.first_name = data.get('first_name') if data.get('last_name') is not None: user.last_name = data.get('last_name') if data.get('admin_validation') is not None: user.admin_validation = data.get('admin_validation') if data.get('roles') is not None: roles = [] if data.get('roles') == 'ADMIN': role = Role.query.filter_by(name='ADMIN').one() roles.append(role) elif data.get('roles') == 'ANNOTATOR': role = Role.query.filter_by(name='VIEWER').one() roles.append(role) else: role = Role.query.filter_by(name='VIEWER').one() roles.append(role) user.roles = roles db.session.add(user) db.session.commit() return { 'user': user.to_dictionary(), }
def reset_password(): # get the post data post_data = request.get_json() # check if user already exists user = User.query.filter(User.email == post_data.get('email')).first() if user: try: ''' user = User( password=post_data.get('password'), ) ''' if app.config['MAIL_USERNAME'] and app.config['MAIL_PASSWORD']: send_reset_email(app, post_data.get('email')) else: user.confirmed = True user.confirmed_on = datetime.now() # insert the user db.session.add(user) db.session.commit() return { 'user': user.to_dictionary(), } except Exception as e: errors.unauthorized(e) else: message = 'user_already_exists' if user.username == post_data.get('username'): message = 'username_already_used' elif user.email == post_data.get('email'): message = 'email_already_used' return errors.bad_request(message=message)