Add user tag for identify a device from form

This commit is contained in:
Cayo Puigdefabregas 2024-08-01 14:33:09 +02:00
parent 1a44d40227
commit aa2b8a750d
4 changed files with 78 additions and 7 deletions

View File

@ -9,7 +9,6 @@ from evidence.models import Annotation
class UploadForm(forms.Form):
evidence_file = MultipleFileField(label=_("File"))
def clean(self):
@ -47,3 +46,30 @@ class UploadForm(forms.Form):
for ev in self.evidences:
Build(ev[1], user)
class UserTagForm(forms.Form):
tag = forms.CharField(label=_("Tag"))
def __init__(self, *args, **kwargs):
self.uuid = kwargs.pop('uuid', None)
super().__init__(*args, **kwargs)
def clean(self):
data = self.cleaned_data.get('tag')
if not data:
return False
self.tag = data
return True
def save(self, user, commit=True):
if not commit:
return
Annotation.objects.create(
uuid=self.uuid,
owner=user,
type=Annotation.Type.SYSTEM,
key='CUSTOM_ID',
value=self.tag
)

View File

@ -45,5 +45,29 @@
{% endfor %}
</div>
</div>
<div class="tab-pane fade" id="tag">
{% load django_bootstrap5 %}
<div class="list-group col-6">
<form role="form" method="post">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert">
<div class="icon"><span class="mdi mdi-close-circle-o"></span></div>
<div class="message">
{% for field, error in form.errors.items %}
{{ error }}<br />
{% endfor %}
<button class="btn-close" type="button" data-dismiss="alert" aria-label="Close"></button>
</div>
</div>
{% endif %}
{% bootstrap_form form %}
<div class="container">
<a class="btn btn-grey" href="{% url 'dashboard:unassigned_devices' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
</form>
</div>
</div>
</div>
{% endblock %}

View File

@ -7,7 +7,7 @@ from django.views.generic.edit import (
from dashboard.mixins import DashboardView, Http403
from evidence.models import Evidence
from evidence.forms import UploadForm
from evidence.forms import UploadForm, UserTagForm
# from django.shortcuts import render
# from rest_framework import viewsets
# from snapshot.serializers import SnapshotSerializer
@ -52,11 +52,13 @@ class UploadView(DashboardView, FormView):
return response
class EvidenceView(DashboardView, TemplateView):
class EvidenceView(DashboardView, FormView):
template_name = "ev_details.html"
section = "evidences"
title = _("Evidences")
breadcrumb = "Evidences / Details"
success_url = reverse_lazy('evidence:list')
form_class = UserTagForm
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
@ -73,3 +75,22 @@ class EvidenceView(DashboardView, TemplateView):
'object': self.object,
})
return context
def get_form_kwargs(self):
self.pk = self.kwargs.get('pk')
kwargs = super().get_form_kwargs()
kwargs['uuid'] = self.pk
return kwargs
def form_valid(self, form):
form.save(self.request.user)
response = super().form_valid(form)
return response
def form_invalid(self, form):
response = super().form_invalid(form)
return response
def get_success_url(self):
success_url = reverse_lazy('evidence:details', args=[self.pk])
return success_url