def create_user(): data = request.get_json() if User.get_by_username(data.get('username')): return api_error(dict(username=['Username already exists'])) user = User.from_json(data) user.save() return jsonify(UserAdministrationResource(user))
def login(): json = request.get_json() if 'username' in json: user = User.get_by_username(json.get('username').lower()) if user is not None and user.check_password(json.get('password')): login_user(user) return jsonify(CurrentUserResource(user)) return api_error(dict(form=['Invalid username/password.']))
def update_user(user_id): user = User.get_by_id(user_id) if not user: return form_error('User not found') data = request.get_json() if data.get('username') != user.username and User.get_by_username( data.get('username')): return api_error(dict(username=['Username already in use'])) if 'password' in data and len(data.get('password')) > 0: user.set_password(data.get('password')) user.name = data.get('name') user.username = data.get('username') user.phone_number = data.get('phone_number') user.role = data.get('role') user.save() return jsonify(UserAdministrationResource(user))