From 15acc02f746b7fe37b1f917eeb2d31b76cbbb85c Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Wed, 4 Dec 2019 20:13:09 +0000 Subject: [PATCH] Switched URL to use asset_id rather than the database ID --- assets/models.py | 2 +- assets/templates/asset_create.html | 2 +- assets/templates/asset_update.html | 2 +- assets/templates/partials/asset_buttons.html | 6 ++--- .../partials/asset_list_table_body.html | 8 +++---- assets/templates/partials/parent_form.html | 4 ++-- assets/urls.py | 6 ++--- assets/views.py | 24 ++++++++++++++----- 8 files changed, 33 insertions(+), 21 deletions(-) diff --git a/assets/models.py b/assets/models.py index 23653c65..7abddfd2 100644 --- a/assets/models.py +++ b/assets/models.py @@ -100,7 +100,7 @@ class Asset(models.Model): return row[0] def get_absolute_url(self): - return reverse('asset_detail', kwargs={'pk': self.pk}) + return reverse('asset_detail', kwargs={'pk': self.asset_id}) def __str__(self): out = str(self.asset_id) + ' - ' + self.description diff --git a/assets/templates/asset_create.html b/assets/templates/asset_create.html index fb9bc627..b0bb8297 100644 --- a/assets/templates/asset_create.html +++ b/assets/templates/asset_create.html @@ -16,7 +16,7 @@ {% if duplicate %} -
+ {% else %} {% endif %} diff --git a/assets/templates/asset_update.html b/assets/templates/asset_update.html index dfb9f552..5a7eec87 100644 --- a/assets/templates/asset_update.html +++ b/assets/templates/asset_update.html @@ -15,7 +15,7 @@ {% endif %} - + {% include 'form_errors.html' %} {% csrf_token %} diff --git a/assets/templates/partials/asset_buttons.html b/assets/templates/partials/asset_buttons.html index 4d6507db..39402cbe 100644 --- a/assets/templates/partials/asset_buttons.html +++ b/assets/templates/partials/asset_buttons.html @@ -11,15 +11,15 @@ {% else %} {% endif %} {% if create or edit or duplicate %}
{% endif %} diff --git a/assets/templates/partials/asset_list_table_body.html b/assets/templates/partials/asset_list_table_body.html index eeff32d4..0198ef92 100644 --- a/assets/templates/partials/asset_list_table_body.html +++ b/assets/templates/partials/asset_list_table_body.html @@ -11,17 +11,17 @@ info {% endif %} "> - {{ item.asset_id }} + {{ item.asset_id }} {{ item.description }} {{ item.category }} {{ item.status }}
- View + View {% if perms.assets.change_asset %} - Edit - Duplicate + Edit + Duplicate {% endif %}
diff --git a/assets/templates/partials/parent_form.html b/assets/templates/partials/parent_form.html index 10435818..5f29240d 100644 --- a/assets/templates/partials/parent_form.html +++ b/assets/templates/partials/parent_form.html @@ -15,7 +15,7 @@
Parent
{% if object.parent %} - + {{ object.parent.asset_id }} - {{ object.parent.description }} {% else %} @@ -27,7 +27,7 @@ {% if object.asset_parent.all %} {% for child in object.asset_parent.all %}
- + {{ child.asset_id }} - {{ child.description }}
diff --git a/assets/urls.py b/assets/urls.py index 4ae4ddf2..fa5facd0 100644 --- a/assets/urls.py +++ b/assets/urls.py @@ -6,10 +6,10 @@ from PyRIGS.decorators import permission_required_with_403 urlpatterns = [ path('', views.AssetList.as_view(), name='asset_index'), path('asset/list/', views.AssetList.as_view(), name='asset_list'), - path('asset//', views.AssetDetail.as_view(), name='asset_detail'), + path('asset/id//', views.AssetDetail.as_view(), name='asset_detail'), path('asset/create/', permission_required_with_403('assets.create_asset')(views.AssetCreate.as_view()), name='asset_create'), - path('asset//edit/', permission_required_with_403('assets.change_asset')(views.AssetEdit.as_view()), name='asset_update'), - path('asset//duplicate/', permission_required_with_403('assets.create_asset')(views.AssetDuplicate.as_view()), name='asset_duplicate'), + path('asset/id//edit/', permission_required_with_403('assets.change_asset')(views.AssetEdit.as_view()), name='asset_update'), + path('asset/id//duplicate/', permission_required_with_403('assets.create_asset')(views.AssetDuplicate.as_view()), name='asset_duplicate'), path('asset/search/', views.AssetSearch.as_view(), name='asset_search_json'), diff --git a/assets/views.py b/assets/views.py index 77b480f6..2d1807c8 100644 --- a/assets/views.py +++ b/assets/views.py @@ -65,12 +65,25 @@ class AssetSearch(AssetList): return JsonResponse(result, safe=False) -class AssetDetail(LoginRequiredMixin, generic.DetailView): +class AssetIDUrlMixin: + def get_object(self, queryset=None): + pk = self.kwargs.get(self.pk_url_kwarg) + queryset = models.Asset.objects.filter(asset_id=pk) + try: + # Get the single item from the filtered queryset + obj = queryset.get() + except queryset.model.DoesNotExist: + raise Http404(_("No %(verbose_name)s found matching the query") % + {'verbose_name': queryset.model._meta.verbose_name}) + return obj + + +class AssetDetail(LoginRequiredMixin, AssetIDUrlMixin, generic.DetailView): model = models.Asset template_name = 'asset_update.html' -class AssetEdit(LoginRequiredMixin, generic.UpdateView): +class AssetEdit(LoginRequiredMixin, AssetIDUrlMixin, generic.UpdateView): template_name = 'asset_update.html' model = models.Asset form_class = forms.AssetForm @@ -83,7 +96,7 @@ class AssetEdit(LoginRequiredMixin, generic.UpdateView): return context def get_success_url(self): - return reverse("asset_detail", kwargs={"pk": self.object.id}) + return reverse("asset_detail", kwargs={"pk": self.object.asset_id}) class AssetCreate(LoginRequiredMixin, generic.CreateView): @@ -104,7 +117,7 @@ class AssetCreate(LoginRequiredMixin, generic.CreateView): return initial def get_success_url(self): - return reverse("asset_detail", kwargs={"pk": self.object.id}) + return reverse("asset_detail", kwargs={"pk": self.object.asset_id}) class DuplicateMixin: @@ -114,7 +127,7 @@ class DuplicateMixin: return self.render_to_response(self.get_context_data()) -class AssetDuplicate(DuplicateMixin, AssetCreate): +class AssetDuplicate(DuplicateMixin, AssetIDUrlMixin, AssetCreate): model = models.Asset def get_context_data(self, **kwargs): @@ -122,7 +135,6 @@ class AssetDuplicate(DuplicateMixin, AssetCreate): context["create"] = None context["duplicate"] = True context['previous_asset_id'] = self.get_object().asset_id - context["previous_asset_pk"] = self.kwargs.get(self.pk_url_kwarg) return context