WIP: rework/properties #31

Draft
rskthomas wants to merge 19 commits from rework/properties into main
13 changed files with 104 additions and 107 deletions
Showing only changes of commit b7d7b9041d - Show all commits

View File

@ -7,7 +7,7 @@ app_name = 'api'
urlpatterns = [
path('v1/snapshot/', views.NewSnapshotView.as_view(), name='new_snapshot'),
path('v1/annotation/<str:pk>/', views.AddAnnotationView.as_view(), name='new_annotation'),
path('v1/property/<str:pk>/', views.AddPropertyView.as_view(), name='new_property'),
path('v1/device/<str:pk>/', views.DetailsDeviceView.as_view(), name='device'),
path('v1/tokens/', views.TokenView.as_view(), name='tokens'),
path('v1/tokens/new', views.TokenNewView.as_view(), name='new_token'),

View File

@ -21,7 +21,7 @@ from django.views.generic.edit import (
from utils.save_snapshots import move_json, save_in_disk
from django.views.generic.edit import View
from dashboard.mixins import DashboardView
from evidence.models import Annotation
from evidence.models import SystemProperty, UserProperty, Property
from evidence.parse_details import ParseSnapshot
from evidence.parse import Build
from device.models import Device
@ -90,11 +90,11 @@ class NewSnapshotView(ApiMixing):
logger.error("%s", txt)
return JsonResponse({'status': txt}, status=500)
exist_annotation = Annotation.objects.filter(
exist_property = SystemProperty.objects.filter(
uuid=data['uuid']
).first()
if exist_annotation:
if exist_property:
txt = "error: the snapshot {} exist".format(data['uuid'])
logger.warning("%s", txt)
return JsonResponse({'status': txt}, status=500)
@ -111,25 +111,25 @@ class NewSnapshotView(ApiMixing):
text = "fail: It is not possible to parse snapshot"
return JsonResponse({'status': text}, status=500)
annotation = Annotation.objects.filter(
property = SystemProperty.objects.filter(
uuid=data['uuid'],
type=Annotation.Type.SYSTEM,
type=Property.Type.SYSTEM,
# TODO this is hardcoded, it should select the user preferred algorithm
key="hidalgo1",
owner=self.tk.owner.institution
).first()
if not annotation:
logger.error("Error: No annotation for uuid: %s", data["uuid"])
if not property:
logger.error("Error: No property for uuid: %s", data["uuid"])
return JsonResponse({'status': 'fail'}, status=500)
url_args = reverse_lazy("device:details", args=(annotation.value,))
url_args = reverse_lazy("device:details", args=(property.value,))
url = request.build_absolute_uri(url_args)
response = {
"status": "success",
"dhid": annotation.value[:6].upper(),
"dhid": property.value[:6].upper(),
"url": url,
# TODO replace with public_url when available
"public_url": url
@ -255,22 +255,21 @@ class DetailsDeviceView(ApiMixing):
"components": snapshot.get("components"),
})
uuids = Annotation.objects.filter(
uuids = SystemProperty.objects.filter(
owner=self.tk.owner.institution,
value=self.pk
).values("uuid")
annotations = Annotation.objects.filter(
properties = UserProperty.objects.filter(
uuid__in=uuids,
owner=self.tk.owner.institution,
type = Annotation.Type.USER
).values_list("key", "value")
data.update({"annotations": list(annotations)})
data.update({"properties": list(properties)})
return data
class AddAnnotationView(ApiMixing):
class AddPropertyView(ApiMixing):
def post(self, request, *args, **kwargs):
response = self.auth()
@ -279,13 +278,13 @@ class AddAnnotationView(ApiMixing):
self.pk = kwargs['pk']
institution = self.tk.owner.institution
self.annotation = Annotation.objects.filter(
self.property = SystemProperty.objects.filter(
owner=institution,
value=self.pk,
type=Annotation.Type.SYSTEM
type=Property.Type.SYSTEM
).first()
if not self.annotation:
if not self.property:
return JsonResponse({}, status=404)
try:
@ -296,10 +295,9 @@ class AddAnnotationView(ApiMixing):
logger.error("Invalid Snapshot of user %s", self.tk.owner)
return JsonResponse({'error': 'Invalid JSON'}, status=500)
Annotation.objects.create(
uuid=self.annotation.uuid,
UserProperty.objects.create(
uuid=self.property.uuid,
owner=self.tk.owner.institution,
type = Annotation.Type.USER,
key = key,
value = value
)

View File

@ -6,7 +6,7 @@ from django.core.exceptions import PermissionDenied
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.base import TemplateView
from device.models import Device
from evidence.models import Annotation
from evidence.models import SystemProperty
from lot.models import LotTag
@ -49,7 +49,7 @@ class DashboardView(LoginRequiredMixin):
dev_ids = self.request.session.pop("devices", [])
self._devices = []
for x in Annotation.objects.filter(value__in=dev_ids).filter(
for x in SystemProperty.objects.filter(value__in=dev_ids).filter(
owner=self.request.user.institution
).distinct():
self._devices.append(Device(id=x.value))

View File

@ -6,7 +6,7 @@ from django.shortcuts import Http404
from django.db.models import Q
from dashboard.mixins import InventaryMixin, DetailsMixin
from evidence.models import Annotation
from evidence.models import Property
from evidence.xapian import search
from device.models import Device
from lot.models import Lot
@ -74,7 +74,7 @@ class SearchView(InventaryMixin):
for x in matches:
# devices.append(self.get_annotations(x))
dev = self.get_annotations(x)
dev = self.get_property(x)
if dev.id not in dev_id:
devices.append(dev)
dev_id.append(dev.id)
@ -83,7 +83,7 @@ class SearchView(InventaryMixin):
# TODO fix of pagination, the count is not correct
return devices, count
def get_annotations(self, xp):
def get_properties(self, xp):
snap = xp.document.get_data()
uuid = json.loads(snap).get('uuid')
return Device.get_annotation_from_uuid(uuid, self.request.user.institution)

View File

@ -1,5 +1,5 @@
from django import forms
from utils.device import create_annotation, create_doc, create_index
from utils.device import create_property, create_doc, create_index
from utils.save_snapshots import move_json, save_in_disk

View File

@ -1,7 +1,7 @@
from django.db import models, connection
from utils.constants import ALGOS
from evidence.models import Annotation, Evidence
from evidence.models import SystemProperty, UserProperty, Property, Evidence
from lot.models import DeviceLot
@ -29,7 +29,7 @@ class Device:
self.shortid = self.pk[:6].upper()
self.algorithm = None
self.owner = None
self.annotations = []
self.properties = []
self.hids = []
self.uuids = []
self.evidences = []
@ -38,76 +38,75 @@ class Device:
self.get_last_evidence()
def initial(self):
self.get_annotations()
self.get_properties()
self.get_uuids()
self.get_hids()
self.get_evidences()
self.get_lots()
def get_annotations(self):
if self.annotations:
return self.annotations
def get_properties(self):
if self.properties:
return self.properties
self.annotations = Annotation.objects.filter(
type=Annotation.Type.SYSTEM,
self.properties = SystemProperty.objects.filter(
type=Property.Type.SYSTEM,
value=self.id
).order_by("-created")
if self.annotations.count():
self.algorithm = self.annotations[0].key
self.owner = self.annotations[0].owner
if self.properties.count():
self.algorithm = self.properties[0].key
self.owner = self.properties[0].owner
return self.annotations
return self.properties
def get_user_annotations(self):
def get_user_properties(self):
if not self.uuids:
self.get_uuids()
annotations = Annotation.objects.filter(
user_properties = UserProperty.objects.filter(
uuid__in=self.uuids,
owner=self.owner,
type=Annotation.Type.USER
)
return annotations
return user_properties
def get_user_documents(self):
if not self.uuids:
self.get_uuids()
annotations = Annotation.objects.filter(
properties = SystemProperty.objects.filter(
uuid__in=self.uuids,
owner=self.owner,
type=Annotation.Type.DOCUMENT
type=Property.Type.DOCUMENT
)
return annotations
return properties
def get_uuids(self):
for a in self.get_annotations():
for a in self.get_properties():
if a.uuid not in self.uuids:
self.uuids.append(a.uuid)
def get_hids(self):
annotations = self.get_annotations()
properties = self.get_properties()
algos = list(ALGOS.keys())
algos.append('CUSTOM_ID')
self.hids = list(set(annotations.filter(
type=Annotation.Type.SYSTEM,
self.hids = list(set(properties.filter(
type=Property.Type.SYSTEM,
key__in=algos,
).values_list("value", flat=True)))
def get_evidences(self):
def get_properties(self):
if not self.uuids:
self.get_uuids()
self.evidences = [Evidence(u) for u in self.uuids]
self.properties = [SystemProperty(u) for u in self.uuids]
def get_last_evidence(self):
annotations = self.get_annotations()
if not annotations.count():
properties = self.get_properties()
if not properties.count():
return
annotation = annotations.first()
self.last_evidence = Evidence(annotation.uuid)
property = property.first()
self.last_evidence = Evidence(property.uuid)
def is_eraseserver(self):
if not self.uuids:
@ -115,13 +114,13 @@ class Device:
if not self.uuids:
return False
annotation = Annotation.objects.filter(
property = SystemProperty.objects.filter(
uuid__in=self.uuids,
owner=self.owner,
type=Annotation.Type.ERASE_SERVER
type=Property.Type.ERASE_SERVER
).first()
if annotation:
if property:
return True
return False
@ -164,7 +163,7 @@ class Device:
row_num = 1
""".format(
institution=institution.id,
type=Annotation.Type.SYSTEM,
type=Property.Type.SYSTEM,
)
if limit:
sql += " limit {} offset {}".format(int(limit), int(offset))

View File

@ -12,7 +12,7 @@ from django.views.generic.edit import (
)
from django.views.generic.base import TemplateView
from dashboard.mixins import DashboardView, Http403
from evidence.models import Annotation
from evidence.models import UserProperty, SystemProperty
from lot.models import LotTag
from device.models import Device
from device.forms import DeviceFormSet
@ -69,7 +69,7 @@ class EditDeviceView(DashboardView, UpdateView):
title = _("Update Device")
breadcrumb = "Device / Update Device"
success_url = reverse_lazy('dashboard:unassigned_devices')
model = Annotation
model = SystemProperty
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
@ -87,7 +87,7 @@ class DetailsView(DashboardView, TemplateView):
template_name = "details.html"
title = _("Device")
breadcrumb = "Device / Details"
model = Annotation
model = SystemProperty
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
@ -172,7 +172,7 @@ class AddUserPropertyView(DashboardView, CreateView):
title = _("New User Property")
breadcrumb = "Device / New Property"
success_url = reverse_lazy('dashboard:unassigned_devices')
model = Annotation
model = UserProperty
fields = ("key", "value")
def form_valid(self, form):
@ -205,21 +205,21 @@ class AddDocumentView(DashboardView, CreateView):
title = _("New Document")
breadcrumb = "Device / New document"
success_url = reverse_lazy('dashboard:unassigned_devices')
model = Annotation
model = SystemProperty
fields = ("key", "value")
def form_valid(self, form):
form.instance.owner = self.request.user.institution
form.instance.user = self.request.user
form.instance.uuid = self.annotation.uuid
form.instance.type = Annotation.Type.DOCUMENT
form.instance.type = Property.Type.DOCUMENT
response = super().form_valid(form)
return response
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
institution = self.request.user.institution
self.annotation = Annotation.objects.filter(
self.annotation = SystemProperty.objects.filter(
owner=institution,
value=pk,
type=Annotation.Type.SYSTEM

View File

@ -4,11 +4,11 @@ import pandas as pd
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from utils.device import create_annotation, create_doc, create_index
from utils.device import create_property, create_doc, create_index
from utils.forms import MultipleFileField
from device.models import Device
from evidence.parse import Build
from evidence.models import Annotation
from evidence.models import SystemProperty
from utils.save_snapshots import move_json, save_in_disk
@ -30,11 +30,11 @@ class UploadForm(forms.Form):
try:
file_json = json.loads(file_data)
Build(file_json, None, check=True)
exist_annotation = Annotation.objects.filter(
exists_property = SystemProperty.objects.filter(
uuid=file_json['uuid']
).first()
if exist_annotation:
if exists_property:
raise ValidationError(
_("The snapshot already exists"),
code="duplicate_snapshot",
@ -86,7 +86,7 @@ class UserTagForm(forms.Form):
if not data:
return False
self.tag = data
self.instance = Annotation.objects.filter(
self.instance = SystemProperty.objects.filter(
uuid=self.uuid,
type=Annotation.Type.SYSTEM,
key='CUSTOM_ID',
@ -106,9 +106,9 @@ class UserTagForm(forms.Form):
self.instance.save()
return
Annotation.objects.create(
SystemProperty.objects.create(
uuid=self.uuid,
type=Annotation.Type.SYSTEM,
type=Property.Type.SYSTEM,
key='CUSTOM_ID',
value=self.tag,
owner=self.user.institution,

View File

@ -65,22 +65,22 @@ class Evidence:
self.doc = None
self.created = None
self.dmi = None
self.annotations = []
self.properties = []
self.components = []
self.default = "n/a"
self.get_owner()
self.get_time()
def get_annotations(self):
self.annotations = Annotation.objects.filter(
def get_properties(self):
self.properties = SystemProperty.objects.filter(
uuid=self.uuid
).order_by("created")
def get_owner(self):
if not self.annotations:
self.get_annotations()
a = self.annotations.first()
if not self.properties:
self.get_properties()
a = self.properties.first()
if a:
self.owner = a.owner
@ -106,7 +106,7 @@ class Evidence:
self.created = self.doc.get("endTime")
if not self.created:
self.created = self.annotations.last().created
self.created = self.properties.last().created
def get_components(self):
if self.is_legacy():
@ -157,9 +157,9 @@ class Evidence:
@classmethod
def get_all(cls, user):
return Annotation.objects.filter(
return SystemProperty.objects.filter(
owner=user.institution,
type=Annotation.Type.SYSTEM,
type=SystemProperty.Type.SYSTEM,
key="hidalgo1",
).order_by("-created").values_list("uuid", "created").distinct()

View File

@ -6,7 +6,7 @@ from dmidecode import DMIParse
from json_repair import repair_json
from evidence.parse_details import get_lshw_child
from evidence.models import Annotation
from evidence.models import SystemProperty
from evidence.xapian import index
from utils.constants import CHASSIS_DH
@ -46,7 +46,7 @@ class Build:
return
self.index()
self.create_annotations()
self.create_properties()
def index(self):
snap = json.dumps(self.json)
@ -72,24 +72,24 @@ class Build:
return hashlib.sha3_256(hid.encode()).hexdigest()
def create_annotations(self):
annotation = Annotation.objects.filter(
def create_properties(self):
property = SystemProperty.objects.filter(
uuid=self.uuid,
owner=self.user.institution,
type=Annotation.Type.SYSTEM,
type=Property.Type.SYSTEM,
)
if annotation:
txt = "Warning: Snapshot %s already registered (annotation exists)"
if property:
txt = "Warning: Snapshot %s already registered (property exists)"
logger.warning(txt, self.uuid)
return
for k, v in self.algorithms.items():
Annotation.objects.create(
SystemProperty.objects.create(
uuid=self.uuid,
owner=self.user.institution,
user=self.user,
type=Annotation.Type.SYSTEM,
type=Property.Type.SYSTEM,
key=k,
value=v
)

View File

@ -20,5 +20,5 @@ urlpatterns = [
path("<uuid:pk>", views.EvidenceView.as_view(), name="details"),
path("<uuid:pk>/eraseserver", views.EraseServerView.as_view(), name="erase_server"),
path("<uuid:pk>/download", views.DownloadEvidenceView.as_view(), name="download"),
path('annotation/<int:pk>/del', views.AnnotationDeleteView.as_view(), name='delete_annotation'),
path('user_property/<int:pk>/del', views.UserPropertyDeleteView.as_view(), name='delete_user_property'),
]

View File

@ -13,7 +13,7 @@ from django.views.generic.edit import (
)
from dashboard.mixins import DashboardView, Http403
from evidence.models import Evidence, Annotation
from evidence.models import Property, SystemProperty, UserProperty
from evidence.forms import (
UploadForm,
UserTagForm,
@ -95,7 +95,7 @@ class EvidenceView(DashboardView, FormView):
if self.object.owner != self.request.user.institution:
raise Http403
self.object.get_annotations()
self.object.get_properties()
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
@ -141,8 +141,8 @@ class DownloadEvidenceView(DashboardView, TemplateView):
return response
class AnnotationDeleteView(DashboardView, DeleteView):
model = Annotation
class UserPropertyDeleteView(DashboardView, DeleteView):
model = UserProperty
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
@ -182,7 +182,7 @@ class EraseServerView(DashboardView, FormView):
if self.object.owner != self.request.user.institution:
raise Http403
self.object.get_annotations()
self.object.get_properties()
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):

View File

@ -6,7 +6,7 @@ import logging
from django.core.exceptions import ValidationError
from evidence.xapian import index
from evidence.models import Annotation
from evidence.models import SystemProperty, Property
from device.models import Device
@ -68,7 +68,7 @@ def create_doc(data):
return doc
def create_annotation(doc, user, commit=False):
def create_property(doc, user, commit=False):
if not doc or not doc.get('uuid') or not doc.get("CUSTOMER_ID"):
return []
@ -81,20 +81,20 @@ def create_annotation(doc, user, commit=False):
'value': doc['CUSTOMER_ID'],
}
if commit:
annotation = Annotation.objects.filter(
property = SystemProperty.objects.filter(
uuid=doc["uuid"],
owner=user.institution,
type=Annotation.Type.SYSTEM,
type=Property.Type.SYSTEM,
)
if annotation:
txt = "Warning: Snapshot %s already registered (annotation exists)"
if property:
txt = "Warning: Snapshot %s already registered (system property exists)"
logger.warning(txt, doc["uuid"])
return annotation
return property
return Annotation.objects.create(**data)
return SystemProperty.objects.create(**data)
return Annotation(**data)
return SystemProperty(**data)
def create_index(doc, user):