diff --git a/evidence/models.py b/evidence/models.py index ece8616..dbb47a0 100644 --- a/evidence/models.py +++ b/evidence/models.py @@ -7,6 +7,25 @@ from evidence.xapian import search from user.models import User +class Annotation(models.Model): + class Type(models.IntegerChoices): + SYSTEM= 0, "System" + USER = 1, "User" + DOCUMENT = 2, "Document" + + created = models.DateTimeField(auto_now_add=True) + uuid = models.UUIDField() + owner = models.ForeignKey(User, on_delete=models.CASCADE) + type = models.SmallIntegerField(choices=Type) + key = models.CharField(max_length=STR_EXTEND_SIZE) + value = models.CharField(max_length=STR_EXTEND_SIZE) + + class Meta: + constraints = [ + models.UniqueConstraint(fields=["type", "key", "uuid"], name="unique_type_key_uuid") + ] + + class Evidence: def __init__(self, uuid): self.uuid = uuid @@ -22,7 +41,7 @@ class Evidence: self.annotations = Annotation.objects.filter( uuid=self.uuid ).order_by("created") - + def get_owner(self): if not self.annotations: self.get_annotations() @@ -51,21 +70,9 @@ class Evidence: def components(self): return self.doc.get('components', []) - -class Annotation(models.Model): - class Type(models.IntegerChoices): - SYSTEM= 0, "System" - USER = 1, "User" - DOCUMENT = 2, "Document" - - created = models.DateTimeField(auto_now_add=True) - uuid = models.UUIDField() - owner = models.ForeignKey(User, on_delete=models.CASCADE) - type = models.SmallIntegerField(choices=Type) - key = models.CharField(max_length=STR_EXTEND_SIZE) - value = models.CharField(max_length=STR_EXTEND_SIZE) - - class Meta: - constraints = [ - models.UniqueConstraint(fields=["type", "key", "uuid"], name="unique_type_key_uuid") - ] + @classmethod + def get_all(cls, user): + return Annotation.objects.filter( + owner=user, + type=Annotation.Type.SYSTEM, + ).order_by("-created").values_list("uuid", flat=True).distinct() diff --git a/evidence/parse.py b/evidence/parse.py index f303b47..614c2ec 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -10,11 +10,15 @@ from utils.constants import ALGOS class Build: - def __init__(self, evidence_json, user): + def __init__(self, evidence_json, user, check=False): self.json = evidence_json self.uuid = self.json['uuid'] self.user = user self.hid = None + self.generate_chids() + + if check: + return self.index() self.create_annotations() @@ -23,6 +27,11 @@ class Build: snap = json.dumps(self.json) index(self.uuid, snap) + def generate_chids(self): + self.algorithms = { + 'hidalgo1': self.get_hid_14(), + } + def get_hid_14(self): device = self.json['device'] manufacturer = device.get("manufacturer", '') @@ -34,22 +43,8 @@ class Build: return hashlib.sha3_256(hid.encode()).hexdigest() def create_annotations(self): - algorithms = { - 'hidalgo1': self.get_hid_14(), - } - # TODO is neccesary? - annotation = Annotation.objects.filter( - owner=self.user, - type=Annotation.Type.SYSTEM, - key='hidalgo1', - value = algorithms['hidalgo1'] - ).first() - - for k, v in algorithms.items(): - if annotation and k == annotation.key: - continue - + for k, v in self.algorithms.items(): Annotation.objects.create( uuid=self.uuid, owner=self.user, @@ -57,4 +52,3 @@ class Build: key=k, value=v ) - diff --git a/evidence/templates/evidences.html b/evidence/templates/evidences.html index 3ce592d..3df647a 100644 --- a/evidence/templates/evidences.html +++ b/evidence/templates/evidences.html @@ -8,33 +8,19 @@ -
-
- {% csrf_token %} - - - - - - - {% for snap in evicendes %} - - - - - - - + +
+
- Title -
- - - {{ snap.uuid }} {{ snap.sid }} {{ snap.version }} - - {{ snap.computer.device.manufacturer }} {{ snap.computer.device.model }} -
+ {% for ev in evidences %} + + + + {% endfor %}
+ {{ ev }} + + +
-
{% endblock %} diff --git a/evidence/views.py b/evidence/views.py index 08cb886..77d1577 100644 --- a/evidence/views.py +++ b/evidence/views.py @@ -2,13 +2,11 @@ from django.utils.translation import gettext_lazy as _ from django.views.generic.base import TemplateView from django.urls import reverse_lazy from django.views.generic.edit import ( - CreateView, - UpdateView, FormView, ) from dashboard.mixins import DashboardView -from evidence.models import Evidence, Annotation +from evidence.models import Evidence from evidence.forms import UploadForm # from django.shortcuts import render # from rest_framework import viewsets @@ -28,8 +26,8 @@ class ListEvidencesView(DashboardView, TemplateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - # evidences = Evidence.objects.filter(owner=self.request.user) - evidences = [] + evidences = Evidence.get_all(self.request.user) + context.update({ 'evidences': evidences, })