Beispiel #1
0
    def put(self, id):
        author = AuthorModel.query.get(id)
        if not author:
            return f"No author with id={id}", 404
        parser = reqparse.RequestParser()
        parser.add_argument("name")
        parser.add_argument("surname")
        author_data = parser.parse_args()

        if author.name == author_data["name"] and author.surname == author_data["surname"]:
            return "The name and surname of the author you want to change" \
                   " matches the ones you pass in the request." \
                   " Enter another name or surname of the author." \
                   " The changes will not be applied.", 400

        author.name = author_data["name"] or author.name
        author.surname = author_data["surname"] or author.surname

        try:
            db.session.add(author)
            db.session.commit()
            return author.to_dict(), 200
        except:
            return "An error occurred while changing the author. " \
                   "Maybe The name and surname of the author you want to change" \
                   " matches the ones you pass in the request." \
                   " Enter another name or surname of the author." \
                   " The changes will not be applied.", 404
 def post(self):
     parser = reqparse.RequestParser()
     parser.add_argument("name", required=True)
     author_data = parser.parse_args()
     author = AuthorModel(author_data["name"])
     db.session.add(author)
     db.session.commit()
     return author.to_dict(), 201
Beispiel #3
0
 def post(self):
     parser = reqparse.RequestParser()
     parser.add_argument("username", required=True)
     parser.add_argument("password", required=True)
     user_data = parser.parse_args()
     user = UserModel(**user_data)
     db.session.add(user)
     db.session.commit()
     return user_schema.dump(user), 201
 def put(self, author_id):
     parser = reqparse.RequestParser()
     parser.add_argument("name", required=True)
     author_data = parser.parse_args()
     author = AuthorModel.query.get(author_id)
     if author is None:
         return {"Error": f"Author id={author_id} not found"}, 404
     author.name = author_data["name"]
     db.session.commit()
     return author.to_dict(), 200
    def put(self, quote_id):
        parser = reqparse.RequestParser()
        parser.add_argument("author")
        parser.add_argument("text")
        new_data = parser.parse_args()

        quote = QuoteModel.query.get(quote_id)
        quote.author = new_data["author"]
        quote.text = new_data["text"]
        db.session.commit()
        return quote.to_dict(), 200
Beispiel #6
0
 def post(self, author_id):
     parser = reqparse.RequestParser()
     parser.add_argument("quote", required=True)
     quote_data = parser.parse_args()
     author = AuthorModel.query.get(author_id)
     if author:
         quote = QuoteModel(author, quote_data["quote"])
         db.session.add(quote)
         db.session.commit()
         return quote.to_dict(), 201
     return {"Error": f"Author id={author_id} not found"}, 404
Beispiel #7
0
 def put(self, author_id):
     parser = reqparse.RequestParser()
     parser.add_argument("name", required=True)
     author_data = parser.parse_args()
     author = AuthorModel.query.get(author_id)
     if author is None:
         author = AuthorModel(author_data["name"])
         db.session.add(author)
         db.session.commit()
         return author.to_dict(), 201
     author.name = author_data["name"]
     db.session.commit()
     return author.to_dict(), 200
Beispiel #8
0
    def post(self, author_id):
        parser = reqparse.RequestParser()
        parser.add_argument("quote")
        quote_data = parser.parse_args()

        author = AuthorModel.query.get(author_id)
        if not author:
            return f"Author with id={author_id} is not exists."
        quote = QuoteModel(author, quote_data["quote"])
        db.session.add(quote)
        db.session.commit()

        return quote.to_dict(), 201
    def post(self, author_id):
        parser = reqparse.RequestParser()
        parser.add_argument("text", required=True)
        quote_data = parser.parse_args()
        # TODO: раскомментируйте строку ниже, чтобы посмотреть quote_data
        #   print(f"{quote_data=}")
        author = AuthorModel.query.get(author_id)
        if author is None:
            return {"Error": f"Author id={author_id} not found"}, 404

        quote = QuoteModel(author, quote_data["text"])
        db.session.add(quote)
        db.session.commit()
        return quote.to_dict(), 201
Beispiel #10
0
    def put(self, author_id, quote_id):
        quote = QuoteModel.query.filter(QuoteModel.author_id == author_id,
                                        QuoteModel.id == quote_id).all()
        if not quote:
            return f"This author has no quote with id={quote_id}", 404
        parser = reqparse.RequestParser()
        parser.add_argument("quote")
        quote_data = parser.parse_args()

        quote[0].quote = quote_data["quote"] or quote[0].quote

        db.session.add(quote[0])
        db.session.commit()
        return quote[0].to_dict(), 200
Beispiel #11
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("name", required=True)
        parser.add_argument("surname", required=True)
        author_data = parser.parse_args()

        author = AuthorModel.query.filter(AuthorModel.name == author_data["name"],
                                          AuthorModel.surname == author_data["surname"]).all()
        if author:
            if author[0].name == author_data["name"] or author[0].surname == author_data["surname"]:
                return "An author with such name or surname already exists. " \
                       "You can only add a unique name and surname", 400

        author = AuthorModel(author_data["name"], author_data["surname"])

        try:
            db.session.add(author)
            db.session.commit()
            return author.to_dict(), 201
        except:
            return "An error occurred while adding new author" \
                   "or an author with such name or surname already exists. " \
                    "You can only add a unique name and surname", 404
Beispiel #12
0
def length_restricted(min_length, max_length, base_type):
    """Type to restrict string length."""
    def validate(s):
        if len(s) < min_length:
            raise ValueError("Must be at least %i characters long" %
                             min_length)
        if len(s) > max_length:
            raise ValueError("Must be at most %i characters long" % max_length)
        return s

    return validate


# Achievement request schema
achievement_req = reqparse.RequestParser()
achievement_req.add_argument('name',
                             required=True,
                             type=str,
                             location='json',
                             help='Name of the achievement.',
                             error='Achievement name is required')
achievement_req.add_argument(
    'score',
    required=True,
    type=inputs.natural,
    location='json',
    help='Point value of the achievement (positive integer).',
    error='The achievement\'s score must be a positive integer')
achievement_req.add_argument('description',
                             required=True,