Commit 6611e7d5 authored by Dmitry Volodin's avatar Dmitry Volodin
Browse files

@collection decorator to denote json-synchronizable collections

--HG--
branch : develop
parent 1305e745
......@@ -14,8 +14,10 @@ from mongoengine.fields import StringField, UUIDField
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
@collection
class ErrorType(Document):
meta = {
"collection": "noc.errortypes",
......
......@@ -22,8 +22,10 @@ from alarmclassjob import AlarmClassJob
from alarmplugin import AlarmPlugin
from noc.lib.escape import json_escape as q
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
@collection
class AlarmClass(nosql.Document):
"""
Alarm class
......
......@@ -2,7 +2,7 @@
##----------------------------------------------------------------------
## AlarmSeverity model
##----------------------------------------------------------------------
## Copyright (C) 2007-2014 The NOC Project
## Copyright (C) 2007-2015 The NOC Project
## See LICENSE for details
##----------------------------------------------------------------------
......@@ -15,8 +15,10 @@ from noc.main.models.style import Style
from noc.lib.nosql import ForeignKeyField
from noc.lib.text import quote_safe_path
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class AlarmSeverity(Document):
"""
Alarm severities
......
......@@ -14,8 +14,10 @@ from mongoengine import fields
## NOC modules
from noc.lib.text import quote_safe_path
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class CloneClassificationRule(Document):
"""
Classification rules cloning
......
......@@ -12,8 +12,10 @@ from mongoengine.fields import StringField, DictField, UUIDField
## Python modules
from noc.lib.text import quote_safe_path
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class Enumeration(Document):
meta = {
"collection": "noc.enumerations",
......
......@@ -17,6 +17,7 @@ from noc.lib import nosql
from alarmclass import AlarmClass
from noc.lib.escape import json_escape as q
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
class EventClassVar(EmbeddedDocument):
......@@ -191,6 +192,7 @@ class EventClassCategory(nosql.Document):
super(EventClassCategory, self).save(*args, **kwargs)
@collection
class EventClass(Document):
"""
Event class
......
......@@ -17,6 +17,7 @@ from datasource import DataSource
from noc.lib.nosql import PlainReferenceField
from noc.lib.escape import json_escape as jq
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
class EventClassificationRuleVar(EmbeddedDocument):
......@@ -72,6 +73,7 @@ class EventClassificationPattern(EmbeddedDocument):
return self.key_re == other.key_re and self.value_re == other.value_re
@collection
class EventClassificationRule(Document):
"""
Classification rules
......
......@@ -11,9 +11,10 @@ from mongoengine.document import Document
from mongoengine.fields import StringField, UUIDField
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class MIBAlias(Document):
"""
MIB Aliases
......
......@@ -11,8 +11,10 @@ from mongoengine.document import Document
from mongoengine.fields import StringField, UUIDField, IntField
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class MIBPreference(Document):
meta = {
"collection": "noc.mibpreferences",
......
......@@ -11,8 +11,10 @@ from mongoengine.document import Document
from mongoengine.fields import StringField, UUIDField
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class OIDAlias(Document):
meta = {
"collection": "noc.oidaliases",
......
......@@ -11,8 +11,10 @@ from mongoengine.document import Document
from mongoengine.fields import (StringField, UUIDField, DictField)
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class SyntaxAlias(Document):
meta = {
"collection": "noc.syntaxaliases",
......
......@@ -15,8 +15,10 @@ from mongoengine.fields import (StringField, UUIDField, IntField,
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
@collection
class Layer(Document):
meta = {
"collection": "noc.layers",
......
......@@ -15,8 +15,10 @@ from mongoengine.fields import (StringField, UUIDField, ObjectIdField)
from noc.main.models.doccategory import category
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
@collection
@category
class Capability(Document):
meta = {
......
......@@ -15,6 +15,7 @@ from mongoengine.fields import (StringField, UUIDField,
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
class Context(EmbeddedDocument):
......@@ -84,6 +85,7 @@ class Rule(EmbeddedDocument):
}
@collection
class ConnectionRule(Document):
"""
Equipment vendor
......
......@@ -17,8 +17,10 @@ from noc.lib.nosql import PlainReferenceField
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.main.models.doccategory import category
from noc.lib.collection import collection
@collection
@category
class ConnectionType(Document):
"""
......
......@@ -19,6 +19,8 @@ from noc.lib.escape import json_escape as q
from noc.sa.interfaces.base import (StringParameter, BooleanParameter,
FloatParameter, IntParameter,
StringListParameter)
from noc.lib.collection import collection
T_MAP = {
"str": StringParameter(),
......@@ -81,6 +83,7 @@ class ModelInterfaceAttr(EmbeddedDocument):
return False
@collection
class ModelInterface(Document):
"""
Equipment vendor
......
......@@ -24,6 +24,7 @@ from noc.main.models.doccategory import category
from noc.lib.nosql import PlainReferenceField
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
class ObjectModelConnection(EmbeddedDocument):
......@@ -82,6 +83,7 @@ class ObjectModelConnection(EmbeddedDocument):
return r
@collection
@category
class ObjectModel(Document):
"""
......
......@@ -14,8 +14,10 @@ from mongoengine.fields import StringField, UUIDField
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
@collection
class Technology(Document):
"""
Equipment vendor
......
......@@ -12,8 +12,10 @@ from mongoengine.fields import (StringField, BooleanField, URLField,
UUIDField)
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class Vendor(Document):
"""
Equipment vendor
......
......@@ -523,18 +523,6 @@ class Collection(object):
tr += ["en"]
cls.TRANSLATIONS[cn] = tr
@classmethod
def install(cls):
"""
Install collections creation hooks
"""
mongoengine.signals.class_prepared.connect(cls.on_new_document)
@classmethod
def on_new_document(cls, sender, *args, **kwargs):
if "json_collection" in sender._meta:
cls.COLLECTIONS[sender._meta["json_collection"]] = sender
@classmethod
def iter_collections(cls):
"""
......@@ -567,5 +555,24 @@ class Collection(object):
Collection.setup()
def collection(cls):
"""
Decorator to denote JSON-synchronizable collections
@collection
class MyDocument(Document):
meta = {
"json_collection": ...
}
"""
assert "json_collection" in cls._meta, "Class %s must have json_collection" % cls.__name__
cn = cls._meta["json_collection"]
logger.debug("Registering collection %s", cn)
Collection.COLLECTIONS[cn] = cls
return cls
##
from noc.main.models.collectioncache import CollectionCache
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment