Example #1
0
    def getPosts(offset):
        posts = PostModel.get_posts_by_offset(offset)
        print(posts)
        tags = []

        for p in posts:
            ts = p.post.tags

            for t in ts:
                if t.tagId not in tags:
                    tags.append(t.tagId)

        tagsFound = TagModel.get_all_tags(tags)
        content = []

        for p in posts:
            x = p.post.json()
            ts = p.post.tags
            xtags = []

            for t in ts:
                for y in tagsFound:
                    if t.tagId == y.id:
                        xtags.append(y.json())

            x.append({"tags": xtags})

            content.append(x)

        return {"error": 0, "content": content}
Example #2
0
    def delete(self, param):

        try:
            post = PostModel.find_by_id(param)
            post.delete()

            return {"error": 0}
        except:
            return {"error": 1, "error_msg": "Post doesn't exist"}
Example #3
0
    def get(self, param):
        p = PostModel.find_by_id(int(param))

        if p:
            x = {}
            x['log'] = p.json()
            post = p.get_post()
            x['post'] = post.json()
            x['post']['tags'] = post.get_tags()

            return {"error": 0, "content": x}
        else:
            return {"error": 1}
Example #4
0
    def post(self, param):

        data = AdminProduct.parser.parse_args()

        try:
            product = ProductModel(data.pro__title, data.pro__body,
                                   data.pro__summary, data.pro__image)

            product.save()

            post = PostModel(product.id, 1)
            post.save()

            image = RepoFileModel.find_by_id(data.pro__image)
            image.increase_users()

            for tag in json.loads(data.pro__tags):
                newTag = ProductTagModel(product.id, tag)
                newTag.save()

            return {"error": 0}
        except:
            return {"error": 1}
Example #5
0
    def getArticle(self, param):
        p = PostModel.find_by_id(int(param))

        if p:
            x = {}

            x['log'] = p.json()
            post = p.get_post()
            post.get_stats()

            x['post'] = post.json()
            x['post']['tags'] = post.get_tags()

            post.set_visitor(request.remote_addr)

            return {"error": 0, "content": x}
        else:
            return {"error": 1}
    def post(self, param, param2, offset):
        parser = reqparse.RequestParser()

        parser.add_argument('name',
                            required=True,
                            help="The name field is required")

        parser.add_argument('email',
                            required=True,
                            help="The email field is required")

        parser.add_argument('comment',
                            required=True,
                            help="The comment field is required")

        data = parser.parse_args()

        try:
            post = PostModel.find_by_id(param2)

            if not post:
                return {"error": 3}

            if int(param) == 1:
                comment = ArticleCommentModel(post.postId, data.name,
                                              data.email, data.comment)
                comment.save()

                article = ArticleModel.find_by_id(post.postId)
                article.comments += 1
                article.save()

                return {"error": 0}
            else:
                return {"error": 2}
        except:
            return {
                "error": 1,
                "error_msg": "Failed to post comment. Try again later!"
            }
Example #7
0
    def getTopArticles(number):
        articles = ArticleModel.get_top_articles(number)
        tags = []

        for a in articles:
            ts = a.tags

            for t in ts:
                if t.tagId not in tags:
                    tags.append(t.tagId)

        tagsFound = TagModel.get_all_tags(tags)
        postLogs = PostModel.find_by_posts([x.id for x in articles])
        content = []

        for a in articles:
            x = {}

            for pl in postLogs:
                if pl.postId == a.id:
                    x['log'] = pl.json()
                    break

            x['post'] = a.json()
            x['post']['body'] = ""

            ts = a.tags
            xtags = []

            for t in ts:
                for y in tagsFound:
                    if t.tagId == y.id:
                        xtags.append(y.json())

            x['tags'] = xtags

            content.append(x)

        return {"error": 0, "content": content}
Example #8
0
    def getPosts(offset):
        posts = PostModel.get_posts_by_offset(offset)
        tags = []

        for p in posts:
            post = p.get_post()
            ts = post.tags

            for t in ts:
                if t.tagId not in tags:
                    tags.append(t.tagId)

        tagsFound = TagModel.get_all_tags(tags)
        content = []

        for p in posts:
            x = {}
            post = p.get_post()
            x['log'] = p.json()
            x['post'] = post.json()
            x['post']['body'] = ""

            ts = post.tags
            xtags = []

            for t in ts:
                for y in tagsFound:
                    if t.tagId == y.id:
                        xtags.append(y.json())
            
            x['tags'] = xtags

            content.append(x)


        return {"error":0,"content":content}