✨ Add some pies
This commit is contained in:
1 parent
aead00a893
commit
9e80669a1f
10 files changed
+578
-5
No files matched your search
@@ -1,4 +1,6 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
from typing import Any, NewType
|
||||
|
||||
import strawberry
|
||||
import strawberry_django
|
||||
@@ -9,6 +11,13 @@ from main import models
|
||||
|
||||
UserModel = get_user_model()
|
||||
|
||||
JSONScalar = strawberry.scalar(
|
||||
NewType("JSONScalar", Any),
|
||||
serialize=lambda v: v,
|
||||
parse_value=lambda v: json.loads(v),
|
||||
description="The GenericScalar scalar type represents a generic GraphQL scalar value that could be: List or Object.",
|
||||
)
|
||||
|
||||
|
||||
@strawberry_django.filters.filter(models.Product)
|
||||
class ProductFilter:
|
||||
@@ -23,6 +32,8 @@ class Product:
|
||||
image_url: str
|
||||
sale_price_int: auto
|
||||
sale_price_ext: auto
|
||||
# FIXME: stricter types
|
||||
user_counts: JSONScalar
|
||||
|
||||
|
||||
@strawberry_django.type(models.UserProfile)
|
||||
|
||||
@@ -2,8 +2,25 @@ from datetime import datetime
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
from django.db.models import Sum
|
||||
from django.templatetags.static import static
|
||||
|
||||
EXTERNAL_USER = "Ekstern"
|
||||
|
||||
|
||||
def _format_counts(per_user):
|
||||
per_user = list(per_user)
|
||||
for row in per_user:
|
||||
if row.get("order__customer__first_name") is None:
|
||||
who = EXTERNAL_USER
|
||||
else:
|
||||
first = row.pop("order__customer__first_name")
|
||||
last_initial = row.pop("order__customer__last_name")[0]
|
||||
who = f"{first} {last_initial}"
|
||||
row["name"] = who
|
||||
|
||||
return per_user
|
||||
|
||||
|
||||
class Product(models.Model):
|
||||
name = models.CharField(max_length=64)
|
||||
@@ -47,6 +64,13 @@ class Product(models.Model):
|
||||
def image_url(self):
|
||||
return self.image.url if self.image else ""
|
||||
|
||||
def user_counts(self):
|
||||
name_fields = ["order__customer__first_name", "order__customer__last_name"]
|
||||
per_user = OrderLine.objects.filter(product=self)
|
||||
per_user = per_user.values(*name_fields).annotate(count=Sum("amount")).order_by("-count")
|
||||
|
||||
return _format_counts(per_user)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
Reference in new issue
Block a user