Exemple #1
0
def test_rule_container():
    app, db, admin = setup()

    Model1, _ = create_models(db)
    db.create_all()

    view = CustomModelView(Model1,
                           db.session,
                           create_template='macro.html',
                           form_create_rules=(rules.Container(
                               'wrap',
                               rules.Macro('test_lib.another_test')), ))
    admin.add_view(view)

    client = app.test_client()

    rv = client.get('/admin/model1/new/')
    eq_(rv.status_code, 200)

    data = rv.data.decode('utf-8')
    pos1 = data.find('<wrapper>')
    pos2 = data.find('another_test')
    pos3 = data.find('</wrapper>')
    ok_(pos1 != -1)
    ok_(pos2 != -1)
    ok_(pos3 != -1)
    ok_(pos1 < pos2 < pos3)
Exemple #2
0
class RuleView(sqla.ModelView):
    form_create_rules = [
        # Header and four fields. Email field will go above phone field.
        rules.FieldSet(('first_name', 'last_name', 'email', 'phone'),
                       'Personal'),
        # Separate header and few fields
        rules.Header('Address'),
        rules.Field('address'),
        # String is resolved to form field, so there's no need to explicitly use `rules.Field`
        'city',
        'zip',
        # Show macro from Flask-Admin lib.html (it is included with 'lib' prefix)
        rules.Container('rule_demo.wrap', rules.Field('notes'))
    ]

    # Use same rule set for edit page
    form_edit_rules = form_create_rules

    create_template = 'rule_create.html'
    edit_template = 'rule_edit.html'
Exemple #3
0
class UserView(sqla.ModelView):
    """
    This class demonstrates the use of 'rules' for controlling the rendering of forms.
    """
    form_create_rules = [
        # Header and four fields. Email field will go above phone field.
        rules.FieldSet(('first_name', 'last_name', 'email', 'phone'),
                       'Personal'),
        # Separate header and few fields
        rules.Header('Location'),
        rules.Field('city'),
        # String is resolved to form field, so there's no need to explicitly use `rules.Field`
        'country',
        # Show macro from Flask-Admin lib.html (it is included with 'lib' prefix)
        rules.Container('rule_demo.wrap', rules.Field('notes'))
    ]

    # Use same rule set for edit page
    form_edit_rules = form_create_rules

    create_template = 'rule_create.html'
    edit_template = 'rule_edit.html'
Exemple #4
0
class FileView(MyModelView):
    column_list = ('title', 'file_path', 'file_bytes')
    column_searchable_list = ('title', 'file_path')
    column_default_sort = 'file_path'
    column_labels = {'file_bytes': 'Size'}
    column_formatters = {
        'file_bytes':
        lambda v, c, m, n: '-' if m.file_bytes is None else Markup(
            '<nobr>%s</nobr>' % humanize.naturalsize(m.file_bytes)),
    }

    class SizeRule(rules.BaseRule):
        def __call__(self, form, form_opts=None, field_args={}):
            if form._obj.file_bytes:
                return humanize.naturalsize(form._obj.file_bytes)
            return '-'

    class UrlRule(rules.BaseRule):
        def __call__(self, form, form_opts=None, field_args={}):
            url = url_for('docs', path=form.file_path.data)
            return Markup('<a href="%s" target="_blank">%s</a>' % (url, url))

    form_columns = (
        'title',
        'description',
        'file_path',
        'file_mime',
        'file_bytes',
    )
    form_widget_args = {
        'file_mime': {
            'readonly': True
        },
        'file_path': {
            'readonly': True
        },
        'file_bytes': {
            'readonly': True
        },
    }

    form_edit_rules = [
        'title',
        'description',
        'file_path',
        'file_mime',
        rules.Container('rules.staticfield', SizeRule(), label='Size'),
        rules.Container('rules.staticfield', UrlRule(), label='URL'),
    ]

    form_create_rules = ['title', 'description', 'upload']

    def get_create_form(self):
        # allow user to upload a file when creating form
        form = super(FileView, self).get_create_form()
        form.upload = fields.FileField('Upload a file', [data_required()])
        return form

    def on_model_change(self, form, model, is_create):
        if is_create:
            file_data = request.files.get(form.upload.name)
            model.from_upload(file_data)