def patch_admin(model, admin_site=None, AdminClass=None): """ Enables version control with full admin integration for a model that has already been registered with the django admin site. This is excellent for adding version control to existing Django contrib applications. """ admin_site = admin_site or admin.site try: ModelAdmin = admin_site._registry[model].__class__ except KeyError: raise NotRegistered("The model {model} has not been registered with the admin site.".format( model = model, )) # Unregister existing admin class. admin_site.unregister(model) # Register patched admin class. if not AdminClass: from reversion_compare.admin import CompareVersionAdmin class PatchedModelAdmin(CompareVersionAdmin, ModelAdmin): pass else: class PatchedModelAdmin(AdminClass, ModelAdmin): pass admin_site.register(model, PatchedModelAdmin)
def patch_admin(model, admin_site=None): """ Enables version control with full admin integration for a model that has already been registered with the django admin site. This is excellent for adding version control to existing Django contrib applications. """ admin_site = admin_site or admin.site try: ModelAdmin = admin_site._registry[model].__class__ except KeyError: raise NotRegistered( "The model %r has not been registered with the admin site." % model) # Unregister existing admin class. admin_site.unregister(model) # Register patched admin class. class PatchedModelAdmin( GuardedModelAdmin, ModelAdmin): # Remove VersionAdmin, if you don't use reversion. pass admin_site.register(model, PatchedModelAdmin)
def unregister(self, component): self._schemas.pop(component, None) try: self._components.pop(component) except KeyError: raise NotRegistered( _("The component %s is not " "registered" % component.__name__))
def get(self, namespace, report_slug): slug_id = '%s_%s' % (namespace, report_slug) try: return self._store[slug_id.lower()] except KeyError: raise NotRegistered( "Report '%s' base model '%s' not found, Did you register it? If yes, then maybe it's has different base model ?" % (report_slug, namespace))
def get(self, model_name): try: model = self._registry[model_name.lower()] except: raise NotRegistered('Model name %s not identified. Are you sure that the model exsits and its ' 'app is added to INSTALLED_APPS ?' % model_name) return model
def _unregister(self, report_class, namespace): if type(self._registry[namespace]) is list: if report_class not in self._registry[namespace]: raise NotRegistered('This report is not registered') self._registry[namespace].remove(report_class) slug_id = '%s_%s' % (report_class.namespace, report_class.get_report_slug()) self._slugs_registry.remove(slug_id) self._base_models.append(report_class.base_model)
def unregister(self, report_field): """ To unregister a Report Field :param report_field: a Report field class or a ReportField Name :return: None """ name = report_field if type(report_field) is str else report_field.name if name not in self._registry: raise NotRegistered(report_field) del self._registry[name]
def get(self, model_name): try: model = self._registry[model_name.lower()] except: options = self._registry.keys() raise NotRegistered( f'Model name {model_name} not identified. options are {options}. \n Are you sure that the model exsits and its ' 'app is added to INSTALLED_APPS ?') return model
def unregister(self, model_or_iterable): """ Unregisters the given model(s). If a model isn't already registered, this will raise NotRegistered. """ 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 model not in self._registry: raise NotRegistered('The model %s is not registered' % model.__name__) del self._registry[model]
def patch_admin(model, admin_site=None, AdminClass=None, skip_non_revision=False): """ Enables version control with full admin integration for a model that has already been registered with the django admin site. This is excellent for adding version control to existing Django contrib applications. :param skip_non_revision: If ==True: Skip models that are not register with ModelAdmin """ admin_site = admin_site or admin.site try: ModelAdmin = admin_site._registry[model].__class__ except KeyError: raise NotRegistered( f"The model {model} has not been registered with the admin site.") if skip_non_revision: if not hasattr(ModelAdmin, "object_history_template"): logger.info( f"Skip activate compare admin, because" f" model {model._meta.object_name!r} is not registered with revision manager." ) return # Unregister existing admin class. admin_site.unregister(model) # Register patched admin class. if not AdminClass: from reversion_compare.admin import CompareVersionAdmin class PatchedModelAdmin(CompareVersionAdmin, ModelAdmin): pass else: class PatchedModelAdmin(AdminClass, ModelAdmin): pass admin_site.register(model, PatchedModelAdmin)
def unregister_setting(self, app_label, setting_name): name = '.'.join([app_label, setting_name]) if name not in self.settings: raise NotRegistered('The setting %s is not registered' % name) del self.settings[name]
def unregister_report(self, name): if name not in self.reports: raise NotRegistered('The currency %s is not registered' % name) del self.reports[name]
def unregister_currency(self, currency): if currency.iso not in self.currencies: raise NotRegistered('The currency %s is not registered' % currency.__name__) del self.currencies[currency.iso]
def unregister_module(self, module): if module not in self.modules: raise NotRegistered('The model %s is not registered' % module.__name__) del self.modules[module]
def get_field_by_name(self, name): if name in self._registry: return self._registry[name] else: raise NotRegistered(name)
def unregister(self, report_field): if report_field.name not in self._registry: raise NotRegistered(report_field) del self._registry[report_field.name]