From 7ac99dd44b3b3c7a6e01cdb308d88afb9024227e Mon Sep 17 00:00:00 2001 From: radex Date: Mon, 11 May 2020 22:19:42 +0200 Subject: [PATCH] introduce item.short_id + lookup by short id --- storage/models.py | 4 ++++ storage/views.py | 12 ++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/storage/models.py b/storage/models.py index 2333e4e..9d8134a 100644 --- a/storage/models.py +++ b/storage/models.py @@ -63,6 +63,10 @@ class Item(models.Model, TreeModelMixin): objects = hstore.HStoreManager() + def short_id(self): + # let's just hope we never have 4 294 967 296 things :) + return str(self.pk)[:8] # collisions? what collisions? + def __str__(self): return '- ' * (self.get_level() or 0) + self.name diff --git a/storage/views.py b/storage/views.py index 0138eb0..b827f77 100644 --- a/storage/views.py +++ b/storage/views.py @@ -98,8 +98,16 @@ def item_display(request, pk): def label_lookup(request, pk): - label = get_object_or_404(Label, pk=pk) - return redirect(label.item) + try: + label = Label.objects.get(pk=pk) + return redirect(label.item) + except Label.DoesNotExist: + try: + # look up by short id + item = Item.objects.get(uuid__startswith=pk) + return redirect(item) + except Item.DoesNotExist: + raise Http404("Very sad to say, I could not find this thing") @login_required