From 05ba6704dfe1db3e74e8bf841175f7f037d4e12f Mon Sep 17 00:00:00 2001 From: Mikan <72257910+Mikan-DS@users.noreply.github.com> Date: Mon, 16 Sep 2024 15:48:35 +0300 Subject: [PATCH] Item page --- common/models.py | 14 ++++++++++++-- common/views.py | 4 +--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/common/models.py b/common/models.py index a5ad032..09bd31e 100644 --- a/common/models.py +++ b/common/models.py @@ -73,12 +73,22 @@ class ItemData(models.Model): "updatedAt": self.updated_at.timestamp(), } + @property + def item_dict(self) -> typing.Dict[str, typing.Union[str, int, float, None]]: + return { + "image": self.image.url, + "title": self.title, + "shortDescription": self.short_description, + "description": self.description, + } + + @property def card_dict(self) -> typing.Dict[str, typing.Union[str, int, float, None]]: return { "id": self.id, "title": self.title, - "preview": self.preview + "preview": self.preview.url } @property @@ -93,6 +103,6 @@ class ItemData(models.Model): return { "id": self.id, "title": self.title, - "preview": self.preview + "preview": self.preview.url } diff --git a/common/views.py b/common/views.py index 04f3424..9b0b6ad 100644 --- a/common/views.py +++ b/common/views.py @@ -31,9 +31,7 @@ def get_cards(request: HttpRequest, variant: str, collection: str) -> HttpRespon def get_item(request: HttpRequest, collection: str, item_id: int) -> HttpResponse: try: item: ItemData = ItemData.objects.get(collection__name=collection, id=item_id) - return JsonResponse({ - 'item': item.dict - }) + return JsonResponse(item.item_dict) except Exception as e: if request.user.is_superuser: return HttpResponse(f"Произошла ошибка: {repr(e)}", status=500)