diff --git a/api/urls.py b/api/urls.py index 4fa86d6..a5b157f 100644 --- a/api/urls.py +++ b/api/urls.py @@ -7,7 +7,7 @@ app_name = 'api' urlpatterns = [ path('v1/snapshot/', views.NewSnapshotView.as_view(), name='new_snapshot'), - path('v1/annotation//', views.AddAnnotationView.as_view(), name='new_annotation'), + path('v1/property//', views.AddPropertyView.as_view(), name='new_property'), path('v1/device//', 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'), diff --git a/api/views.py b/api/views.py index 87177fb..87d1d05 100644 --- a/api/views.py +++ b/api/views.py @@ -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 @@ -94,11 +94,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=ev_uuid ).first() - if exist_annotation: + if exist_property: txt = "error: the snapshot {} exist".format(ev_uuid) logger.warning("%s", txt) return JsonResponse({'status': txt}, status=500) @@ -115,25 +115,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=ev_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", ev_uuid) + if not property: + logger.error("Error: No property for uuid: %s", ev_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 @@ -259,22 +259,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() @@ -283,13 +282,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: @@ -300,10 +299,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 ) diff --git a/dashboard/mixins.py b/dashboard/mixins.py index af01a1b..b245c96 100644 --- a/dashboard/mixins.py +++ b/dashboard/mixins.py @@ -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)) diff --git a/dashboard/views.py b/dashboard/views.py index 2ec12a0..0689271 100644 --- a/dashboard/views.py +++ b/dashboard/views.py @@ -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 = json.loads(xp.document.get_data()) if snap.get("credentialSubject"): uuid = snap["credentialSubject"]["uuid"] diff --git a/device/forms.py b/device/forms.py index 6c4d6e2..b69bc9e 100644 --- a/device/forms.py +++ b/device/forms.py @@ -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 diff --git a/device/models.py b/device/models.py index 7ca2a1f..75d28db 100644 --- a/device/models.py +++ b/device/models.py @@ -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 @@ -30,7 +30,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 = [] @@ -39,69 +39,68 @@ 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): if self.last_evidence: @@ -111,12 +110,12 @@ class Device: self.last_evidence = Evidence(self.uuid) return - 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) - self.uuid = annotation.uuid + prop = properties.first() + self.last_evidence = Evidence(prop.uuid) + self.uuid = prop.uuid def is_eraseserver(self): if not self.uuids: @@ -124,13 +123,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 @@ -175,7 +174,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)) diff --git a/device/views.py b/device/views.py index 93d02b9..8afc4aa 100644 --- a/device/views.py +++ b/device/views.py @@ -10,7 +10,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 @@ -70,7 +70,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') @@ -88,7 +88,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'] @@ -180,7 +180,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): @@ -213,21 +213,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 diff --git a/evidence/forms.py b/evidence/forms.py index 8be2887..a9d631e 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -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) snap = Build(file_json, None, check=True) - exist_annotation = Annotation.objects.filter( + exists_property = SystemProperty.objects.filter( uuid=snap.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, diff --git a/evidence/models.py b/evidence/models.py index 1beb793..51c7e81 100644 --- a/evidence/models.py +++ b/evidence/models.py @@ -67,22 +67,22 @@ class Evidence: self.created = None self.dmi = None self.inxi = 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 @@ -144,7 +144,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(): @@ -214,9 +214,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() diff --git a/evidence/parse.py b/evidence/parse.py index db2d32d..f1e0d10 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -5,7 +5,7 @@ import logging from dmidecode import DMIParse from evidence.parse_details import ParseSnapshot -from evidence.models import Annotation +from evidence.models import SystemProperty from evidence.xapian import index from evidence.parse_details import get_inxi_key, get_inxi from django.conf import settings @@ -51,7 +51,7 @@ class Build: return self.index() - self.create_annotations() + self.create_properties() if settings.DPP: self.register_device_dlt() @@ -123,24 +123,24 @@ class Build: return doc - 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 ) diff --git a/evidence/urls.py b/evidence/urls.py index 9a4e2a2..501e867 100644 --- a/evidence/urls.py +++ b/evidence/urls.py @@ -20,5 +20,5 @@ urlpatterns = [ path("", views.EvidenceView.as_view(), name="details"), path("/eraseserver", views.EraseServerView.as_view(), name="erase_server"), path("/download", views.DownloadEvidenceView.as_view(), name="download"), - path('annotation//del', views.AnnotationDeleteView.as_view(), name='delete_annotation'), + path('user_property//del', views.UserPropertyDeleteView.as_view(), name='delete_user_property'), ] diff --git a/evidence/views.py b/evidence/views.py index c2868b2..ddec292 100644 --- a/evidence/views.py +++ b/evidence/views.py @@ -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): diff --git a/utils/device.py b/utils/device.py index beaa4b8..6f246c8 100644 --- a/utils/device.py +++ b/utils/device.py @@ -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):