✨ UserDetail and UserList views
This commit is contained in:
1 parent
ec0549154d
commit
e2a84c25a6
16 files changed
+201
-22
No files matched your search
@@ -1,5 +1,7 @@
|
||||
import strawberry
|
||||
import strawberry_django
|
||||
from django.contrib.auth.models import User as DjangoUser
|
||||
from strawberry import ID
|
||||
|
||||
from main import models
|
||||
from main.api import mutations
|
||||
@@ -23,6 +25,10 @@ class Query:
|
||||
order_list: list[Order] = strawberry_django.field(pagination=True, order=OrderOrdering)
|
||||
transaction_list: list[Transaction] = strawberry_django.field(pagination=True, order=TransactionOrdering)
|
||||
|
||||
@strawberry.field
|
||||
def user(self, user_id: ID) -> User:
|
||||
return DjangoUser.objects.get(pk=user_id)
|
||||
|
||||
@strawberry.field
|
||||
def order_stats(self) -> OrderStats:
|
||||
return OrderStats(
|
||||
|
||||
+11
-2
@@ -36,15 +36,24 @@ class Product:
|
||||
user_counts: JSONScalar
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class TopMonth:
|
||||
period: str
|
||||
count: int
|
||||
sum: int
|
||||
|
||||
|
||||
@strawberry_django.type(models.UserProfile)
|
||||
class UserProfile:
|
||||
id: auto
|
||||
balance: auto
|
||||
image: auto
|
||||
last_purchase_date: datetime
|
||||
order_sum_total: float
|
||||
|
||||
@strawberry.field()
|
||||
def last_purchase_date(self) -> datetime:
|
||||
return self.last_purchase_date()
|
||||
def top_months(self) -> list[TopMonth]:
|
||||
return [TopMonth(**month) for month in self.top_months()]
|
||||
|
||||
|
||||
@strawberry_django.type(UserModel)
|
||||
|
||||
+14
-2
@@ -5,7 +5,7 @@ from typing import Type
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
from django.db.models import Count, Sum
|
||||
from django.db.models.functions import Extract, ExtractHour, ExtractIsoWeekDay, ExtractMonth, ExtractYear
|
||||
from django.db.models.functions import Extract, ExtractHour, ExtractIsoWeekDay, ExtractMonth, ExtractYear, TruncMonth
|
||||
|
||||
EXTERNAL_USER = "Ekstern"
|
||||
|
||||
@@ -178,11 +178,23 @@ class UserProfile(models.Model):
|
||||
def last_purchase(self):
|
||||
return self.user.orders.order_by("-created").first()
|
||||
|
||||
def last_purchase_date(self):
|
||||
def last_purchase_date(self) -> datetime:
|
||||
last = self.last_purchase()
|
||||
last_p = last.created if last else datetime.min
|
||||
return last_p
|
||||
|
||||
def top_months(self, limit=5):
|
||||
return (
|
||||
Order.objects.filter(customer_id=self.user_id)
|
||||
.annotate(period=TruncMonth("created"))
|
||||
.values("period")
|
||||
.annotate(sum=Sum("order_sum"), count=Count("pk"))
|
||||
.order_by("-sum")[:limit]
|
||||
)
|
||||
|
||||
def order_sum_total(self) -> float:
|
||||
return Order.objects.filter(customer_id=self.user_id).aggregate(sum=Sum("order_sum"))["sum"] or 0
|
||||
|
||||
def image_url(self) -> str:
|
||||
if self.user.email:
|
||||
md5_hash = hashlib.md5(self.user.email.lower().strip().encode()).hexdigest()
|
||||
|
||||
Reference in new issue
Block a user