first stab at admin_stats endpoint
This commit is contained in:
@@ -10,7 +10,7 @@ from django.contrib.auth import get_user_model
|
||||
from django.utils.http import urlsafe_base64_decode
|
||||
from django.utils.encoding import force_text
|
||||
from django.db import IntegrityError
|
||||
from django.contrib.gis.geos import Point
|
||||
from django.contrib.gis.geos import Point, GEOSGeometry
|
||||
from django.shortcuts import redirect
|
||||
from django.conf import settings
|
||||
|
||||
@@ -23,7 +23,8 @@ from rest_framework.decorators import api_view, permission_classes
|
||||
|
||||
from companies.models import Company
|
||||
from companies.serializers import CompanySerializer
|
||||
from geo.models import City
|
||||
from products.models import Product
|
||||
from geo.models import City, Region
|
||||
|
||||
from . import models
|
||||
from . import serializers as core_serializers
|
||||
@@ -197,3 +198,26 @@ def activate_user(request, uidb64, token):
|
||||
return Response(f"Tu cuenta de usuario {user.email} ha sido activada")
|
||||
else:
|
||||
return Response({"error": f"Tu token de verificacion no coincide con ningún usuario registrado"}, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||
|
||||
|
||||
@api_view(['GET',])
|
||||
@permission_classes([IsAdminUser,])
|
||||
def admin_stats(request):
|
||||
company_count = Company.objects.count()
|
||||
product_count = Product.objects.count()
|
||||
companies_per_region = {}
|
||||
products_per_region = {}
|
||||
|
||||
for region in Region.objects.all():
|
||||
count = Company.objects.filter(geo__dwithin=region.geo).count()
|
||||
companies_per_region[region.name] = count
|
||||
count = Product.objects.filter(company__geo__dwithin=region.geo).count()
|
||||
products_per_region[region.name] = count
|
||||
|
||||
data = {
|
||||
'company_count': company_count,
|
||||
'product_count': product_count,
|
||||
'companies_per_region': companies_per_region,
|
||||
'products_per_region': products_per_region,
|
||||
}
|
||||
return Response(data=data)
|
||||
|
||||
Reference in New Issue
Block a user