Features: 1) Introduced get_object method for reusable object retrieval logic in Category and Brand viewsets; 2) Enhanced SEO meta retrieval using the new get_object method.
Fixes: 1) Removed reliance on suppressed exceptions for UUID lookup; 2) Improved handling of invalid or missing objects during lookup. Extra: 1) Cleaned up unused import `suppress`; 2) Simplified code by replacing redundant lookup logic with a unified method.
This commit is contained in:
parent
ea22ca69a3
commit
5c18404b53
1 changed files with 46 additions and 13 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
import logging
|
import logging
|
||||||
import uuid
|
import uuid
|
||||||
from contextlib import suppress
|
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from constance import config
|
from constance import config
|
||||||
|
|
@ -286,6 +285,28 @@ class CategoryViewSet(EvibesViewSet):
|
||||||
}
|
}
|
||||||
additional = {"seo_meta": "ALLOW"}
|
additional = {"seo_meta": "ALLOW"}
|
||||||
|
|
||||||
|
def get_object(self):
|
||||||
|
queryset = self.filter_queryset(self.get_queryset())
|
||||||
|
lookup_value = self.kwargs[self.lookup_url_kwarg]
|
||||||
|
|
||||||
|
obj = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
uuid_obj = UUID(lookup_value)
|
||||||
|
obj = queryset.filter(uuid=uuid_obj).first()
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not obj:
|
||||||
|
obj = queryset.filter(slug=lookup_value).first()
|
||||||
|
|
||||||
|
if not obj:
|
||||||
|
name = "Category"
|
||||||
|
raise Http404(f"{name} does not exist: {lookup_value}")
|
||||||
|
|
||||||
|
self.check_object_permissions(self.request, obj)
|
||||||
|
return obj
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
qs = super().get_queryset()
|
qs = super().get_queryset()
|
||||||
if self.request.user.has_perm("core.view_category"):
|
if self.request.user.has_perm("core.view_category"):
|
||||||
|
|
@ -301,13 +322,7 @@ class CategoryViewSet(EvibesViewSet):
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def seo_meta(self, request, **kwargs):
|
def seo_meta(self, request, **kwargs):
|
||||||
lookup_key = getattr(self, "lookup_url_kwarg", "pk")
|
category = self.get_object()
|
||||||
lookup_val = kwargs.get(lookup_key)
|
|
||||||
|
|
||||||
category = get_object_or_404(Category.objects.select_related("parent"), slug=str(lookup_val))
|
|
||||||
|
|
||||||
with suppress(Exception):
|
|
||||||
category = Category.objects.select_related("parent").get(uuid=UUID(str(lookup_val)))
|
|
||||||
|
|
||||||
title = f"{category.name} | {config.PROJECT_NAME}"
|
title = f"{category.name} | {config.PROJECT_NAME}"
|
||||||
description = (category.description or "")[:180]
|
description = (category.description or "")[:180]
|
||||||
|
|
@ -390,6 +405,28 @@ class BrandViewSet(EvibesViewSet):
|
||||||
}
|
}
|
||||||
additional = {"seo_meta": "ALLOW"}
|
additional = {"seo_meta": "ALLOW"}
|
||||||
|
|
||||||
|
def get_object(self):
|
||||||
|
queryset = self.filter_queryset(self.get_queryset())
|
||||||
|
lookup_value = self.kwargs[self.lookup_url_kwarg]
|
||||||
|
|
||||||
|
obj = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
uuid_obj = UUID(lookup_value)
|
||||||
|
obj = queryset.filter(uuid=uuid_obj).first()
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not obj:
|
||||||
|
obj = queryset.filter(slug=lookup_value).first()
|
||||||
|
|
||||||
|
if not obj:
|
||||||
|
name = "Brand"
|
||||||
|
raise Http404(f"{name} does not exist: {lookup_value}")
|
||||||
|
|
||||||
|
self.check_object_permissions(self.request, obj)
|
||||||
|
return obj
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
queryset = Brand.objects.all()
|
queryset = Brand.objects.all()
|
||||||
|
|
||||||
|
|
@ -411,11 +448,7 @@ class BrandViewSet(EvibesViewSet):
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def seo_meta(self, request, **kwargs):
|
def seo_meta(self, request, **kwargs):
|
||||||
lookup_key = getattr(self, "lookup_url_kwarg", "pk")
|
brand = self.get_object()
|
||||||
lookup_val = kwargs.get(lookup_key)
|
|
||||||
brand = get_object_or_404(Brand, slug=str(lookup_val))
|
|
||||||
with suppress(Exception):
|
|
||||||
brand = Brand.objects.get(uuid=UUID(str(lookup_val)))
|
|
||||||
|
|
||||||
title = f"{brand.name} | {config.PROJECT_NAME}"
|
title = f"{brand.name} | {config.PROJECT_NAME}"
|
||||||
description = (brand.description or "")[:180]
|
description = (brand.description or "")[:180]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue