diff --git a/action/urls.py b/action/urls.py index d38bc20..0cdd630 100644 --- a/action/urls.py +++ b/action/urls.py @@ -8,4 +8,5 @@ urlpatterns = [ path("new/", views.ChangeStateView.as_view(), name="change_state"), path('note/add/', views.AddNoteView.as_view(), name='add_note'), path('note/edit/', views.UpdateNoteView.as_view(), name='update_note'), + path('note/delete/', views.DeleteNoteView.as_view(), name='delete_note'), ] diff --git a/action/views.py b/action/views.py index 9e78390..8cf0a81 100644 --- a/action/views.py +++ b/action/views.py @@ -72,7 +72,7 @@ class AddNoteView(View): class UpdateNoteView(UpdateView): model = Note fields = ['description'] - template_name = 'device/templates/details.html' + template_name = "blank.html" pk_url_kwarg = 'pk' def form_valid(self, form): @@ -94,3 +94,38 @@ class UpdateNoteView(UpdateView): 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 = _(" 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) diff --git a/device/templates/details.html b/device/templates/details.html index b8191e3..1ed0789 100644 --- a/device/templates/details.html +++ b/device/templates/details.html @@ -19,10 +19,10 @@
{% trans "Latest Notes" %}
-
+
{% for note in device_notes|slice:":4" %}
-
+
{{ note.date|timesince }} {% trans "ago" %} @@ -49,6 +49,7 @@ {% if user == note.user or user.is_admin %}
+
-
- {% csrf_token %} - - - -
+ +
+
+ {% csrf_token %} + +
{% endif %}
@@ -188,6 +195,7 @@ {% include 'tabs/evidences.html' %} +