Beispiel #1
0
 def patch(self, help_request_id: str) -> JSONResponse:
     help_request = models.HelpRequest.query.filter_by(
         id=help_request_id,
     ).one()
     help_request_data = request.json
     accepted_by = help_request_data.pop('accepted_by', '')
     if accepted_by:
         if accepted_by == 'self':
             user = current_user
         else:
             user = User.query.filter_by(id=accepted_by).one()
         help_request.accepted_by = user
         help_request.accepted_at = datetime.datetime.now(
             tz=datetime.timezone.utc,
         )
     help_request = schemas.HelpRequestPartialUpdateSchema().load(
         help_request_data,
         instance=help_request,
     )
     db.session.add(help_request)
     db.session.commit()
     return JSONResponse(
         {RESPONSE_DATA_KEY: schemas.HelpRequestSchema().dump(help_request)},
         HTTPStatus.OK,
     )
Beispiel #2
0
 def post(self) -> JSONResponse:
     data = schemas.AuthLoginSchema().load(request.json)
     invalid_credentials_response = JSONResponse(
         {'message': 'Invalid credentials'},
         HTTPStatus.UNAUTHORIZED,
     )
     try:
         user = User.query.filter_by(email=data['email']).one()
     except (NoResultFound, MultipleResultsFound):
         return invalid_credentials_response
     if user.password != data['password']:
         return invalid_credentials_response
     access_token = create_access_token(identity=user)
     return JSONResponse(
         {current_app.config['JWT_JSON_KEY']: access_token},
         HTTPStatus.OK,
     )
Beispiel #3
0
 def get(self, help_request_id: str) -> JSONResponse:
     help_request = models.HelpRequest.query.filter_by(
         id=help_request_id,
     ).one()
     return JSONResponse(
         {RESPONSE_DATA_KEY: schemas.HelpRequestSchema().dump(help_request)},
         HTTPStatus.OK,
     )
Beispiel #4
0
 def post(self) -> JSONResponse:
     user = schemas.UserSchema().load(request.json)
     db.session.add(user)
     db.session.commit()
     return JSONResponse(
         {'data': schemas.UserSchema(exclude=('password', )).dump(user)},
         HTTPStatus.CREATED,
         headers={
             'Location': url_for('.user-detail', user_id=str(user.id)),
         },
     )
Beispiel #5
0
 def get(self, user_id: str) -> JSONResponse:
     if user_id == 'self':
         user = current_user
     else:
         # TODO: add exception handler that handle sqlachemy exceptions
         # like `NoResultFound` and return `HTTP_404` response with proper
         # message
         user = models.User.query.get(user_id)
     return JSONResponse(
         {'data': schemas.UserSchema().dump(user)},
         HTTPStatus.OK,
     )
Beispiel #6
0
 def get(self) -> JSONResponse:
     accepted_by = request.args.get('accepted_by', None)
     if accepted_by == 'self':
         accepted_by = current_user.id
     help_requests = models.HelpRequest.query.filter_by(
         accepted_by_id=accepted_by,
     )
     return JSONResponse(
         {
             RESPONSE_DATA_KEY: schemas.HelpRequestSchema(many=True).dump(
                 help_requests,
             ),
         },
         HTTPStatus.OK,
     )
Beispiel #7
0
 def post(self) -> JSONResponse:
     payload = json.loads(request.form['payload'])
     help_request_data = schemas.HelpRequestCreateSchema().load(payload)
     help_request = schemas.HelpRequestSchema().load(help_request_data)
     db.session.add(help_request)
     db.session.commit()
     return JSONResponse(
         {RESPONSE_DATA_KEY: schemas.HelpRequestSchema().dump(help_request)},
         HTTPStatus.CREATED,
         headers={
             'Location': url_for(
                 '.help_requests-detail',
                 help_request_id=str(help_request.id),
             ),
         },
     )
Beispiel #8
0
 def delete(self) -> JSONResponse:
     # TODO: blacklist token
     return JSONResponse(None, HTTPStatus.OK)