fix: evitando duplicado de productos en la importacion.
This commit is contained in:
		| @@ -1,4 +1,4 @@ | ||||
| "producto","unidad","precio","categorias" | ||||
| "Aceite de Coco Artesanal 500ml","Unidad", 50000,"Aceites&Alimentos" | ||||
| "Café 500ml","Unidad", 14000,"Cafes&Alimentos" | ||||
| "Café 250ml","Unidad", 7000,"Cafes&Alimentos" | ||||
| "Aceite","Unidad", 50000,"Aceites&Alimentos" | ||||
| "Café","Unidad", 14000,"Cafes&Alimentos" | ||||
| "Arroz","Unidad", 7000,"Cafes&Alimentos" | ||||
|   | ||||
| 
 | 
							
								
								
									
										4
									
								
								tienda_ilusion/don_confiao/example_products2.csv
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								tienda_ilusion/don_confiao/example_products2.csv
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,4 @@ | ||||
| "producto","unidad","precio","categorias" | ||||
| "Aceite","Unidad", 50000,"Aceites&Alimentos" | ||||
| "Café","Unidad", 15000,"Cafes&Alimentos" | ||||
| "Arroz","Unidad", 6000,"Cafes&Alimentos" | ||||
| 
 | 
| @@ -0,0 +1,18 @@ | ||||
| # Generated by Django 5.0.6 on 2024-06-29 21:48 | ||||
|  | ||||
| from django.db import migrations, models | ||||
|  | ||||
|  | ||||
| class Migration(migrations.Migration): | ||||
|  | ||||
|     dependencies = [ | ||||
|         ('don_confiao', '0010_alter_productcategory_name'), | ||||
|     ] | ||||
|  | ||||
|     operations = [ | ||||
|         migrations.AlterField( | ||||
|             model_name='product', | ||||
|             name='name', | ||||
|             field=models.CharField(max_length=100, unique=True), | ||||
|         ), | ||||
|     ] | ||||
| @@ -27,7 +27,7 @@ class ProductCategory(models.Model): | ||||
|         return self.name | ||||
|  | ||||
| class Product(models.Model): | ||||
|     name = models.CharField(max_length=100) | ||||
|     name = models.CharField(max_length=100, unique=True) | ||||
|     price = models.DecimalField(max_digits=9, decimal_places=2) | ||||
|     measuring_unit = models.CharField( | ||||
|         max_length=20, | ||||
|   | ||||
| @@ -34,6 +34,13 @@ class TestProducts(TestCase): | ||||
|             categories_on_csv | ||||
|         ) | ||||
|  | ||||
|     def test_update_products(self): | ||||
|         self._import_csv() | ||||
|         first_products = self._get_products() | ||||
|         self._import_csv('example_products2.csv') | ||||
|         seconds_products = self._get_products() | ||||
|         self.assertCountEqual(first_products, seconds_products) | ||||
|  | ||||
|     def _get_products(self): | ||||
|         products_response = self.client.get("/don_confiao/productos") | ||||
|         return json.loads(products_response.content.decode('utf-8')) | ||||
|   | ||||
| @@ -59,8 +59,10 @@ def handle_import_products_file(csv_file): | ||||
|     data = io.StringIO(csv_file.read().decode('utf-8')) | ||||
|     reader = csv.DictReader(data, quotechar='"') | ||||
|     for row in reader: | ||||
|         product = Product() | ||||
|         product.name = row['producto'] | ||||
|         product, _ = Product.objects.get_or_create( | ||||
|             name=row['producto'], | ||||
|             defaults={'price': row['precio']} | ||||
|         ) | ||||
|         product.price = row['precio'] | ||||
|         product.measuring_unit = row['unidad'] | ||||
|         product.save() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user