From 0bd6f8d69f10546da4d05ec53e2b77c73c296ac1 Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Fri, 15 Nov 2024 19:11:05 -0300 Subject: [PATCH] added userproperty update view --- device/urls.py | 2 +- device/views.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/device/urls.py b/device/urls.py index dc7fddc..875a21e 100644 --- a/device/urls.py +++ b/device/urls.py @@ -9,7 +9,7 @@ urlpatterns = [ path("/", views.DetailsView.as_view(), name="details"), path("/user_property/add", views.AddUserPropertyView.as_view(), name="add_user_property"), path("user_property//delete", views.DeleteUserPropertyView.as_view(), name="delete_user_property"), - path("user_property//update", views.AddUserPropertyView.as_view(), name="update_user_property"), + path("user_property//update", views.UpdateUserPropertyView.as_view(), name="update_user_property"), path("/document/add", views.AddDocumentView.as_view(), name="add_document"), path("/public/", views.PublicDeviceWebView.as_view(), name="device_web"), diff --git a/device/views.py b/device/views.py index 76905f5..b97c062 100644 --- a/device/views.py +++ b/device/views.py @@ -201,6 +201,45 @@ class AddUserPropertyView(DashboardView, CreateView): kwargs = super().get_form_kwargs() return kwargs +class UpdateUserPropertyView(DashboardView, UpdateView): + template_name = "new_user_property.html" + title = _("Update User Property") + breadcrumb = "Device / Update Property" + model = UserProperty + fields = ("key", "value") + + def form_valid(self, form): + form.instance.owner = self.request.user.institution + form.instance.user = self.request.user + form.instance.type = Property.Type.USER + response = super().form_valid(form) + + messages.success(self.request, _("User property updated successfully.")) + return response + + def get_success_url(self): + referer = self.request.META.get('HTTP_REFERER') + if referer: + return referer + else: + return reverse_lazy('device:details', args=[self.object.device.pk]) + + def get_form_kwargs(self): + pk = self.kwargs.get('pk') + user_property = get_object_or_404(UserProperty, pk=pk, owner=self.request.user.institution) + self.annotation = SystemProperty.objects.filter( + owner=self.request.user.institution, + uuid=user_property.uuid, + type=Property.Type.SYSTEM + ).first() + + if not self.annotation: + raise Http404 + + kwargs = super().get_form_kwargs() + kwargs['instance'] = user_property + return kwargs + class DeleteUserPropertyView(DashboardView, DeleteView): model = UserProperty