Example #1
0
    def delete(self, id):
        try:
            comment = models.Comment.select().where(
                models.Comment.id == id).get()

        except:
            abort(404, message="Comment doesn't exist")

        if g.user != comment.author:
            # unauthorized
            abort(401)

        try:
            content = '[removed by user]'
            query = models.Comment.update(content=content).where(
                models.Comment.id == id)
            query.execute()

            query_2 = models.Comment.get(models.Comment.id == id)

            comment_schema = models.CommentSchema(
                only=('content', 'post_id', 'author.name', 'author.id', 'id',
                      'created_at', 'last_modified'))

            comment = comment_schema.dump(query_2).data

            return jsonify({'comment': comment})
        except:
            abort(500, message="Oh, no! The Community is in turmoil!")

        return Response(status=204, mimetype='application/json')
Example #2
0
 def get(self, id):
     try:
         query = models.Comment.get(models.Comment.id == id)
         comment_schema = models.CommentSchema(
             only=('content', 'id', 'post_id', 'created_at',
                   'last_modified', 'author.id', 'author.name',
                   'parent_id'))
         output = comment_schema.dump(query).data
         return jsonify({'comment': output})
     except models.DoesNotExist:
         abort(404, message='Comment does not exists.')
Example #3
0
    def post(self):
        print('log 1')
        if (request.is_json):
            print('log 2')
            data = request.get_json(force=True)
            print('log 3')
            try:

                print('log 4')

                content = data['content']
                author = data['author']
                post_id = data['post_id']
                parent_id = data['parent_id']

                print('log 5')
                print('author:', author)

                user = models.User.select().where(
                    models.User.name == author).get()

            except:
                abort(400, message='Missing or invalid fields')

            print('user who made comment: ', user.name)
            try:
                query = models.Comment.select().where(
                    models.Comment.content == content,
                    models.Comment.post_id == post_id,
                    models.Comment.author == user.id)
            except:
                print('log 6.1')
                abort(409, message='Duplicate comment.')

            if query.exists():
                abort(409, message='Duplicate comment.')

            comment_id = models.Comment.insert(content=content,
                                               author=user.id,
                                               post_id=post_id,
                                               parent_id=parent_id).execute()

            query = models.Comment.get(models.Comment.id == comment_id)

            comment_schema = models.CommentSchema(
                only=('content', 'id', 'post_id', 'created_at',
                      'last_modified', 'author.id', 'author.name',
                      'parent_id'))

            output = comment_schema.dump(query).data

            return jsonify({'comment': output})
        else:
            abort(400, message='Request is not JSON')
Example #4
0
    def get(self):
        try:
            query = models.Comment.select().order_by(models.Comment.id)
            #query = models.Comment.select().join(models.User)
            comment_schema = models.CommentSchema(
                many=True,
                only=('content', 'id', 'post_id', 'created_at',
                      'last_modified', 'author.id', 'author.name',
                      'parent_id'))
            output = comment_schema.dump(query).data

            return jsonify({'comments': output})
        except:
            abort(500, message="Oh, no! The Community is in turmoil!")
Example #5
0
    def get(self, id):

        try:
            query = (models.Comment.select(
                models.Comment).where(models.Comment.post == id))

        except:
            abort(404, message="Record does not exist.")

        comment_schema = models.CommentSchema(
            many=True,
            only=('id', 'content', 'author.id', 'author.name', 'created_at',
                  'last_modified', 'parent_id'))
        output = comment_schema.dump(query).data
        return jsonify({'comments': output})
Example #6
0
def load_content():
    '''
    flexible post-based loading from sql.

    POST request must define:
    table:  what table to load results from (comment, link, etc.)
    id:  id of desired resource
    '''
    table = flask.request.form.get('table')
    id = flask.request.form.get("id")

    if table == "comment":
        results = db.sqla.session.query(models.Comment).get(id)
        print(type(results.parent_id))
        schema = models.CommentSchema()
        serialized = schema.dump(results)
        print("RESULTS ARE:    ", serialized)
        return flask.jsonify(serialized)
    else:
        return "bad query", 500
Example #7
0
    def get(self, name):

        try:
            user = models.User.get(models.User.name == name)
        except:
            abort(404, message="user not found")

        query = models.Comment.select().where(
            models.Comment.author == user.id).order_by(models.Comment.id)

        comment_schema = models.CommentSchema(
            many=True,
            only=('id', 'content', 'author.name', 'author.id', 'post_id',
                  'post.title', 'created_at', 'last_modified'))

        output = comment_schema.dump(query).data

        count = len(output)

        print('count', count)

        return jsonify({'comments': output, 'count': count})
Example #8
0
    def put(self, id):
        if (request.is_json):

            data = request.get_json(force=True)

            try:
                comment = models.Comment.select().where(
                    models.Comment.id == id).get()

            except:
                abort(404, message="Comment doesn't exist")

            if g.user != comment.author:
                abort(401)

            if ('content' in data):
                content = data['content'].strip()

                query = models.Comment.update(content=content).where(
                    models.Comment.id == id)
                query.execute()

                query_2 = models.Comment.get(models.Comment.id == id)

                comment_schema = models.CommentSchema(
                    only=('content', 'post_id', 'author.name', 'author.id',
                          'id', 'created_at', 'last_modified'))

                comment = comment_schema.dump(query_2).data

                return jsonify({'comment': comment})
            else:
                abort(400, message="Missing or invalid fields.")

        else:
            abort(400, message='Not JSON data')