Exemple #1
0
def save_comment(id=None):
    user = auth.service.get_user()
    data = dict(request.form)
    data['upload'] = request.files.getlist('upload')

    v = Validator(data)
    v.fields('upload').image()
    v.fields('file.id').integer(nullable=True)
    if v.is_valid():
        if not id:
            v.field('entity_name').required()
            v.field('entity_id').integer(nullable=True).required()
            if not v.valid_data.list('url') and not v.valid_data.list(
                    'upload'):
                v.field('comment').required(
                    message="Напишите хоть что-нибудь...")
        if v.is_valid() and user.is_authorized():
            data = v.valid_data
            if not id:
                comment = Comment()
                comment.author_id = user.id
                comment.entity = data.entity_name
                comment.entity_id = data.entity_id
            else:
                comment = Comment.get(id)
                if comment:
                    comment.modify_datetime = datetime.datetime.now()
                    comment.status = Comment.Status.MODIFIED

            if comment:
                return save(comment, data)

        v.add_error('comment', 'Что-то пошло не так... Попробуйте позже.')
    return jsonify({'status': 'fail', 'errors': v.errors})
Exemple #2
0
def save_comment(id=None):
    user = auth.service.get_user()
    data = dict(request.form)
    data['upload'] = request.files.getlist('upload')

    v = Validator(data)
    v.fields('upload').image()
    v.fields('file.id').integer(nullable=True)
    if v.is_valid():
        if not id:
            v.field('entity_name').required()
            v.field('entity_id').integer(nullable=True).required()
            if not v.valid_data.list('url') and not v.valid_data.list('upload'):
                v.field('comment').required(message="Напишите хоть что-нибудь...")
        if v.is_valid() and user.is_authorized():
            data = v.valid_data
            if not id:
                comment = Comment()
                comment.author_id = user.id
                comment.entity = data.entity_name
                comment.entity_id = data.entity_id
            else:
                comment = Comment.get(id)
                if comment:
                    comment.modify_datetime = datetime.datetime.now()
                    comment.status = Comment.Status.MODIFIED

            if comment:
                return save(comment, data)

        v.add_error('comment', 'Что-то пошло не так... Попробуйте позже.')
    return jsonify({'status': 'fail',
                    'errors': v.errors})
Exemple #3
0
def delete(id):
    user = auth.service.get_user()
    if user.is_authorized():
        comment = Comment.get(id)
        if comment and (user.is_admin or comment.author == user
                        or 'manage_comments' in user.get_permissions()):
            comment_json = None

            def delete_parent(comment):
                should_delete = True

                if comment is not None and comment.status == Comment.Status.DELETED:
                    for quote in comment.quotes:
                        if quote.status != Comment.Status.DELETED:
                            should_delete = False
                    if should_delete:
                        db.session.delete(comment)
                        db.session.flush()
                        delete_parent(comment.quote_for)

            if comment.quotes:
                comment.status = Comment.Status.DELETED
                comment_json = get_comment_json(comment)
            else:
                db.session.delete(comment)
                db.session.flush()
                delete_parent(comment.quote_for)

            db.session.flush()
            entity = comment.get_entity()

            if entity:
                entity.after_delete_comment(comment)

            db.session.commit()
            return jsonify({'status': 'ok', 'comment': comment_json})

    return jsonify({'status': 'fail'})
Exemple #4
0
def delete(id):
    user = auth.service.get_user()
    if user.is_authorized():
        comment = Comment.get(id)
        if comment and (user.is_admin or comment.author == user or 'manage_comments' in user.get_permissions()):
            comment_json = None

            def delete_parent(comment):
                should_delete = True

                if comment is not None and comment.status == Comment.Status.DELETED:
                    for quote in comment.quotes:
                        if quote.status != Comment.Status.DELETED:
                            should_delete = False
                    if should_delete:
                        db.session.delete(comment)
                        db.session.flush()
                        delete_parent(comment.quote_for)

            if comment.quotes:
                comment.status = Comment.Status.DELETED
                comment_json = get_comment_json(comment)
            else:
                db.session.delete(comment)
                db.session.flush()
                delete_parent(comment.quote_for)

            db.session.flush()
            entity = comment.get_entity()

            if entity:
                entity.after_delete_comment(comment)

            db.session.commit()
            return jsonify({'status': 'ok',
                            'comment': comment_json})

    return jsonify({'status': 'fail'})