def add_extra_model_fields(sender, **kwargs): """ Injects custom fields onto the given sender model as defined by the ``EXTRA_MODEL_FIELDS`` setting. """ model_path = "%s.%s" % (sender.__module__, sender.__name__) extra_fields = fields.get(model_path, {}) if (extra_fields): for field_name, field in extra_fields.items(): field.contribute_to_class(sender, field_name) del fields[model_path] # re-send the class_prepared signal class_prepared.send(sender=sender)
def __init__(cls, name, bases, attrs): super(IndexableFedoraObjectMetaclass, cls).__init__(name, list(bases), attrs) indexed_fields = tuple( IndexableFedoraObjectMetaclass.all_indexed_fields(cls)) processed_rdf_names = set() for p in indexed_fields: if p.rdf_name in processed_rdf_names: raise AttributeError( "Property with rdf name %s already implemented" % p.rdf_name) processed_rdf_names.add(p.rdf_name) p.instrument(cls, p.name) # store django _meta cls._meta = DjangoMetadataBridge(cls, indexed_fields) for k, v in IndexableFedoraObjectMetaclass.fill_from_meta(cls).items(): if not hasattr(cls._meta, k): # print("Setting on %s: %s -> %s" % (cls, k, v)) setattr(cls._meta, k, v) else: # print("Ignoring on %s: %s" % (cls, k)) pass if not hasattr(cls._meta, 'rdf_types'): setattr(cls._meta, 'rdf_types', ()) application = cls.__module__ + "." + cls.__class__.__name__ application = application.split('.')[:-1] if application and application[-1] == 'models': application = application[:-1] setattr(cls._meta, 'application', '.'.join(application)) if cls._meta.rdf_types and not cls.__name__.endswith('_bound'): FedoraTypeManager.register_model(cls, on_rdf_type=cls._meta.rdf_types) class_prepared.send(sender=cls)
def _prepare(cls): """ Create some methods once self._meta has been populated. Importantly, this is where the Manager class gets added. """ opts = cls._meta opts._prepare(cls) # Give the class a docstring -- its definition. if cls.__doc__ is None: cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join(f.name for f in opts.fields)) if any(f.name == 'objects' for f in opts.fields): raise ValueError( "Model %s must specify a custom Manager, because it has a " "field named 'objects'." % cls.__name__ ) manager = opts.manager_class() cls.add_to_class('objects', manager) class_prepared.send(sender=cls)
def _prepare(cls): """Create some methods once self._meta has been populated.""" opts = cls._meta opts._prepare(cls) if opts.order_with_respect_to: cls.get_next_in_order = partialmethod( cls._get_next_or_previous_in_order, is_next=True) cls.get_previous_in_order = partialmethod( cls._get_next_or_previous_in_order, is_next=False) # Defer creating accessors on the foreign class until it has been # created and registered. If remote_field is None, we're ordering # with respect to a GenericForeignKey and don't know what the # foreign class is - we'll add those accessors later in # contribute_to_class(). if opts.order_with_respect_to.remote_field: wrt = opts.order_with_respect_to remote = wrt.remote_field.model lazy_related_operation(make_foreign_order_accessors, cls, remote) # Give the class a docstring -- its definition. if cls.__doc__ is None: cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join( f.name for f in opts.fields)) get_absolute_url_override = settings.ABSOLUTE_URL_OVERRIDES.get( opts.label_lower) if get_absolute_url_override: setattr(cls, 'get_absolute_url', get_absolute_url_override) # Set the name of _meta.indexes. This can't be done in # Options.contribute_to_class() because fields haven't been added to # the model at that point. for index in cls._meta.indexes: if not index.name: index.set_name_with_model(cls) class_prepared.send(sender=cls)
def __init__(cls, name, bases, attrs): super(IndexableFedoraObjectMetaclass, cls).__init__(name, list(bases), attrs) def create_property(prop): def _convert_to_rdf(data): if data is None: return [] return prop.convert_to_rdf(data) def getter(self): ret = self.metadata[prop.rdf_name] if isinstance(prop, IndexedLanguageField): return prop.convert_from_rdf(ret) if not prop.multi_valued: # simple type -> return the first item only if len(ret): return prop.convert_from_rdf(ret[0]) else: return None return StringLikeList([prop.convert_from_rdf(x) for x in self.metadata[prop.rdf_name]]) def setter(self, value): collected_streams = __get_streams(value) if len(collected_streams) > 0: if not hasattr(self, '__streams'): setattr(self, '__streams', {}) streams = getattr(self, '__streams') streams[prop] = collected_streams else: if isinstance(value, list) or isinstance(value, tuple): value = [_convert_to_rdf(x) for x in value] else: value = _convert_to_rdf(value) self.metadata[prop.rdf_name] = value def __get_streams(value): streams = [] if isinstance(value, tuple) or isinstance(value, list): for x in value: rr = __get_streams(x) streams.extend(rr) elif isinstance(value, UploadedFile) or isinstance(value, TypedStream): streams.append(value) return streams return property(getter, setter) indexed_fields = tuple(IndexableFedoraObjectMetaclass.all_indexed_fields(cls)) processed_rdf_names = set() for p in indexed_fields: if p.rdf_name in processed_rdf_names: raise AttributeError("Property with rdf name %s already implemented" % p.rdf_name) processed_rdf_names.add(p.rdf_name) setattr(cls, p.name, create_property(p)) # store django _meta cls._meta = DjangoMetadataBridge(cls, indexed_fields) for k, v in IndexableFedoraObjectMetaclass.fill_from_meta(cls).items(): if not hasattr(cls._meta, k): # print("Setting on %s: %s -> %s" % (cls, k, v)) setattr(cls._meta, k, v) else: # print("Ignoring on %s: %s" % (cls, k)) pass if not hasattr(cls._meta, 'rdf_types'): setattr(cls._meta, 'rdf_types', ()) if cls._meta.rdf_types and not cls.__name__.endswith('_bound'): FedoraTypeManager.register_model(cls, on_rdf_type=cls._meta.rdf_types) class_prepared.send(sender=cls)
def ready(self): super(MultiDomainConfig, self).ready() class_prepared.send(self.get_model( "Domain")) # send signal, for register actual domain model