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 0de3e5a..be689e6 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 @@ -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 ) 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/templates/unassigned_devices.html b/dashboard/templates/unassigned_devices.html index 369edd2..a8fefd7 100644 --- a/dashboard/templates/unassigned_devices.html +++ b/dashboard/templates/unassigned_devices.html @@ -20,9 +20,9 @@ {% trans 'Exports' %} {% if lot %} - + - {% trans 'Annotations' %} + {% trans 'properties' %} {% endif %} diff --git a/dashboard/views.py b/dashboard/views.py index 8d91732..cb36078 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, SystemProperty 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_properties(x) if dev.id not in dev_id: devices.append(dev) dev_id.append(dev.id) @@ -83,10 +83,10 @@ 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) + return Device.get_properties_from_uuid(uuid, self.request.user.institution) def search_hids(self, query, offset, limit): qry = Q() @@ -95,8 +95,8 @@ class SearchView(InventaryMixin): if i: qry |= Q(value__startswith=i) - chids = Annotation.objects.filter( - type=Annotation.Type.SYSTEM, + chids = SystemProperty.objects.filter( + type=Property.Type.SYSTEM, owner=self.request.user.institution ).filter( qry diff --git a/device/forms.py b/device/forms.py index 6c4d6e2..1fd1f0b 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 @@ -59,7 +59,7 @@ class BaseDeviceFormSet(forms.BaseFormSet): path_name = save_in_disk(doc, self.user.institution.name, place="placeholder") create_index(doc, self.user) - create_annotation(doc, user, commit=commit) + create_property(doc, user, commit=commit) move_json(path_name, self.user.institution.name, place="placeholder") return doc diff --git a/device/models.py b/device/models.py index 4e0778f..717af74 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 @@ -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,61 +38,60 @@ 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))) @@ -103,11 +102,12 @@ class Device: self.evidences = [Evidence(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 = properties.first() + + self.last_evidence = Evidence(property.uuid) def is_eraseserver(self): if not self.uuids: @@ -115,13 +115,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 @@ -136,7 +136,7 @@ class Device: def get_unassigned(cls, institution, offset=0, limit=None): sql = """ - WITH RankedAnnotations AS ( + WITH RankedProperties AS ( SELECT t1.value, t1.key, @@ -149,8 +149,8 @@ class Device: ELSE 3 END, t1.created DESC - ) AS row_num - FROM evidence_annotation AS t1 + ) AS row_num + FROM evidence_systemproperty AS t1 LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id WHERE t2.device_id IS NULL AND t1.owner_id = {institution} @@ -159,24 +159,24 @@ class Device: SELECT DISTINCT value FROM - RankedAnnotations + RankedProperties WHERE 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)) sql += ";" - annotations = [] + properties = [] with connection.cursor() as cursor: cursor.execute(sql) - annotations = cursor.fetchall() + properties = cursor.fetchall() - devices = [cls(id=x[0]) for x in annotations] + devices = [cls(id=x[0]) for x in properties] count = cls.get_unassigned_count(institution) return devices, count @@ -184,7 +184,7 @@ class Device: def get_unassigned_count(cls, institution): sql = """ - WITH RankedAnnotations AS ( + WITH RankedProperties AS ( SELECT t1.value, t1.key, @@ -198,30 +198,30 @@ class Device: END, t1.created DESC ) AS row_num - FROM evidence_annotation AS t1 + FROM evidence_systemproperty AS t1 LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id WHERE t2.device_id IS NULL AND t1.owner_id = {institution} - AND t1.type = {type} + And t1.type = '{type}' ) SELECT COUNT(DISTINCT value) FROM - RankedAnnotations + RankedProperties WHERE row_num = 1 """.format( institution=institution.id, - type=Annotation.Type.SYSTEM, + type=Property.Type.SYSTEM, ) with connection.cursor() as cursor: cursor.execute(sql) return cursor.fetchall()[0][0] @classmethod - def get_annotation_from_uuid(cls, uuid, institution): + def get_properties_from_uuid(cls, uuid, institution): sql = """ - WITH RankedAnnotations AS ( + WITH RankedProperties AS ( SELECT t1.value, t1.key, @@ -235,31 +235,31 @@ class Device: END, t1.created DESC ) AS row_num - FROM evidence_annotation AS t1 + FROM evidence_systemproperty AS t1 LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id WHERE t2.device_id IS NULL AND t1.owner_id = {institution} - AND t1.type = {type} + AND t1.type = '{type}' AND t1.uuid = '{uuid}' ) SELECT DISTINCT value FROM - RankedAnnotations + RankedProperties WHERE row_num = 1; """.format( uuid=uuid.replace("-", ""), institution=institution.id, - type=Annotation.Type.SYSTEM, + type=Property.Type.SYSTEM, ) - annotations = [] + properties = [] with connection.cursor() as cursor: cursor.execute(sql) - annotations = cursor.fetchall() + properties = cursor.fetchall() - return cls(id=annotations[0][0]) + return cls(id=properties[0][0]) @property def is_websnapshot(self): diff --git a/device/templates/details.html b/device/templates/details.html index 137a4f6..82c52ee 100644 --- a/device/templates/details.html +++ b/device/templates/details.html @@ -15,7 +15,7 @@ {% trans 'General details' %}