How to use the jsonmodels.fields.EmbeddedField function in jsonmodels

To help you get started, we’ve selected a few jsonmodels examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jazzband / jsonmodels / tests / test_lazy_loading.py View on Github external
class Sixth(models.Base):

    name = fields.StringField()
    secondary = fields.EmbeddedField('...tests.test_lazy_loading.Secondary')


class Seventh(models.Base):

    name = fields.StringField()
    secondary = fields.EmbeddedField('....tests.test_lazy_loading.Secondary')


class Eighth(models.Base):

    name = fields.StringField()
    secondary = fields.EmbeddedField('.SomeWrongEntity')


class Secondary(models.Base):

    data = fields.IntField()


@pytest.mark.parametrize(['model'], [
    (Primary,),
    (Third,),
    (Fourth,),
    (Fifth,),
    (Sixth,),
])
def test_embedded_model(model):
    entity = model()
github jazzband / jsonmodels / tests / test_jsonmodels.py View on Github external
def test_embedded_inheritance():

    class Car(models.Base):
        pass

    class Viper(Car):
        pass

    class Lamborghini(Car):
        pass

    class ParkingPlace(models.Base):

        location = fields.StringField()
        car = fields.EmbeddedField([Viper, Lamborghini])

    place = ParkingPlace()

    place.car = Viper()
    place.car = Lamborghini()

    with pytest.raises(errors.ValidationError):
        place.car = Car()

    class ParkingPlace(models.Base):

        location = fields.StringField()
        car = fields.EmbeddedField(Car)

    place = ParkingPlace()
github jazzband / jsonmodels / tests / test_jsonmodels.py View on Github external
def test_embedded_model():

    class Secondary(models.Base):

        data = fields.IntField()

    class Primary(models.Base):

        name = fields.StringField()
        secondary = fields.EmbeddedField(Secondary)

    entity = Primary()
    assert entity.secondary is None
    entity.name = 'chuck'
    entity.secondary = Secondary()
    entity.secondary.data = 42

    with pytest.raises(errors.ValidationError):
        entity.secondary.data = '42'

    entity.secondary.data = 42

    with pytest.raises(errors.ValidationError):
        entity.secondary = 'something different'

    entity.secondary = None
github jazzband / jsonmodels / tests / test_data_initialization.py View on Github external
def test_deep_initialization():

    class Car(models.Base):

        brand = fields.StringField()

    class ParkingPlace(models.Base):

        location = fields.StringField()
        car = fields.EmbeddedField(Car)

    data = {
        'location': 'somewhere',
        'car': {
            'brand': 'awesome brand'
        }
    }

    parking1 = ParkingPlace(**data)
    parking2 = ParkingPlace()
    parking2.populate(**data)
    for parking in [parking1, parking2]:
        assert parking.location == 'somewhere'
        car = parking.car
        assert isinstance(car, Car)
        assert car.brand == 'awesome brand'
github jazzband / jsonmodels / tests / test_jsonmodels.py View on Github external
def test_required_embedded_field():

    class Secondary(models.Base):

        data = fields.IntField()

    class Primary(models.Base):

        name = fields.StringField()
        secondary = fields.EmbeddedField(Secondary, required=True)

    entity = Primary()
    with pytest.raises(errors.ValidationError):
        entity.validate()
    entity.secondary = Secondary()
    entity.validate()

    class Primary(models.Base):

        name = fields.StringField()
        secondary = fields.EmbeddedField(Secondary, required=False)

    entity = Primary()
    entity.validate()

    entity.secondary = None
github saghul / CallRoulette / app.py View on Github external
class Jsep(models.Base):
    type = StringChoiceField(choices=['offer', 'answer'], required=True)
    sdp = fields.StringField(required=True)


class Candidate(models.Base):
    candidate = fields.StringField(required=True)
    sdpMid = fields.StringField(required=True)
    sdpMLineIndex = fields.IntField(required=True)


class YoPayload(models.Base):
    yo = fields.StringField(required=True)
    jsep = fields.EmbeddedField(Jsep)
    candidate = fields.EmbeddedField(Candidate)


class Connection:
    def __init__(self, ws):
        self.ws = ws
        self._closed = False
        self._closed_fut = asyncio.Future(loop=ws._loop)

    @property
    def closed(self):
        return self._closed or self.ws.closing

    @asyncio.coroutine
    def read(self, timeout=None):
        try:
            msg = yield from asyncio.wait_for(self.ws.receive(), timeout)
