diff --git a/action/forms.py b/action/forms.py index 4d391f2..cf86646 100644 --- a/action/forms.py +++ b/action/forms.py @@ -2,21 +2,15 @@ from django import forms from .models import State -class AddStateForm(forms.Form): - add_note = forms.BooleanField(required=False) +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=False, + required=True, widget=forms.Textarea(attrs={'rows': 4, 'maxlength': 200, 'placeholder': 'Max 200 characters'}), ) - state_id = forms.IntegerField(required=True, widget=forms.HiddenInput()) - snapshot_uuid = forms.UUIDField(required=True, widget=forms.HiddenInput()) - - - def clean(self): - cleaned_data = super().clean() - add_note = cleaned_data.get('add_note') - note = cleaned_data.get('note') - - if add_note == True and not note: - self.add_error('note', 'Please enter a note if you checked "Add a note".') - return cleaned_data \ No newline at end of file diff --git a/action/models.py b/action/models.py index 7c3fa07..8a5633d 100644 --- a/action/models.py +++ b/action/models.py @@ -58,7 +58,7 @@ 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(max_lenght=250) + description = models.TextField() snapshot_uuid = models.UUIDField() class Meta: diff --git a/action/urls.py b/action/urls.py index 9c002dd..193a431 100644 --- a/action/urls.py +++ b/action/urls.py @@ -5,7 +5,8 @@ app_name = 'action' urlpatterns = [ - path("new/", views.NewActionView.as_view(), name="new_action"), - path('state//undo/', views.ActionUndoView.as_view(), name='undo_action'), + path("new/", views.ChangeStateView.as_view(), name="change_state"), + path('state//undo/', views.UndoStateView.as_view(), name='undo_state'), + path('note/add/', views.AddNoteView.as_view(), name='add_note'), ] diff --git a/action/views.py b/action/views.py index 3c70e4d..a3e036c 100644 --- a/action/views.py +++ b/action/views.py @@ -1,10 +1,11 @@ from django.views import View from django.shortcuts import redirect, get_object_or_404 from django.contrib import messages -from action.forms import AddStateForm -from django.views.generic.edit import DeleteView +from action.forms import ChangeStateForm, AddNoteForm +from django.views.generic.edit import DeleteView, CreateView, FormView from django.urls import reverse_lazy -from action.models import State, StateDefinition +from django.utils.translation import gettext_lazy as _ +from action.models import State, StateDefinition, Note from device.models import Device import logging @@ -12,36 +13,35 @@ import logging device_logger = logging.getLogger('device_log') -class NewActionView(View): +class ChangeStateView(View): def post(self, request, *args, **kwargs): - form = AddStateForm(request.POST) + form = ChangeStateForm(request.POST) if form.is_valid(): - state_definition_id = form.cleaned_data['state_id'] - state_definition = get_object_or_404(StateDefinition, pk=state_definition_id) + previous_state = form.cleaned_data['previous_state'] + new_state = form.cleaned_data['new_state'] snapshot_uuid = form.cleaned_data['snapshot_uuid'] - #TODO: implement notes - note = form.cleaned_data.get('note', '') - state = State.objects.create( + State.objects.create( snapshot_uuid=snapshot_uuid, - state=state_definition.state, - user=request.user, - institution=request.user.institution, + state=new_state, + user=self.request.user, + institution=self.request.user.institution, ) - #TODO: also change logger for full fledged table - device_logger.info(f" State to '{state_definition.state}', for device '{snapshot_uuid}') by user {self.request.user}.") - messages.success(request, f"Action to '{state_definition.state}' has been added.") - return redirect(request.META.get('HTTP_REFERER')) + device_logger.info(f" State to '{new_state}', from '{previous_state}' ) by user {self.request.user}.") + + message = _("State changed from '{}' to '{}'.".format(previous_state, new_state) ) + messages.success(request,message) else: messages.error(request, "There was an error with your submission.") - return redirect(request.META.get('HTTP_REFERER')) + + return redirect(request.META.get('HTTP_REFERER') ) -class ActionUndoView(DeleteView): - model = State +class UndoStateView(DeleteView): + model = State def delete(self, request, *args, **kwargs): self.object = self.get_object() @@ -52,3 +52,24 @@ class ActionUndoView(DeleteView): messages.info(self.request, f"Action to state: {self.object.state} has been deleted.") device_logger.info(f" State '{self.object.state}', for device '{self.object.snapshot_uuid}') by user {self.request.user}.") return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details', args=[self.object.snapshot_uuid])) + + +class AddNoteView(View): + + def post(self, request, *args, **kwargs): + form = AddNoteForm(request.POST) + + if form.is_valid(): + note = form.cleaned_data['note'] + snapshot_uuid = form.cleaned_data['snapshot_uuid'] + Note.objects.create( + snapshot_uuid=snapshot_uuid, + description=note, + user=self.request.user, + institution=self.request.user.institution, + ) + messages.success(request, _("Note has been added")) + else: + messages.error(request, "There was an error with your submission.") + + return redirect(request.META.get('HTTP_REFERER') ) diff --git a/device/templates/details.html b/device/templates/details.html index 221c845..20efa5e 100644 --- a/device/templates/details.html +++ b/device/templates/details.html @@ -5,36 +5,56 @@
-

{{ object.shortid }} -

+

{{ object.shortid }}

- {% if state_definitions %} - + {% else %} + + {% endif %} + + - {% endif %} + +
+