def model_form(model, base_class=form.BaseForm, only=None, exclude=None, field_args=None, converter=None): return orm.model_form(model, base_class=base_class, only=only, exclude=exclude, field_args=field_args, converter=converter)
def scaffold_form(self): form_class = model_form(self.model, base_class=form.BaseForm, only=self.form_columns, exclude=self.excluded_form_columns, field_args=self.form_args, converter=CustomModelConverter()) if self.inline_models: raise NotImplementedError("Inline forms not implemented") #form_class = contribute_inline(self.model, form_class, self.inline_models) return form_class
# Can't change a post if it is published def has_change_permission(self, request, obj): return not obj.is_published # Can't delete a post if it is published def has_delete_permission(self, request, obj): return not obj.is_published class TestDocument(db.Document): name = db.StringField() other = db.StringField() dictfield = db.DictField() is_new = db.BooleanField() TestBaseForm = model_form(TestDocument) class TestForm(TestBaseForm): name = TextField(validators=[length(min=3, max=8)]) class TestResource(Resource): form = TestForm document = TestDocument class TestFormResource(Resource): form = TestForm document = TestDocument uri_prefix = "/testform/" class TestFieldsResource(Resource): document = TestDocument
def has_change_permission(self, request, obj): return not obj.is_published # Can't delete a post if it is published def has_delete_permission(self, request, obj): return not obj.is_published class TestDocument(db.Document): name = db.StringField() other = db.StringField() dictfield = db.DictField() is_new = db.BooleanField() TestBaseForm = model_form(TestDocument) class TestForm(TestBaseForm): name = TextField(validators=[length(min=3, max=8)]) class TestResource(Resource): form = TestForm document = TestDocument class TestFormResource(Resource): form = TestForm document = TestDocument uri_prefix = "/testform/"
def contribute_inline(model, form_class, inline_models): # Contribute columns for p in inline_models: # Figure out settings # FIXME - not sure if works if isinstance(p, tuple): info = InlineFormAdmin(p[0], **p[1]) print "----- contribute_inline: got tuple:", info elif isinstance(p, InlineFormAdmin): info = p print "----- contribute_inline: got InlineFormAdmin:", info elif isinstance(p, TopLevelDocumentMetaclass): info = InlineFormAdmin(p) print "----- contribute_inline: got TopLevelDocumentMetaclass:", info else: raise Exception('Unknown inline model admin: %s' % repr(p)) # Find property from target model to current model reverse_field = None print "--- contribute_inline - model: ", model print "--- contribute_inline - info.model: ", info.model print "--- contribute_inline - iterating through info.model fields: ", info.model for name, field in model_fields(info.model).items(): if field.kwargs.has_key('model'): print "field with model: ", field.kwargs['model'] # field.to == model reverse_field = field break else: raise Exception('Cannot find reverse relation for model %s' % info.model) # Remove reverse property from the list ignore = [reverse_field.kwargs['model']._class_name] if info.excluded_form_columns: exclude = ignore + info.excluded_form_columns else: exclude = ignore # Create field converter = CustomModelConverter() child_form = model_form(info.model, base_class=form.BaseForm, only=info.form_columns, exclude=exclude, field_args=info.form_args, #allow_pk=True, converter=converter) print model.__dict__ prop_name = 'fa_%s' % model.__name__ setattr(form_class, prop_name, InlineModelFormList(child_form, info.model, reverse_field.kwargs['model']._class_name, label=info.model.__name__)) # setattr(field.to, # prop_name, # property(lambda self: self.id)) return form_class
class Phone(validators.Regexp): def __init__(self): super(Phone, self).__init__( r'1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?', message="That\'s not a valid phone number") class ForgotForm(Form): email = TextField(u"Email") phone = TelField(u"Phone Number", [validators.optional(), Phone()]) submit = SubmitField("") UserForm = model_form(User) class AccountForm(Form): """ Account Form """ username = TextField(u"Username", [validators.Required()]) email = EmailField(u"E-Mail", [ validators.Required(), validators.Email(message=u'That\'s not a valid email address.') ]) subscribe = BooleanField(u'', default=True) phone = TelField(u"Phone Number", [validators.optional(), Phone()]) password = PasswordField(u"Password") new_passwrd = PasswordField(u"New Password") address = TextField(u"Full Address")