def export_loop(self,
                    model_instance,
                    field_converter,
                    role=None,
                    print_none=False):
        """
        Calls the main `export_loop` implementation because they are both
        supposed to operate on models.
        """
        if isinstance(model_instance, self.model_class):
            model_class = model_instance.__class__
        else:
            model_class = self.model_class

        if role in self.view_claim_statuses and getattr(
                model_instance, 'type') == 'claim':
            role = 'view_claim'

        shaped = export_loop(model_class,
                             model_instance,
                             field_converter,
                             role=role,
                             print_none=print_none)

        if shaped and len(shaped) == 0 and self.allow_none():
            return shaped
        elif shaped:
            return shaped
        elif print_none:
            return shaped
    def export_loop(self, model_instance, field_converter,
                    role=None, print_none=False):
        """
        Calls the main `export_loop` implementation because they are both
        supposed to operate on models.
        """
        if isinstance(model_instance, self.model_class):
            model_class = model_instance.__class__
        else:
            model_class = self.model_class

        tender = model_instance.__parent__
        if role not in [None, 'plain'] and getattr(model_instance, 'status') == 'unsuccessful':
            role = 'bid.unsuccessful'

        shaped = export_loop(model_class, model_instance,
                             field_converter,
                             role=role, print_none=print_none)

        if shaped and len(shaped) == 0 and self.allow_none():
            return shaped
        elif shaped:
            return shaped
        elif print_none:
            return shaped
示例#3
0
 def to_patch(self, role=None):
     """Return data as it would be validated. No filtering of output unless
     role is defined.
     """
     field_converter = lambda field, value: field.to_primitive(value)
     data = export_loop(self.__class__, self, field_converter, role=role, raise_error_on_role=True, print_none=True)
     return data
示例#4
0
    def export_loop(self,
                    model_instance,
                    field_converter,
                    role=None,
                    print_none=False):
        """
        Calls the main `export_loop` implementation because they are both
        supposed to operate on models.
        """
        if isinstance(model_instance, self.model_class):
            model_class = model_instance.__class__
        else:
            model_class = self.model_class

        tender = model_instance.__parent__
        tender_date = get_first_revision_date(tender, default=get_now())
        status = getattr(model_instance, "status")
        if tender_date > BID_UNSUCCESSFUL_FROM and role not in [
                None, "plain"
        ] and status == "unsuccessful":
            role = "bid.unsuccessful"

        shaped = export_loop(model_class,
                             model_instance,
                             field_converter,
                             role=role,
                             print_none=print_none)

        if shaped and len(shaped) == 0 and self.allow_none():
            return shaped
        elif shaped:
            return shaped
        elif print_none:
            return shaped
    def export_loop(self, model_instance, field_converter,
                    role=None, print_none=False):
        """
        Calls the main `export_loop` implementation because they are both
        supposed to operate on models.
        """
        if isinstance(model_instance, self.model_class):
            model_class = model_instance.__class__
        else:
            model_class = self.model_class

        tender = model_instance.__parent__
        if (tender.revisions[0].date if tender.revisions else get_now()) > BID_UNSUCCESSFUL_FROM and role not in [None, 'plain'] and getattr(model_instance, 'status') == 'unsuccessful':
            role = 'bid.unsuccessful'

        shaped = export_loop(model_class, model_instance,
                             field_converter,
                             role=role, print_none=print_none)

        if shaped and len(shaped) == 0 and self.allow_none():
            return shaped
        elif shaped:
            return shaped
        elif print_none:
            return shaped
示例#6
0
def test_serialize_none_fields_if_field_says_so():
    class TestModel(Model):
        inst_id = StringType(required=True, serialize_when_none=True)

    q = TestModel({'inst_id': 1})

    d = export_loop(TestModel, q, lambda field, value, context: None)
    assert d == {'inst_id': None}
 def to_patch(self, role=None):
     """
     Return data as it would be validated. No filtering of output unless
     role is defined.
     """
     field_converter = lambda field, value: field.to_primitive(value)
     data = export_loop(self.__class__, self, field_converter, role=role, raise_error_on_role=True, print_none=True)
     return data
示例#8
0
def test_serialize_print_none_always_gets_you_something():
    class TestModel(Model):
        pass

    q = TestModel()

    d = export_loop(TestModel, q, lambda field, value, context: None, export_level=DEFAULT)
    assert d == {}
def test_serialize_print_none_always_gets_you_something():
    class TestModel(Model):
        pass

    q = TestModel()

    d = export_loop(TestModel, q, lambda field, value: None, print_none=True)
    assert d == {}
示例#10
0
def test_serialize_none_fields_if_export_loop_says_so():
    class TestModel(Model):
        inst_id = StringType(required=True, serialize_when_none=False)

    q = TestModel({'inst_id': 1})

    d = export_loop(TestModel, q, lambda field, value: None, print_none=True)
    assert d == {'inst_id': None}
示例#11
0
def test_serialize_print_none_always_gets_you_something():
    class TestModel(Model):
        pass

    q = TestModel()

    d = export_loop(TestModel, q, lambda field, value, context: None, export_level=DEFAULT)
    assert d == {}
示例#12
0
 def to_patch(self, role=None, *arg, **kwargs):
     """
     Return data as it would be validated. No filtering of output unless
     role is defined.
     """
     field_converter = lambda field, value, context: field.to_primitive(value, context)
     data = export_loop(self.__class__, self, field_converter=field_converter, role=role, raise_error_on_role=False, *arg, **kwargs)
     return data
示例#13
0
    def export_loop(self, model_instance, field_converter, role=None, print_none=False):
        if not isinstance(model_instance, self.model_class) or getattr(field_converter, "to_mongo", False):
            return field_converter(self, model_instance)
        else:
            model_class = model_instance.__class__
            shaped = export_loop(model_class, model_instance, field_converter, role=role, print_none=print_none)

            if shaped and len(shaped) == 0 and self.allow_none():
                return shaped
            elif shaped:
                return shaped
            elif print_none:
                return shaped
示例#14
0
文件: types.py 项目: st4lk/TurboKit
    def export_loop(self,
                    model_instance,
                    field_converter,
                    role=None,
                    print_none=False):
        if not isinstance(model_instance, self.model_class) or\
                getattr(field_converter, 'to_mongo', False):
            return field_converter(self, model_instance)
        else:
            model_class = model_instance.__class__
            shaped = export_loop(model_class,
                                 model_instance,
                                 field_converter,
                                 role=role,
                                 print_none=print_none)

            if shaped and len(shaped) == 0 and self.allow_none():
                return shaped
            elif shaped:
                return shaped
            elif print_none:
                return shaped