Ejemplo n.º 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})
Ejemplo n.º 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})