1
0
Fork 0

introduce item.short_id + lookup by short id

This commit is contained in:
radex 2020-05-11 22:19:42 +02:00 committed by Piotr Dobrowolski
parent d313274615
commit 7ac99dd44b
2 changed files with 14 additions and 2 deletions

View File

@ -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

View File

@ -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