def retrieve_user(pk): """ Retrieve user with pk Query string params: None Response: User object { birthday: "YYYY-MM-DD", email: "*****@*****.**", first_name: "First", last_name: "Last", pk: 1, relations: [ { hero: 1, pk: 1, user: 1 }, ... ] } """ user = User.query.filter_by(pk=pk).first_or_404() return user_schema.jsonify(user)
def list_users(): """ List all users Query string params: None Response: List of user objects [ { birthday: "YYYY-MM-DD", email: "*****@*****.**", first_name: "First", last_name: "Last", pk: 1, relations: [ { hero: 1, pk: 1, user: 1 }, ... ] }, ... ] """ users = User.query.all() return user_schema.jsonify(users, many=True)
def post(self): self.error_message = {} mandatory_fields = ['first_name', 'email', 'phone_number', 'dob'] for field in mandatory_fields: if field not in request.json: self.error_message[field] = 'This field is required' if self.error_message: response = jsonify(self.error_message) response.status_code = status.HTTP_400_BAD_REQUEST return response try: first_name = request.json['first_name'] last_name = request.json.get('last_name', None) email = request.json['email'] phone_number = request.json['phone_number'] dob = request.json['dob'] new_entry = UserData(first_name, last_name, email, phone_number, dob) db.session.add(new_entry) db.session.commit() response = user_schema.jsonify(new_entry) response.status_code = status.HTTP_201_CREATED return response except exc.IntegrityError as e: self.error_message["message"] = str(e.orig) response = jsonify(self.error_message) response.status_code = status.HTTP_400_BAD_REQUEST return response except Exception as e: self.error_message["message"] = "Something went wrong" response = jsonify(self.error_message) response.status_code = status.HTTP_501_NOT_IMPLEMENTED return response
def get_one_user(current_user, public_id): if not current_user.admin: return jsonify({'message': 'Cannot perform that operation'}) user = User.query.filter_by(public_id=public_id).first() if not user: return jsonify({'message': 'no such user exists'}) return user_schema.jsonify(user)
def add_user(): username = request.json['username'] email = request.json['email'] email = request.json['email'] password = request.json['password'] new_user = User(username, email, password) db.session.add(new_user) db.session.commit() return user_schema.jsonify(new_user)
def edit_user(pk): """ Edit user data. Currently only implements editing user hero relations. Query string params: None PATCH body format: { add: List of hero pks, e.g. [1, 4, 5, ...] remove: List of relation pks, e.g. [1, 3, 7, ...] } Response: User object { birthday: "YYYY-MM-DD", email: "*****@*****.**", first_name: "First", last_name: "Last", pk: 1, relations: [ { hero: 1, pk: 1, user: 1 }, ... ] } """ user = User.query.filter_by(pk=pk).first_or_404() data = request.get_json() if not data: raise BadRequest( "Request must include json in its body and be of content-type: application/json" ) relations_to_remove = data.get("remove", []) heroes_to_add = data.get("add", []) if relations_to_remove: sq = db.session.query(Relation.pk).filter_by(user_pk=1).filter( Relation.pk.in_(relations_to_remove)).subquery() Relation.query.filter( Relation.pk.in_(sq)).delete(synchronize_session="fetch") db.session.commit() if heroes_to_add: heroes = Hero.query.filter(Hero.pk.in_(heroes_to_add)).all() for hero in heroes: user.heroes.append(hero) db.session.commit() return user_schema.jsonify(user)
def login(): username = request.json['username'] password = request.json['password'] user = User.query.filter_by(username=username).first() if user is None: return jsonify({"err": "Invalid username."}) else: if user.verify_login(password): return user_schema.jsonify(user) else: return jsonify({"err": "Invalid password."})
def get_user(pk): user = User.query.get(pk) return user_schema.jsonify(user)
def getuser(): user = User.query.filter_by(userId=get_jwt_identity()).first() if not user: return jsonify({'error': 'No such user found'}), 401 return user_schema.jsonify(user)