Manejo de productos #2

Merged
mono merged 9 commits from manejo_productos into main 2024-06-29 18:30:57 -05:00
2 changed files with 17 additions and 9 deletions
Showing only changes of commit 252720b324 - Show all commits

View File

@ -17,13 +17,14 @@ class TestProducts(TestCase):
example_csv = os.path.join(app_dir, 'example_products.csv') example_csv = os.path.join(app_dir, 'example_products.csv')
products_in_example = 3 products_in_example = 3
with open(example_csv, 'rb') as csv: with open(example_csv, 'rb') as csv:
response = self.client.post( self.client.post(
"/don_confiao/importar_productos", "/don_confiao/importar_productos",
{"csv_file": csv} {"csv_file": csv}
) )
products_response = self.client.get("/don_confiao/productos") products_response = self.client.get("/don_confiao/productos")
products = json.loads(products_response.content.decode('utf-8')) all_products = json.loads(products_response.content.decode('utf-8'))
self.assertEqual( self.assertEqual(
len(products), len(all_products),
products_in_example products_in_example
) )
self.assertIn("Aceites", all_products[0]["categories"])

View File

@ -2,7 +2,7 @@ from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.template import loader from django.template import loader
from .models import Sale, Product from .models import Sale, Product, ProductCategory
from .forms import ImportProductsForm from .forms import ImportProductsForm
import csv import csv
@ -32,7 +32,7 @@ def products(request):
"name": product.name, "name": product.name,
"price_list": product.price, "price_list": product.price,
"uom": product.measuring_unit, "uom": product.measuring_unit,
"category": ""#.join('&', [c for c in product.categories]) "categories": [c.name for c in product.categories.all()]
} }
rproducts.append(rproduct) rproducts.append(rproduct)
@ -64,3 +64,10 @@ def handle_import_products_file(csv_file):
product.price = row['precio'] product.price = row['precio']
product.measuring_unit = row['unidad'] product.measuring_unit = row['unidad']
product.save() 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)