コード例 #1
0
 def get(self, first_name=None):
     if not first_name:
         return jsonify(**{'error': 'Incorrect Author'})
     authors = Author.objects().distinct('first_name')
     if first_name not in authors:
         abort(404, message=f'Not found posts with author: {first_name}')
     author = Author.objects(first_name=first_name).get()
     posts = Post.objects(author=author)
     return PostSchema().dump(posts, many=True)
コード例 #2
0
def new_quote():
    json_data = request.get_json()
    
    if not json_data:
        return {"message": "No input data provided"}, 400

    # Validate and deserialize input
    try:
        data = quote_schema.load(json_data)
    except ValidationError as err:
        return err.messages, 422
    
    first, last = data["author"]["first"], data["author"]["last"]
    author = Author.query.filter_by(first=first, last=last).first()
    
    if author is None:
        # Create a new author
        author = Author(first=first, last=last)
        db.session.add(author)
    # Create new quote
    quote = Quote(
        content=data["content"], author=author, posted_at=datetime.datetime.utcnow()
    )
    db.session.add(quote)
    db.session.commit()
    result = quote_schema.dump(
        Quote.query.get(quote.id)
    )

    return {"message": "Created new quote.", "quote": result}, 201
コード例 #3
0
ファイル: author.py プロジェクト: denkuc/ITEA_courses
 def get(nick_name=None):
     try:
         author_id = Author.objects(nick_name=nick_name)[0].id
         posts = Post.objects(author=author_id)
         for post in posts:
             post.views += 1
             post.save()
         return PostSchema(many=True).dump(posts)
     except IndexError:
         return []
コード例 #4
0
from models.models import Tag, Author, Post
from random import choice

tag = []
author = []

for i in range(1, 6):
    tag_ex = Tag(**{'title': f'#tag{i}'}).save()
    tag.append(tag_ex)

for i in range(1, 3):
    author_ex = Author(**{
        'first_name': f'First Name {i}',
        'last_name': f'Last Name {i}'
    }).save()
    author.append(author_ex)

for i in range(1, 11):
    post_ex = {
        'title': f'title by post number {i}',
        'content': f'content by post number {i}',
        'author': choice(author),
        'tag': [choice(tag), choice(tag)]
    }
    Post(**post_ex).save()
コード例 #5
0
 def delete(self, id=None):
     if not id:
         abort(404, message='ID incorrect')
     AuthorSchema().dump(Author.objects(id=id).delete())
コード例 #6
0
 def put(self, id):
     obj = Author.objects(id=id).get()
     obj.update(**request.json)
     return AuthorSchema().dump(obj.reload())
コード例 #7
0
 def post(self):
     validity = AuthorSchema().validate(request.json)
     if validity:
         return validity
     obj = Author(**request.json).save()
     return AuthorSchema().dump(obj)
コード例 #8
0
 def get(self, id=None):
     if not id:
         objects = Author.objects()
         return AuthorSchema().dump(objects, many=True)
     return AuthorSchema().dump(Author.objects(id=id).get())
コード例 #9
0
def create_database():
    for nick_name in NICK_NAMES:
        author = Author()
        author.nick_name = nick_name
        author.first_name = choice(NAMES)
        author.last_name = f'{choice(NAMES)}ченко'
        author.posts = 0
        author.save()

    for post_text in POSTS:
        post_words = post_text.split(' ')
        post_words = list(filter(None, post_words))
        post_tags = [word for word in post_words if word[0] == '#']
        tags_ids = []
        for tag_text in post_tags:
            try:
                tag = Tag.objects(tag=tag_text.lower())[0]
            except IndexError:
                tag = Tag()
                tag.tag = tag_text.lower()
                tag.save()
            tags_ids.append(tag.id)

        print(tags_ids)
        post = Post()
        post.name = f'{post_words[0]}_{post_words[1]}'
        post.content = post_text

        author = Author.objects(nick_name=choice(NICK_NAMES))[0]
        author.posts += 1
        author.save()

        post.author = author.id
        post.datetime = datetime.now()
        post.views = 0
        post.tag = tags_ids
        post.save()