Example #1
0
 def _api_install_json(self, request, id):
     """
     Expose JSON collection item when available
     """
     from noc.core.collection.base import Collection
     o = self.get_object_or_404(self.model, id=id)
     Collection.install(o.to_json())
     return True
Example #2
0
 def handle_install(self, install_files=None, remove=False, load=False):
     install_files = install_files or []
     for fp in install_files:
         if not os.path.isfile(fp):
             self.die("File not found: %s" % fp)
         with open(fp) as f:
             data = ujson.load(f)
         try:
             Collection.install(data)
             if load:
                 c = Collection(data["$collection"])
                 c.update_item(data)
         except ValueError as e:
             self.die("%s - %s" % (fp, (str(e))))
         if remove:
             os.unlink(fp)
Example #3
0
 def handle_sync(self):
     connect()
     for c in Collection.iter_collections():
         try:
             c.sync()
         except ValueError as e:
             self.die(str(e))
Example #4
0
 def _bulk_field_is_builtin(self, data):
     """
     Apply is_builtin field
     :param data:
     :return:
     """
     builtins = Collection.get_builtins(self.json_collection)
     for x in data:
         u = x.get("uuid")
         x["is_builtin"] = u and u in builtins
     return data
Example #5
0
 def report_html(self, request, result=None, query=None):
     builtins = Collection.get_builtins("fm.eventclassificationrules")
     r = ["["]
     r += [
         ",\n".join([
             indent(rr.to_json())
             for rr in EventClassificationRule.objects.order_by("name")
             if rr.uuid and smart_text(rr.uuid) not in builtins
         ])
     ]
     r += ["]", ""]
     return "<pre>" + escape("\n".join(r)) + "</pre>"
Example #6
0
    def handle_export(self, list_collection=False, 
                      export_path=None, export_collections=None, 
                      export_model_names=None, export_model_uuids=None):
        MODELS = {}
        for c in COLLECTIONS:
            cm = get_model(c)
            cn = cm._meta["json_collection"]
            MODELS[cn] = cm
        if list_collection is not None:
            if list_collection is True:
                for c in Collection.iter_collections():
                    print("%s" % c.name, file=self.stdout)
            else:
                if list_collection not in MODELS:
                    print("Collection not found", file=self.stdout)
                    return
                objs = MODELS[list_collection].objects.all().order_by('name')
                for o in objs:
                    print("uuid:%s name:\"%s\"" % (o.uuid, o.name),
                          file=self.stdout)
        else:
            if not export_path or not export_collections:
                return
            if not os.path.isdir(export_path):
                self.die("Path not found: %s" % export_path)

            for ecname in export_collections:
                if ecname not in MODELS:
                    print("Collection not found", file=self.stdout)
                    continue
                kwargs = {}
                if export_model_names:
                    kwargs['name__in'] = export_model_names
                elif export_model_uuids:
                    kwargs['uuid__in'] = export_model_uuids
                objs = MODELS[ecname].objects.filter(**kwargs).order_by('name')
                for o in objs:
                    path = os.path.join(
                        export_path,
                        ecname,
                        o.get_json_path()
                    )
                    print("export \"%s\" to %s" % (o.name, path),
                          file=self.stdout)
                    safe_rewrite(
                        path,
                        o.to_json(),
                        mode=0o644
                    )
Example #7
0
 def cleaned_query(self, q):
     q = q.copy()
     for p in self.ignored_params:
         if p in q:
             del q[p]
     for p in (
             self.limit_param,
             self.page_param,
             self.start_param,
             self.format_param,
             self.sort_param,
             self.query_param,
             self.only_param,
     ):
         if p in q:
             del q[p]
     # Extract IN
     # extjs not working with same parameter name in query
     for p in list(q.keys()):
         if p.endswith("__in") and self.rx_oper_splitter.match(p):
             field = self.rx_oper_splitter.match(p).group("field") + "__in"
             if field not in q:
                 q[field] = [q[p]]
             else:
                 q[field] += [q[p]]
             del q[p]
     # Normalize parameters
     for p in q:
         if p.endswith("__exists"):
             v = BooleanParameter().clean(q[p])
             q[p] = v
         elif p in self.clean_fields:
             q[p] = self.clean_fields[p].clean(q[p])
     # @todo: correct __ lookups
     if any(p for p in q if p.endswith("__referred")):
         del q[p]
     # builtin
     is_builtin = q.pop("is_builtin", None)
     if self.json_collection and is_builtin in ("true", "false"):
         builtins = [
             uuid.UUID(x)
             for x in Collection.get_builtins(self.json_collection)
         ]
         if is_builtin == "true":
             q["uuid__in"] = builtins
         else:
             q["uuid__nin"] = builtins
     return q
Example #8
0
 def api_import(self, request, json):
     try:
         jdata = ujson.loads(json)
     except Exception as e:
         return {"status": False, "error": "Invalid JSON: %s" % e}
     try:
         if isinstance(jdata, list):
             for d in jdata:
                 Collection.install(d)
                 c = Collection(d["$collection"])
                 c.update_item(d)
         else:
             Collection.install(jdata)
             c = Collection(jdata["$collection"])
             c.update_item(jdata)
     except ValueError as e:
         return {"status": False, "error": str(e)}
     return {"status": True}
Example #9
0
 def get_count(cn, m):
     collection = Collection(cn)
     total = collection.model.objects.count()
     builtin = len(collection.get_items())
     local = total - builtin
     return [builtin, local, total]
Example #10
0
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------

# NOC modules
import operator
import os

# Third-party modules
import pytest

# NOC modules
from noc.core.collection.base import Collection


@pytest.fixture(params=list(Collection.iter_collections()), ids=operator.attrgetter("name"))
def collection(request):
    return request.param


@pytest.mark.xfail
def test_collection_path(collection):
    assert any(os.path.isdir(p) for p in collection.get_path())


@pytest.mark.usefixtures("database")
def test_collection_load(database, collection):
    """
    Load collection
    :param database:
    :return:
Example #11
0
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------

# NOC modules
import operator
import os

# Third-party modules
import pytest

# NOC modules
from noc.core.collection.base import Collection


@pytest.fixture(params=list(Collection.iter_collections()),
                ids=operator.attrgetter("name"))
def collection(request):
    return request.param


@pytest.mark.xfail
def test_collection_path(collection):
    assert any(os.path.isdir(p) for p in collection.get_path())


@pytest.mark.usefixtures("database")
def test_collection_load(database, collection):
    """
    Load collection
    :param database: