Compare commits

...

109 commits

Author SHA1 Message Date
pedro 88fd31d111 init end-to-end tests with playwright 2025-01-22 16:08:13 +01:00
pedro 37071049b9 docker: on demo situation create_default_states 2025-01-22 10:33:31 +01:00
Cayo Puigdefabregas 445b7e9213 fix base template 2025-01-21 14:27:37 +01:00
Cayo Puigdefabregas f5123e89e4 split long line 2025-01-21 14:27:37 +01:00
Cayo Puigdefabregas 95bc8c26fb add version 2025-01-21 14:27:37 +01:00
Cayo Puigdefabregas 953f19bf96 replace waring for yellow in details 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 3bd1b8ae7c deleting obsolete if statement 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki a0172d75de renamed property variable to prop 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 703dce8bdb added missing components tab 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki ee80e20f52 added logging for evidence tag changes 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 9571cf74db text size adjustment and displaying none states 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 9bacf1b579 more contrast on save note button 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 8487598db4 states and notes view refactoring 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 1652670518 userproperties views refactoring 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 61b05949d1 better bootstrap tables 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 3f227d3304 view changes 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 70d53ac393 better representiation of delete/edit notes 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki a3d8cecb90 edit update cannot be blank 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 203be7b9ed adding remove button for notes 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 1170b27054 notes now can be updated 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki fcd8816ee6 deleted undo state view 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki d41707d92c added a sidebar notes display 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 77488dd75d changes to state defiinitions list 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki ca3a146e4c better success message and removed devicelog var 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki c679922347 normalized deviceLog messages 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 42caf63d98 command for adding default states 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki c9ed0f5f45 erased old logger 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki fd13d6c021 minor cosmetic changes 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 34fd152c27 state definition list changes and disabled logging 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 057ddce58d default value for no state 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 717c8eecee adding orm migrations 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 2b82b44756 log list now shows log table 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 4431b74da9 added logging for states, user properties and notes 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 37fc6bd634 simpler state change and action input 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 501a23d646 current state table erased and spacing fix 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki e08c226c44 deleted unique constraint on userproperty 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki a61ead0aaa notes and log models added 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 3777f74be8 device tag bugfix 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 118c433656 lotproperties delete and update added 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 4d64d78b20 added action migration 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki e4ed0b04c9 now check for state on delete modal 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 38fa30ae41 cosmetic changes to statesdefinitions list 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki fca81f0be7 change state delete to undo last state 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki ae5a86cce9 help icon added and new icon on add button 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki b3ea4e0745 minor changes to states view 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 7742da0747 statedefinitions update and delete popup changes 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 88b5354262 changes to state definitions list 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki b005c0fbb3 statedefinitions edit popup added and url changes 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki cf50a55bce state button rework and warning if same state 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki d6e2be116e current state helper function added 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 237708691b Device-details html modularized into several files 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki b9a0bd8b5c statedefinitions list updated 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 60b6659fc7 updated logging for states 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 934dc2997d statedefinitions delete now uses correct id 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 877b9b17ac logging for statedefinitions 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 973ad79c5e better delete modal 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki b86a7dfe24 erase_server type now userproperty 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 081e4896a4 deleted obsolete field type for sysproperties 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 3ed4038be8 updated views for new model structure 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki c9bddb9ebf more property model refactoring 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki c926c21c3e property models refactor 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 5d37b224a9 added logging for user property creation 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki ac6981f879 made log var a env variable 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki bde5d6649b added loggin for state actions 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 2a1a4da4af added state delete 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 0efb976bd9 changed tab to log and added logging 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki cc2c360b01 changed state visual and ordering 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki b5cf4616cb minor fix for correct tab handling 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 22d770e146 added current state to device details 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 925147cf48 added view and url for new action 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 64350b5db6 state institution now nullable 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki fb1e4fc9a2 condition checking and renaming 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 2f594e503f stylish new popup for state change 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 34e19fa833 added Sortable js dependency for tables 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki be8cddb082 added form for state definition order update 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 161484d005 minor cosmetic changes 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 1142c94a91 changed delete button to icon 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki b7078cbd3c send button hidden until changes are made 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 420ba37e35 sortable list now updates order of definitions 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 089eb00e8a added Sortable js for state definitions list 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki e4875fe68c added model level constraints 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki a9bca4ffa7 modals for state definition deletion 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 1f919ebaee delete view added and some refactorig 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 1f5812a7d0 added add state definition view 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 1a5e2bd602 changed constrain on model 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki c7f1fd2925 fixed models confusion 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki c516c92c83 added model constraints 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki f345192a24 added admin state definition panel 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki e99b73781e initial orm state models 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 7fc7588755 centered popup modals 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki d8b9ffcd5c disabled lotPoperty field 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 5ab13051af added logging for device operations 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 91251ff2de renaming lotAnnotations to Lotproperty 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki d933c57bff renaming of annotation to property 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 60e40a2b86 change edit view to modal popup 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 53934b3f86 added userproperty update view 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 2e89a7a797 added user_property 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 3c96b16453 model constraints changed and moved url to /device 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki ac029a955d fixed search and moved delete user property class 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 8515f4d838 renaming to new Property tables 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki b20e0e1cda details view changed to now use properties 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 74ff621b1d fixed user_properties list not working 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 43a465df27 renaming to property 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 6a39905e06 changed imports 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 9a1d09307f fixed self inflicted recursion 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 2f1b808f98 renaming annotation to variable 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 84ddcce936 variables and function semantic renaming 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 13469d8729 split into system and user properties 2025-01-21 14:27:37 +01:00
Thomas Nahuel Rusiecki 8494898796 annotations renaming on views 2025-01-21 14:27:37 +01:00
58 changed files with 2404 additions and 537 deletions

16
action/forms.py Normal file
View file

@ -0,0 +1,16 @@
from django import forms
from .models import State
class ChangeStateForm(forms.Form):
previous_state = forms.CharField(widget=forms.HiddenInput())
snapshot_uuid = forms.UUIDField(widget=forms.HiddenInput())
new_state = forms.CharField(widget=forms.HiddenInput())
class AddNoteForm(forms.Form):
snapshot_uuid = forms.UUIDField(widget=forms.HiddenInput())
note = forms.CharField(
required=True,
widget=forms.Textarea(attrs={'rows': 4, 'maxlength': 200, 'placeholder': 'Max 200 characters'}),
)

View file

@ -0,0 +1,43 @@
#!/usr/bin/env python3
import logging
from django.core.management.base import BaseCommand
from action.models import StateDefinition, Institution
from django.utils.translation import gettext as _
logger = logging.getLogger('django')
class Command(BaseCommand):
help = 'Create default StateDefinitions for a given institution. "'
def add_arguments(self, parser):
parser.add_argument('institution_name', type=str, help='The name of the institution')
def handle(self, *args, **kwargs):
default_states = [
_("INBOX"),
_("VISUAL INSPECTION"),
_("REPAIR"),
_("INSTALL"),
_("TEST"),
_("PACKAGING"),
_("DONATION"),
_("DISMANTLE")
]
institution_name = kwargs['institution_name']
institution = Institution.objects.filter(name=institution_name).first()
if not institution:
txt = "No institution found for: %s. Please create an institution first"
logger.error(txt, institution.name)
return
for state in default_states:
state_def, created = StateDefinition.objects.get_or_create(
institution=institution,
state=state
)
if created:
self.stdout.write(self.style.SUCCESS(f'Successfully created state: {state}'))
else:
self.stdout.write(self.style.WARNING(f'State already exists: {state}'))

View file

@ -0,0 +1,83 @@
# Generated by Django 5.0.6 on 2024-12-11 18:05
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
("user", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name="State",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("date", models.DateTimeField(auto_now_add=True)),
("state", models.CharField(max_length=50)),
("snapshot_uuid", models.UUIDField()),
(
"institution",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="user.institution",
),
),
(
"user",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL,
),
),
],
),
migrations.CreateModel(
name="StateDefinition",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("order", models.PositiveIntegerField(default=0)),
("state", models.CharField(max_length=50)),
(
"institution",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="user.institution",
),
),
],
options={
"ordering": ["order"],
},
),
migrations.AddConstraint(
model_name="statedefinition",
constraint=models.UniqueConstraint(
fields=("institution", "state"), name="unique_institution_state"
),
),
]

View file

@ -0,0 +1,89 @@
# Generated by Django 5.0.6 on 2024-12-17 19:40
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("action", "0001_initial"),
("user", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name="DeviceLog",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("date", models.DateTimeField(auto_now_add=True)),
("event", models.CharField(max_length=255)),
("snapshot_uuid", models.UUIDField()),
(
"institution",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="user.institution",
),
),
(
"user",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"ordering": ["-date"],
},
),
migrations.CreateModel(
name="Note",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("date", models.DateTimeField(auto_now_add=True)),
("description", models.TextField()),
("snapshot_uuid", models.UUIDField()),
(
"institution",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="user.institution",
),
),
(
"user",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"ordering": ["-date"],
},
),
]

View file

@ -1,3 +1,82 @@
from django.db import models
from django.db import models, connection
from django.db.models import Max
from user.models import User, Institution
from django.core.exceptions import ValidationError
# Create your models here.
class State(models.Model):
date = models.DateTimeField(auto_now_add=True)
institution = models.ForeignKey(Institution, on_delete=models.SET_NULL, null=True)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
state = models.CharField(max_length=50)
snapshot_uuid = models.UUIDField()
def clean(self):
if not StateDefinition.objects.filter(institution=self.institution, state=self.state).exists():
raise ValidationError(f"The state '{self.state}' is not valid for the institution '{self.institution.name}'.")
def save(self, *args, **kwargs):
self.clean()
super().save(*args, **kwargs)
def __str__(self):
return f"{self.institution.name} - {self.state} - {self.snapshot_uuid}"
class StateDefinition(models.Model):
institution = models.ForeignKey(Institution, on_delete=models.CASCADE)
order = models.PositiveIntegerField(default=0)
state = models.CharField(max_length=50)
class Meta:
ordering = ['order']
constraints = [
models.UniqueConstraint(fields=['institution', 'state'], name='unique_institution_state')
]
def save(self, *args, **kwargs):
if not self.pk:
# set the order to be last
max_order = StateDefinition.objects.filter(institution=self.institution).aggregate(Max('order'))['order__max']
self.order = (max_order or 0) + 1
super().save(*args, **kwargs)
def delete(self, *args, **kwargs):
institution = self.institution
order = self.order
super().delete(*args, **kwargs)
# Adjust the order of other instances
StateDefinition.objects.filter(institution=institution, order__gt=order).update(order=models.F('order') - 1)
def __str__(self):
return f"{self.institution.name} - {self.state}"
class Note(models.Model):
institution = models.ForeignKey(Institution, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
date = models.DateTimeField(auto_now_add=True)
description = models.TextField()
snapshot_uuid = models.UUIDField()
class Meta:
ordering = ['-date']
def __str__(self):
return f" Note: {self.description}, by {self.user.username} @ {self.user.institution} - {self.date}, for {self.snapshot_uuid}"
class DeviceLog(models.Model):
institution = models.ForeignKey(Institution, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
date = models.DateTimeField(auto_now_add=True)
event = models.CharField(max_length=255)
snapshot_uuid = models.UUIDField()
class Meta:
ordering = ['-date']
def __str__(self):
return f"{self.event} by {self.user.username} @ {self.institution.name} - {self.date}, for {self.snapshot_uuid}"

View file

@ -1 +1,12 @@
from django.urls import path, include
from action import views
app_name = 'action'
urlpatterns = [
path("new/", views.ChangeStateView.as_view(), name="change_state"),
path('note/add/', views.AddNoteView.as_view(), name='add_note'),
path('note/edit/<int:pk>', views.UpdateNoteView.as_view(), name='update_note'),
path('note/delete/<int:pk>', views.DeleteNoteView.as_view(), name='delete_note'),
]

View file

@ -1 +1,140 @@
# from django.shortcuts import render
from django.views import View
from django.shortcuts import redirect, get_object_or_404
from django.contrib import messages
from action.forms import ChangeStateForm, AddNoteForm
from django.views.generic.edit import DeleteView, CreateView, UpdateView, FormView
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from action.models import State, StateDefinition, Note, DeviceLog
from device.models import Device
class ChangeStateView(FormView):
form_class = ChangeStateForm
def form_valid(self, form):
previous_state = form.cleaned_data['previous_state']
new_state = form.cleaned_data['new_state']
snapshot_uuid = form.cleaned_data['snapshot_uuid']
State.objects.create(
snapshot_uuid=snapshot_uuid,
state=new_state,
user=self.request.user,
institution=self.request.user.institution,
)
message = _("<Created> State '{}'. Previous State: '{}'").format(new_state, previous_state)
DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution,
)
messages.success(self.request, _("State successfully changed from '{}' to '{}'").format(previous_state, new_state))
return super().form_valid(form)
def form_invalid(self, form):
messages.error(self.request, _("There was an error with your submission."))
return redirect(self.get_success_url())
def get_success_url(self):
return self.request.META.get('HTTP_REFERER') or reverse_lazy('device:details')
class AddNoteView(FormView):
form_class = AddNoteForm
def form_valid(self, form):
note_text = form.cleaned_data['note']
snapshot_uuid = form.cleaned_data['snapshot_uuid']
Note.objects.create(
snapshot_uuid=snapshot_uuid,
description=note_text,
user=self.request.user,
institution=self.request.user.institution,
)
message = _("<Created> Note: '{}'").format(note_text)
DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution,
)
messages.success(self.request, _("Note has been added"))
return super().form_valid(form)
def form_invalid(self, form):
messages.error(self.request, _("There was an error with your submission."))
return redirect(self.get_success_url())
def get_success_url(self):
return self.request.META.get('HTTP_REFERER') or reverse_lazy('device:details')
class UpdateNoteView(UpdateView):
model = Note
fields = ['description']
pk_url_kwarg = 'pk'
def form_valid(self, form):
old_description = self.get_object().description
new_description = self.object.description
snapshot_uuid = self.object.snapshot_uuid
if old_description != new_description:
message = _("<Updated> Note. Old Description: '{}'. New Description: '{}'").format(old_description, new_description)
DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution,
)
messages.success(self.request, "Note has been updated.")
return super().form_valid(form)
def form_invalid(self, form):
new_description = form.cleaned_data.get('description', '').strip()
if not new_description:
messages.error(self.request, _("Note cannot be empty."))
super().form_invalid(form)
return redirect(self.get_success_url())
def get_success_url(self):
return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details'))
class DeleteNoteView(View):
model = Note
def post(self, request, *args, **kwargs):
self.pk = kwargs['pk']
referer = request.META.get('HTTP_REFERER')
if not referer:
raise Http404("No referer header found")
self.object = get_object_or_404(
self.model,
pk=self.pk,
institution=self.request.user.institution
)
description = self.object.description
snapshot_uuid= self.object.snapshot_uuid
if request.user != self.object.user and not request.user.is_admin:
messages.error(request, _("You do not have permission to delete this note."))
return redirect(referer)
message = _("<Deleted> Note. Description: '{}'. ").format(description)
DeviceLog.objects.create(
snapshot_uuid=snapshot_uuid,
event=message,
user=request.user,
institution=request.user.institution,
)
messages.warning(self.request, _("Note '{}' deleted successfully.").format(description))
self.object.delete()
return redirect(referer)

5
admin/forms.py Normal file
View file

@ -0,0 +1,5 @@
from django import forms
class OrderingStateForm(forms.Form):
ordering = forms.CharField()

View file

@ -0,0 +1,233 @@
{% extends "base.html" %}
{% load i18n django_bootstrap5 %}
{% block content %}
<div class="row">
<div class="col">
<h3>{{ subtitle }}</h3>
</div>
<div class="col text-end">
<button type="button" class="btn btn-green-admin" data-bs-toggle="modal" data-bs-target="#addStateModal">
{% trans "Add" %}
</button>
</div>
</div>
<div class="row mt-4">
<div class="col">
{% if state_definitions %}
<table class="table table-hover table-bordered align-middle">
<caption class="text-muted small">
{% trans 'Move and drag state definitions to reorder' %}
</caption>
<thead class="table-light">
<tr>
<th scope="col" width="1%" class="text-start">
</th>
<th scope="col" width="5%" class="text-center">
#</th>
<th scope="col">{% trans "State Definition" %}
</th>
<th scope="col" width="15%" class="text-center">{% trans "Actions" %}
</th>
</tr>
</thead>
<tbody id="sortable_list">
{% for state_definition in state_definitions %}
<tr
data-lookup="{{ state_definition.id }}"
style="cursor: grab;"
class="align-items-center">
<td class="">
<i class="bi bi-grip-vertical" aria-hidden="true">
</i>
</td>
<td class="text-center">
<strong>{{ state_definition.order }} </strong>
</td>
<td class="font-monospace">
{{ state_definition.state }}
</td>
<!-- action buttons -->
<td>
<div class="btn-group float-end">
<button
type="button"
class="btn btn-sm btn-outline-info d-flex align-items-center"
data-bs-toggle="modal" data-bs-target="#editStateModal{{ state_definition.id }}">
<i class="bi bi-pencil me-1"></i>
{% trans 'Edit' %}
</button>
<button
type="button" class="btn btn-sm btn-outline-danger d-flex align-items-center"
data-bs-toggle="modal"
data-bs-target="#deleteStateModal{{ state_definition.id }}" >
<i class="bi bi-trash me-1"></i>
{% trans 'Delete' %}
</button>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<form id="orderingForm" method="post" action="{% url 'admin:update_state_order' %}">
{% csrf_token %}
<input type="hidden" id="orderingInput" name="ordering">
<button id="saveOrderBtn" class="btn btn-success mt-5 float-start collapse" >{% trans "Update Order" %}</button>
</form>
{% else %}
<div class="alert alert-primary text-center mt-5" role="alert">
{% trans "No states found on current organization" %}
</div>
{% endif %}
</div>
</div>
<!-- add state definition Modal -->
<div class="modal fade" id="addStateModal" tabindex="-1" aria-labelledby="addStateModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addStateModalLabel">{% trans "Add State Definition" %}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" action="{%url 'admin:add_state_definition'%}">
{% csrf_token %}
<div class="mb-3">
<label for="stateInput" class="form-label">{% trans "State" %}</label>
<input type="text" class="form-control" id="stateInput" name="state" maxlength="50" required>
<div class="form-text">{% trans "Maximum 50 characters." %}</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Close" %}</button>
<button type="submit" class="btn btn-primary">{% trans "Add state definition" %}</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Edit State Definition Modals -->
{% for state_definition in state_definitions %}
<div class="modal fade" id="editStateModal{{ state_definition.id }}" tabindex="-1" aria-labelledby="editStateModalLabel{{ state_definition.id }}" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form method="post" action="{% url 'admin:edit_state_definition' state_definition.id %}">
{% csrf_token %}
<div class="modal-header">
<h5 class="modal-title" id="editStateModalLabel{{ state_definition.id }}">
{% trans "Edit State Definition" %}
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{% trans 'Close' %}"></button>
</div>
<div class="modal-body">
<div class="alert alert-warning text-center" role="alert">
{% trans "Existing devices with this state will not have their state names changed." %}
</div>
<div class="mb-3">
<label for="editStateInput{{ state_definition.id }}" class="form-label">{% trans "State" %}</label>
<input type="text" class="form-control" id="editStateInput{{ state_definition.id }}" name="state" maxlength="50" value="{{ state_definition.state }}" required>
<div class="form-text">{% trans "Maximum 50 characters." %}</div>
</div>
<p class="text-muted text-end">{% trans "Any changes in order will not be saved." %}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
<button type="submit" class="btn btn-green-admin">{% trans "Save Changes" %}</button>
</div>
</form>
</div>
</div>
</div>
{% endfor %}
<!-- delete state definition Modal -->
{% for state_definition in state_definitions %}
<div class="modal fade" id="deleteStateModal{{ state_definition.id }}" tabindex="-1" aria-labelledby="deleteStateModalLabel{{ state_definition.id }}" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title fw-bold" id="deleteStateModalLabel{{ state_definition.id }}">
{% trans "Delete State Definition" %}
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{% trans 'Close' %}"></button>
</div>
<div class="modal-body">
<div class="alert alert-warning text-center" role="alert">
{% trans "Devices with a State of this description will not have their State altered" %}
</div>
<div class="d-flex align-items-center border rounded p-3 mt-3">
<span class="badge bg-secondary me-3 display-6">{{ state_definition.order }}</span>
<div>
<p class="mb-0 fw-bold">{{ state_definition.state }}</p>
</div>
</div>
<p class="text-muted text-end mt-3">{% trans "Any changes in order will not be saved." %}</p>
</div>
<div class="modal-footer">
<form method="post" action="{% url 'admin:delete_state_definition' state_definition.pk %}">
{% csrf_token %}
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
{% trans "Cancel" %}
</button>
<button type="submit" class="btn btn-danger">
{% trans "Delete" %}
</button>
</form>
</div>
</div>
</div>
</div>
{% endfor %}
<script>
//following https://dev.to/nemecek_f/django-how-to-let-user-re-order-sort-table-of-content-with-drag-and-drop-3nlp
const saveOrderingButton = document.getElementById('saveOrderBtn');
const orderingForm = document.getElementById('orderingForm');
const formInput = orderingForm.querySelector('#orderingInput');
const sortable_table = document.getElementById('sortable_list');
const sortable = new Sortable(sortable_table, {
animation: 150,
swapThreshold: 0.10,
onChange: () => {
//TODO: change hide/show animation to a nicer one
const collapse = new bootstrap.Collapse(saveOrderingButton, {
toggle: false
});
collapse.show();
}
});
function saveOrdering() {
const rows = sortable_table.querySelectorAll('tr');
let ids = [];
for (let row of rows) {
ids.push(row.dataset.lookup);
}
formInput.value = ids.join(',');
orderingForm.submit();
}
saveOrderingButton.addEventListener('click', saveOrdering);
</script>
{% endblock %}

View file

@ -10,4 +10,9 @@ urlpatterns = [
path("users/edit/<int:pk>", views.EditUserView.as_view(), name="edit_user"),
path("users/delete/<int:pk>", views.DeleteUserView.as_view(), name="delete_user"),
path("institution/<int:pk>", views.InstitutionView.as_view(), name="institution"),
path("states/", views.StatesPanelView.as_view(), name="states_panel"),
path("states/add", views.AddStateDefinitionView.as_view(), name="add_state_definition"),
path("states/delete/<int:pk>", views.DeleteStateDefinitionView.as_view(), name='delete_state_definition'),
path("states/update_order/", views.UpdateStateOrderView.as_view(), name='update_state_order'),
path("states/edit/<int:pk>/", views.UpdateStateDefinitionView.as_view(), name='edit_state_definition'),
]

View file

@ -1,16 +1,23 @@
import logging
from smtplib import SMTPException
from django.contrib import messages
from django.urls import reverse_lazy
from django.shortcuts import get_object_or_404
from django.shortcuts import get_object_or_404, redirect, Http404
from django.utils.translation import gettext_lazy as _
from django.views.generic.base import TemplateView
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic.base import TemplateView, ContextMixin
from django.views.generic.edit import (
CreateView,
UpdateView,
DeleteView,
)
from django.core.exceptions import ValidationError
from django.db import IntegrityError, transaction
from dashboard.mixins import DashboardView, Http403
from admin.forms import OrderingStateForm
from user.models import User, Institution
from admin.email import NotifyActivateUserByEmail
from action.models import StateDefinition
class AdminView(DashboardView):
@ -124,3 +131,101 @@ class InstitutionView(AdminView, UpdateView):
self.object = self.request.user.institution
kwargs = super().get_form_kwargs()
return kwargs
class StateDefinitionContextMixin(ContextMixin):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
"state_definitions": StateDefinition.objects.filter(institution=self.request.user.institution).order_by('order'),
"help_text": _('State definitions are the custom finite states that a device can be in.'),
})
return context
class StatesPanelView(AdminView, StateDefinitionContextMixin, TemplateView):
template_name = "states_panel.html"
title = _("States Panel")
breadcrumb = _("admin / States Panel") + " /"
class AddStateDefinitionView(AdminView, StateDefinitionContextMixin, CreateView):
template_name = "states_panel.html"
title = _("New State Definition")
breadcrumb = "Admin / New state"
success_url = reverse_lazy('admin:states_panel')
model = StateDefinition
fields = ('state',)
def form_valid(self, form):
form.instance.institution = self.request.user.institution
form.instance.user = self.request.user
try:
response = super().form_valid(form)
messages.success(self.request, _("State definition successfully added."))
return response
except IntegrityError:
messages.error(self.request, _("State is already defined."))
return self.form_invalid(form)
def form_invalid(self, form):
return super().form_invalid(form)
class DeleteStateDefinitionView(AdminView, StateDefinitionContextMixin, SuccessMessageMixin, DeleteView):
model = StateDefinition
success_url = reverse_lazy('admin:states_panel')
def get_success_message(self, cleaned_data):
return f'State definition: {self.object.state}, has been deleted'
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
#only an admin of current institution can delete
if not object.institution == self.request.user.institution:
raise Http404
return super().delete(request, *args, **kwargs)
class UpdateStateOrderView(AdminView, TemplateView):
success_url = reverse_lazy('admin:states_panel')
def post(self, request, *args, **kwargs):
form = OrderingStateForm(request.POST)
if form.is_valid():
ordered_ids = form.cleaned_data["ordering"].split(',')
with transaction.atomic():
current_order = 1
_log = []
for lookup_id in ordered_ids:
state_definition = StateDefinition.objects.get(id=lookup_id)
state_definition.order = current_order
state_definition.save()
_log.append(f"{state_definition.state} (ID: {lookup_id} -> Order: {current_order})")
current_order += 1
messages.success(self.request, _("Order changed succesfuly."))
return redirect(self.success_url)
else:
return Http404
class UpdateStateDefinitionView(AdminView, UpdateView):
model = StateDefinition
template_name = 'states_panel.html'
fields = ['state']
pk_url_kwarg = 'pk'
def get_queryset(self):
return StateDefinition.objects.filter(institution=self.request.user.institution)
def get_success_url(self):
messages.success(self.request, _("State definition updated successfully."))
return reverse_lazy('admin:states_panel')
def form_valid(self, form):
return super().form_valid(form)

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
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,24 @@ class NewSnapshotView(ApiMixing):
text = "fail: It is not possible to parse snapshot"
return JsonResponse({'status': text}, status=500)
annotation = Annotation.objects.filter(
prop = SystemProperty.objects.filter(
uuid=ev_uuid,
type=Annotation.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 prop:
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 +258,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 +281,12 @@ 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
).first()
if not self.annotation:
if not self.property:
return JsonResponse({}, status=404)
try:
@ -300,10 +297,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))

2
dashboard/static/js/Sortable.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -18,6 +18,7 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<link rel="stylesheet" href= "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
<link href="{% static "/css/bootstrap.min.css" %}" rel="stylesheet">
<script src="{% static 'js/Sortable.min.js' %}"></script>
<style>
.bd-placeholder-img {
@ -51,7 +52,7 @@
}
</style>
<!-- Custom styles for this template -->
<link href="{% static "/css/dashboard.css" %}" rel="stylesheet">
{% endblock %}
@ -81,21 +82,26 @@
<ul class="nav flex-column">
{% if user.is_admin %}
<li class="nav-item">
<a class="admin {% if path in 'panel users' %}active {% endif %}nav-link fw-bold" data-bs-toggle="collapse" data-bs-target="#ul_admin" aria-expanded="false" aria-controls="ul_admin" href="javascript:void()">
<a class="admin {% if path in 'panel users states_panel edit_user delete_user new_user institution' %}active {% endif %}nav-link fw-bold" data-bs-toggle="collapse" data-bs-target="#ul_admin" aria-expanded="false" aria-controls="ul_admin" href="javascript:void()">
<i class="bi bi-person-fill-gear icon_sidebar"></i>
{% trans 'Admin' %}
</a>
<ul class="flex-column mb-2 ul_sidebar accordion-collapse {% if path in 'panel users' %}expanded{% else %}collapse{% endif %}" id="ul_admin" data-bs-parent="#sidebarMenu">
<ul class="flex-column mb-2 ul_sidebar accordion-collapse {% if path in 'panel institution users edit_user new_user delete_user states_panel' %}expanded{% else %}collapse{% endif %}" id="ul_admin" data-bs-parent="#sidebarMenu">
<li class="nav-item">
<a class="nav-link{% if path == 'panel' %} active2{% endif %}" href="{% url 'admin:panel' %}">
<a class="nav-link{% if path in 'panel institution' %} active2{% endif %}" href="{% url 'admin:panel' %}">
{% trans 'Panel' %}
</a>
</li>
<li class="nav-item">
<a class="nav-link{% if path == 'users' %} active2{% endif %}" href="{% url 'admin:users' %}">
<a class="nav-link{% if path in 'users edit_user new_user delete_user' %} active2{% endif %}" href="{% url 'admin:users' %}">
{% trans 'Users' %}
</a>
</li>
<li class="nav-item">
<a class="nav-link{% if path == 'states_panel' %} active2{% endif %}" href="{% url 'admin:states_panel' %}">
{% trans 'States' %}
</a>
</li>
</ul>
</li>
{% endif %}
@ -178,9 +184,15 @@
{% endfor %}
{% endblock messages %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2">
<h1 class="h2">{{ title }}</h1>
<h1 class="h2">{{ title }}
{% if help_text %}
<span class="ms-1" data-bs-toggle="tooltip" data-bs-placement="right" title="{{ help_text }}">
<i class="fas fa-question-circle text-secondary h6 align-top"></i>
</span>
{% endif %}
</h1>
<form method="post" action="{% url 'dashboard:search' %}">
<form method="post" action="{% url 'dashboard:search' %}">
{% csrf_token %}
<div class="input-group rounded">
<input type="search" name="search" class="form-control rounded" placeholder="Search your device..." aria-label="Search" aria-describedby="search-addon" />
@ -189,9 +201,9 @@
</span>
</div>
</form>
</div>
<div class="row border-bottom mb-3">
<div class="col">
<small style="color:#899bbd"><i>{{ breadcrumb }}</i></small>
@ -221,4 +233,13 @@
{% block extrascript %}{% endblock %}
{% endblock %}
</body>
<script>
//If help_text is passed to the view as context, a hover-able help icon is displayed
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl);
});
</script>
</html>

View file

@ -20,9 +20,9 @@
{% trans 'Exports' %}
</a>
{% if lot %}
<a href="{% url 'lot:annotations' object.id %}" type="button" class="btn btn-green-admin">
<a href="{% url 'lot:properties' object.id %}" type="button" class="btn btn-green-admin">
<i class="bi bi-tag"></i>
{% trans 'Annotations' %}
{% trans 'properties' %}
</a>
{% endif %}
</div>

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 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,13 +83,14 @@ 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"]
else:
uuid = snap["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()
@ -98,8 +99,7 @@ class SearchView(InventaryMixin):
if i:
qry |= Q(value__startswith=i)
chids = Annotation.objects.filter(
type=Annotation.Type.SYSTEM,
chids = SystemProperty.objects.filter(
owner=self.request.user.institution
).filter(
qry

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
@ -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

View file

@ -1,8 +1,9 @@
from django.db import models, connection
from utils.constants import ALGOS
from evidence.models import Annotation, Evidence
from evidence.models import SystemProperty, UserProperty, Evidence
from lot.models import DeviceLot
from action.models import State
class Device:
@ -30,7 +31,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,61 +40,59 @@ 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(
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
type=UserProperty.Type.USER,
)
return annotations
return user_properties
def get_user_documents(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.DOCUMENT
type=UserProperty.Type.DOCUMENT
)
return annotations
return user_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(
key__in=algos,
).values_list("value", flat=True)))
@ -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)
def is_eraseserver(self):
if not self.uuids:
@ -124,13 +123,13 @@ class Device:
if not self.uuids:
return False
annotation = Annotation.objects.filter(
prop = UserProperty.objects.filter(
uuid__in=self.uuids,
owner=self.owner,
type=Annotation.Type.ERASE_SERVER
type=UserProperty.Type.ERASE_SERVER
).first()
if annotation:
if prop:
return True
return False
@ -139,6 +138,11 @@ class Device:
return self.uuid
return self.uuids[0]
def get_current_state(self):
uuid = self.last_uuid
return State.objects.filter(snapshot_uuid=uuid).order_by('-date').first()
def get_lots(self):
self.lots = [
x.lot for x in DeviceLot.objects.filter(device_id=self.id)]
@ -147,7 +151,7 @@ class Device:
def get_unassigned(cls, institution, offset=0, limit=None):
sql = """
WITH RankedAnnotations AS (
WITH RankedProperties AS (
SELECT
t1.value,
t1.key,
@ -160,34 +164,32 @@ 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}
AND t1.type = {type}
)
SELECT DISTINCT
value
FROM
RankedAnnotations
RankedProperties
WHERE
row_num = 1
""".format(
institution=institution.id,
type=Annotation.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
@ -195,7 +197,7 @@ class Device:
def get_unassigned_count(cls, institution):
sql = """
WITH RankedAnnotations AS (
WITH RankedProperties AS (
SELECT
t1.value,
t1.key,
@ -209,30 +211,28 @@ 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}
)
SELECT
COUNT(DISTINCT value)
FROM
RankedAnnotations
RankedProperties
WHERE
row_num = 1
""".format(
institution=institution.id,
type=Annotation.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,
@ -246,31 +246,29 @@ 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.uuid = '{uuid}'
)
SELECT DISTINCT
value
FROM
RankedAnnotations
RankedProperties
WHERE
row_num = 1;
""".format(
uuid=uuid.replace("-", ""),
institution=institution.id,
type=Annotation.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):

View file

@ -2,11 +2,162 @@
{% load i18n %}
{% block content %}
<div class="row">
<div class="col">
<h3>{{ object.shortid }}</h3>
<div class="position-fixed" style="bottom: 2rem; right: 2rem; z-index: 9999; display: flex; gap: 0.5rem;">
<button class="btn btn-yellow d-flex align-items-center shadow" type="button"
data-bs-toggle="offcanvas" data-bs-target="#notesOffcanvas" aria-controls="notesOffcanvas"
data-bs-toggle="tooltip" data-bs-placement="left" title="{% trans 'View recent notes' %}">
<i class="bi bi-journal-text me-1"></i>
{% trans "Journal" %}
</button>
</div>
<!-- side panel for latest notes -->
<div class="offcanvas offcanvas-end" tabindex="-1" id="notesOffcanvas" aria-labelledby="notesOffcanvasLabel">
<div class="offcanvas-header">
<h5 id="notesOffcanvasLabel">{% trans "Latest Notes" %}</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body" style="margin-bottom: 5rem;">
{% for note in device_notes|slice:":4" %}
<div class="card mb-3 shadow-sm">
<div class="card-body">
<div>
<small class="text-muted">
{{ note.date|timesince }} {% trans "ago" %}
</small>
{% if user == note.user or user.is_admin %}
<span class="badge bg-yellow text-dark ms-2">{% trans "Editable" %}</span>
</div>
<blockquote
class="blockquote mt-2 p-2 bg-light fst-italic"
contenteditable="true"
style="font-size: 1.2em!important"
data-note-id="{{ note.id }}"
title="{% trans 'Click to edit this note' %}"
oninput="toggleSaveLink(this)">
{% else %}
</div>
<blockquote style="font-size: 1.2em!important" class="blockquote mt-2 p-2 fst-italic">
{% endif %}
<p data-note-id="{{ note.id }}">
{{ note.description }}
</p>
<footer class="blockquote-footer text-end mt-2" contenteditable="false">
<small>{{ note.user.get_full_name|default:note.user.username }}</small>
</footer>
</blockquote>
{% if user == note.user or user.is_admin %}
<div class="d-flex justify-content-end align-items-center">
<!-- update note button -->
<form
id="updateNoteForm{{ note.id }}"
method="post"
action="{% url 'action:update_note' note.id %}"
class="d-inline"
>
{% csrf_token %}
<input type="hidden" name="description" id="descriptionInput{{ note.id }}" value="">
<a
type="submit"
id="saveLink{{ note.id }}"
class="text-muted disabled me-4 border border-light rounded"
style="pointer-events: none;"
title="{% trans 'Save changes' %}"
onclick="submitUpdatedNote('{{ note.id }}'); return false;"
>
<i class="fas fa-save px-1"></i>
</a>
</form>
<!-- delete note button -->
<button type="button" class="btn btn-link btn-outline-danger btn-sm text-danger" id="deleteIcon{{ note.id }}" title="{% trans 'Delete note' %}" data-bs-toggle="collapse" data-bs-target="#confirmDelete{{ note.id }}">
<i class="bi bi-trash"></i>
</button>
</div>
<form class="d-inline" method="post" action="{% url 'action:delete_note' note.id %}">
{% csrf_token %}
<div class="collapse mt-2" id="confirmDelete{{ note.id }}">
<div class="card card-body border border-danger text-center">
<p class="mb-2">{% trans 'Are you sure you want to delete this note?' %}</p>
<a
href="#"
class="btn btn-sm btn-outline-danger"
onclick="submitDeleteForm({{ note.id }}); return false;"
>
{% trans 'Confirm delete' %}
</a>
</div>
</div>
</form>
{% endif %}
</div>
</div>
{% empty %}
<p>{% trans "No notes available." %}</p>
{% endfor %}
</div>
</div>
<!-- Top bar buttons -->
<div class="row">
<div class="col">
<h3>{{ object.shortid }}</h3>
</div>
<div class="col text-end">
<div class="btn-group" role="group" aria-label="Actions">
<!-- change state button -->
{% if state_definitions %}
<div class="dropdown ms-2">
<a class="btn btn-green-admin dropdown-toggle" id="addStateDropdown" data-bs-toggle="dropdown" aria-expanded="false">
{% trans "Change state" %}
{% if device_states %}
({{ device_states.0.state }})
{% else %}
( {% trans "None" %} )
{% endif %}
</a>
<ul class="dropdown-menu" aria-labelledby="addStateDropdown" style="width: 100%;">
{% for state in state_definitions %}
<li style="width: 100%;">
<form id="changeStateForm{{ state.id }}" method="post" action="{% url 'action:change_state' %}">
{% csrf_token %}
<input type="hidden" name="previous_state" value="{{ device_states.0.state|default:"nil" }}">
<input type="hidden" name="snapshot_uuid" value="{{ object.last_uuid }}">
<input type="hidden" name="new_state" value="{{ state.state }}">
<a class="dropdown-item d-flex justify-content-between align-items-center" href="#" onclick="document.getElementById('changeStateForm{{ state.id }}').submit(); return false;">
<span class="font-monospace">{{ state.state }}</span>
<span class="badge bg-secondary rounded-pill-sm">{{ forloop.counter }}</span>
</a>
</form>
</li>
{% endfor %}
</ul>
</div>
{% else %}
<button class="btn btn-green-admin" type="button" disabled>
<i class="bi bi-plus"></i> {% trans "Change state" %}
{% if device_states %}
({{ device_states.0.state }})
{% endif %}
</button>
{% endif %}
<!-- Add note button -->
<button class="btn btn-yellow ms-2" type="button" data-bs-toggle="modal" data-bs-target="#addNoteModal">
<i class="bi bi-sticky"></i> {% trans "Add a note" %}
</button>
</div>
</div>
</div>
<div class="row">
<div class="col">
@ -15,7 +166,10 @@
<a href="#details" class="nav-link active" data-bs-toggle="tab" data-bs-target="#details">{% trans 'General details' %}</a>
</li>
<li class="nav-item">
<a href="#annotations" class="nav-link" data-bs-toggle="tab" data-bs-target="#annotations">{% trans 'User annotations' %}</a>
<a href="#log" class="nav-link" data-bs-toggle="tab" data-bs-target="#log">{% trans 'Log' %}</a>
</li>
<li class="nav-item">
<a href="#user_properties" class="nav-link" data-bs-toggle="tab" data-bs-target="#user_properties">{% trans 'User properties' %}</a>
</li>
<li class="nav-item">
<a href="#documents" class="nav-link" data-bs-toggle="tab" data-bs-target="#documents">{% trans 'Documents' %}</a>
@ -40,227 +194,48 @@
</ul>
</div>
</div>
<div class="tab-content pt-4">
<div class="tab-content pt-2">
<div class="tab-pane fade show active" id="details">
<h5 class="card-title">{% trans 'Details' %}</h5>
<div class="row mb-3">
<div class="col-lg-3 col-md-4 label">Phid</div>
<div class="col-lg-9 col-md-8">{{ object.id }}</div>
</div>
{% include 'tabs/general_details.html' %}
{% if object.is_eraseserver %}
<div class="row mb-3">
<div class="col-lg-3 col-md-4 label">
{% trans 'Is a erase server' %}
</div>
<div class="col-lg-9 col-md-8"></div>
{% include 'tabs/log.html' %}
{% include 'tabs/user_properties.html' %}
{% include 'tabs/documents.html' %}
{% include 'tabs/lots.html' %}
{% include 'tabs/components.html' %}
{% include 'tabs/evidences.html' %}
<!-- Add a note popup -->
<div class="modal fade" id="addNoteModal" tabindex="-1" aria-labelledby="addNoteModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addNoteModalLabel">{% trans "Add a Note" %}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{% trans 'Close' %}"></button>
</div>
{% endif %}
<div class="row mb-1">
<div class="col-lg-3 col-md-4 label">Type</div>
<div class="col-lg-9 col-md-8">{{ object.type }}</div>
</div>
{% if object.is_websnapshot and object.last_user_evidence %}
{% for k, v in object.last_user_evidence %}
<div class="row mb-1">
<div class="col-lg-3 col-md-4 label">{{ k }}</div>
<div class="col-lg-9 col-md-8">{{ v|default:'' }}</div>
</div>
{% endfor %}
{% else %}
<div class="row mb-1">
<div class="col-lg-3 col-md-4 label">
{% trans 'Manufacturer' %}
</div>
<div class="col-lg-9 col-md-8">{{ object.manufacturer|default:'' }}</div>
</div>
<div class="row mb-1">
<div class="col-lg-3 col-md-4 label">
{% trans 'Model' %}
</div>
<div class="col-lg-9 col-md-8">{{ object.model|default:'' }}</div>
</div>
<div class="row mb-1">
<div class="col-lg-3 col-md-4 label">
{% trans 'Version' %}
</div>
<div class="col-lg-9 col-md-8">{{ object.version|default:'' }}</div>
</div>
<div class="row mb-1">
<div class="col-lg-3 col-md-4 label">
{% trans 'Serial Number' %}
</div>
<div class="col-lg-9 col-md-8">{{ object.serial_number|default:'' }}</div>
</div>
{% endif %}
<div class="row mb-3">
<div class="col-lg-3 col-md-4 label">
{% trans 'Identifiers' %}
</div>
</div>
{% for chid in object.hids %}
<div class="row mb-3">
<div class="col">{{ chid|default:'' }}</div>
</div>
{% endfor %}
</div>
<div class="tab-pane fade" id="annotations">
<div class="btn-group mt-1 mb-3">
<a href="{% url 'device:add_annotation' object.pk %}" class="btn btn-primary">
<i class="bi bi-plus"></i>
{% trans 'Add new annotation' %}
</a>
</div>
<h5 class="card-title">{% trans 'Annotations' %}</h5>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">
{% trans 'Key' %}
</th>
<th scope="col">
{% trans 'Value' %}
</th>
<th scope="col" data-type="date" data-format="YYYY-MM-DD HH:mm">
{% trans 'Created on' %}
</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{% for a in object.get_user_annotations %}
<tr>
<td>{{ a.key }}</td>
<td>{{ a.value }}</td>
<td>{{ a.created }}</td>
<td></td>
<td></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="tab-pane fade" id="documents">
<div class="btn-group mt-1 mb-3">
<a href="{% url 'device:add_document' object.pk %}" class="btn btn-primary">
<i class="bi bi-plus"></i>
{% trans 'Add new document' %}
</a>
</div>
<h5 class="card-title">{% trans 'Documents' %}</h5>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">
{% trans 'Key' %}
</th>
<th scope="col">
{% trans 'Value' %}
</th>
<th scope="col" data-type="date" data-format="YYYY-MM-DD HH:mm">
{% trans 'Created on' %}
</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{% for a in object.get_user_documents %}
<tr>
<td>{{ a.key }}</td>
<td>{{ a.value }}</td>
<td>{{ a.created }}</td>
<td></td>
<td></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="tab-pane fade" id="lots">
{% for tag in lot_tags %}
<h5 class="card-title">{{ tag }}</h5>
{% for lot in object.lots %}
{% if lot.type == tag %}
<div class="row mb-3">
<div class="col">
<a href="{% url 'dashboard:lot' lot.id %}">{{ lot.name }}</a>
</div>
<div class="modal-body">
<form method="post" action="{% url 'action:add_note' %}">
{% csrf_token %}
<div class="mb-3">
<input type="hidden" name="snapshot_uuid" value="{{ object.last_uuid }}">
<label for="noteDescription" class="form-label">{% trans "Note" %}</label>
<textarea class="form-control" id="noteDescription" name="note" placeholder="Max 250 characters" name="note" rows="3" required></textarea>
</div>
{% endif %}
{% endfor %}
{% endfor %}
</div>
<div class="tab-pane fade" id="components">
<h5 class="card-title">{% trans 'Components last evidence' %}</h5>
<div class="list-group col-6">
{% for c in object.components %}
<div class="list-group-item">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{ c.type }}</h5>
<small class="text-muted">{{ evidence.created }}</small>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
<button type="submit" class="btn btn-green-admin">{% trans "Save Note" %}</button>
</div>
<p class="mb-1">
{% for k, v in c.items %}
{% if k not in 'actions,type' %}
{{ k }}: {{ v }}<br />
{% endif %}
{% endfor %}
</p>
</div>
{% endfor %}
</form>
</div>
</div>
</div>
<div class="tab-pane fade" id="evidences">
<h5 class="card-title">{% trans 'List of evidences' %}</h5>
<div class="list-group col-6">
{% for snap in object.evidences %}
<div class="list-group-item">
<div class="d-flex w-100 justify-content-between">
<small class="text-muted">{{ snap.created }}</small>
</div>
<p class="mb-1">
<a href="{% url 'evidence:details' snap.uuid %}">{{ snap.uuid }}</a>
</p>
</div>
{% endfor %}
</div>
</div>
{% if dpps %}
<div class="tab-pane fade" id="dpps">
<h5 class="card-title">{% trans 'List of dpps' %}</h5>
<div class="list-group col">
{% for d in dpps %}
<div class="list-group-item">
<div class="d-flex w-100 justify-content-between">
<small class="text-muted">{{ d.timestamp }}</small>
<span>{{ d.type }}</span>
</div>
<p class="mb-1">
<a href="{% url 'did:device_web' d.signature %}">{{ d.signature }}</a>
</p>
</div>
{% endfor %}
</div>
</div>
{% endif %}
</div>
{% endblock %}
{% block extrascript %}
@ -281,5 +256,27 @@
}
}
})
//Enable save button on note if changes are made to it
function toggleSaveLink(blockquoteElem) {
const saveLink = document.getElementById("saveLink" + blockquoteElem.dataset.noteId);
saveLink.classList.remove("disabled", "text-muted", "border-light");
saveLink.classList.add("text-success", "border-success");
saveLink.style.pointerEvents = "auto";
}
//updates note-update-form with new value from blockquote
function submitUpdatedNote(noteId) {
const noteParagraph = document.querySelector('p[data-note-id="' + noteId + '"]');
const newText = noteParagraph.innerText.trim();
const descriptionField = document.getElementById('descriptionInput' + noteId);
descriptionField.value = newText;
document.getElementById('updateNoteForm' + noteId).submit();
}
//simpler are u sure? confirmation message
function submitDeleteForm(noteId) {
document.getElementById('confirmDelete' + noteId).closest('form').submit();
}
</script>
{% endblock %}

View file

@ -0,0 +1,27 @@
{% load i18n %}
<div class="tab-pane fade" id="components">
<h5 class="card-title">{% trans 'Components last evidence' %}
</h5>
<div class="list-group col-6">
{% for c in object.components %}
<div class="list-group-item">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{ c.type }}
</h5>
<small class="text-muted">{{ evidence.created }}
</small>
</div>
<p class="mb-1">
{% for k, v in c.items %}
{% if k not in 'actions,type' %}
{{ k }}: {{ v }}
<br />
{% endif %}
{% endfor %}
</p>
</div>
{% endfor %}
</div>
</div>

View file

@ -0,0 +1,49 @@
{% load i18n %}
<div class="tab-pane fade" id="documents">
<div class="btn-group mt-1 mb-3">
<a href="{% url 'device:add_document' object.pk %}" class="btn btn-primary">
<i class="bi bi-plus">
</i>
{% trans 'Add new document' %}
</a>
</div>
<h5 class="card-title">{% trans 'Documents' %}
</h5>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">
{% trans 'Key' %}
</th>
<th scope="col">
{% trans 'Value' %}
</th>
<th scope="col" data-type="date" data-format="YYYY-MM-DD HH:mm">
{% trans 'Created on' %}
</th>
<th>
</th>
<th>
</th>
</tr>
</thead>
<tbody>
{% for a in object.get_user_documents %}
<tr>
<td>{{ a.key }}
</td>
<td>{{ a.value }}
</td>
<td>{{ a.created }}
</td>
<td>
</td>
<td>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>

View file

@ -0,0 +1,19 @@
{% load i18n %}
<div class="tab-pane fade" id="evidences">
<h5 class="card-title">{% trans 'List of evidences' %}</h5>
<div class="list-group col-6">
{% for snap in object.evidences %}
<div class="list-group-item">
<div class="d-flex w-100 justify-content-between">
<small class="text-muted">{{ snap.created }}</small>
</div>
<p class="mb-1">
<a href="{% url 'evidence:details' snap.uuid %}">{{ snap.uuid }}</a>
</p>
</div>
{% endfor %}
</div>
</div>
</div>

View file

@ -0,0 +1,81 @@
{% load i18n %}
<!-- Device Details -->
<div class="tab-pane fade show active" id="details">
<h5 class="card-title">{% trans 'Details' %}
</h5>
<hr>
<div class="row mb-3">
<div class="col-sm-4 text-muted fw-bold">{% trans 'Phid' %}
</div>
<div class="col-sm-8">{{ object.id }}
</div>
</div>
{% if object.is_eraseserver %}
<div class="row mb-3">
<div class="col-sm-4 text-muted fw-bold">{% trans 'Is an erase server' %}
</div>
<div class="col-sm-8">{% trans 'Yes' %}
</div>
</div>
{% endif %}
<div class="row mb-3">
<div class="col-sm-4 text-muted fw-bold">{% trans 'Type' %}
</div>
<div class="col-sm-8">{{ object.type }}
</div>
</div>
{% if object.is_websnapshot and object.last_user_evidence %}
{% for k, v in object.last_user_evidence.items %}
<div class="row mb-3">
<div class="col-sm-4 text-muted fw-bold">{{ k }}
</div>
<div class="col-sm-8">{{ v|default:'' }}
</div>
</div>
{% endfor %}
{% else %}
<div class="row mb-3">
<div class="col-sm-4 text-muted fw-bold">{% trans 'Manufacturer' %}
</div>
<div class="col-sm-8">{{ object.manufacturer|default:'' }}
</div>
</div>
<div class="row mb-3">
<div class="col-sm-4 text-muted fw-bold">{% trans 'Model' %}
</div>
<div class="col-sm-8">{{ object.model|default:'' }}
</div>
</div>
<div class="row mb-3">
<div class="col-sm-4 text-muted fw-bold">
{% trans 'Version' %}
</div>
<div class="col-sm-8">{{ object.version|default:'' }}</div>
</div>
<div class="row mb-3">
<div class="col-sm-4 text-muted fw-bold">{% trans 'Serial Number' %}
</div>
<div class="col-sm-8">{{ object.serial_number|default:'' }}
</div>
</div>
{% endif %}
<div class="row mb-3">
<div class="col-sm-4 text-muted fw-bold">{% trans 'Identifiers' %}
</div>
<div class="col-sm-8">
{% for chid in object.hids %}
<div>{{ chid|default:'' }}
</div>
{% endfor %}
</div>
</div>
</div>

View file

@ -0,0 +1,28 @@
{% load i18n %}
<div class="tab-pane fade" id="log">
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered bg-gradient">
<thead >
<tr>
<th scope="col">{% trans 'Date' %}</th>
<th scope="col">{% trans 'Event' %}</th>
<th scope="col">{% trans 'User' %}</th>
</tr>
</thead>
<tbody>
{% for log in device_logs %}
<tr>
<td width="13%">{{ log.date|date:"M j, Y, H:i" }}</td>
<td class="fst-italic">{{ log.event }}</td>
<td>{{ log.user.get_full_name|default:log.user.username }}</td>
</tr>
{% empty %}
<tr>
<td colspan="3" class="text-center">{% trans 'No logs recorded.' %}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>

View file

@ -0,0 +1,19 @@
{% load i18n %}
<div class="tab-pane fade" id="lots">
{% for tag in lot_tags %}
<h5 class="card-title">{{ tag }}
</h5>
{% for lot in object.lots %}
{% if lot.type == tag %}
<div class="row mb-3">
<div class="col">
<a href="{% url 'dashboard:lot' lot.id %}">{{ lot.name }}
</a>
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
</div>

View file

@ -0,0 +1,131 @@
{% load i18n %}
<div class="tab-pane fade" id="user_properties">
<div class="d-flex justify-content-end mt-1 mb-3">
<a href="{% url 'device:add_user_property' object.pk %}"
class="btn btn-green-admin d-flex align-items-center">
<i class="bi bi-plus me-1"></i>
{% trans 'New user property' %}
</a>
</div>
<h5 class="card-title">{% trans 'User properties' %}</h5>
<table class="table table-hover table-bordered table-responsive align-middle">
<thead class="table-light">
<tr>
<th scope="col">{% trans 'Key' %}</th>
<th scope="col">{% trans 'Value' %}</th>
<th scope="col" data-type="date" class="text-end" data-format="YYYY-MM-DD HH:mm">{% trans 'Created on' %}</th>
<th scope="col" width="5%" class="text-end" title="{% trans 'Actions' %}"></th>
</tr>
</thead>
<tbody>
{% for a in object.get_user_properties %}
<tr>
<td>{{ a.key }}
</td>
<td>{{ a.value }}
</td>
<td class="text-end">{{ a.created }}
</td>
<td>
<div class="btn-group ">
<button
type="button"
class="btn btn-sm btn-outline-info d-flex align-items-center" data-bs-toggle="modal"
data-bs-target="#editModal{{ a.id }}" >
<i class="bi bi-pencil me-1"></i>
{% trans 'Edit' %}
</button>
<button
type="button"
class="btn btn-sm btn-outline-danger d-flex align-items-center"
data-bs-toggle="modal" data-bs-target="#deleteModal{{ a.id }}">
<i class="bi bi-trash me-1"></i>
{% trans 'Delete' %}
</button>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<!-- pop up modal for delete confirmation -->
{% for a in object.get_user_properties %}
<div class="modal fade" id="deleteModal{{ a.id }}" tabindex="-1" aria-labelledby="deleteModalLabel{{ a.id }}" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel{{ a.id }}">{% trans "Confirm Deletion" %}
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
</button>
</div>
<div class="modal-body">
<p>
<strong>{% trans "Key:" %}
</strong> {{ a.key }}
</p>
<p>
<strong>{% trans "Value:" %}
</strong> {{ a.value }}
</p>
<p>
<strong>{% trans "Created on:" %}
</strong> {{ a.created }}
</p>
</div>
<div class="modal-footer justify-content-center">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}
</button>
<form method="post" action="{% url 'device:delete_user_property' a.id %}">
{% csrf_token %}
<button type="submit" class="btn btn-danger">{% trans "Delete" %}
</button>
</form>
</div>
</div>
</div>
</div>
{% endfor %}
<!-- popup modals for edit button -->
{% for a in object.get_user_properties %}
<div class="modal fade" id="editModal{{ a.id }}" tabindex="-1" aria-labelledby="editModalLabel{{ a.id }}" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editModalLabel{{ a.id }}">{% trans "Edit User Property" %}
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
</button>
</div>
<div class="modal-body">
<form id="editForm{{ a.id }}" method="post" action="{% url 'device:update_user_property' a.id %}">
{% csrf_token %}
<div class="mb-3">
<label for="key" class="form-label">{% trans "Key" %}
</label>
<input type="text" class="form-control" id="key" name="key" value="{{ a.key }}">
</div>
<div class="mb-3">
<label for="value" class="form-label">{% trans "Value" %}
</label>
<input type="text" class="form-control" id="value" name="value" value="{{ a.value }}">
</div>
<div class="modal-footer justify-content-center">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}
</button>
<button type="submit" class="btn btn-primary">{% trans "Save changes" %}
</button>
</div>
</form>
</div>
</div>
</div>
</div>
{% endfor %}

View file

@ -7,7 +7,9 @@ urlpatterns = [
path("add/", views.NewDeviceView.as_view(), name="add"),
path("edit/<str:pk>/", views.EditDeviceView.as_view(), name="edit"),
path("<str:pk>/", views.DetailsView.as_view(), name="details"),
path("<str:pk>/annotation/add", views.AddAnnotationView.as_view(), name="add_annotation"),
path("<str:pk>/user_property/add", views.AddUserPropertyView.as_view(), name="add_user_property"),
path("user_property/<int:pk>/delete", views.DeleteUserPropertyView.as_view(), name="delete_user_property"),
path("user_property/<int:pk>/update", views.UpdateUserPropertyView.as_view(), name="update_user_property"),
path("<str:pk>/document/add", views.AddDocumentView.as_view(), name="add_document"),
path("<str:pk>/public/", views.PublicDeviceWebView.as_view(), name="device_web"),

View file

@ -1,16 +1,21 @@
import json
import logging
from django.http import JsonResponse
from django.conf import settings
from django.urls import reverse_lazy
from django.shortcuts import get_object_or_404, Http404
from django.contrib import messages
from django.shortcuts import get_object_or_404, redirect, Http404
from django.utils.translation import gettext_lazy as _
from django.views.generic.edit import (
CreateView,
UpdateView,
FormView,
DeleteView,
)
from django.views.generic.base import TemplateView
from action.models import StateDefinition, State, DeviceLog, Note
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 +75,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 +93,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']
@ -110,11 +115,20 @@ class DetailsView(DashboardView, TemplateView):
uuid__in=self.object.uuids,
type=PROOF_TYPE["IssueDPP"]
)
last_evidence= self.object.get_last_evidence(),
uuid=self.object.last_uuid()
state_definitions = StateDefinition.objects.filter(
institution=self.request.user.institution
).order_by('order')
context.update({
'object': self.object,
'snapshot': self.object.get_last_evidence(),
'snapshot': last_evidence,
'lot_tags': lot_tags,
'dpps': dpps,
"state_definitions": state_definitions,
"device_states": State.objects.filter(snapshot_uuid=uuid).order_by('-date'),
"device_logs": DeviceLog.objects.filter(snapshot_uuid=uuid).order_by('-date'),
"device_notes": Note.objects.filter(snapshot_uuid=uuid).order_by('-date'),
})
return context
@ -175,65 +189,136 @@ class PublicDeviceWebView(TemplateView):
return JsonResponse(device_data)
class AddAnnotationView(DashboardView, CreateView):
template_name = "new_annotation.html"
title = _("New annotation")
breadcrumb = "Device / New annotation"
success_url = reverse_lazy('dashboard:unassigned_devices')
model = Annotation
class AddUserPropertyView(DashboardView, CreateView):
template_name = "new_user_property.html"
title = _("New User Property")
breadcrumb = "Device / New Property"
model = UserProperty
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.USER
form.instance.uuid = self.property.uuid
form.instance.type = UserProperty.Type.USER
message = _("<Created> UserProperty: {}: {}".format(form.instance.key, form.instance.value))
DeviceLog.objects.create(
snapshot_uuid=form.instance.uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution
)
messages.success(self.request, _("User property successfully added."))
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(
owner=institution,
value=pk,
type=Annotation.Type.SYSTEM
).first()
self.property = get_object_or_404(SystemProperty, owner=institution, value=pk)
if not self.annotation:
raise Http404
return super().get_form_kwargs()
self.success_url = reverse_lazy('device:details', args=[pk])
kwargs = super().get_form_kwargs()
return kwargs
def get_success_url(self):
return reverse_lazy('device:details', args=[self.kwargs.get('pk')])
class UpdateUserPropertyView(DashboardView, UpdateView):
template_name = "new_user_property.html"
title = _("Update User Property")
breadcrumb = "Device / Update Property"
model = UserProperty
fields = ("key", "value")
def get_queryset(self):
pk = self.kwargs.get('pk')
institution = self.request.user.institution
return UserProperty.objects.filter(pk=pk, owner=institution)
def form_valid(self, form):
old_instance = self.get_object()
old_key = old_instance.key
old_value = old_instance.value
form.instance.owner = self.request.user.institution
form.instance.user = self.request.user
form.instance.type = UserProperty.Type.USER
new_key = form.cleaned_data['key']
new_value = form.cleaned_data['value']
message = _("<Updated> UserProperty: {}: {} to {}: {}".format(old_key, old_value, new_key, new_value))
DeviceLog.objects.create(
snapshot_uuid=form.instance.uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution
)
messages.success(self.request, _("User property updated successfully."))
return super().form_valid(form)
def get_success_url(self):
return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details', args=[self.object.pk]))
class DeleteUserPropertyView(DashboardView, DeleteView):
model = UserProperty
def get_queryset(self):
return UserProperty.objects.filter(owner=self.request.user.institution)
#using post() method because delete() method from DeleteView has some issues with messages framework
def post(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.delete()
message = _("<Deleted> User Property: {}:{}".format(self.object.key, self.object.value ))
DeviceLog.objects.create(
snapshot_uuid=self.object.uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution
)
messages.info(self.request, _("User property deleted successfully."))
return self.handle_success()
def handle_success(self):
return redirect(self.get_success_url())
def get_success_url(self):
return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details', args=[self.object.pk]))
class AddDocumentView(DashboardView, CreateView):
template_name = "new_annotation.html"
template_name = "new_user_property.html"
title = _("New Document")
breadcrumb = "Device / New document"
success_url = reverse_lazy('dashboard:unassigned_devices')
model = Annotation
model = UserProperty
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.uuid = self.property.uuid
form.instance.type = UserProperty.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.property = SystemProperty.objects.filter(
owner=institution,
value=pk,
type=Annotation.Type.SYSTEM
).first()
if not self.annotation:
if not self.property:
raise Http404
self.success_url = reverse_lazy('device:details', args=[pk])

View file

@ -65,6 +65,7 @@ ENABLE_EMAIL = config("ENABLE_EMAIL", default=True, cast=bool)
EVIDENCES_DIR = config("EVIDENCES_DIR", default=os.path.join(BASE_DIR, "db"))
# Application definition
INSTALLED_APPS = [
@ -215,6 +216,10 @@ LOGGING = {
'()': CustomFormatter,
'format': '%(levelname)s %(asctime)s %(message)s'
},
'verbose': {
'format': '{levelname} {asctime} {module} {message}',
'style': '{',
},
},
"handlers": {
"console": {
@ -237,7 +242,7 @@ LOGGING = {
"handlers": ["console"],
"level": "ERROR",
"propagate": False,
}
},
}
}

View file

@ -22,6 +22,7 @@ urlpatterns = [
path("", include("login.urls")),
path("dashboard/", include("dashboard.urls")),
path("evidence/", include("evidence.urls")),
path('action/', include('action.urls')),
path("device/", include("device.urls")),
path("admin/", include("admin.urls")),
path("user/", include("user.urls")),

View file

@ -141,6 +141,7 @@ config_phase() {
# # 15. Add inventory snapshots for user "${INIT_USER}".
if [ "${DEMO:-}" = 'true' ]; then
/usr/bin/time ./manage.py up_snapshots example/snapshots/ "${INIT_USER}"
./manage.py create_default_states "${INIT_ORG}"
fi
# remain next command as the last operation for this if conditional

View file

@ -4,12 +4,13 @@ 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, UserProperty
from utils.save_snapshots import move_json, save_in_disk
from action.models import DeviceLog
class UploadForm(forms.Form):
@ -30,11 +31,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",
@ -68,9 +69,8 @@ class UserTagForm(forms.Form):
self.pk = None
self.uuid = kwargs.pop('uuid', None)
self.user = kwargs.pop('user')
instance = Annotation.objects.filter(
instance = SystemProperty.objects.filter(
uuid=self.uuid,
type=Annotation.Type.SYSTEM,
key='CUSTOM_ID',
owner=self.user.institution
).first()
@ -86,9 +86,8 @@ 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',
owner=self.user.institution
).first()
@ -100,20 +99,31 @@ class UserTagForm(forms.Form):
return
if self.instance:
old_value = self.instance.value
if not self.tag:
message =_("<Deleted> Evidence Tag. Old Value: '{}'").format(old_value)
self.instance.delete()
self.instance.value = self.tag
self.instance.save()
return
Annotation.objects.create(
uuid=self.uuid,
type=Annotation.Type.SYSTEM,
key='CUSTOM_ID',
value=self.tag,
owner=self.user.institution,
user=self.user
)
else:
self.instance.value = self.tag
self.instance.save()
if old_value != self.tag:
message=_("<Updated> Evidence Tag. Old Value: '{}'. New Value: '{}'").format(old_value, self.tag)
else:
message =_("<Created> Evidence Tag. Value: '{}'").format(self.tag)
SystemProperty.objects.create(
uuid=self.uuid,
key='CUSTOM_ID',
value=self.tag,
owner=self.user.institution,
user=self.user
)
DeviceLog.objects.create(
snapshot_uuid=self.uuid,
event= message,
user=self.user,
institution=self.user.institution
)
class ImportForm(forms.Form):
@ -164,8 +174,8 @@ class ImportForm(forms.Form):
table = []
for row in self.rows:
doc = create_doc(row)
annotation = create_annotation(doc, self.user)
table.append((doc, annotation))
property = create_property(doc, self.user)
table.append((doc, property))
if commit:
for doc, cred in table:
@ -186,9 +196,9 @@ class EraseServerForm(forms.Form):
self.pk = None
self.uuid = kwargs.pop('uuid', None)
self.user = kwargs.pop('user')
instance = Annotation.objects.filter(
instance = UserProperty.objects.filter(
uuid=self.uuid,
type=Annotation.Type.ERASE_SERVER,
type=UserProperty.Type.ERASE_SERVER,
key='ERASE_SERVER',
owner=self.user.institution
).first()
@ -201,9 +211,9 @@ class EraseServerForm(forms.Form):
def clean(self):
self.erase_server = self.cleaned_data.get('erase_server', False)
self.instance = Annotation.objects.filter(
self.instance = UserProperty.objects.filter(
uuid=self.uuid,
type=Annotation.Type.ERASE_SERVER,
type=UserProperty.Type.ERASE_SERVER,
key='ERASE_SERVER',
owner=self.user.institution
).first()
@ -222,9 +232,9 @@ class EraseServerForm(forms.Form):
if self.instance:
return
Annotation.objects.create(
UserProperty.objects.create(
uuid=self.uuid,
type=Annotation.Type.ERASE_SERVER,
type=UserProperty.Type.ERASE_SERVER,
key='ERASE_SERVER',
value=self.erase_server,
owner=self.user.institution,

View file

@ -5,7 +5,7 @@ import logging
from django.core.management.base import BaseCommand
from django.conf import settings
from utils.device import create_annotation, create_doc, create_index
from utils.device import create_property, create_doc, create_index
from user.models import Institution
from evidence.parse import Build
@ -70,7 +70,7 @@ class Command(BaseCommand):
def build_placeholder(self, s, user, f_path):
try:
create_index(s, user)
create_annotation(s, user, commit=True)
create_property(s, user, commit=True)
except Exception as err:
txt = "In placeholder %s \n%s"
logger.warning(txt, f_path, err)

View file

@ -0,0 +1,107 @@
# Generated by Django 5.0.6 on 2024-12-10 19:37
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("evidence", "0002_alter_annotation_type"),
("user", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name="SystemProperty",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created", models.DateTimeField(auto_now_add=True)),
("key", models.CharField(max_length=256)),
("value", models.CharField(max_length=256)),
("uuid", models.UUIDField()),
(
"owner",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="user.institution",
),
),
(
"user",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL,
),
),
],
),
migrations.CreateModel(
name="UserProperty",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created", models.DateTimeField(auto_now_add=True)),
("key", models.CharField(max_length=256)),
("value", models.CharField(max_length=256)),
("uuid", models.UUIDField()),
(
"type",
models.SmallIntegerField(
choices=[(1, "User"), (2, "Document"), (3, "EraseServer")],
default=1,
),
),
(
"owner",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="user.institution",
),
),
(
"user",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL,
),
),
],
),
migrations.DeleteModel(
name="Annotation",
),
migrations.AddConstraint(
model_name="systemproperty",
constraint=models.UniqueConstraint(
fields=("key", "uuid"), name="system_unique_type_key_uuid"
),
),
migrations.AddConstraint(
model_name="userproperty",
constraint=models.UniqueConstraint(
fields=("key", "uuid", "type"), name="user_unique_type_key_uuid"
),
),
]

View file

@ -0,0 +1,17 @@
# Generated by Django 5.0.6 on 2024-12-18 12:11
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("evidence", "0003_systemproperty_userproperty_delete_annotation_and_more"),
]
operations = [
migrations.RemoveConstraint(
model_name="userproperty",
name="user_unique_type_key_uuid",
),
]

View file

@ -4,35 +4,48 @@ import hashlib
from dmidecode import DMIParse
from django.db import models
from django.db.models import Q
from utils.constants import STR_EXTEND_SIZE, CHASSIS_DH
from evidence.xapian import search
from evidence.parse_details import ParseSnapshot, get_inxi, get_inxi_key
from user.models import User, Institution
class Annotation(models.Model):
class Type(models.IntegerChoices):
SYSTEM = 0, "System"
USER = 1, "User"
DOCUMENT = 2, "Document"
ERASE_SERVER = 3, "EraseServer"
class Property(models.Model):
created = models.DateTimeField(auto_now_add=True)
uuid = models.UUIDField()
owner = models.ForeignKey(Institution, on_delete=models.CASCADE)
user = models.ForeignKey(
User, on_delete=models.SET_NULL, null=True, blank=True)
type = models.SmallIntegerField(choices=Type)
key = models.CharField(max_length=STR_EXTEND_SIZE)
value = models.CharField(max_length=STR_EXTEND_SIZE)
class Meta:
#Only for shared behaviour, it is not a table
abstract = True
class SystemProperty(Property):
uuid = models.UUIDField()
class Meta:
constraints = [
models.UniqueConstraint(
fields=["type", "key", "uuid"], name="unique_type_key_uuid")
fields=["key", "uuid"], name="system_unique_type_key_uuid")
]
class UserProperty(Property):
uuid = models.UUIDField()
class Type(models.IntegerChoices):
USER = 1, "User"
DOCUMENT = 2, "Document"
ERASE_SERVER = 3, "EraseServer"
type = models.SmallIntegerField(choices=Type, default=Type.USER)
class Evidence:
def __init__(self, uuid):
self.uuid = uuid
@ -41,22 +54,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
@ -118,7 +131,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():
@ -188,9 +201,8 @@ class Evidence:
@classmethod
def get_all(cls, user):
return Annotation.objects.filter(
return SystemProperty.objects.filter(
owner=user.institution,
type=Annotation.Type.SYSTEM,
key="hidalgo1",
).order_by("-created").values_list("uuid", "created").distinct()

View file

@ -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,22 @@ 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,
)
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,
key=k,
value=v
)

View file

@ -45,9 +45,8 @@
</th>
</tr>
</thead>
{% for snap in object.annotations %}
{% for snap in object.properties %}
<tbody>
{% if snap.type == 0 %}
<tr>
<td>
{{ snap.key }}
@ -63,7 +62,6 @@
</small>
</td>
</tr>
{% endif %}
</tbody>
{% endfor %}
</table>
@ -94,7 +92,7 @@
</div>
{% if form.tag.value %}
<div class="col-1">
<a class="btn btn-yellow" href="{% url 'evidence:delete_annotation' form.pk %}">{% translate "Delete" %}</a>
<a class="btn btn-yellow" href="{% url 'evidence:delete_tag' form.pk %}">{% translate "Delete" %}</a>
</div>
{% endif %}
</div>

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("tag/<str:pk>/delete", views.DeleteEvidenceTagView.as_view(), name="delete_tag"),
]

View file

@ -12,8 +12,9 @@ from django.views.generic.edit import (
FormView,
)
from action.models import DeviceLog
from dashboard.mixins import DashboardView, Http403
from evidence.models import Evidence, Annotation
from evidence.models import SystemProperty, UserProperty, Evidence
from evidence.forms import (
UploadForm,
UserTagForm,
@ -95,7 +96,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,33 +142,6 @@ class DownloadEvidenceView(DashboardView, TemplateView):
return response
class AnnotationDeleteView(DashboardView, DeleteView):
model = Annotation
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
try:
referer = self.request.META["HTTP_REFERER"]
path_referer = urlparse(referer).path
resolver_match = resolve(path_referer)
url_name = resolver_match.view_name
kwargs_view = resolver_match.kwargs
except:
# if is not possible resolve the reference path return 404
raise Http404
self.object = get_object_or_404(
self.model,
pk=self.pk,
owner=self.request.user.institution
)
self.object.delete()
return redirect(url_name, **kwargs_view)
class EraseServerView(DashboardView, FormView):
template_name = "ev_eraseserver.html"
section = "evidences"
@ -182,7 +156,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):
@ -211,3 +185,35 @@ class EraseServerView(DashboardView, FormView):
def get_success_url(self):
success_url = reverse_lazy('evidence:details', args=[self.pk])
return success_url
class DeleteEvidenceTagView(DashboardView, DeleteView):
model = SystemProperty
def get_queryset(self):
# only those with 'CUSTOM_ID'
return SystemProperty.objects.filter(owner=self.request.user.institution, key='CUSTOM_ID')
def get(self, request, *args, **kwargs):
self.object = self.get_object()
message = _("<Deleted> Evidence Tag: {}").format(self.object.value)
DeviceLog.objects.create(
snapshot_uuid=self.object.uuid,
event=message,
user=self.request.user,
institution=self.request.user.institution
)
self.object.delete()
messages.info(self.request, _("Evicende Tag deleted successfully."))
return self.handle_success()
def handle_success(self):
return redirect(self.get_success_url())
def get_success_url(self):
return self.request.META.get(
'HTTP_REFERER',
reverse_lazy('evidence:details', args=[self.object.uuid])
)

View file

@ -0,0 +1,77 @@
# Generated by Django 5.0.6 on 2024-12-10 19:37
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("lot", "0002_alter_lot_closed"),
("user", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name="LotProperty",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created", models.DateTimeField(auto_now_add=True)),
("key", models.CharField(max_length=256)),
("value", models.CharField(max_length=256)),
(
"type",
models.SmallIntegerField(
choices=[
(0, "System"),
(1, "User"),
(2, "Document"),
(3, "EraseServer"),
],
default=1,
),
),
(
"lot",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="lot.lot"
),
),
(
"owner",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="user.institution",
),
),
(
"user",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL,
),
),
],
),
migrations.DeleteModel(
name="LotAnnotation",
),
migrations.AddConstraint(
model_name="lotproperty",
constraint=models.UniqueConstraint(
fields=("key", "lot", "type"), name="lot_unique_type_key_lot"
),
),
]

View file

@ -0,0 +1,24 @@
# Generated by Django 5.0.6 on 2024-12-18 12:11
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("lot", "0003_lotproperty_delete_lotannotation_and_more"),
]
operations = [
migrations.RemoveConstraint(
model_name="lotproperty",
name="lot_unique_type_key_lot",
),
migrations.AlterField(
model_name="lotproperty",
name="type",
field=models.SmallIntegerField(
choices=[(0, "System"), (1, "User"), (2, "Document")], default=1
),
),
]

View file

@ -6,9 +6,9 @@ from utils.constants import (
STR_EXTEND_SIZE,
)
from user.models import User, Institution
from user.models import User, Institution
from evidence.models import Property
# from device.models import Device
# from evidence.models import Annotation
class LotTag(models.Model):
@ -45,17 +45,12 @@ class Lot(models.Model):
for d in DeviceLot.objects.filter(lot=self, device_id=v):
d.delete()
class LotProperty (Property):
lot = models.ForeignKey(Lot, on_delete=models.CASCADE)
class LotAnnotation(models.Model):
class Type(models.IntegerChoices):
SYSTEM= 0, "System"
SYSTEM = 0, "System"
USER = 1, "User"
DOCUMENT = 2, "Document"
created = models.DateTimeField(auto_now_add=True)
lot = models.ForeignKey(Lot, on_delete=models.CASCADE)
owner = models.ForeignKey(Institution, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
type = models.SmallIntegerField(choices=Type)
key = models.CharField(max_length=STR_EXTEND_SIZE)
value = models.CharField(max_length=STR_EXTEND_SIZE)
type = models.SmallIntegerField(choices=Type.choices, default=Type.USER)

View file

@ -1,48 +0,0 @@
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<div class="row">
<div class="col">
<h3>Lot {{ lot.name }}</h3>
</div>
</div>
<div class="row">
<div class="tab-pane fade show active" id="details">
<div class="btn-group dropdown ml-1 mt-1" uib-dropdown="">
<a href="{% url 'lot:add_annotation' lot.pk %}" class="btn btn-primary">
<i class="bi bi-plus"></i>
Add new annotation
<span class="caret"></span>
</a>
</div>
<h5 class="card-title mt-2">Annotations</h5>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Key</th>
<th scope="col">Value</th>
<th scope="col" data-type="date" data-format="YYYY-MM-DD hh:mm">Created on</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{% for a in annotations %}
<tr>
<td>{{ a.key }}</td>
<td>{{ a.value }}</td>
<td>{{ a.created }}</td>
<td></td>
<td></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}

View file

@ -0,0 +1,105 @@
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<div class="row">
<div class="col">
<h3>Lot {{ lot.name }}</h3>
</div>
</div>
<div class="row">
<div class="tab-pane fade show active" id="details">
<div class="btn-group dropdown ml-1 mt-1" uib-dropdown="">
<a href="{% url 'lot:add_property' lot.pk %}" class="btn btn-primary">
<i class="bi bi-plus"></i>
Add new lot Property
<span class="caret"></span>
</a>
</div>
<h5 class="card-title mt-2">Properties</h5>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Key</th>
<th scope="col">Value</th>
<th scope="col" data-type="date" data-format="YYYY-MM-DD hh:mm">Created on</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{% for a in properties %}
<tr>
<td>{{ a.key }}</td>
<td>{{ a.value }}</td>
<td>{{ a.created }}</td>
<td class="text-center">
<a href="#" class="text-info" data-bs-toggle="modal" data-bs-target="#editPropertyModal{{ a.id }}">
<i class="bi bi-pencil"></i>
</a>
</td>
<td class="text-center">
<a href="#" class="text-danger" data-bs-toggle="modal" data-bs-target="#deletePropertyModal{{ a.id }}">
<i class="bi bi-trash"></i>
</a>
</td>
</tr>
<div class="modal fade" id="editPropertyModal{{ a.id }}" tabindex="-1" aria-labelledby="editPropertyModalLabel{{ a.id }}" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editPropertyModalLabel{{ a.id }}">{% trans "Edit Property" %}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{% trans 'Close' %}"></button>
</div>
<div class="modal-body">
<form method="post" action="{% url 'lot:update_property' a.id %}">
{% csrf_token %}
<div class="mb-3">
<label for="propertyKey{{ a.id }}" class="form-label">{% trans "Key" %}</label>
<input type="text" class="form-control" id="propertyKey{{ a.id }}" name="key" value="{{ a.key }}" required>
</div>
<div class="mb-3">
<label for="propertyValue{{ a.id }}" class="form-label">{% trans "Value" %}</label>
<input type="text" class="form-control" id="propertyValue{{ a.id }}" name="value" value="{{ a.value }}" required>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
<button type="submit" class="btn btn-primary">{% trans "Save changes" %}</button>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="modal fade" id="deletePropertyModal{{ a.id }}" tabindex="-1" aria-labelledby="deletePropertyModalLabel{{ a.id }}" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deletePropertyModalLabel{{ a.id }}">{% trans "Delete Property" %}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{% trans 'Close' %}"></button>
</div>
<div class="modal-body">
<p>{% trans "Are you sure you want to delete this property?" %}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
<form method="post" action="{% url 'lot:delete_property' a.id %}">
{% csrf_token %}
<button type="submit" class="btn btn-danger">{% trans "Delete" %}</button>
</form>
</div>
</div>
</div>
</div>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}

View file

@ -12,6 +12,8 @@ urlpatterns = [
path("tag/<int:pk>/", views.LotsTagsView.as_view(), name="tag"),
path("<int:pk>/document/", views.LotDocumentsView.as_view(), name="documents"),
path("<int:pk>/document/add", views.LotAddDocumentView.as_view(), name="add_document"),
path("<int:pk>/annotation", views.LotAnnotationsView.as_view(), name="annotations"),
path("<int:pk>/annotation/add", views.LotAddAnnotationView.as_view(), name="add_annotation"),
path("<int:pk>/property", views.LotPropertiesView.as_view(), name="properties"),
path("<int:pk>/property/add", views.AddLotPropertyView.as_view(), name="add_property"),
path("<int:pk>/property/update", views.UpdateLotPropertyView.as_view(), name="update_property"),
path("<int:pk>/property/delete", views.DeleteLotPropertyView.as_view(), name="delete_property"),
]

View file

@ -1,5 +1,6 @@
from django.urls import reverse_lazy
from django.shortcuts import get_object_or_404
from django.shortcuts import get_object_or_404, redirect, Http404
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
from django.views.generic.base import TemplateView
from django.views.generic.edit import (
@ -9,10 +10,9 @@ from django.views.generic.edit import (
FormView,
)
from dashboard.mixins import DashboardView
from lot.models import Lot, LotTag, LotAnnotation
from lot.models import Lot, LotTag, LotProperty
from lot.forms import LotsForm
class NewLotView(DashboardView, CreateView):
template_name = "new_lot.html"
title = _("New lot")
@ -143,18 +143,18 @@ class LotsTagsView(DashboardView, TemplateView):
class LotAddDocumentView(DashboardView, CreateView):
template_name = "new_annotation.html"
template_name = "new_property.html"
title = _("New Document")
breadcrumb = "Device / New document"
success_url = reverse_lazy('dashboard:unassigned_devices')
model = LotAnnotation
model = LotProperty
fields = ("key", "value")
def form_valid(self, form):
form.instance.owner = self.request.user.institution
form.instance.user = self.request.user
form.instance.lot = self.lot
form.instance.type = LotAnnotation.Type.DOCUMENT
form.instance.type = LotProperty.Type.DOCUMENT
response = super().form_valid(form)
return response
@ -169,16 +169,16 @@ class LotAddDocumentView(DashboardView, CreateView):
class LotDocumentsView(DashboardView, TemplateView):
template_name = "documents.html"
title = _("New Document")
breadcrumb = "Device / New document"
breadcrumb = "Devicce / New document"
def get_context_data(self, **kwargs):
self.pk = kwargs.get('pk')
context = super().get_context_data(**kwargs)
lot = get_object_or_404(Lot, owner=self.request.user.institution, id=self.pk)
documents = LotAnnotation.objects.filter(
documents = LotProperty.objects.filter(
lot=lot,
owner=self.request.user.institution,
type=LotAnnotation.Type.DOCUMENT,
type=LotProperty.Type.DOCUMENT,
)
context.update({
'lot': lot,
@ -189,48 +189,106 @@ class LotDocumentsView(DashboardView, TemplateView):
return context
class LotAnnotationsView(DashboardView, TemplateView):
template_name = "annotations.html"
title = _("New Annotation")
breadcrumb = "Device / New annotation"
class LotPropertiesView(DashboardView, TemplateView):
template_name = "properties.html"
title = _("New Lot Property")
breadcrumb = "Lot / New property"
def get_context_data(self, **kwargs):
self.pk = kwargs.get('pk')
context = super().get_context_data(**kwargs)
lot = get_object_or_404(Lot, owner=self.request.user.institution, id=self.pk)
annotations = LotAnnotation.objects.filter(
properties = LotProperty.objects.filter(
lot=lot,
owner=self.request.user.institution,
type=LotAnnotation.Type.USER,
type=LotProperty.Type.USER,
)
context.update({
'lot': lot,
'annotations': annotations,
'properties': properties,
'title': self.title,
'breadcrumb': self.breadcrumb
})
return context
class LotAddAnnotationView(DashboardView, CreateView):
template_name = "new_annotation.html"
title = _("New Annotation")
breadcrumb = "Device / New annotation"
class AddLotPropertyView(DashboardView, CreateView):
template_name = "new_property.html"
title = _("New Lot Property")
breadcrumb = "Device / New property"
success_url = reverse_lazy('dashboard:unassigned_devices')
model = LotAnnotation
model = LotProperty
fields = ("key", "value")
def form_valid(self, form):
form.instance.owner = self.request.user.institution
form.instance.user = self.request.user
form.instance.lot = self.lot
form.instance.type = LotAnnotation.Type.USER
form.instance.type = LotProperty.Type.USER
response = super().form_valid(form)
return response
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
self.lot = get_object_or_404(Lot, pk=pk, owner=self.request.user.institution)
self.success_url = reverse_lazy('lot:annotations', args=[pk])
self.success_url = reverse_lazy('lot:properties', args=[pk])
kwargs = super().get_form_kwargs()
return kwargs
class UpdateLotPropertyView(DashboardView, UpdateView):
template_name = "properties.html"
title = _("Update lot Property")
breadcrumb = "Lot / Update Property"
model = LotProperty
fields = ("key", "value")
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
lot_property = get_object_or_404(LotProperty, pk=pk, owner=self.request.user.institution)
if not lot_property:
raise Http404
kwargs = super().get_form_kwargs()
kwargs['instance'] = lot_property
return kwargs
def form_valid(self, form):
old_key= self.object.key
old_value = self.object.value
new_key = form.cleaned_data['key']
new_value = form.cleaned_data['value']
form.instance.owner = self.request.user.institution
form.instance.user = self.request.user
form.instance.type = LotProperty.Type.USER
response = super().form_valid(form)
messages.success(self.request, _("Lot property updated successfully."))
return response
def get_success_url(self):
return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details', args=[self.object.pk]))
class DeleteLotPropertyView(DashboardView, DeleteView):
model = LotProperty
def post(self, request, *args, **kwargs):
self.pk = kwargs['pk']
referer = request.META.get('HTTP_REFERER')
if not referer:
raise Http404("No referer header found")
self.object = get_object_or_404(
self.model,
pk=self.pk,
owner=self.request.user.institution
)
old_value = self.object.key
self.object.delete()
messages.success(self.request, _("Lot property deleted successfully."))
# Redirect back to the original URL
return redirect(referer)

8
tests/end-to-end/.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
node_modules/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
test-examples
example.spec.ts

97
tests/end-to-end/package-lock.json generated Normal file
View file

@ -0,0 +1,97 @@
{
"name": "end-to-end",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "end-to-end",
"version": "1.0.0",
"license": "ISC",
"devDependencies": {
"@playwright/test": "^1.49.1",
"@types/node": "^22.10.7"
}
},
"node_modules/@playwright/test": {
"version": "1.49.1",
"resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.49.1.tgz",
"integrity": "sha512-Ky+BVzPz8pL6PQxHqNRW1k3mIyv933LML7HktS8uik0bUXNCdPhoS/kLihiO1tMf/egaJb4IutXd7UywvXEW+g==",
"dev": true,
"license": "Apache-2.0",
"dependencies": {
"playwright": "1.49.1"
},
"bin": {
"playwright": "cli.js"
},
"engines": {
"node": ">=18"
}
},
"node_modules/@types/node": {
"version": "22.10.7",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.10.7.tgz",
"integrity": "sha512-V09KvXxFiutGp6B7XkpaDXlNadZxrzajcY50EuoLIpQ6WWYCSvf19lVIazzfIzQvhUN2HjX12spLojTnhuKlGg==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": "~6.20.0"
}
},
"node_modules/fsevents": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz",
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/playwright": {
"version": "1.49.1",
"resolved": "https://registry.npmjs.org/playwright/-/playwright-1.49.1.tgz",
"integrity": "sha512-VYL8zLoNTBxVOrJBbDuRgDWa3i+mfQgDTrL8Ah9QXZ7ax4Dsj0MSq5bYgytRnDVVe+njoKnfsYkH3HzqVj5UZA==",
"dev": true,
"license": "Apache-2.0",
"dependencies": {
"playwright-core": "1.49.1"
},
"bin": {
"playwright": "cli.js"
},
"engines": {
"node": ">=18"
},
"optionalDependencies": {
"fsevents": "2.3.2"
}
},
"node_modules/playwright-core": {
"version": "1.49.1",
"resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.49.1.tgz",
"integrity": "sha512-BzmpVcs4kE2CH15rWfzpjzVGhWERJfmnXmniSyKeRZUs9Ws65m+RGIi7mjJK/euCegfn3i7jvqWeWyHe9y3Vgg==",
"dev": true,
"license": "Apache-2.0",
"bin": {
"playwright-core": "cli.js"
},
"engines": {
"node": ">=18"
}
},
"node_modules/undici-types": {
"version": "6.20.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.20.0.tgz",
"integrity": "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==",
"dev": true,
"license": "MIT"
}
}
}

View file

@ -0,0 +1,15 @@
{
"name": "end-to-end",
"version": "1.0.0",
"main": "index.js",
"scripts": {},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"description": "",
"devDependencies": {
"@playwright/test": "^1.49.1",
"@types/node": "^22.10.7"
}
}

View file

@ -0,0 +1,77 @@
import { defineConfig, devices } from '@playwright/test';
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},
/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },
/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],
/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
});

21
tests/end-to-end/run.sh Executable file
View file

@ -0,0 +1,21 @@
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-or-later
set -e
set -u
# DEBUG
set -x
main() {
cd "$(dirname "${0}")"
browser="${browser:-firefox}"
project="${project:-firefox}"
headed="${headed:---headed}"
npx playwright test --project "${project}" "${headed}"
}
main "${@}"
# written in emacs
# -*- mode: shell-script; -*-

View file

@ -0,0 +1,20 @@
import { test, expect } from '@playwright/test';
// TODO after the tests, put again demo.ereuse.org as default
const TEST_SITE = process.env.TEST_SITE || 'https://lab1.ereuse.org'
const TEST_USER = 'user@example.org'
const TEST_PASSWD = '1234'
async function login(page, date, time) {
await page.goto(TEST_SITE);
await page.getByPlaceholder('Email address').click();
await page.getByPlaceholder('Email address').fill(TEST_USER);
await page.getByPlaceholder('Password').fill(TEST_PASSWD);
await page.getByPlaceholder('Password').press('Enter');
await page.getByRole('button', { name: 'Next' }).click();
}
test('example', async ({ page }) => {
await login(page);
await page.pause();
});

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
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 []
@ -76,25 +76,23 @@ def create_annotation(doc, user, commit=False):
'uuid': doc['uuid'],
'owner': user.institution,
'user': user,
'type': Annotation.Type.SYSTEM,
'key': 'CUSTOMER_ID',
'value': doc['CUSTOMER_ID'],
}
if commit:
annotation = Annotation.objects.filter(
prop = SystemProperty.objects.filter(
uuid=doc["uuid"],
owner=user.institution,
type=Annotation.Type.SYSTEM,
)
if annotation:
txt = "Warning: Snapshot %s already registered (annotation exists)"
if prop:
txt = "Warning: Snapshot %s already registered (system property exists)"
logger.warning(txt, doc["uuid"])
return annotation
return prop
return Annotation.objects.create(**data)
return SystemProperty.objects.create(**data)
return Annotation(**data)
return SystemProperty(**data)
def create_index(doc, user):