introduce item.short_id + lookup by short id

update
radex 2020-05-11 22:19:42 +02:00 zatwierdzone przez Piotr Dobrowolski
rodzic d313274615
commit 7ac99dd44b
2 zmienionych plików z 14 dodań i 2 usunięć

Wyświetl plik

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

Wyświetl plik

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