Esempio n. 1
0
    def post(self) -> dict:
        user_params = self.params()
        user = session.query(User).filter(User.active).filter(User.email == user_params['email']).first()

        if user:
            password = user.password.encode()

            if bcrypt.check_password_hash(password, user_params['password']):
                user.authenticated = True
                session.commit()

                access_token = create_access_token(user.email)
                refresh_token = create_refresh_token(user.email)

                response = {
                    "access_token": access_token,
                    "refresh_token": refresh_token,
                    "user": user.id
                }

                response = BasicResponse(response)
                return BasicResponseSchema().dump(response)
            else:
                raise BasicError(message='Wrong password.', status=400)

        raise BasicError(message='User not found.', status=400)
    def delete(self, id: int) -> dict:
        ingredient = session.query(Ingredient).get(id)
        session.delete(ingredient)
        session.commit()

        response = BasicResponse(None)
        return BasicResponseSchema().dump(response)
Esempio n. 3
0
def other_error_handler(error):
    print(error)
    basic_error_element = BasicError(parent_error=error)
    basic_response = BasicResponse(error=basic_error_element)
    response = jsonify(BasicResponseSchema().dump(basic_response))
    response.status_code = 500
    return response
    def post(self) -> dict:
        create_params = self.create_params()
        ingredient = Ingredient(**create_params)

        session.add(ingredient)
        session.commit()

        ingredient = IngredientClientSchema().dump(ingredient)
        response = BasicResponse(ingredient)
        return BasicResponseSchema().dump(response)
    def put(self, id: int) -> dict:
        ingredient = session.query(Ingredient).get(id)

        update_params = self.update_params()

        for key, value in update_params.items():
            setattr(ingredient, key, value)

        session.commit()

        ingredient = IngredientClientSchema().dump(ingredient)
        response = BasicResponse(ingredient)
        return BasicResponseSchema().dump(response)
    def get(self) -> dict:
        user_id = self.search_params()

        if user_id:
            user = session.query(User).get(user_id)
            if not user:
                raise BasicError('User not found.', 404)
            receipts = user.receipts
        else:
            receipts = session.query(Receipt).all()

        receipts = ReceiptClientSchema().dump(receipts, many=True)
        response = BasicResponse(receipts)
        return BasicResponseSchema().dump(response)
    def post(self, id: int) -> dict:
        current_user = get_jwt_identity()
        user = session.query(User).filter(User.email == current_user).one()

        receipt = session.query(Receipt).get(id)

        if not receipt:
            raise BasicError('Receipt not found.', 404)

        user.receipts.append(receipt)

        session.add(user)
        session.commit()

        response = BasicResponse()

        return BasicResponseSchema().dump(response)
    def post(self) -> dict:
        create_params = self.create_params()

        hash_password = bcrypt.generate_password_hash(
            create_params["password"])
        hash_password = hash_password.decode('utf-8')
        create_params["password"] = hash_password

        user = User(**create_params)

        try:
            session.add(user)
            session.commit()

            user = UserClientSchema().dump(user)
            response = BasicResponse(user)
        except IntegrityError:
            session.rollback()
            raise BasicError(message='email is already exist.', status=400)

        return BasicResponseSchema().dump(response)
    def put(self, id: int) -> dict:
        user = session.query(User).get(id)

        update_params = self.update_params()
        if update_params.get('password'):
            hash_password = bcrypt.generate_password_hash(
                update_params["password"])
            hash_password = hash_password.decode('utf-8')
            update_params["password"] = hash_password

        for key, value in update_params.items():
            setattr(user, key, value)

        try:
            session.commit()

            user = UserClientSchema().dump(user)
            response = BasicResponse(user)
        except IntegrityError:
            session.rollback()
            raise BasicError(message='email is already exist.', status=400)

        return BasicResponseSchema().dump(response)
 def get(self, id: int) -> dict:
     ingredient = session.query(Ingredient).get(id)
     ingredient = IngredientClientSchema().dump(ingredient)
     response = BasicResponse(ingredient)
     return BasicResponseSchema().dump(response)
 def get(self) -> dict:
     ingredients = session.query(Ingredient).all()
     ingredients = IngredientClientSchema().dump(ingredients, many=True)
     response = BasicResponse(ingredients)
     return BasicResponseSchema().dump(response)
Esempio n. 12
0
def basic_error_handler(error):
    basic_response = BasicResponse(error=error)
    response = jsonify(BasicResponseSchema().dump(basic_response))
    response.status_code = error.status
    return response
 def get(self, id: int) -> dict:
     receipt = session.query(Receipt).get(id)
     receipt = ReceiptClientSchema().dump(receipt)
     response = BasicResponse(receipt)
     return BasicResponseSchema().dump(response)
Esempio n. 14
0
 def get(self, id: int) -> dict:
     user = session.query(User).get(id)
     user = UserClientSchema().dump(user)
     response = BasicResponse(user)
     return BasicResponseSchema().dump(response)
Esempio n. 15
0
 def get(self) -> dict:
     users = session.query(User).all()
     users = UserClientSchema().dump(users, many=True)
     response = BasicResponse(users)
     return BasicResponseSchema().dump(response)