github openstack / dragonflow / dragonflow / cli / df_model.py View on Github external
def _process_field(self, key, field):
        if isinstance(field, field_types.ListOfField):
            is_single = False
            is_embedded = not isinstance(field,
                                         field_types.ReferenceListField)
            field_model = field.field
        elif isinstance(field, fields.ListField):
            is_single = False
            is_embedded = False
            field_model = field.items_types[0]
            if isinstance(field, field_types.EnumListField):
                restrictions = list(field._valid_values)
        elif isinstance(field, fields.EmbeddedField):
            is_single = True
            is_embedded = True
            field_model = field.types[0]
        else:
            is_single = True
            is_embedded = False
            field_model = field
        field_type, restrictions = self._stringify_field_type(field_model)

        if field_type not in self._basic_types:
            if isinstance(field_model, field_types.ReferenceField):
                model = field_model._model
            else:
                model = field_model
            self._all_models.add(model)
            # As we iterate over the models by their dependencies, if we did
github openstack / dragonflow / dragonflow / db / model_framework.py View on Github external
def iterate_embedded_model_instances(self):
        for name, field in self.iterate_over_set_fields():
            if isinstance(field, fields.EmbeddedField):
                subobjs = (getattr(self, name),)
            elif isinstance(field, fields.ListField):
                subobjs = getattr(self, name)
            else:
                continue

            for subobj in subobjs:
                if isinstance(subobj, ModelBase):
                    yield subobj
github openstack / dragonflow / dragonflow / db / models / l2.py View on Github external
EVENT_LOCAL_UPDATED,
    EVENT_REMOTE_UPDATED,
}, indexes={
    'chassis_id': 'binding.chassis.id',
    'lswitch_id': 'lswitch.id',
    'ip,lswitch': ('ips', 'lswitch.id'),
    'switch,owner': ('lswitch.unique_key', 'device_owner')
})
class LogicalPort(mf.ModelBase, mixins.Name, mixins.Version, mixins.Topic,
                  mixins.UniqueKey, mixins.BasicEvents):
    table_name = "lport"
    ips = df_fields.ListOfField(df_fields.IpAddressField())
    subnets = df_fields.ReferenceListField(Subnet)
    macs = df_fields.ListOfField(df_fields.MacAddressField())
    enabled = fields.BoolField()
    binding = fields.EmbeddedField(PortBinding)
    lswitch = df_fields.ReferenceField(LogicalSwitch)
    security_groups = df_fields.ReferenceListField(secgroups.SecurityGroup)
    allowed_address_pairs = fields.ListField(AddressPair)
    port_security_enabled = fields.BoolField()
    device_owner = fields.StringField()
    device_id = fields.StringField()
    qos_policy = df_fields.ReferenceField(qos.QosPolicy)
    dhcp_params = fields.EmbeddedField(DhcpParams)
    binding_vnic_type = df_fields.EnumField(portbindings.VNIC_TYPES)

    @property
    def ip(self):
        try:
            return self.ips[0]
        except IndexError:
            return None
github allegroai / trains / trains / backend_api / session / response.py View on Github external
    @property
    def is_valid(self):
        return self._is_valid

    @classmethod
    def from_raw_data(cls, status_code, text, endpoint=None):
        return cls(is_valid=False, result_code=status_code, result_subcode=0, result_msg=text,
                   endpoint=_ResponseEndpoint(name=(endpoint or 'unknown')))

    def __init__(self, is_valid=True, **kwargs):
        super(ResponseMeta, self).__init__(**kwargs)
        self._is_valid = is_valid

    id = jsonmodels.fields.StringField(required=True)
    trx = jsonmodels.fields.StringField(required=True)
    endpoint = jsonmodels.fields.EmbeddedField([_ResponseEndpoint], required=True)
    result_code = jsonmodels.fields.IntField(required=True)
    result_subcode = jsonmodels.fields.IntField()
    result_msg = jsonmodels.fields.StringField(required=True)
    error_stack = jsonmodels.fields.StringField()

    def __str__(self):
        if self.result_code == requests.codes.ok:
            return "<%d: %s/v%s>" % (self.result_code, self.endpoint.name, self.endpoint.actual_version)
        elif self._is_valid:
            return "<%d/%d: %s/v%s (%s)>" % (self.result_code, self.result_subcode, self.endpoint.name,
                                             self.endpoint.actual_version, self.result_msg)
        return "<%d/%d: %s (%s)>" % (self.result_code, self.result_subcode, self.endpoint.name, self.result_msg)