Beispiel #1
0
 def get(self, tag):
     if not tag:
         return jsonify(**{'error': 'Incorrect tag'})
     tags = Tag.objects().distinct('title')
     if tag not in tags:
         abort(404, message=f'Not found posts with tag: {tag}')
     tag = Tag.objects(title=tag).get()
     posts = Post.objects(tag=tag)
     return PostSchema().dump(posts, many=True)
Beispiel #2
0
 def get(tag=None):
     try:
         tag_id = Tag.objects(tag='#' + tag.lower())[0].id
         posts = Post.objects(tag=tag_id)
         for post in posts:
             post.views += 1
             post.save()
         return PostSchema(many=True).dump(posts)
     except IndexError:
         return []
Beispiel #3
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()
Beispiel #4
0
 def get(self, id=None):
     if not id:
         objects = Tag.objects()
         return TagSchema().dump(objects, many=True)
     return TagSchema().dump(Tag.objects(id=id).get())
Beispiel #5
0
 def delete(self, id=None):
     if not id:
         abort(404, message='ID incorrect')
     TagSchema().dump(Tag.objects(id=id).delete())
Beispiel #6
0
 def put(self, id):
     obj = Tag.objects(id=id).get()
     obj.update(**request.json)
     return TagSchema().dump(obj.reload())