Fix: Clean Home, Crear lineas de Venta
This commit is contained in:
		| @@ -7,6 +7,7 @@ from .models import Sale, SaleLine | ||||
| class ImportProductsForm(forms.Form): | ||||
|     csv_file = forms.FileField() | ||||
|  | ||||
|  | ||||
| class PurchaseForm(forms.ModelForm): | ||||
|     class Meta: | ||||
|         model = Sale | ||||
| @@ -20,6 +21,7 @@ class PurchaseForm(forms.ModelForm): | ||||
|             'date': DateInput(attrs={'type': 'date'}) | ||||
|         } | ||||
|  | ||||
|  | ||||
| class PurchaseLineForm(forms.ModelForm): | ||||
|     class Meta: | ||||
|         model = SaleLine | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| from django.db import models | ||||
| from django.utils.translation import gettext_lazy as _ | ||||
|  | ||||
|  | ||||
| class Sale(models.Model): | ||||
|  | ||||
|     customer = models.CharField(max_length=100) | ||||
| @@ -23,15 +24,18 @@ class SaleLine(models.Model): | ||||
|     def __str__(self): | ||||
|         return f"{self.sale} - {self.product}" | ||||
|  | ||||
|  | ||||
| class MeasuringUnits(models.TextChoices): | ||||
|     UNIT = 'UNIT', _('Unit') | ||||
|  | ||||
|  | ||||
| class ProductCategory(models.Model): | ||||
|     name = models.CharField(max_length=100, unique=True) | ||||
|  | ||||
|     def __str__(self): | ||||
|         return self.name | ||||
|  | ||||
|  | ||||
| class Product(models.Model): | ||||
|     name = models.CharField(max_length=100, unique=True) | ||||
|     price = models.DecimalField(max_digits=9, decimal_places=2) | ||||
|   | ||||
							
								
								
									
										1
									
								
								tienda_ilusion/don_confiao/tests/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								tienda_ilusion/don_confiao/tests/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| #!/usr/bin/env python3 | ||||
| @@ -1,13 +1,11 @@ | ||||
| from django.test import Client, TestCase | ||||
| from django.contrib.auth.models import AnonymousUser, User | ||||
| from django.conf import settings | ||||
| 
 | ||||
| from .views import import_products, products | ||||
| from .models import ProductCategory, Product | ||||
| from ..models import ProductCategory, Product | ||||
| 
 | ||||
| import os | ||||
| import json | ||||
| 
 | ||||
| 
 | ||||
| class TestProducts(TestCase): | ||||
|     def setUp(self): | ||||
|         self.client = Client() | ||||
| @@ -54,7 +52,8 @@ class TestProducts(TestCase): | ||||
|         first_categories = {p["name"]: p["categories"] for p in first_products} | ||||
|         self._import_csv('example_products2.csv') | ||||
|         updated_products = self._get_products() | ||||
|         updated_categories = {p["name"]: p["categories"] for p in updated_products} | ||||
|         updated_categories = { | ||||
|             p["name"]: p["categories"] for p in updated_products} | ||||
| 
 | ||||
|         self.assertIn('Cafes', first_categories['Arroz']) | ||||
|         self.assertNotIn('Granos', first_categories['Arroz']) | ||||
| @@ -62,7 +61,6 @@ class TestProducts(TestCase): | ||||
|         self.assertIn('Granos', updated_categories['Arroz']) | ||||
|         self.assertNotIn('Cafes', updated_categories['Arroz']) | ||||
| 
 | ||||
| 
 | ||||
|     def test_update_price(self): | ||||
|         self._import_csv() | ||||
|         first_products = self._get_products() | ||||
| @@ -1,5 +1,5 @@ | ||||
| from django.test import TestCase | ||||
| from .models import Sale, SaleLine | ||||
| from ..models import Sale, SaleLine | ||||
| 
 | ||||
| 
 | ||||
| class ConfiaoTest(TestCase): | ||||
| @@ -1,6 +1,6 @@ | ||||
| from django.shortcuts import render | ||||
| from django.http import HttpResponse, HttpResponseRedirect, JsonResponse | ||||
| from django.template import loader | ||||
| from django.http import HttpResponseRedirect, JsonResponse | ||||
| # from django.template import loader | ||||
|  | ||||
| from .models import Sale, Product, ProductCategory | ||||
| from .forms import ImportProductsForm, PurchaseForm, LineaFormSet | ||||
| @@ -8,18 +8,22 @@ from .forms import ImportProductsForm, PurchaseForm, LineaFormSet | ||||
| import csv | ||||
| import io | ||||
|  | ||||
|  | ||||
| def index(request): | ||||
|     return render(request, 'don_confiao/index.html') | ||||
|  | ||||
|  | ||||
| def buy(request): | ||||
|     if request.method == "POST": | ||||
|         sale_form = PurchaseForm(request.POST) | ||||
|         sale_linea_form = LineaFormSet(request.POST) | ||||
|         if sale_form.is_valid(): | ||||
|             sale_form.save() | ||||
|         if sale_linea_form.is_valid(): | ||||
|             sale_linea_form.save() | ||||
|         return HttpResponseRedirect("productos") | ||||
|         if sale_form.is_valid() and sale_linea_form.is_valid(): | ||||
|             sale = sale_form.save() | ||||
|             lines = sale_linea_form.save(commit=False) | ||||
|             for line in lines: | ||||
|                 line.sale = sale | ||||
|                 line.save() | ||||
|             return HttpResponseRedirect("compras") | ||||
|     else: | ||||
|         sale_form = PurchaseForm() | ||||
|         sale_linea_form = LineaFormSet() | ||||
| @@ -32,6 +36,7 @@ def buy(request): | ||||
|         } | ||||
|     ) | ||||
|  | ||||
|  | ||||
| def purchases(request): | ||||
|     purchases = Sale.objects.all() | ||||
|     context = { | ||||
| @@ -69,14 +74,17 @@ def import_products(request): | ||||
|         {'form': form} | ||||
|     ) | ||||
|  | ||||
|  | ||||
| def _categories_from_csv_string(categories_string, separator="&"): | ||||
|     categories = categories_string.split(separator) | ||||
|     clean_categories = [c.strip() for c in categories] | ||||
|     return [_category_from_name(category) for category in clean_categories] | ||||
|  | ||||
|  | ||||
| def _category_from_name(name): | ||||
|     return ProductCategory.objects.get_or_create(name=name)[0] | ||||
|  | ||||
|  | ||||
| def handle_import_products_file(csv_file): | ||||
|     data = io.StringIO(csv_file.read().decode('utf-8')) | ||||
|     reader = csv.DictReader(data, quotechar='"') | ||||
|   | ||||
		Reference in New Issue
	
	Block a user