def queryset(self, request, query=None): """ Filter records for lookup """ qs = super().queryset(request) if not request.user.is_superuser: user_ads = UserAccess.get_domains(request.user) qs = qs.filter(Q(administrative_domain=[]) | Q(administrative_domain__in=user_ads)) if query and self.query_fields: q = qs.filter(self.get_Q(request, query)) if q: return q sq = ManagedObject.get_search_Q(query) if sq: obj = ManagedObject.objects.filter(sq) else: obj = ManagedObject.objects.filter(name__contains=query) if obj: mos = obj.values_list("id", flat=True) ao = AffectedObjects.objects.filter(affected_objects__object__in=mos).values_list( "maintenance" ) return ao return qs.filter(type=None) else: return qs
def api_list(self, request): q = dict((str(k), v[0] if len(v) == 1 else v) for k, v in request.GET.lists()) # find mac request select max(ts), managed_object, interface, vlan from mac # where like(MACNumToString(mac), 'A0:AB:1B%') group by managed_object, interface, vlan; query = q.get("__query") start = q.get("__start") limit = q.get("__limit") # page = q.get("__page") out = [] if not query: return self.response(out, status=self.OK) try: mac = int(MAC(MACAddressParameter(accept_bin=False).clean(query))) out = self.api_macdb({"mac": mac}, limit=limit, offset=start) except ValueError: if self.mac_search_re.match(query): out = self.api_macdb({"mac__like": "%s%%" % str(query.upper())}, limit=limit, offset=start) elif self.mac_search_re_inv.match(query): out = self.api_macdb({"mac__like": "%%%s" % str(query.upper())}, limit=limit, offset=start) else: # Try MO search # @todo ManagedObject search self.logger.debug("MACDB ManagedObject search") mo_q = ManagedObject.get_search_Q(query) if not mo_q: mo_q = d_Q(name__contains=query) mos = [mo.bi_id for mo in ManagedObject.objects.filter(mo_q)[:2]] if mos: out = self.api_macdb({"managed_object__in": mos}, limit=limit, offset=start) # out = self.api_get_maclog(request, mac) return self.response(out, status=self.OK)
def search(cls, handler, query): q = Q(name__icontains=query) sq = ManagedObject.get_search_Q(query) if sq: q |= sq if not handler.current_user.is_superuser: q &= UserAccess.Q(handler.current_user) r = [] for mo in ManagedObject.objects.filter(q): r += [{ "scope": "managedobject", "id": mo.id, "label": "%s (%s) [%s]" % (mo.name, mo.address, mo.platform), }] return r
def queryset(self, request, query=None): """ Filter records for lookup """ self.logger.info("Queryset %s" % query) if self.managed_filter: q = d_Q(is_managed=True) else: q = d_Q() if not request.user.is_superuser: q &= UserAccess.Q(request.user) if query: sq = ManagedObject.get_search_Q(query) if sq: q &= sq else: q &= d_Q(name__contains=query) return self.model.objects.filter(q)
def queryset(self, request, query=None): """ Filter records for lookup """ if query and self.query_fields: q = self.model.objects.filter(self.get_Q(request, query)) if q: return q sq = ManagedObject.get_search_Q(query) if sq: obj = ManagedObject.objects.filter(sq) else: obj = ManagedObject.objects.filter(name__contains=query) if obj: mos = obj.values_list("id", flat=True) return Maintenance.objects.filter(affected_objects__object__in=mos) else: return self.model.objects.all()
def get_data(self, request, repo="config", days=1, adm_domain=None, managed_object=None, **kwargs): baseline = datetime.datetime.now() - datetime.timedelta(days=days) coll = get_db()["noc.gridvcs.%s.files" % repo].with_options( read_preference=ReadPreference.SECONDARY_PREFERRED) pipeline = [ { "$match": { "ts": { "$gte": baseline } } }, { "$group": { "_id": "$object", "last_ts": { "$max": "$ts" } } }, { "$sort": { "_id": 1 } }, ] if repo == "config": objects = ManagedObject.objects.filter() if not request.user.is_superuser: objects = objects.filter(administrative_domain__in=UserAccess. get_domains(request.user)) if adm_domain: adm_domain = AdministrativeDomain.get_nested_ids(adm_domain) objects = objects.filter(administrative_domain__in=adm_domain) if managed_object: mo_q = ManagedObject.get_search_Q(managed_object) if not mo_q: objects = objects.filter(name__contains=managed_object) else: objects = objects.filter(mo_q) pipeline = [{ "$match": { "object": { "$in": list(objects.values_list("id", flat=True)) } } }] + pipeline # Perform query data = list(coll.aggregate(pipeline)) # Resolve names result = [] if data: seen_ids = list(set(d["_id"] for d in data)) n_map = {} if repo == "config": n_map = { x[0]: x[1:] for x in ManagedObject.objects.filter( id__in=list(seen_ids)).values_list( "id", "name", "address", "profile") } elif repo == "dns": n_map = { x[0]: x[1:] for x in DNSZone.objects.filter( id__in=list(seen_ids)).values_list( "id", "name", "address", "profile") } for d in data: name, profile = n_map.get(d["_id"], ("-", None)) result += [(d["_id"], name, Profile.get_by_id(profile) if profile else "-", d["last_ts"])] return self.from_dataset( title="%s: %s in %d days" % (self.title, repo, days), columns=[ "ID", "Name", "Address", "Profile", TableColumn(_("Last Changed"), format="datetime"), ], data=result, enumerate=True, )
def get_Q(self, request, query): q = super(ManagedObjectApplication, self).get_Q(request, query) sq = ManagedObject.get_search_Q(query) if sq: q |= sq return q