+ More info about client in statistics/clients
This commit is contained in:
parent
edf1118f9f
commit
2fe77442c3
|
|
@ -2,8 +2,7 @@ import calendar
|
|||
from datetime import timedelta
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import login
|
||||
from django.db.models import F, Count, Sum
|
||||
from django.db.models import F, Count, Sum, OuterRef, Subquery
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from rest_framework import generics, permissions, mixins, status, viewsets
|
||||
from rest_framework.decorators import action
|
||||
|
|
@ -270,7 +269,7 @@ class StatisticsAPI(viewsets.GenericViewSet):
|
|||
}
|
||||
|
||||
def _create_empty_stats():
|
||||
return {k: set() for k in options.keys()}
|
||||
return {k: [] for k in options.keys()}
|
||||
|
||||
qs = self.get_queryset() \
|
||||
.filter(buyer_phone__isnull=False) \
|
||||
|
|
@ -279,6 +278,26 @@ class StatisticsAPI(viewsets.GenericViewSet):
|
|||
.filter(order_count__gt=1) \
|
||||
.order_by('month')
|
||||
|
||||
# Temporary hack: collect the most recent info about client
|
||||
# mapping buyer_phone -> buyer info (name, telegram)
|
||||
client_mapping = {}
|
||||
|
||||
recent_created_at = Checklist.objects.all() \
|
||||
.filter(buyer_phone=OuterRef('buyer_phone')) \
|
||||
.order_by('-created_at') \
|
||||
.values('created_at')[:1]
|
||||
|
||||
client_qs = Checklist.objects.filter(
|
||||
created_at=Subquery(recent_created_at),
|
||||
buyer_phone=F('buyer_phone')
|
||||
).distinct()
|
||||
|
||||
for checklist in client_qs:
|
||||
client_mapping[checklist.buyer_phone] = {
|
||||
'phone': checklist.buyer_phone,
|
||||
'name': checklist.buyer_name,
|
||||
'telegram': checklist.buyer_telegram}
|
||||
|
||||
result = {}
|
||||
# Add empty stats
|
||||
for i in range(1, 13):
|
||||
|
|
@ -291,7 +310,8 @@ class StatisticsAPI(viewsets.GenericViewSet):
|
|||
|
||||
for key, size in reversed(options.items()):
|
||||
if stat['order_count'] > size:
|
||||
result[month_name][key].add(stat['buyer_phone'])
|
||||
client_info = client_mapping[stat['buyer_phone']]
|
||||
result[month_name][key].append(client_info)
|
||||
break
|
||||
|
||||
return Response(result)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user