Exemplo n.º 1
0
    # We use __dict__ instead of hasattr to not check its presence within the
    # parent, and thus update the parent dict instead of creating a local dict.
    if not entity.__dict__.get('options_defaults'):
        entity.options_defaults = {}
    entity.options_defaults.update(kwargs)


def using_options_handler(entity, *args, **kwargs):
    for kwarg in kwargs:
        if kwarg in valid_options:
            setattr(entity._descriptor, kwarg, kwargs[kwarg])
        else:
            raise Exception("'%s' is not a valid option for Elixir entities." %
                            kwarg)


def using_table_options_handler(entity, *args, **kwargs):
    entity._descriptor.table_args.extend(list(args))
    entity._descriptor.table_options.update(kwargs)


def using_mapper_options_handler(entity, *args, **kwargs):
    entity._descriptor.mapper_options.update(kwargs)


using_options_defaults = ClassMutator(using_options_defaults_handler)
using_options = ClassMutator(using_options_handler)
using_table_options = ClassMutator(using_table_options_handler)
using_mapper_options = ClassMutator(using_mapper_options_handler)
Exemplo n.º 2
0
        if self.deferred:
            group = None
            if isinstance(self.deferred, six.string_types):
                group = self.deferred
            self.property = deferred(self.column, group=group)
        elif self.name != self.colname:
            # if the property name is different from the column name, we need
            # to add an explicit property (otherwise nothing is needed as it's
            # done automatically by SA)
            self.property = self.column

        if self.property is not None:
            self.add_mapper_property(self.name, self.property)

        if self.synonym:
            self.add_mapper_property(self.synonym, synonym(self.name))


def has_field_handler(entity, name, *args, **kwargs):
    if 'through' in kwargs:
        setattr(
            entity, name,
            association_proxy(kwargs.pop('through'),
                              kwargs.pop('attribute', name), **kwargs))
        return
    field = Field(*args, **kwargs)
    field.attach(entity, name)


has_field = ClassMutator(has_field_handler)