From db3772f0f98fb0a32b66c1cb5856182469664dfd Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Thu, 5 Dec 2024 23:09:17 -0300 Subject: [PATCH] added view and url for new action --- action/urls.py | 9 +++++++++ action/views.py | 35 ++++++++++++++++++++++++++++++++++- dhub/urls.py | 1 + 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/action/urls.py b/action/urls.py index 9373af6..c6aa6d8 100644 --- a/action/urls.py +++ b/action/urls.py @@ -1 +1,10 @@ from django.urls import path, include +from action import views + +app_name = 'action' + +urlpatterns = [ + + path("new/", views.NewActionView.as_view(), name="new_action"), + +] diff --git a/action/views.py b/action/views.py index 5d608b0..1fdfe9d 100644 --- a/action/views.py +++ b/action/views.py @@ -1 +1,34 @@ -# 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 AddStateForm +from action.models import State, StateDefinition +from device.models import Device +import logging + +logger = logging.getLogger(__name__) + +class NewActionView(View): + + def post(self, request, *args, **kwargs): + form = AddStateForm(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) + snapshot_uuid = form.cleaned_data['snapshot_uuid'] + #TODO: implement notes + note = form.cleaned_data.get('note', '') + + state = State.objects.create( + snapshot_uuid=snapshot_uuid, + state=state_definition.state, + user=request.user, + institution=request.user.institution, + ) + + messages.success(request, f"Action to '{state_definition.state}' has been added.") + return redirect(request.META.get('HTTP_REFERER')) + else: + messages.error(request, "There was an error with your submission.") + return redirect(request.META.get('HTTP_REFERER')) \ No newline at end of file diff --git a/dhub/urls.py b/dhub/urls.py index 0b77144..39405bd 100644 --- a/dhub/urls.py +++ b/dhub/urls.py @@ -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")),