finished admin_stats endpoint

This commit is contained in:
Sam
2021-03-12 12:14:30 +00:00
parent 712cabe3da
commit d0609fd1fa
4 changed files with 36 additions and 3 deletions

View File

@@ -34,10 +34,9 @@ SECRET_KEY = 'td*#7t-(1e9^(g0cod*hs**dp(%zvg@=$cug_-dtzcj#i2mrz@'
# Application definition
INSTALLED_APPS = [
'suit',
'dal',
'dal_select2',
'suit',
'django.contrib.admin',
'django.contrib.auth',

View File

@@ -26,6 +26,7 @@ from companies import views as company_views
from stats import views as stat_views
from .routers import router
admin.site.site_header = 'LaTiendaCOOP Administration'
urlpatterns = [
path('admin/', admin.site.urls),

View File

@@ -702,6 +702,7 @@ class AdminStatsTest(APITestCase):
# Assert access is forbidden
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
# admin user
def test_admin_can_get_data(self):
# make user admin
self.user.is_staff = True
@@ -714,7 +715,7 @@ class AdminStatsTest(APITestCase):
response = self.client.get(self.endpoint)
self.assertEquals(response.status_code, 200)
payload = response.json()
expected_entries = ['company_count', 'product_count', 'companies_per_region', 'products_per_region']
expected_entries = ['company_count', 'product_count', 'companies_per_region', 'products_per_region', 'companies_timeline', 'products_timeline', 'users_timeline', 'contact_timeline', 'shopping_timeline']
for name in expected_entries:
self.assertTrue(name in payload)

View File

@@ -25,6 +25,7 @@ from companies.models import Company
from companies.serializers import CompanySerializer
from products.models import Product
from geo.models import City, Region
from stats.models import StatsLog
from . import models
from . import serializers as core_serializers
@@ -214,10 +215,41 @@ def admin_stats(request):
count = Product.objects.filter(company__geo__within=region.geo).count()
products_per_region[region.name] = count
today = datetime.date.today()
# companies timeline: count companies at increments of 4 weeks
companies_timeline = {}
# products timeline: count products at increments of 4 weeks
products_timeline = {}
# users timeline: count users at increments of 4 weeks
users_timeline = {}
# contact timeline: count statlogs from contact at increments of 4 weeks
contact_timeline = {}
# shopping timeline: count statlogs from shopping at increments of 4 weeks
shopping_timeline = {}
for i in range(1, 13):
before = today - datetime.timedelta(weeks=( (i-1) * 4 ))
after = today - datetime.timedelta(weeks=(i*4))
# companies
companies_timeline[i] = Company.objects.filter(created__range= [after, before]).count()
# products
products_timeline[i] = Product.objects.filter(created__range= [after, before]).count()
# users
users_timeline[i] = User.objects.filter(created__range= [after, before]).count()
# contact
contact_timeline[i] = StatsLog.objects.filter(contact=True, created__range= [after, before]).count()
# shopping
shopping_timeline[i] = StatsLog.objects.filter(shop=True, created__range= [after, before]).count()
data = {
'company_count': company_count,
'product_count': product_count,
'companies_per_region': companies_per_region,
'products_per_region': products_per_region,
'companies_timeline': companies_timeline,
'products_timeline': products_timeline,
'users_timeline': users_timeline,
'contact_timeline': contact_timeline,
'shopping_timeline': shopping_timeline,
}
return Response(data=data)