userproperties views refactoring
This commit is contained in:
parent
ca4dd434e1
commit
344baab1ce
|
@ -181,7 +181,6 @@ class AddUserPropertyView(DashboardView, CreateView):
|
||||||
template_name = "new_user_property.html"
|
template_name = "new_user_property.html"
|
||||||
title = _("New User Property")
|
title = _("New User Property")
|
||||||
breadcrumb = "Device / New Property"
|
breadcrumb = "Device / New Property"
|
||||||
success_url = reverse_lazy('dashboard:unassigned_devices')
|
|
||||||
model = UserProperty
|
model = UserProperty
|
||||||
fields = ("key", "value")
|
fields = ("key", "value")
|
||||||
|
|
||||||
|
@ -206,17 +205,13 @@ class AddUserPropertyView(DashboardView, CreateView):
|
||||||
def get_form_kwargs(self):
|
def get_form_kwargs(self):
|
||||||
pk = self.kwargs.get('pk')
|
pk = self.kwargs.get('pk')
|
||||||
institution = self.request.user.institution
|
institution = self.request.user.institution
|
||||||
self.property = SystemProperty.objects.filter(
|
self.property = get_object_or_404(SystemProperty, owner=institution, value=pk)
|
||||||
owner=institution,
|
|
||||||
value=pk,
|
|
||||||
).first()
|
|
||||||
|
|
||||||
if not self.property:
|
return super().get_form_kwargs()
|
||||||
raise Http404
|
|
||||||
|
def get_success_url(self):
|
||||||
|
return reverse_lazy('device:details', args=[self.kwargs.get('pk')])
|
||||||
|
|
||||||
self.success_url = reverse_lazy('device:details', args=[pk])
|
|
||||||
kwargs = super().get_form_kwargs()
|
|
||||||
return kwargs
|
|
||||||
|
|
||||||
class UpdateUserPropertyView(DashboardView, UpdateView):
|
class UpdateUserPropertyView(DashboardView, UpdateView):
|
||||||
template_name = "new_user_property.html"
|
template_name = "new_user_property.html"
|
||||||
|
@ -225,37 +220,34 @@ class UpdateUserPropertyView(DashboardView, UpdateView):
|
||||||
model = UserProperty
|
model = UserProperty
|
||||||
fields = ("key", "value")
|
fields = ("key", "value")
|
||||||
|
|
||||||
def get_form_kwargs(self):
|
def get_queryset(self):
|
||||||
pk = self.kwargs.get('pk')
|
pk = self.kwargs.get('pk')
|
||||||
user_property = get_object_or_404(UserProperty, pk=pk, owner=self.request.user.institution)
|
institution = self.request.user.institution
|
||||||
|
return UserProperty.objects.filter(pk=pk, owner=institution)
|
||||||
|
|
||||||
if not user_property:
|
|
||||||
raise Http404
|
|
||||||
|
|
||||||
kwargs = super().get_form_kwargs()
|
|
||||||
kwargs['instance'] = user_property
|
|
||||||
return kwargs
|
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
old_key= self.object.key
|
|
||||||
old_value = self.object.value
|
old_instance = self.get_object()
|
||||||
new_key = form.cleaned_data['key']
|
old_key = old_instance.key
|
||||||
new_value = form.cleaned_data['value']
|
old_value = old_instance.value
|
||||||
|
|
||||||
form.instance.owner = self.request.user.institution
|
form.instance.owner = self.request.user.institution
|
||||||
form.instance.user = self.request.user
|
form.instance.user = self.request.user
|
||||||
form.instance.type = UserProperty.Type.USER
|
form.instance.type = UserProperty.Type.USER
|
||||||
response = super().form_valid(form)
|
|
||||||
messages.success(self.request, _("User property updated successfully."))
|
|
||||||
|
|
||||||
message = _("<Updated> UserProperty: {}: {} to {}: {}".format(old_key, old_value, new_key, new_value ))
|
new_key = form.cleaned_data['key']
|
||||||
|
new_value = form.cleaned_data['value']
|
||||||
|
|
||||||
|
message = _("<Updated> UserProperty: {}: {} to {}: {}".format(old_key, old_value, new_key, new_value))
|
||||||
DeviceLog.objects.create(
|
DeviceLog.objects.create(
|
||||||
snapshot_uuid=form.instance.uuid,
|
snapshot_uuid=form.instance.uuid,
|
||||||
event=message,
|
event=message,
|
||||||
user=self.request.user,
|
user=self.request.user,
|
||||||
institution=self.request.user.institution
|
institution=self.request.user.institution
|
||||||
)
|
)
|
||||||
return response
|
|
||||||
|
messages.success(self.request, _("User property updated successfully."))
|
||||||
|
return super().form_valid(form)
|
||||||
|
|
||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details', args=[self.object.pk]))
|
return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details', args=[self.object.pk]))
|
||||||
|
@ -264,17 +256,14 @@ class UpdateUserPropertyView(DashboardView, UpdateView):
|
||||||
class DeleteUserPropertyView(DashboardView, DeleteView):
|
class DeleteUserPropertyView(DashboardView, DeleteView):
|
||||||
model = UserProperty
|
model = UserProperty
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def get_queryset(self):
|
||||||
self.pk = kwargs['pk']
|
return UserProperty.objects.filter(owner=self.request.user.institution)
|
||||||
referer = request.META.get('HTTP_REFERER')
|
|
||||||
if not referer:
|
#using post() method because delete() method from DeleteView has some issues with messages framework
|
||||||
raise Http404("No referer header found")
|
def post(self, request, *args, **kwargs):
|
||||||
|
self.object = self.get_object()
|
||||||
|
self.object.delete()
|
||||||
|
|
||||||
self.object = get_object_or_404(
|
|
||||||
self.model,
|
|
||||||
pk=self.pk,
|
|
||||||
owner=self.request.user.institution
|
|
||||||
)
|
|
||||||
message = _("<Deleted> User Property: {}:{}".format(self.object.key, self.object.value ))
|
message = _("<Deleted> User Property: {}:{}".format(self.object.key, self.object.value ))
|
||||||
DeviceLog.objects.create(
|
DeviceLog.objects.create(
|
||||||
snapshot_uuid=self.object.uuid,
|
snapshot_uuid=self.object.uuid,
|
||||||
|
@ -282,13 +271,16 @@ class DeleteUserPropertyView(DashboardView, DeleteView):
|
||||||
user=self.request.user,
|
user=self.request.user,
|
||||||
institution=self.request.user.institution
|
institution=self.request.user.institution
|
||||||
)
|
)
|
||||||
|
|
||||||
self.object.delete()
|
|
||||||
messages.info(self.request, _("User property deleted successfully."))
|
messages.info(self.request, _("User property deleted successfully."))
|
||||||
|
|
||||||
# Redirect back to the original URL
|
return self.handle_success()
|
||||||
return redirect(referer)
|
|
||||||
|
|
||||||
|
def handle_success(self):
|
||||||
|
return redirect(self.get_success_url())
|
||||||
|
|
||||||
|
def get_success_url(self):
|
||||||
|
return self.request.META.get('HTTP_REFERER', reverse_lazy('device:details', args=[self.object.pk]))
|
||||||
|
|
||||||
class AddDocumentView(DashboardView, CreateView):
|
class AddDocumentView(DashboardView, CreateView):
|
||||||
template_name = "new_user_property.html"
|
template_name = "new_user_property.html"
|
||||||
|
|
Loading…
Reference in a new issue