fix: evitando duplicado de productos en la importacion.
This commit is contained in:
parent
84c295e7de
commit
64a9b75ad7
@ -1,4 +1,4 @@
|
|||||||
"producto","unidad","precio","categorias"
|
"producto","unidad","precio","categorias"
|
||||||
"Aceite de Coco Artesanal 500ml","Unidad", 50000,"Aceites&Alimentos"
|
"Aceite","Unidad", 50000,"Aceites&Alimentos"
|
||||||
"Café 500ml","Unidad", 14000,"Cafes&Alimentos"
|
"Café","Unidad", 14000,"Cafes&Alimentos"
|
||||||
"Café 250ml","Unidad", 7000,"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
|
return self.name
|
||||||
|
|
||||||
class Product(models.Model):
|
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)
|
price = models.DecimalField(max_digits=9, decimal_places=2)
|
||||||
measuring_unit = models.CharField(
|
measuring_unit = models.CharField(
|
||||||
max_length=20,
|
max_length=20,
|
||||||
|
@ -34,6 +34,13 @@ class TestProducts(TestCase):
|
|||||||
categories_on_csv
|
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):
|
def _get_products(self):
|
||||||
products_response = self.client.get("/don_confiao/productos")
|
products_response = self.client.get("/don_confiao/productos")
|
||||||
return json.loads(products_response.content.decode('utf-8'))
|
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'))
|
data = io.StringIO(csv_file.read().decode('utf-8'))
|
||||||
reader = csv.DictReader(data, quotechar='"')
|
reader = csv.DictReader(data, quotechar='"')
|
||||||
for row in reader:
|
for row in reader:
|
||||||
product = Product()
|
product, _ = Product.objects.get_or_create(
|
||||||
product.name = row['producto']
|
name=row['producto'],
|
||||||
|
defaults={'price': row['precio']}
|
||||||
|
)
|
||||||
product.price = row['precio']
|
product.price = row['precio']
|
||||||
product.measuring_unit = row['unidad']
|
product.measuring_unit = row['unidad']
|
||||||
product.save()
|
product.save()
|
||||||
|
Loading…
Reference in New Issue
Block a user