def _register_report(self, report, namespace): """ Actual registry function, recursive :param report: :param namespace: :return: """ namespace_existing = namespace in self._registry full_name = '%s_%s' % (namespace, report.get_report_slug()) if namespace_existing: if report in self._registry[namespace]: raise AlreadyRegistered( 'This report class is already registered %s' % report) if full_name in self._slugs_registry: raise AlreadyRegistered( 'report slug `%s` is already registered for `%s`' % (report.get_report_slug(), namespace)) self._registry[namespace].append(report) else: self._registry[namespace] = [report] if report.base_model not in self._base_models: self._base_models.append(report.base_model) self._slugs_registry.append(full_name) self._store[full_name] = report
def register(self, model_or_iterable, admin_class=None, **options): """ Registers the given model(s) with the given admin class. The model(s) should be Model classes, not instances. If an admin class isn't given, it will use ModelAdmin (the default admin options). If keyword arguments are given -- e.g., list_display -- they'll be applied as options to the admin class. If a model is already registered, this will raise AlreadyRegistered. If a model is abstract, this will raise ImproperlyConfigured. """ if isinstance(model_or_iterable, ModelBase) and not admin_class: admin_class = ModelAdmin if isinstance(model_or_iterable, TopLevelDocumentMetaclass) and not admin_class: admin_class = DocumentAdmin # Don't import the humongous validation code unless required #if admin_class and settings.DEBUG: # from mongoadmin.validation import validate #else: validate = lambda model, adminclass: None if isinstance(model_or_iterable, ModelBase) or \ isinstance(model_or_iterable, TopLevelDocumentMetaclass): model_or_iterable = [model_or_iterable] for model in model_or_iterable: if isinstance(model, TopLevelDocumentMetaclass): init_document_options(model) if hasattr(model._meta, 'abstract') and model._meta.abstract: raise ImproperlyConfigured('The model %s is abstract, so it ' 'cannot be registered with admin.' % model.__name__) if model in self._registry: raise AlreadyRegistered('The model %s is already registered' % model.__name__) # Ignore the registration if the model has been # swapped out. if model._meta.swapped: continue # If we got **options then dynamically construct a subclass of # admin_class with those **options. if options: # For reasons I don't quite understand, without a __module__ # the created class appears to "live" in the wrong place, # which causes issues later on. options['__module__'] = __name__ admin_class = type("%sAdmin" % model.__name__, (admin_class,), options) # Validate (which might be a no-op) validate(admin_class, model) # Instantiate the admin class to save in the registry self._registry[model] = admin_class(model, self)
def register(self, model_class, admin_class): '''Register Ext model admin in catalog interface''' if model_class not in self._registry: self.get_registry().update({model_class: admin_class}) # register many to many relations specially for m2m_field_name in admin_class.m2ms: base_field = model_class._meta.get_field_by_name( m2m_field_name)[0] # workaround reverse relations from django.db.models.related import RelatedObject if type(base_field) is RelatedObject: rel_model_class = base_field.model else: rel_model_class = base_field.rel.to m2m_name = '%s-%s-%s' % (model_class.__name__, m2m_field_name, rel_model_class.__name__) self._m2ms.update({ m2m_name.lower(): { 'base_model': model_class, 'fk_attr': m2m_field_name, 'rel_model': rel_model_class, 'url': m2m_name.lower(), } }) else: raise AlreadyRegistered('Model %s already registered' % model_class.__str__)
def unregister_related(self, model_or_iterable, admin_class=None, related_to=None, **options): if not model_or_iterable or not admin_class or not related_to: return tool_name = admin_class and (getattr(admin_class, 'tool_name', None) or getattr(model_or_iterable._meta, 'module_name', None)) if not tool_name: raise Exception( 'Can not unregister %s modeladmin without a tool_name' % admin_class.__name__) model_tools = self.tools.get(related_to, {}) if tool_name in model_tools and admin_class != model_tools[ tool_name].__class__: raise AlreadyRegistered( 'Already registered a modeladmin with %s as tool_name' % tool_name) model_admin = model_tools.get(tool_name, admin_class(model_or_iterable, self)) del model_tools[tool_name] base_model_registry = self.related_registry.get(related_to, {}) related_modeladmins = base_model_registry.get(model_or_iterable, []) if model_admin not in related_modeladmins: related_modeladmins += [model_admin] del base_model_registry[model_or_iterable] self.related_registry[related_to] = base_model_registry self.tools[related_to] = model_tools
def register(self, model, admin_class=None): if admin_class is None: admin_class = ModelAdmin if model in self._registry: raise AlreadyRegistered('The model %s is already registered' % model.__name__) self._registry[model] = admin_class(model, self)
def register(self, component, *schemas): logger.debug("Registering admin component... %s" % component.__name__) if component in self._components: raise AlreadyRegistered( _("The component %s is already " "registered" % component.__name__)) self._components[component] = component(self.name) if schemas: self._schemas[component] = schemas
def bmfregister_module(self, module): """ register a module with the framework """ if module.model in self.bmf_modules: raise AlreadyRegistered('The module %s is already registered' % module.model.__name__) self.bmf_modules[module.model] = module(self) return self.bmf_modules[module.model]
def bmfregister_relationship(self, relationship, model): r = relationship() r._related_model = model for obj in self.bmf_relations: if obj == r: raise AlreadyRegistered( 'Can not register the relationship %s' % relationship.__name__) self.bmf_relations.append(r)
def register_module(self, model, **options): if not hasattr(model, '_bmfmeta'): raise ImproperlyConfigured( 'The module %s needs to be an BMF-Model in order to be' 'registered with django BMF.' % model.__name__) if model in self.modules: raise AlreadyRegistered('The module %s is already registered' % model.__name__) self.modules[model] = Module(model, **options)
def register(self, service, plugin, version=1): if plugin in self._registry: raise AlreadyRegistered('The class %s is already registered' % plugin.__name__) self._registry[u"%s v%s" % (service, version)] = { 'service': service, 'plugin': plugin, 'version': version }
def add_module(self, module): """ Adds a module to the dashboard """ if module.model in self.site.modules: raise AlreadyRegistered('The module %s is already registered' % module.model.__name__) self.site.modules[module.model] = module() logger.debug('Registered Module "%s"', module.__name__) return self.site.modules[module.model]
def bmfregister_module(module): """ register a module with the framework """ for mod in self.bmf_modules: if mod.model == module.model: raise AlreadyRegistered('The module %s is already registered' % module.model.__name__) mod = module() self.bmf_modules.append(mod) return mod
def register(self, report_field, override=False): """ Register a report_field into the registry, :param report_field: :param override: if True, a report_field will get replaced if found, else it would throw an AlreadyRegistered :return: report_field passed """ if report_field.name in self._registry and not override: raise AlreadyRegistered('This field is already registered') self._registry[report_field.name] = report_field return report_field
def add_relation(self, cls, model_from): """ """ relation = cls() relation._model_from = model_from for obj in self._relations: if obj == relation: raise AlreadyRegistered( 'Can not register the relationship %s' % cls.__name__ ) self._relations.append(relation)
def add_report(self, report_class): """ Adds a report to the dashboard """ for report in self.reports: if isinstance(report_class, report): raise AlreadyRegistered('The report %s is already registered' % report_class.__name__) report = report_class() self.reports.append(report) logger.debug('Registered Report "%s"', report.key) return report
def register(self, model_or_iterable, admin_class=None, **options): """ Registers the given model(s) with the given admin class. The model(s) should be Model classes, not instances. If an admin class isn't given, it will use ModelAdmin (the default admin options). If keyword arguments are given -- e.g., list_display -- they'll be applied as options to the admin class. If a model is already registered, this will raise AlreadyRegistered. If a model is abstract, this will raise ImproperlyConfigured. """ if not admin_class: admin_class = ModelAdmin if isinstance(model_or_iterable, ModelBase) or \ isinstance(model_or_iterable, ResourceBase): model_or_iterable = [model_or_iterable] for model in model_or_iterable: if model._meta.abstract: raise ImproperlyConfigured('The model %s is abstract, so it ' 'cannot be registered with admin.' % model.__name__) if model in self._registry: raise AlreadyRegistered('The model %s is already registered' % model.__name__) # Ignore the registration if the model has been # swapped out. if not model._meta.swapped: # If we got **options then dynamically construct a subclass of # admin_class with those **options. if options: # For reasons I don't quite understand, without a __module__ # the created class appears to "live" in the wrong place, # which causes issues later on. options['__module__'] = __name__ admin_class = type("%sAdmin" % model.__name__, (admin_class, ), options) if admin_class is not ModelAdmin and settings.DEBUG: system_check_errors.extend(admin_class.check(model)) # Instantiate the admin class to save in the registry self._registry[model] = admin_class(model, self)
def register(self, view_class, **options): if options: class Meta: pass for att, val in options.items(): setattr(Meta, att, val) attrs = { '__module__': view_class.__module__, 'Meta': Meta, } view_class = type(view_class)( view_class.__name__, (view_class, ), attrs) path = view_class._meta.path.strip('/') if path in self._registry: raise AlreadyRegistered("path '%s' has been already registered" % ( path)) self._registry[path] = view_class
def register(self, model_or_iterable, admin_class=None, **options): """ Registers the given model(s) with the given admin class. It's copied from django one. The only difference is that this registration does not will raise AlreadyRegistered exception if you try register two times same model_or_iterable and admin_class. However, It will raise exception if you try to register same model_or_iterable with different admin_class. """ if not admin_class: admin_class = ModelAdmin # Don't import the humongous validation code unless required if admin_class and settings.DEBUG: from django.contrib.admin.validation import validate else: validate = lambda model, adminclass: None # pyflakes:ignore if isinstance(model_or_iterable, ModelBase): model_or_iterable = [model_or_iterable] for model in model_or_iterable: # Here is the difference between django.contrib.admin.sites register and merengue one if model in self._registry and not isinstance( self._registry[model], admin_class): raise AlreadyRegistered('The model %s is already registered' % model.__name__) # If we got **options then dynamically construct a subclass of # admin_class with those **options. if options: # For reasons I don't quite understand, without a __module__ # the created class appears to "live" in the wrong place, # which causes issues later on. options['__module__'] = __name__ admin_class = type("%sAdmin" % model.__name__, (admin_class, ), options) # Validate (which might be a no-op) validate(admin_class, model) # Instantiate the admin class to save in the registry self._registry[model] = admin_class(model, self)
def bmfregister_module(self, module): """ register a module with the framework """ if module.model in self.bmf_modules: raise AlreadyRegistered('The module %s is already registered' % module.model.__name__) self.bmf_modules[module.model] = module() # register files if module has them if module.model._bmfmeta.has_files: from djangobmf.core.serializers.document import DocumentSerializer class FileDownload(DocumentRelationship): model = module.model serializer = DocumentSerializer self.bmfregister_relationship(FileDownload, self.get_model("Document")) return self.bmf_modules[module.model]
def register_setting(self, app_label, setting_name, field): name = '.'.join([app_label, setting_name]) if name in self.settings: raise AlreadyRegistered('The setting %s is already registered' % name) self.settings[name] = field
def register_report(self, name, cls): if name in self.reports: raise AlreadyRegistered('The report %s is already registered' % name) self.reports[name] = cls
def register_currency(self, currency): if currency.iso in self.currencies: raise AlreadyRegistered('The currency %s is already registered' % currency.__name__) self.currencies[currency.iso] = currency
def register(self, report_field): if report_field.name in self._registry: raise AlreadyRegistered('This field is already registered') self._registry[report_field.name] = report_field return report_field