diff --git a/tienda_ilusion/don_confiao/test_products.py b/tienda_ilusion/don_confiao/test_products.py index 3713a38..6743a5e 100644 --- a/tienda_ilusion/don_confiao/test_products.py +++ b/tienda_ilusion/don_confiao/test_products.py @@ -17,13 +17,14 @@ class TestProducts(TestCase): example_csv = os.path.join(app_dir, 'example_products.csv') products_in_example = 3 with open(example_csv, 'rb') as csv: - response = self.client.post( + self.client.post( "/don_confiao/importar_productos", {"csv_file": csv} ) - products_response = self.client.get("/don_confiao/productos") - products = json.loads(products_response.content.decode('utf-8')) - self.assertEqual( - len(products), - products_in_example - ) + products_response = self.client.get("/don_confiao/productos") + all_products = json.loads(products_response.content.decode('utf-8')) + self.assertEqual( + len(all_products), + products_in_example + ) + self.assertIn("Aceites", all_products[0]["categories"]) diff --git a/tienda_ilusion/don_confiao/views.py b/tienda_ilusion/don_confiao/views.py index 253eb70..bca21a5 100644 --- a/tienda_ilusion/don_confiao/views.py +++ b/tienda_ilusion/don_confiao/views.py @@ -2,7 +2,7 @@ from django.shortcuts import render from django.http import HttpResponse, HttpResponseRedirect, JsonResponse from django.template import loader -from .models import Sale, Product +from .models import Sale, Product, ProductCategory from .forms import ImportProductsForm import csv @@ -32,7 +32,7 @@ def products(request): "name": product.name, "price_list": product.price, "uom": product.measuring_unit, - "category": ""#.join('&', [c for c in product.categories]) + "categories": [c.name for c in product.categories.all()] } rproducts.append(rproduct) @@ -64,3 +64,10 @@ def handle_import_products_file(csv_file): product.price = row['precio'] product.measuring_unit = row['unidad'] product.save() + + categories = [n.strip for n in row["categorias"].split("&")] + for category in categories: + category_model, _ = ProductCategory.objects.get_or_create( + name=category + ) + product.categories.add(category_model)