Example #1
0
 def add_log_entry(
     cls,
     actor,
     verb,
     template,
     subsystem=None,
     object=None,
     target=None,
     before=None,
     after=None,
 ):
     """LogEntry factory"""
     self = cls()
     dict = {'actor': actor, 'object': object, 'target': target}
     for k, v in dict.items():
         dict[k] = getattr(v, 'audit_logname', u'%s' % v)
     try:
         self.summary = template.format(**dict)
     except KeyError as error:
         self.summary = 'Error creating summary - see error log'
         _logger.error('KeyError when creating summary: %s', error)
     self.verb = verb
     self.actor_model = find_modelname(actor)
     self.object_model = find_modelname(object) if object else None
     self.target_model = find_modelname(target) if target else None
     self.actor_pk = actor.pk
     self.object_pk = object.pk if object else None
     self.target_pk = target.pk if target else None
     self.timestamp = utcnow()
     self.subsystem = subsystem if subsystem else None
     self.before = force_str(before)
     self.after = force_str(after)
     self.save()
     return self
Example #2
0
File: forms.py Project: Uninett/nav
 def clean_endpoints(self):
     """Convert endpoints from list to dictionary"""
     endpoints = self.cleaned_data.get('endpoints')
     return {
         x: force_str(self.available_endpoints.get(x))
         for x in endpoints
     }
Example #3
0
File: utils.py Project: Uninett/nav
def get_auditlog_entries(
    modelname=None,
    queryset=None,
    pks=None,
    limit=LATEST_N_AUDITLOG_ENTRIES,
    subsystem=None,
):
    """Get up to <limit> entries involving <modelname>

    Limit results to <pks>, <queryset> and/or <subsystem>
    """
    assert modelname or queryset, ("At least one of <modelname> or "
                                   "<queryset> must be given")
    if pks is None:
        pks = []
    if queryset is not None and queryset.exists():
        qs_pks = set(force_str(o.pk) for o in queryset)
        if qs_pks:
            if pks:
                pks = qs_pks.intersection(pks)
            else:
                pks = qs_pks
        if modelname is None:
            modelname = find_modelname(queryset[0])
    object_query = Q(object_model=modelname)
    target_query = Q(target_model=modelname)
    actor_query = Q(actor_model=modelname)
    filter_query = object_query | target_query | actor_query
    qs = LogEntry.objects.filter(filter_query)
    if pks:
        object_query = Q(object_pk__in=pks)
        target_query = Q(target_pk__in=pks)
        actor_query = Q(actor_pk__in=pks)
        filter_query = object_query | target_query | actor_query
        qs = qs.filter(filter_query)
    if subsystem:
        qs = qs.filter(subsystem=subsystem)
    entries = qs.distinct().order_by('-timestamp')[:limit]
    return entries
Example #4
0
def delete(api_client, endpoint, id):
    """Sends a delete request to endpoint"""
    return api_client.delete(ENDPOINTS[endpoint] + force_str(id) + '/')
Example #5
0
def update(api_client, endpoint, id, data):
    """Sends a patch request to endpoint with data"""
    return api_client.patch(
        ENDPOINTS[endpoint] + force_str(id) + '/', data, format='json'
    )
Example #6
0
def get(api_client, endpoint, id=None):
    endpoint = ENDPOINTS[endpoint]
    if id:
        endpoint = endpoint + force_str(id) + '/'
    return api_client.get(endpoint)
Example #7
0
# -*- coding: utf-8 -*-
from __future__ import print_function
from nav.compatibility import force_str

from datetime import datetime, timedelta
import json
import pytest
from django.urls import reverse

from nav.models.event import AlertHistory
from nav.models.fields import INFINITY
from nav.web.api.v1.views import get_endpoints


ENDPOINTS = {name: force_str(url) for name, url in get_endpoints().items()}


# Data for writable endpoints

TEST_DATA = {
    'account': {'login': '******', 'name': 'Test User', 'accountgroups': [2, 3]},
    'location': {
        'id': 'Kulsås',
        'data': {'a': 'b'},
        'parent': 'mylocation',
        'description': 'ÆØÅ descr',
    },
    'netbox': {
        "ip": "158.38.152.169",
        "roomid": "myroom",
        "organizationid": "myorg",
Example #8
0
 def get_subject(obj):
     """Return textual description of object"""
     return force_str(obj.get_subject())