示例#1
0
 def __init__(self, *args, pkey=False, type_=None, **kwargs):
     if pkey:
         if 'readonly' in kwargs and not kwargs['readonly']:
             raise exceptions.ProgrammingError("attempt to create non-readonly primary key field")
         if type_ is None:
             raise exceptions.ProgrammingError("PKey field requires type")
         kwargs['readonly'] = True
     super().__init__(*args, **kwargs)
     self._pkey = pkey
     self._type = type_
示例#2
0
    def _extract_ids(self, values):
        if not self.multi and len(values) > 1:
            raise exceptions.ProgrammingError('More than one instance on a Single related field')

        ids = []
        for val in values:
            type_ = type(val)
            if val is None:
                raise exceptions.ProgrammingError("None is not accepted for {}".format(type(self)))
            elif type_ in (int, float, str):
                ids.append(val)
            elif issubclass(type_, world().get_instance_class(self.name)):
                ids.append(val.id())
            elif issubclass(type_, dict):
                inst = world().new_instance(self.name)
                inst.update(val)
                ids.append(inst.id())
            else:
                raise exceptions.ProgrammingError('could not create {}'.format(type(self)))
        return sorted(ids)
示例#3
0
    def __init__(self, *args, stores, multi, cascade=False, **kwargs):
        if multi:
            self._val_cls = MultiRelValue
            if 'default' in kwargs and type(kwargs['default']) is not list:
                raise exceptions.ProgrammingError("Multi field default must be a list")
            elif 'default' not in kwargs:
                kwargs['default'] = []
        else:
            self._val_cls = SingleRelValue
            if 'default' in kwargs and type(kwargs['default']) is list:
                raise exceptions.ProgrammingError("Multi field default cant be a list")

        super().__init__(*args, **kwargs)
        self.stores = stores
        self.multi = multi
        self.cascade = cascade

        if cascade and multi:
            #   multi + cascade is not implemented
            #   (i'm not sure how it should work)
            raise NotImplementedError("Cascade=True is not supported for Multi fields")

        #   this might be set in DataModel.connect(), but might also be left None
        self.other = None
示例#4
0
    def load(self, instance, value):
        if value is not None:
            #   just to be sure
            raise exceptions.ProgrammingError("Calc field 'value' should always be None")

        instance.set_value(self, self.val(lambda: self._getter(instance)))
示例#5
0
 def __init__(self, *args, getter=None, setter=None, **kwargs):
     if kwargs.get('hidden'):
         raise exceptions.ProgrammingError("Calc fields with hidden=True are not allowed")
     super().__init__(*args, **kwargs)
     self._getter = getter if getter else self._default_getter
     self._setter = setter if setter else self._default_setter