fix: evitando duplicado de productos en la importacion.

This commit is contained in:
Mono Mono 2024-06-29 16:57:50 -05:00
parent 84c295e7de
commit 64a9b75ad7
6 changed files with 37 additions and 6 deletions

View File

@ -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"

1 producto unidad precio categorias
2 Aceite de Coco Artesanal 500ml Aceite Unidad 50000 Aceites&Alimentos
3 Café 500ml Café Unidad 14000 Cafes&Alimentos
4 Café 250ml Arroz Unidad 7000 Cafes&Alimentos

View File

@ -0,0 +1,4 @@
"producto","unidad","precio","categorias"
"Aceite","Unidad", 50000,"Aceites&Alimentos"
"Café","Unidad", 15000,"Cafes&Alimentos"
"Arroz","Unidad", 6000,"Cafes&Alimentos"
1 producto unidad precio categorias
2 Aceite Unidad 50000 Aceites&Alimentos
3 Café Unidad 15000 Cafes&Alimentos
4 Arroz Unidad 6000 Cafes&Alimentos

View File

@ -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),
),
]

View File

@ -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,

View File

@ -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'))

View File

@ -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()