Skip to content

Commit

Permalink
refactor(stats): add to admin, cleanup (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Sep 28, 2024
1 parent 63f5a76 commit 39866ac
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
3 changes: 2 additions & 1 deletion open_prices/api/stats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ class StatsView(APIView):

@extend_schema(responses=TotalStatsSerializer, tags=["stats"])
def get(self, request: Request) -> Response:
serializer = TotalStatsSerializer(TotalStats.get_solo())
total_stats = TotalStats.get_solo()
serializer = TotalStatsSerializer(total_stats)
return Response(serializer.data, status=200)
16 changes: 16 additions & 0 deletions open_prices/stats/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.contrib import admin
from solo.admin import SingletonModelAdmin

from open_prices.stats.models import TotalStats


@admin.register(TotalStats)
class TotalStatsAdmin(SingletonModelAdmin):
EXCLUDED_FIELDS = ["id"]

# to keep order
def get_fields(self, request, obj=None):
return [f.name for f in obj._meta.fields if f.name not in self.EXCLUDED_FIELDS]

def get_readonly_fields(self, request, obj=None):
return [f.name for f in obj._meta.fields]
1 change: 0 additions & 1 deletion open_prices/stats/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class Migration(migrations.Migration):
],
options={
"verbose_name": "Total Stats",
"verbose_name_plural": "Total Stats",
},
),
]
1 change: 0 additions & 1 deletion open_prices/stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class TotalStats(SingletonModel):

class Meta:
verbose_name = "Total Stats"
verbose_name_plural = "Total Stats"

def update_price_stats(self):
from open_prices.prices.models import Price
Expand Down
16 changes: 14 additions & 2 deletions open_prices/stats/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.test import TestCase

from open_prices.locations.factories import LocationFactory
from open_prices.prices import constants as price_constants
from open_prices.prices.factories import PriceFactory
from open_prices.proofs.factories import ProofFactory
from open_prices.stats.models import TotalStats
Expand Down Expand Up @@ -50,6 +51,17 @@ def setUpTestData(cls):
price=1.0,
owner=cls.user.user_id,
)
PriceFactory(
product_code=None,
category_tag="en:tomatoes",
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
labels_tags=["en:organic"],
origins_tags=["en:france"],
price=3,
price_per=price_constants.PRICE_PER_KILOGRAM,
owner=cls.user.user_id,
)
PriceFactory(
product_code="0123456789101",
location_osm_id=cls.location.osm_id,
Expand All @@ -64,9 +76,9 @@ def test_update_price_stats(self):
self.assertEqual(self.total_stats.price_category_count, 0)
# update_price_stats() will update price_counts
self.total_stats.update_price_stats()
self.assertEqual(self.total_stats.price_count, 2)
self.assertEqual(self.total_stats.price_count, 3)
self.assertEqual(self.total_stats.price_barcode_count, 2)
self.assertEqual(self.total_stats.price_category_count, 0)
self.assertEqual(self.total_stats.price_category_count, 1)

def test_update_product_stats(self):
self.assertEqual(self.total_stats.product_count, 0)
Expand Down

0 comments on commit 39866ac

Please sign in to comment.