From 3efe74c638b2cb8b6455671fb644c9d217b82dd7 Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Fri, 3 Jan 2025 03:24:08 -0300 Subject: [PATCH] notes now can be updated --- action/views.py | 28 +++++++++++- device/templates/details.html | 81 +++++++++++++++++++++++++++++++---- 2 files changed, 100 insertions(+), 9 deletions(-) diff --git a/action/views.py b/action/views.py index 21aadb3..9e78390 100644 --- a/action/views.py +++ b/action/views.py @@ -2,7 +2,7 @@ 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, FormView +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 @@ -68,3 +68,29 @@ class AddNoteView(View): messages.error(request, "There was an error with your submission.") return redirect(request.META.get('HTTP_REFERER') ) + +class UpdateNoteView(UpdateView): + model = Note + fields = ['description'] + template_name = 'device/templates/details.html' + 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 = _(" 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 get_success_url(self): + return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details')) diff --git a/device/templates/details.html b/device/templates/details.html index a53e41c..b8191e3 100644 --- a/device/templates/details.html +++ b/device/templates/details.html @@ -18,20 +18,66 @@
{% trans "Latest Notes" %}
-
+
- {% for note in device_notes|slice:":5" %} + {% for note in device_notes|slice:":4" %}
-
- - {{ note.user.get_full_name|default:note.user.username }} - +
{{ note.date|timesince }} {% trans "ago" %}
-

{{ note.description }}

+ {% if user == note.user or user.is_admin %} +
+ {% else %} +
+ {% endif %} +

+ {{ note.description }} +

+
+ {{ note.user.get_full_name|default:note.user.username }} +
+
+ + {% if user == note.user or user.is_admin %} +
+ +
+ {% csrf_token %} + + + + +
+ +
+ {% csrf_token %} + + + +
+
+ {% endif %} +
{% empty %} @@ -39,7 +85,8 @@ {% endfor %}
- + +

{{ object.shortid }}

@@ -186,5 +233,23 @@ } } }) + + //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"); + saveLink.classList.add("text-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(); + } {% endblock %}