Пример #1
0
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
Пример #2
0
    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")
            })
Пример #3
0
    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")
        })
Пример #4
0
    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")})
Пример #5
0
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
Пример #6
0
    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")
        })
Пример #7
0
 class Options:
     roles = {
         'owner': blacklist(),
         'public': whitelist('title', 'year'),
     }
Пример #8
0
 class Options:
     serialize_when_none = False
     roles = {
         'safe': blacklist('secret'),
     }
Пример #9
0
 class Options:
     roles = {"public": blacklist("result")}
Пример #10
0
 class Options:
     roles = {"public": blacklist("secret")}
Пример #11
0
 class Options:
     roles = {
         'grandchildren': whitelist("age"),
         'public': blacklist("id", "family_secret")
     }
Пример #12
0
 class Options:
     roles = {
         'public': blacklist("id", "gender"),
         'gender': blacklist("gender")
     }
Пример #13
0
 class Options:
     roles = {'public': blacklist("id")}
Пример #14
0
 class Options:
     roles = {
         'safe': blacklist('password'),
     }
Пример #15
0
 class Options:
     roles = {'public': blacklist('private_key')}
Пример #16
0
 class Options:
     roles = {
         'meow': blacklist('owner'),
         'woof': whitelist('id'),
     }
Пример #17
0
 class Options:
     roles = {"public": blacklist("title")}
Пример #18
0
 class Options:
     roles = {
         'owner': blacklist('is_active'),
         'public': whitelist('username', 'name'),
     }
Пример #19
0
 class Options:
     roles = {"public": blacklist("id")}
Пример #20
0
 class Options:
     roles = {
         'owner': blacklist('personal_thoughts'),
         'public': whitelist('author', 'content', 'comments'),
     }
Пример #21
0
 class Options:
     roles = {
         'create': all_fields - ['id'],
         'public': all_fields - ['password'],
         'nospam': blacklist('password') + blacklist('email'),
     }
Пример #22
0
 class Options:
     serialize_when_none = False
     roles = {
         'safe': blacklist('activation_code'),
     }