def model_fields(model, only=None, exclude=None, hidden=None, field_args=None, converter=None): """ Generate a dictionary of fields for a given Django model. See `model_form` docstring for description of parameters. """ from schematics.models import Model if not isinstance(model, Model): raise TypeError('model must be a schematics.Model schema') converter = converter or ModelConverter() field_args = field_args or {} gottago = wholelist() field_dict = { } if only: gottago = whitelist(*only) elif exclude: gottago = blacklist(*exclude) for field_name, field in model._fields.items(): if gottago(field_name, None): continue ishidden = False if hidden: if field_name in hidden: ishidden=True form_field = converter.convert(model, field, field_name, field_args.get(field_name), hidden=ishidden) if form_field is not None: field_dict[field_name] = form_field return field_dict
def test_subclassing_overides_roles(self): class Parent(Model): id = StringType() gender = StringType() name = StringType() class Options: roles = { 'public': blacklist("id", "gender"), 'gender': blacklist("gender") } class GrandParent(Parent): age = IntType() family_secret = StringType() class Options: roles = { 'grandchildren': whitelist("age"), 'public': blacklist("id", "family_secret") } gramps = GrandParent({ "id": "1", "name": "Edward", "gender": "Male", "age": 87, "family_secret": "Secretly Canadian" }) options = gramps._options self.assertEqual( options.roles, { "grandchildren": whitelist("age"), "public": blacklist("id", "family_secret"), "gender": blacklist("gender") })
def test_subclassing_overides_roles(self): class Parent(Model): id = StringType() gender = StringType() name = StringType() class Options: roles = { 'public': blacklist("id", "gender"), 'gender': blacklist("gender") } class GrandParent(Parent): age = IntType() family_secret = StringType() class Options: roles = { 'grandchildren': whitelist("age"), 'public': blacklist("id", "family_secret") } gramps = GrandParent({ "id": "1", "name": "Edward", "gender": "Male", "age": 87, "family_secret": "Secretly Canadian" }) options = gramps._options self.assertEqual(options.roles, { "grandchildren": whitelist("age"), "public": blacklist("id", "family_secret"), "gender": blacklist("gender") })
def test_subclassing_preservers_roles(self): class Parent(Model): id = StringType() name = StringType() class Options: roles = {'public': blacklist("id")} class GrandParent(Parent): age = IntType() gramps = GrandParent({"id": "1", "name": "Edward", "age": 87}) options = gramps._options self.assertEqual(options.roles, {"public": blacklist("id")})
def model_fields(model, only=None, exclude=None, hidden=None, field_args=None, converter=None): """ Generate a dictionary of fields for a given Django model. See `model_form` docstring for description of parameters. """ from schematics.models import Model if not isinstance(model, Model): raise TypeError('model must be a schematics.Model schema') converter = converter or ModelConverter() field_args = field_args or {} gottago = wholelist() field_dict = {} if only: gottago = whitelist(*only) elif exclude: gottago = blacklist(*exclude) for field_name, field in model._fields.items(): if gottago(field_name, None): continue ishidden = False if hidden: if field_name in hidden: ishidden = True form_field = converter.convert(model, field, field_name, field_args.get(field_name), hidden=ishidden) if form_field is not None: field_dict[field_name] = form_field from pprint import pprint #pprint(field_dict) return field_dict
def test_subclassing_preservers_roles(self): class Parent(Model): id = StringType() name = StringType() class Options: roles = {'public': blacklist("id")} class GrandParent(Parent): age = IntType() gramps = GrandParent({ "id": "1", "name": "Edward", "age": 87 }) options = gramps._options self.assertEqual(options.roles, { "public": blacklist("id") })
class Options: roles = { 'owner': blacklist(), 'public': whitelist('title', 'year'), }
class Options: serialize_when_none = False roles = { 'safe': blacklist('secret'), }
class Options: roles = {"public": blacklist("result")}
class Options: roles = {"public": blacklist("secret")}
class Options: roles = { 'grandchildren': whitelist("age"), 'public': blacklist("id", "family_secret") }
class Options: roles = { 'public': blacklist("id", "gender"), 'gender': blacklist("gender") }
class Options: roles = {'public': blacklist("id")}
class Options: roles = { 'safe': blacklist('password'), }
class Options: roles = {'public': blacklist('private_key')}
class Options: roles = { 'meow': blacklist('owner'), 'woof': whitelist('id'), }
class Options: roles = {"public": blacklist("title")}
class Options: roles = { 'owner': blacklist('is_active'), 'public': whitelist('username', 'name'), }
class Options: roles = {"public": blacklist("id")}
class Options: roles = { 'owner': blacklist('personal_thoughts'), 'public': whitelist('author', 'content', 'comments'), }
class Options: roles = { 'create': all_fields - ['id'], 'public': all_fields - ['password'], 'nospam': blacklist('password') + blacklist('email'), }
class Options: serialize_when_none = False roles = { 'safe': blacklist('activation_code'), }