Ejemplo n.º 1
0
def save_quote(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('quote_for').integer().required()
            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
            comment = None
            if not id:
                quote_for = Comment.get(v.valid_data.quote_for)
                if quote_for:
                    comment = Comment()
                    comment.author_id = user.id
                    comment.quote_for = quote_for
                    comment.entity = quote_for.entity
                    comment.entity_id = quote_for.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_quote(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('quote_for').integer().required()
            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
            comment = None
            if not id:
                quote_for = Comment.get(v.valid_data.quote_for)
                if quote_for:
                    comment = Comment()
                    comment.author_id = user.id
                    comment.quote_for = quote_for
                    comment.entity = quote_for.entity
                    comment.entity_id = quote_for.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})