-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the way it stores the prices
The exact same product might have slightly different names in different stores. So we need to make a more complex relation Product -> ProductNameLocation -> ProductPrice
- Loading branch information
Showing
5 changed files
with
154 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
from django.contrib import admin | ||
from .models import Product, ProductCategory, ProductBrand | ||
from .models import Product, ProductCategory, ProductBrand, ProductNameLocation, ProductPrice, Location | ||
|
||
|
||
class ProductAdmin(admin.ModelAdmin): | ||
list_display = ["name", "brand", "category"] | ||
list_display = ["generic_name", "brand", "category"] | ||
|
||
class ProductCategoryAdmin(admin.ModelAdmin): | ||
list_display = ["name", "description"] | ||
|
||
class ProductBrandAdmin(admin.ModelAdmin): | ||
list_display = ["name", "description"] | ||
|
||
class ProductNameLocationAdmin(admin.ModelAdmin): | ||
list_display = ["product", "location", "name"] | ||
|
||
class ProductPriceAdmin(admin.ModelAdmin): | ||
list_display = ["product", "timestamp", "price"] | ||
|
||
class LocationAdmin(admin.ModelAdmin): | ||
list_display = ["name", "base_url", "get_product_url"] | ||
|
||
|
||
admin.site.register(Product, ProductAdmin) | ||
admin.site.register(ProductCategory, ProductCategoryAdmin) | ||
admin.site.register(ProductBrand, ProductBrandAdmin) | ||
admin.site.register(ProductNameLocation, ProductNameLocationAdmin) | ||
admin.site.register(ProductPrice, ProductPriceAdmin) | ||
admin.site.register(Location, LocationAdmin) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,48 @@ | ||
from django.test import TestCase | ||
from .models import Product, ProductBrand, ProductCategory, ProductPrice | ||
from .models import Product, ProductBrand, ProductCategory, ProductPrice, ProductNameLocation, Location | ||
|
||
|
||
class ProductTest(TestCase): | ||
def setUp(self): | ||
brand = ProductBrand.objects.create(name="Brand1", description="Brand1") | ||
category = ProductCategory.objects.create(name="Category1", description="Category1") | ||
Product.objects.create(name="Product1", brand=brand, category=category) | ||
product = Product.objects.create(code="A01", generic_name="Product1", brand=brand, category=category) | ||
location_disco = Location.objects.create(name="Disco") | ||
location_tata = Location.objects.create(name="Ta-Ta") | ||
|
||
def test_product_is_created(self): | ||
product = Product.objects.get(name="Product1") | ||
self.assertEqual(product.name, "Product1") | ||
self.assertEqual(product.brand.name, "Brand1") | ||
self.assertEqual(product.category.name, "Category1") | ||
self.product_name_at_disco = ProductNameLocation.objects.create(product=product, location=location_disco, name="Product #1") | ||
self.product_name_at_tata = ProductNameLocation.objects.create(product=product, location=location_tata, name="Product Number1") | ||
|
||
# def test_product_is_created(self): | ||
# product = Product.objects.get(generic_name="Product1") | ||
# self.assertEqual(product.generic_name, "Product1") | ||
# self.assertEqual(product.brand.name, "Brand1") | ||
# self.assertEqual(product.category.name, "Category1") | ||
|
||
def test_latest_max_and_min_price(self): | ||
product = Product.objects.get(name="Product1") | ||
product = Product.objects.get(generic_name="Product1") | ||
|
||
price1 = ProductPrice.objects.create(product=product, location="Disco", price=99) | ||
price2 = ProductPrice.objects.create(product=product, location="Ta-Ta", price=100) | ||
product.prices.add(price1, price2) | ||
price1 = ProductPrice.objects.create(product=self.product_name_at_disco, price=99) | ||
price2 = ProductPrice.objects.create(product=self.product_name_at_tata, price=100) | ||
# self.product_name_at_disco.prices.add(price1) | ||
|
||
price3 = ProductPrice.objects.create(product=product, location="Disco", price=99) | ||
price4 = ProductPrice.objects.create(product=product, location="Ta-Ta", price=98) | ||
product.prices.add(price3, price4) | ||
price4 = ProductPrice.objects.create(product=self.product_name_at_tata, price=98) | ||
price3 = ProductPrice.objects.create(product=self.product_name_at_disco, price=99) | ||
# product.prices.add(price3, price4) | ||
|
||
price5 = ProductPrice.objects.create(product=product, location="Disco", price=96) | ||
price6 = ProductPrice.objects.create(product=product, location="Ta-Ta", price=97) | ||
product.prices.add(price5, price6) | ||
self.assertEqual(product.get_min_latest_price(), 98) | ||
self.assertEqual(product.get_max_latest_price(), 99) | ||
|
||
self.assertEqual(product.latest_min_price, 96) | ||
self.assertEqual(product.latest_max_price, 97) | ||
|
||
def test_upload_xlsx(self): | ||
# Create a XLSX file | ||
# Send a POST Request imitating the form | ||
# Validate that all products were created, counting them | ||
raise NotImplementedError | ||
# price5 = ProductPrice.objects.create(product=self.product_name_at_disco, price=96) | ||
# price6 = ProductPrice.objects.create(product=self.product_name_at_tata, price=97) | ||
# product.product_name_at_locations.prices.add(price5, price6) | ||
|
||
# self.assertEqual(product.latest_min_price, 96) | ||
# self.assertEqual(product.get_min_latest_price(), 96) | ||
# self.assertEqual(product.get_max_latest_price(), 97) | ||
|
||
# def test_upload_xlsx(self): | ||
# # Create a XLSX file | ||
# # Send a POST Request imitating the form | ||
# # Validate that all products were created, counting them | ||
# raise NotImplementedError |