Exemplo n.º 1
0
 def __init__(self, model):
     self.model = model
     self.app = None
     self.pk_field_name = self.model._meta.pk.name
     # Prepare field converters
     self.clean_fields = self.clean_fields.copy()  # name -> Parameter
     self.fk_fields = {}
     for f in self.model._meta.fields:
         if f.name in self.clean_fields:
             continue  # Overriden behavior
         vf = self.get_validator(f)
         if vf:
             if f.null:
                 vf = NoneParameter() | vf
             self.clean_fields[f.name] = vf
     # Find field_* and populate custom fields
     self.custom_fields = {}
     for fn in [n for n in dir(self) if n.startswith("field_")]:
         h = getattr(self, fn)
         if callable(h):
             self.custom_fields[fn[6:]] = h
     #
     if not self.query_fields:
         # By default - search in unique text fields
         self.query_fields = [
             "%s__%s" % (f.name, self.query_condition)
             for f in self.model._meta.fields
             if f.unique and isinstance(f, CharField)
         ]
     # Add searchable custom fields
     self.query_fields += [
         "%s__%s" % (f.name, self.query_condition)
         for f in self.get_custom_fields() if f.is_searchable
     ]
Exemplo n.º 2
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.db_table = self.model._meta.db_table
     self.pk_field_name = self.model._meta.pk.name
     self.pk = self.pk_field_name
     # Prepare field converters
     self.clean_fields = self.clean_fields.copy()  # name -> Parameter
     self.fk_fields = {}
     self.field_defaults = {}
     for f in self.model._meta.fields:
         if f.name in self.clean_fields:
             continue  # Overriden behavior
         vf = self.get_validator(f)
         if vf:
             if f.null:
                 vf = NoneParameter() | vf
             self.clean_fields[f.name] = vf
         if f.default is not None and not f.blank:
             self.field_defaults[f.name] = f.default
     # m2m fields
     self.m2m_fields = {}  # Name -> Model
     if self.custom_m2m_fields:
         self.m2m_fields.update(self.custom_m2m_fields)
     for f in self.model._meta.many_to_many:
         self.m2m_fields[f.name] = f.remote_field.model
     # Find field_* and populate custom fields
     self.custom_fields = {}
     for fn in [n for n in dir(self) if n.startswith("field_")]:
         h = getattr(self, fn)
         if callable(h):
             self.custom_fields[fn[6:]] = h
     #
     if not self.query_fields:
         # By default - search in unique text fields
         self.query_fields = [
             "%s__%s" % (f.name, self.query_condition)
             for f in self.model._meta.fields
             if f.unique and isinstance(f, CharField)
         ]
     # Add searchable custom fields
     self.query_fields += [
         "%s__%s" % (f.name, self.query_condition)
         for f in self.get_custom_fields()
         if f.is_searchable
     ]
Exemplo n.º 3
0
def test_none_parameter_error(raw, config):
    with pytest.raises(InterfaceTypeError):
        assert NoneParameter(**config).clean(raw)
Exemplo n.º 4
0
def test_none_parameter(raw, config):
    assert NoneParameter(**config).clean(raw) is None
Exemplo n.º 5
0
def test_none_parameter():
    assert NoneParameter().clean(None) is None
    with pytest.raises(InterfaceTypeError):
        NoneParameter().clean("None")