mirror of
https://github.com/narrrl/holy-flavor-rater.git
synced 2026-11-03 16:41:27 +01:00
redesign flavor page
This commit is contained in:
1 parent
c121380c6e
commit
ecedabe8b0
12 files changed
+932
-99
No files matched your search
@@ -132,6 +132,7 @@ class Command(BaseCommand):
|
||||
is_available = any(v.get("available") for v in variants)
|
||||
images = p.get("images", [])
|
||||
image_url = images[0].get("src") if images else None
|
||||
image_urls_list = [img.get("src") for img in images if img.get("src")]
|
||||
description = self.clean_html(p.get("body_html", ""))
|
||||
handle = p.get("handle")
|
||||
shop_url = f"https://weareholy.com/products/{handle}" if handle else None
|
||||
@@ -196,6 +197,7 @@ class Command(BaseCommand):
|
||||
)
|
||||
|
||||
flavor.image_url = image_url
|
||||
flavor.image_urls = image_urls_list
|
||||
flavor.save()
|
||||
synced_external_ids.append(p["id"])
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from django.db.models import Count
|
||||
from rest_framework import serializers
|
||||
|
||||
from api.models import Category, Flavor, Rating
|
||||
@@ -19,6 +20,8 @@ class FlavorSerializer(serializers.ModelSerializer):
|
||||
user_rating = serializers.SerializerMethodField()
|
||||
ratings = RatingSerializer(many=True, read_only=True)
|
||||
image_url = serializers.SerializerMethodField()
|
||||
image_urls = serializers.SerializerMethodField()
|
||||
rating_distribution = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Flavor
|
||||
@@ -34,10 +37,12 @@ class FlavorSerializer(serializers.ModelSerializer):
|
||||
"user_rating",
|
||||
"ratings",
|
||||
"image_url",
|
||||
"image_urls",
|
||||
"image",
|
||||
"is_available",
|
||||
"is_legacy",
|
||||
"shop_url",
|
||||
"rating_distribution",
|
||||
]
|
||||
extra_kwargs = {"image": {"write_only": True}}
|
||||
|
||||
@@ -46,6 +51,23 @@ class FlavorSerializer(serializers.ModelSerializer):
|
||||
return absolute_image_url(self, obj.image)
|
||||
return obj.image_url
|
||||
|
||||
def get_image_urls(self, obj: Flavor) -> list[str]:
|
||||
urls: list[str] = []
|
||||
if obj.image:
|
||||
absolute = absolute_image_url(self, obj.image)
|
||||
if absolute:
|
||||
urls.append(absolute)
|
||||
urls.extend(obj.image_urls or [])
|
||||
return urls
|
||||
|
||||
def get_rating_distribution(self, obj: Flavor) -> dict[str, int]:
|
||||
counts = dict.fromkeys((str(i) for i in range(1, 11)), 0)
|
||||
for row in obj.ratings.values("score").annotate(c=Count("id")):
|
||||
score = row["score"]
|
||||
if 1 <= score <= 10:
|
||||
counts[str(score)] = row["c"]
|
||||
return counts
|
||||
|
||||
def get_user_rating(self, obj: Flavor) -> int | None:
|
||||
request = self.context.get("request")
|
||||
if request and request.user.is_authenticated:
|
||||
|
||||
Reference in new issue
Block a user