def build(): def add(c): mp = c.__module__.split(".") if mp[0] != "noc": return module = mp[1] alias = "%s.%s" % (module, c.__name__) path = "%s.%s" % (c.__module__, c.__name__) models[module] += [(alias, path)] # Load all applications and models site.autodiscover() models = defaultdict(list) # Module -> [(alias, path)] # Enumerate documents for c in six.itervalues(_document_registry): if issubclass(c, Document): add(c) # Enumerate models for c in get_models(): if issubclass(c, Model): add(c) # Dump out = ["## model_id -> full class path", "## Generated by build-models.py", "_MODELS = {"] for app in INSTALLED_APPS: if not app.startswith("noc."): continue module = app[4:] if not models[module]: continue out += [" # %s models" % module] for alias, path in sorted(models[module], key=lambda x: x[0]): out += [' "%s": "%s",' % (alias, path)] out[-1] = out[-1][:-1] # Remove last comma out += ["}"] print("\n".join(out))
def on_activate(self): # Initialize audit trail from noc.main.models.audittrail import AuditTrail AuditTrail.install() # Initialize site self.logger.info("Registering web applications") from noc.lib.app.site import site site.service = self site.autodiscover() # Install Custom fields CustomField.install_fields()
# -*- coding: utf-8 -*- # --------------------------------------------------------------------- # Django URL dispatcher. # --------------------------------------------------------------------- # Copyright (C) 2007-2016 The NOC Project # See LICENSE for details # --------------------------------------------------------------------- # NOC modules from noc.lib.app.site import site, patterns # # Discover all applications # site.autodiscover() # # Install URL handlers # urlpatterns = patterns("", (r"^jsi18n/$", "django.views.i18n.javascript_catalog", {"packages": "django.conf"}) ) + site.urls
def sync(cls): from noc.lib.app.site import site def normalize(app, perm): if ":" in perm: return perm return "%s:%s" % (app.get_app_id().replace(".", ":"), perm) def get_implied(name): try: return ",".join(implied_permissions[name]) except KeyError: return None if not site.apps: # Initialize applications to get permissions site.autodiscover() new_perms = set() implied_permissions = {} diverged_permissions = {} # new -> old for app in six.itervalues(site.apps): new_perms = new_perms.union(app.get_permissions()) for p in app.implied_permissions: ips = sorted( [normalize(app, pp) for pp in app.implied_permissions[p]]) implied_permissions[normalize(app, p)] = ips for p in app.diverged_permissions: diverged_permissions[normalize(app, p)] = normalize( app, app.diverged_permissions[p]) # Check all implied permissions are present for p in implied_permissions: if p not in new_perms: raise ValueError("Implied permission '%s' is not found" % p) nf = [pp for pp in implied_permissions[p] if pp not in new_perms] if nf: raise ValueError("Invalid implied permissions: %s" % pp) # old_perms = set(Permission.objects.values_list("name", flat=True)) # New permissions created_perms = {} # name -> permission for name in new_perms - old_perms: # @todo: add implied permissions p = Permission(name=name, implied=get_implied(name)) p.save() print("+ %s" % name) created_perms[name] = p # Check implied permissions match for name in old_perms.intersection(new_perms): implied = get_implied(name) p = Permission.objects.get(name=name) if p.implied != implied: print("~ %s" % name) p.implied = implied p.save() # Deleted permissions for name in old_perms - new_perms: print("- %s" % name) Permission.objects.get(name=name).delete() # Diverge created permissions for name in created_perms: op_name = diverged_permissions.get(name) if not op_name: continue # Ger original permission op = Permission.get_by_name(op_name) if not op: continue print(": %s -> (%s, %s)" % (op_name, op_name, name)) # Migrate users dp = created_perms[name] for u in op.users.all(): dp.users.add(u) # Migrate groups for g in op.groups.all(): dp.groups.add(g)
def handler(self, **kwargs): site.autodiscover() ReportSubscription.send_reports()
def test_autodiscover(): site.autodiscover() assert site.apps