class BaseDocument(Document): meta = {'abstract': True, 'queryset': BaseQueryset} uuid = UUIDField(default=uuid.uuid4) date_created = DateTimeField(required=True, default=datetime.utcnow) date_updated = DateTimeField(required=True) date_deleted = DateTimeField(null=True) def clean(self): super().clean() fields = list( self._fields) if self.pk is None else self._get_changed_fields() if fields: self.date_updated = datetime.utcnow() for field in fields: clean_method_name = '_clean_{}'.format(field) clean_method = getattr(self, clean_method_name, None) if clean_method: clean_method() def to_dict(self): return {f: getattr(self, f) for f in self._fields}
class Capability(Document): meta = { "collection": "noc.inv.capabilities", "json_collection": "inv.capabilities" } name = StringField(unique=True) uuid = UUIDField(binary=True) description = StringField(required=False) type = StringField(choices=["bool", "str", "int", "float"]) category = ObjectIdField() def __unicode__(self): return self.name @property def json_data(self): r = { "name": self.name, "$collection": self._meta["json_collection"], "uuid": self.uuid, "description": self.description, "type": self.type } return r def to_json(self): return to_json( self.json_data, order=["name", "$collection", "uuid", "description", "type"]) def get_json_path(self): p = [quote_safe_path(n.strip()) for n in self.name.split("|")] return os.path.join(*p) + ".json"
class Enumeration(Document): meta = { "collection": "noc.enumerations", "allow_inheritance": False, "json_collection": "fm.enumerations" } name = StringField(unique=True) uuid = UUIDField(binary=True) values = DictField() # value -> [possible combinations] def __unicode__(self): return self.name def get_json_path(self): return "%s.json" % quote_safe_path(self.name) def to_json(self): return to_json( { "name": self.name, "$collection": self._meta["json_collection"], "uuid": self.uuid, "values": self.values }, order=["name", "$collection", "uuid"])
class ErrorType(Document): meta = { "collection": "noc.errortypes", "strict": False, "auto_create_index": False, "json_collection": "cm.errortypes" } name = StringField(unique=True) uuid = UUIDField(binary=True, unique=True) description = StringField() subject_template = StringField() body_template = StringField() def __unicode__(self): return self.name def get_json_path(self): p = [quote_safe_path(n.strip()) for n in self.name.split("|")] return os.path.join(*p) + ".json" def to_json(self): r = { "name": self.name, "$collection": self._meta["json_collection"], "uuid": self.uuid, "subject_template": self.subject_template, "body_template": self.body_template } if self.description: r["description"] = self.description return to_json(r, order=["name", "$collection", "uuid", "description", "subject_template", "body_template"])
class FranquiciaDashboard(Document): meta = {'collection': 'franquicia_dashboard'} _id = UUIDField() id = IntField() nombre = StringField() tipoDashboard = IntField() alcance = IntField() nombreAlcance = StringField() anioAlcance = IntField() idDimension = IntField() nombreDimension = StringField() idRubro = IntField() nombreRubro = StringField() idEncuesta = IntField() idEncuestaContestada = IntField() ponderacionEvaluador = IntField() ponderacionObtenida = IntField() ponderacionImperdonables = IntField() textoPregunta = StringField() idOpcRespuesta = IntField() textoRespuesta = StringField() tipoRubro = IntField() idPregunta = IntField() ordenDimension = IntField() descripcionScored = StringField() observacionesCalidad = StringField() idTipoOpcionRespuesta = IntField() ordenRubro = IntField() scoredVisible = IntField()
class BaseModel(object): """ Base Model Class. """ uuid = UUIDField(default=uuid1) created_at = DateTimeField(default=datetime.datetime.now) modified_at = DateTimeField() def to_dict(self): """ Converts a document to Dictionary. """ return { "uuid": str(self.uuid), # "name": self.name, "created_at": str(self.created_at), "modified_at": str(self.modified_at), } def render_to_response(self): """ Converts a Dictionary to json. """ return {str(self.uuid): self.to_dict()}
class MIBPreference(Document): meta = { "collection": "noc.mibpreferences", "allow_inheritance": False, "json_collection": "fm.mibpreferences" } mib = StringField(required=True, unique=True) preference = IntField(required=True, unique=True) # The less the better uuid = UUIDField(binary=True) def __unicode__(self): return u"%s(%d)" % (self.mib, self.preference) def get_json_path(self): return "%s.json" % self.mib def to_json(self): return to_json( { "mib": self.mib, "$collection": self._meta["json_collection"], "uuid": self.uuid, "preference": self.preference }, order=["mib", "$collection", "uuid", "preference"])
class Category(Document): uuid = UUIDField(default=uuid4, unique=True) name = StringField(max_length=120, unique=True, required=True) created_at = DateTimeField(default=datetime.now) def to_dict(self): return {'uuid': str(self.uuid), 'name': self.name}
class DashboardLayout(Document): meta = { "collection": "noc.dashboardlayouts", "strict": False, "auto_create_index": False, "json_collection": "bi.dashboardlayouts", } name = StringField() uuid = UUIDField(binary=True) description = StringField() # @todo: Add preview cells = ListField(EmbeddedDocumentField(DashboardCell)) def __str__(self): return self.name def to_json(self): return to_json( { "name": self.name, "$collection": self._meta["json_collection"], "uuid": self.uuid, "description": self.description, "cells": [s.to_json() for s in self.cells], }, order=["name", "uuid", "description", "cells"], ) def get_json_path(self): return "%s.json" % self.name
class SyntaxAlias(Document): meta = { "collection": "noc.syntaxaliases", "strict": False, "auto_create_index": False, "json_collection": "fm.syntaxaliases" } name = StringField(unique=True, required=True) syntax = DictField(required=False) uuid = UUIDField(binary=True) # Lookup cache cache = None def __unicode__(self): return self.name @classmethod def rewrite(cls, name, syntax): if cls.cache is None: cls.cache = dict((o.name, o.syntax) for o in cls.objects.all()) return cls.cache.get(name, syntax) def get_json_path(self): return "%s.json" % self.name.replace(":", "_") def to_json(self): return to_json( { "name": self.name, "$collection": self._meta["json_collection"], "uuid": self.uuid, "syntax": self.syntax }, order=["name", "$collection", "uuid", "syntax"])
class MIBPreference(Document): meta = { "collection": "noc.mibpreferences", "strict": False, "auto_create_index": False, "json_collection": "fm.mibpreferences", "json_unique_fields": ["mib", "uuid"], } mib = StringField(required=True, unique=True) preference = IntField(required=True, unique=True) # The less the better uuid = UUIDField(binary=True) def __str__(self): return "%s(%d)" % (self.mib, self.preference) def get_json_path(self): return "%s.json" % self.mib def to_json(self): return to_json( { "mib": self.mib, "$collection": self._meta["json_collection"], "uuid": self.uuid, "preference": self.preference, }, order=["mib", "$collection", "uuid", "preference"], )
class Session(Document): sessionId = UUIDField() sessionStart = DateTimeField() sessionEnd = DateTimeField() states = ListField() tableId = IntField()
class Technology(Document): """ Equipment vendor """ meta = { "collection": "noc.technologies", "allow_inheritance": False, "json_collection": "inv.technologies" } # Group | Name name = StringField(unique=True) uuid = UUIDField(binary=True) description = StringField() def __unicode__(self): return self.name def get_json_path(self): p = [quote_safe_path(n.strip()) for n in self.name.split("|")] return os.path.join(*p) + ".json" def to_json(self): r = { "name": self.name, "$collection": self._meta["$collection"], "uuid": self.uuid } if self.description: r["description"] = self.description return to_json(r, order=["name", "$collection", "uuid", "description"])
class Enumeration(Document): meta = { "collection": "noc.enumerations", "strict": False, "auto_create_index": False, "json_collection": "fm.enumerations", "json_unique_fields": ["name"], } name = StringField(unique=True) uuid = UUIDField(binary=True) values = DictField() # value -> [possible combinations] def __str__(self): return self.name def get_json_path(self): return "%s.json" % quote_safe_path(self.name) def to_json(self): return to_json( { "name": self.name, "$collection": self._meta["json_collection"], "uuid": self.uuid, "values": self.values, }, order=["name", "$collection", "uuid"], )
class Vendor(Document): """ Equipment vendor """ meta = { "collection": "noc.vendors", "allow_inheritance": False, "json_collection": "inv.vendors" } name = StringField(unique=True) code = StringField() site = URLField(required=False) uuid = UUIDField(binary=True) def __unicode__(self): return self.name def to_json(self): return to_json( { "name": self.name, "$collection": self._meta["json_collection"], "code": self.code, "site": self.site, "uuid": self.uuid }, order=["name", "uuid", "code", "site"]) def get_json_path(self): return "%s.json" % self.code
class Message(Document): id = UUIDField(primary_key=True, default=lambda: uuid4()) user = ReferenceField('User') room = ReferenceField('Room') message = StringField(max_length=500, required=True) timestamp = DateTimeField(required=True, default=lambda: datetime.datetime.now()) location = PointField()
class Expenses(DynamicDocument): uuid = UUIDField() description = StringField() created_at = StringField() amount = IntField() currency = IntField() employee = EmbeddedDocumentField(Employee) meta = {'collection': 'expenses'}
class Capability(Document): meta = { "collection": "noc.inv.capabilities", "strict": False, "auto_create_index": False, "json_collection": "inv.capabilities", "json_unique_fields": ["name", "uuid"], } name = StringField(unique=True) uuid = UUIDField(binary=True) description = StringField(required=False) type = StringField(choices=["bool", "str", "int", "float"]) # Jinja2 template for managed object's card tags card_template = StringField(required=False) category = ObjectIdField() _id_cache = cachetools.TTLCache(maxsize=1000, ttl=60) _name_cache = cachetools.TTLCache(maxsize=1000, ttl=60) def __str__(self): return self.name @classmethod @cachetools.cachedmethod(operator.attrgetter("_id_cache"), lock=lambda _: id_lock) def get_by_id(cls, id): return Capability.objects.filter(id=id).first() @classmethod @cachetools.cachedmethod(operator.attrgetter("_name_cache"), lock=lambda _: id_lock) def get_by_name(cls, name): return Capability.objects.filter(name=name).first() @property def json_data(self): r = { "name": self.name, "$collection": self._meta["json_collection"], "uuid": self.uuid, "description": self.description, "type": self.type, "card_template": self.card_template, } return r def to_json(self): return to_json( self.json_data, order=[ "name", "$collection", "uuid", "description", "type", "card_template" ], ) def get_json_path(self): p = [quote_safe_path(n.strip()) for n in self.name.split("|")] return os.path.join(*p) + ".json"
class Quiz(Document): meta = { "collection": "quiz", "strict": False, "auto_create_index": False, "json_collection": "dev.quiz" } name = StringField(unique=True) uuid = UUIDField(binary=True) description = StringField() revision = IntField(default=1) changes = ListField(EmbeddedDocumentField(QuizChange)) # Information text before quiz disclaimer = StringField() # List of questions questions = ListField(EmbeddedDocumentField(QuizQuestion)) _id_cache = cachetools.TTLCache(maxsize=100, ttl=60) _name_cache = cachetools.TTLCache(maxsize=100, ttl=60) def __unicode__(self): return self.name @classmethod @cachetools.cachedmethod(operator.attrgetter("_id_cache"), lock=lambda _: id_lock) def get_by_id(cls, id): return Quiz.objects.filter(id=id).first() @classmethod @cachetools.cachedmethod(operator.attrgetter("_name_cache"), lock=lambda _: id_lock) def get_by_name(cls, name): return Quiz.objects.filter(name=name).first() @property def json_data(self): return { "name": self.name, "$collection": self._meta["json_collection"], "uuid": str(self.uuid), "description": self.description, "revision": self.revision, "disclaimer": self.disclaimer, "changes": [c.json_data for c in self.changes], "questions": [c.json_data for c in self.questions] } def to_json(self): return to_json(self.json_data, order=[ "name", "$collection", "uuid", "description", "revision", "disclaimer", "changes", "questions" ]) def get_json_path(self): return "%s.json" % quote_safe_path(self.name)
class OIDAlias(Document): meta = { "collection": "noc.oidaliases", "strict": False, "auto_create_index": False, "json_collection": "fm.oidaliases", "json_unique_fields": ["rewrite_oid"], } rewrite_oid = StringField(unique=True) to_oid = StringField() description = StringField(required=False) uuid = UUIDField(binary=True) # Lookup cache cache = None def __str__(self): return "%s -> %s" % (self.rewrite_oid, self.to_oid) @classmethod def rewrite(cls, oid): """ Rewrite OID with alias if any """ if cls.cache is None: # Initialize cache cls.cache = dict((a.rewrite_oid, a.to_oid.split(".")) for a in cls.objects.all()) # Lookup l_oid = oid.split(".") rest = [] while l_oid: c_oid = ".".join(l_oid) try: a_oid = cls.cache[c_oid] # Found return ".".join(a_oid + rest) except KeyError: rest = [l_oid.pop()] + rest # Not found return oid def get_json_path(self): return "%s.json" % self.rewrite_oid def to_json(self): r = { "rewrite_oid": self.rewrite_oid, "to_oid": self.to_oid, "uuid": self.uuid, "$collection": self._meta["json_collection"], } if self.description: r["description"] = self.description return to_json(r, order=["$collection", "rewrite_oid", "to_oid", "uuid"])
class Crashinfo(Document): meta = { "collection": "noc.crashinfo", "strict": False, "auto_create_index": False, "indexes": [("status", "timestamp")] } uuid = UUIDField(primary_key=True) timestamp = DateTimeField(required=True) status = StringField(choices=[("N", "New"), ("r", "Reporting"), ("R", "Reported"), ("X", "Rejected"), ("f", "Fix ready"), ("F", "Fixed")], default="N") process = StringField() branch = StringField() tip = StringField() comment = StringField() priority = StringField(choices=[("I", "Info"), ("L", "Low"), ("M", "Medium"), ("H", "High"), ("C", "Critical")], default="I") # @todo: comment NEW_ROOT = "local/cp/crashinfo/new" def __unicode__(self): return unicode(self.uuid) @classmethod def scan(cls): """ Scan cls.NEW_ROOT for new crashinfos """ for f in os.listdir(cls.NEW_ROOT): if not f.endswith(".json"): continue try: u = uuid.UUID(f[:-5]) except ValueError: continue # Badly formed UUID if Crashinfo.objects.filter(uuid=u).count(): continue # Known logger.info("New crashinfo found: %s", u) try: with open(os.path.join(cls.NEW_ROOT, f)) as f: ci = ujson.loads(f.read()) except Exception, why: logger.error("Unable to load and decode crashinfo %s: %s", u, why) continue c = Crashinfo(uuid=u, timestamp=dateutil.parser.parse(ci["ts"]), process=ci["process"], branch=ci.get("branch"), tip=ci.get("tip"), status="N") c.save()
class ObjectFact(Document): meta = {"collection": "noc.objectfacts", "indexes": ["object"]} uuid = UUIDField(binary=True, primary_key=True) object = ForeignKeyField(ManagedObject) cls = StringField() label = StringField() attrs = DictField() introduced = DateTimeField(default=datetime.datetime.now) changed = DateTimeField(default=datetime.datetime.now) def __unicode__(self): return self.object.name
class AlarmSeverity(Document): """ Alarm severities """ meta = { "collection": "noc.alarmseverities", "allow_inheritance": False, "indexes": ["severity"], "json_collection": "fm.alarmseverities" } name = StringField(required=True, unique=True) uuid = UUIDField(binary=True) description = StringField(required=False) severity = IntField(required=True) style = ForeignKeyField(Style) def __unicode__(self): return self.name @classmethod def get_severity(cls, severity): """ Returns Alarm Severity instance corresponding to numeric value """ s = cls.objects.filter( severity__lte=severity).order_by("-severity").first() if not s: s = cls.objects.order_by("severity").first() return s @classmethod @cachetools.ttl_cache(maxsize=1000, ttl=600) def get_severity_css_class_name(cls, severity): return cls.get_severity(severity).style.css_class_name def get_json_path(self): return "%s.json" % quote_safe_path(self.name) def to_json(self): return to_json( { "name": self.name, "$collection": self._meta["json_collection"], "uuid": self.uuid, "description": self.description, "severity": self.severity, "style__name": self.style.name }, order=[ "name", "$collection", "uuid", "description", "severity", "style" ])
class ActionCommands(Document): meta = { "collection": "noc.actioncommands", "strict": False, "auto_create_index": False, "json_collection": "sa.actioncommands", "json_depends_on": ["sa.actions", "sa.profile"], "json_unique_fields": ["name"] } name = StringField(unique=True) uuid = UUIDField(unique=True) action = ReferenceField(Action) description = StringField() profile = PlainReferenceField(Profile) config_mode = BooleanField(default=False) match = ListField(EmbeddedDocumentField(PlatformMatch)) commands = StringField() preference = IntField(default=1000) timeout = IntField(default=60) def __unicode__(self): return self.name def get_json_path(self): p = [quote_safe_path(n.strip()) for n in self.name.split("|")] return os.path.join(*p) + ".json" @property def json_data(self): r = { "name": self.name, "$collection": self._meta["json_collection"], "uuid": self.uuid, "action__name": self.action.name, "description": self.description, "profile__name": self.profile.name, "config_mode": self.config_mode, "match": [c.json_data for c in self.match], "commands": self.commands, "preference": self.preference, "timeout": self.timeout } return r def to_json(self): return to_json(self.json_data, order=[ "name", "$collection", "uuid", "action__name", "description", "profile__name", "config_mode", "preference", "match", "commands", "timeout" ])
class Service(Document): """ Represents a service instance. """ id = UUIDField(primary_key=True, default=uuid4) created_at = DateTimeField(default=datetime.utcnow) status = StringField() descriptor = DictField(required=True) functions = EmbeddedDocumentListField(Function, required=True) placement = DictField()
class MIBAlias(Document): """ MIB Aliases """ meta = { "collection": "noc.mibaliases", "strict": False, "auto_create_index": False, "json_collection": "fm.mibaliases", "json_unique_fields": ["rewrite_mib"], } rewrite_mib = StringField(unique=True) to_mib = StringField() uuid = UUIDField(binary=True) # Lookup cache cache = None def __str__(self): return "%s -> %s" % (self.rewrite_mib, self.to_mib) @classmethod def rewrite(cls, name): """ Rewrite OID with alias if any """ if cls.cache is None: # Initialize cache cls.cache = dict((a.rewrite_mib, a.to_mib) for a in cls.objects.all()) # Lookup if "::" in name: mib, rest = name.split("::", 1) return "%s::%s" % (cls.cache.get(mib, mib), rest) return cls.cache.get(name, name) def get_json_path(self): return "%s.json" % self.rewrite_mib.replace(":", "_") def to_json(self): return to_json( { "$collection": self._meta["json_collection"], "rewrite_mib": self.rewrite_mib, "to_mib": self.to_mib, "uuid": self.uuid, }, order=["$collection", "rewrite_mib", "to_mib", "uuid"], )
class User(Document, UserMixin): """ User """ active = BooleanField(default=False) email = EmailField(unique=True) first_name = StringField(max_length=255) last_name = StringField(max_length=255) password = StringField(max_length=255) roles = ListField(ReferenceField(Role), default=[]) register_uuid = UUIDField(binary=False) username = StringField(max_length=255, unique=True) def __repr__(self): return '<User %r>' % self.email
class Font(Document): meta = { "collection": "fonts", "strict": False, "auto_create_index": False, "json_collection": "main.fonts", } name = StringField(unique=True) uuid = UUIDField(unique=True, binary=True) font_family = StringField() description = StringField(required=False) stylesheet_href = StringField(required=False) _id_cache = cachetools.TTLCache(maxsize=100, ttl=60) def __str__(self): return self.name @classmethod @cachetools.cachedmethod(operator.attrgetter("_id_cache"), lock=lambda _: id_lock) def get_by_id(cls, id: Union[str, bson.ObjectId]) -> "Font": return Font.objects.filter(id=id).first() @property def json_data(self): r = { "name": self.name, "$collection": self._meta["json_collection"], "uuid": str(self.uuid), "font_family": self.font_family, } if self.description: r["description"] = self.description if self.stylesheet_href: r["stylesheet_href"] = self.stylesheet_href return r def to_json(self): return to_json(self.json_data, order=[ "name", "$collection", "uuid", "font_family", "description" ]) def get_json_path(self): return "%s.json" % quote_safe_path(self.name)
class ConnectionRule(Document): """ Equipment vendor """ meta = { "collection": "noc.connectionrules", "strict": False, "auto_create_index": False, "indexes": [], "json_collection": "inv.connectionrules", "json_unique_fields": ["name", "uuid"], } name = StringField(unique=True) description = StringField() context = ListField(EmbeddedDocumentField(Context)) rules = ListField(EmbeddedDocumentField(Rule)) uuid = UUIDField(binary=True) def __str__(self): return self.name @property def json_data(self): return { "name": self.name, "$collection": self._meta["json_collection"], "uuid": self.uuid, "description": self.description, "context": [c.json_data for c in self.context], "rules": [c.json_data for c in self.rules], } def to_json(self): return to_json(self.json_data, order=[ "name", "$collection", "uuid", "description", "context", "rules" ]) def get_json_path(self): p = [quote_safe_path(n.strip()) for n in self.name.split("|")] return os.path.join(*p) + ".json"
class Glyph(Document): meta = { "collection": "glyphs", "strict": False, "auto_create_index": False, "json_collection": "main.glyphs", } name = StringField(unique=True) uuid = UUIDField(unique=True, binary=True) font = PlainReferenceField(Font) code = IntField() _id_cache = cachetools.TTLCache(maxsize=100, ttl=60) def __str__(self): return self.name @classmethod @cachetools.cachedmethod(operator.attrgetter("_id_cache"), lock=lambda _: id_lock) def get_by_id(cls, id: Union[str, bson.ObjectId]) -> "Glyph": return Glyph.objects.filter(id=id).first() @property def json_data(self): return { "name": self.name, "$collection": self._meta["json_collection"], "uuid": str(self.uuid), "font__name": self.font.name, "code": self.code, } def to_json(self): return to_json( self.json_data, order=["name", "$collection", "uuid", "font__name", "code"]) def get_json_path(self): p = [quote_safe_path(n.strip()) for n in self.name.split("|")] return os.path.join(*p) + ".json"