Implementando api #32
@@ -1 +1,2 @@
 | 
			
		||||
Django==5.0.6
 | 
			
		||||
djangorestframework
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										40
									
								
								tienda_ilusion/don_confiao/api_views.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								tienda_ilusion/don_confiao/api_views.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,40 @@
 | 
			
		||||
from rest_framework import viewsets
 | 
			
		||||
from rest_framework.response import Response
 | 
			
		||||
 | 
			
		||||
from .models import Sale, SaleLine, Customer, Product
 | 
			
		||||
from .serializers import SaleSerializer, ProductSerializer, CustomerSerializer
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SaleView(viewsets.ModelViewSet):
 | 
			
		||||
    queryset = Sale.objects.all()
 | 
			
		||||
    serializer_class = SaleSerializer
 | 
			
		||||
 | 
			
		||||
    def create(self, request):
 | 
			
		||||
        data = request.data
 | 
			
		||||
        customer = Customer.objects.get(pk=data['customer'])
 | 
			
		||||
        date = data['date']
 | 
			
		||||
        lines = data['saleline_set']
 | 
			
		||||
        sale = Sale.objects.create(customer=customer, date=date)
 | 
			
		||||
 | 
			
		||||
        for line in lines:
 | 
			
		||||
            product = Product.objects.get(pk=line['product'])
 | 
			
		||||
            quantity = line['quantity']
 | 
			
		||||
            unit_price = line['unit_price']
 | 
			
		||||
            SaleLine.objects.create(
 | 
			
		||||
                sale=sale,
 | 
			
		||||
                product=product,
 | 
			
		||||
                quantity=quantity,
 | 
			
		||||
                unit_price=unit_price
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        return Response({'message': 'Venta creada con exito'}, status=201)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ProductView(viewsets.ModelViewSet):
 | 
			
		||||
    queryset = Product.objects.all()
 | 
			
		||||
    serializer_class = ProductSerializer
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CustomerView(viewsets.ModelViewSet):
 | 
			
		||||
    queryset = Customer.objects.all()
 | 
			
		||||
    serializer_class = CustomerSerializer
 | 
			
		||||
							
								
								
									
										26
									
								
								tienda_ilusion/don_confiao/serializers.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								tienda_ilusion/don_confiao/serializers.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
from rest_framework import serializers
 | 
			
		||||
 | 
			
		||||
from .models import Sale, SaleLine, Product, Customer
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SaleLineSerializer(serializers.ModelSerializer):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = SaleLine
 | 
			
		||||
        fields = ['id', 'sale', 'product', 'unit_price', 'quantity']
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SaleSerializer(serializers.ModelSerializer):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = Sale
 | 
			
		||||
        fields = ['id', 'customer', 'date', 'saleline_set']
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ProductSerializer(serializers.ModelSerializer):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = Product
 | 
			
		||||
        fields = ['name', 'price', 'measuring_unit', 'categories']
 | 
			
		||||
 | 
			
		||||
class CustomerSerializer(serializers.ModelSerializer):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = Customer
 | 
			
		||||
        fields = ['name', 'address']
 | 
			
		||||
							
								
								
									
										49
									
								
								tienda_ilusion/don_confiao/tests/test_api.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								tienda_ilusion/don_confiao/tests/test_api.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,49 @@
 | 
			
		||||
import json
 | 
			
		||||
from django.urls import reverse
 | 
			
		||||
from rest_framework import status
 | 
			
		||||
from rest_framework.test import APITestCase
 | 
			
		||||
from ..models import Sale, Product, Customer
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestAPI(APITestCase):
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
        self.product = Product.objects.create(
 | 
			
		||||
            name='Panela',
 | 
			
		||||
            price=5000,
 | 
			
		||||
            measuring_unit='UNIT'
 | 
			
		||||
        )
 | 
			
		||||
        self.customer = Customer.objects.create(
 | 
			
		||||
            name='Camilo'
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def test_create_sale(self):
 | 
			
		||||
        url = '/don_confiao/api/sales/'
 | 
			
		||||
        data = {
 | 
			
		||||
            'customer': self.customer.id,
 | 
			
		||||
            'date': '2024-09-02',
 | 
			
		||||
            'saleline_set': [
 | 
			
		||||
                {'product': self.product.id, 'quantity': 2, 'unit_price': 3000},
 | 
			
		||||
                {'product': self.product.id, 'quantity': 3, 'unit_price': 5000}
 | 
			
		||||
            ],
 | 
			
		||||
        }
 | 
			
		||||
        response = self.client.post(url, data, format='json')
 | 
			
		||||
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
 | 
			
		||||
        self.assertEqual(Sale.objects.count(), 1)
 | 
			
		||||
        self.assertEqual(
 | 
			
		||||
            Sale.objects.all()[0].customer.name,
 | 
			
		||||
            self.customer.name
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def test_get_products(self):
 | 
			
		||||
        url = '/don_confiao/api/products/'
 | 
			
		||||
        response = self.client.get(url)
 | 
			
		||||
        json_response = json.loads(response.content.decode('utf-8'))
 | 
			
		||||
        self.assertEqual(response.status_code, 200)
 | 
			
		||||
        self.assertEqual(self.product.name, json_response[0]['name'])
 | 
			
		||||
 | 
			
		||||
    def test_get_customers(self):
 | 
			
		||||
        url = '/don_confiao/api/customers/'
 | 
			
		||||
        response = self.client.get(url)
 | 
			
		||||
        json_response = json.loads(response.content.decode('utf-8'))
 | 
			
		||||
        self.assertEqual(response.status_code, 200)
 | 
			
		||||
        self.assertEqual(self.customer.name, json_response[0]['name'])
 | 
			
		||||
@@ -1,8 +1,17 @@
 | 
			
		||||
from django.urls import path
 | 
			
		||||
from django.urls import path, include
 | 
			
		||||
from rest_framework.routers import DefaultRouter
 | 
			
		||||
 | 
			
		||||
from . import views
 | 
			
		||||
from . import api_views
 | 
			
		||||
 | 
			
		||||
app_name = 'don_confiao'
 | 
			
		||||
 | 
			
		||||
router = DefaultRouter()
 | 
			
		||||
router.register(r'sales', api_views.SaleView, basename='sale')
 | 
			
		||||
router.register(r'customers', api_views.CustomerView, basename='customer')
 | 
			
		||||
router.register(r'products', api_views.ProductView, basename='product')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
urlpatterns = [
 | 
			
		||||
    path("", views.index, name="wellcome"),
 | 
			
		||||
    path("comprar", views.buy, name="buy"),
 | 
			
		||||
@@ -16,4 +25,5 @@ urlpatterns = [
 | 
			
		||||
    path("cuadrar_tarro", views.reconciliate_jar, name="reconciliate_jar"),
 | 
			
		||||
    path("cuadres", views.reconciliate_jar, name="reconciliations"),
 | 
			
		||||
    path("resumen_compra/<int:id>", views.purchase_summary, name="purchase_summary"),
 | 
			
		||||
    path('api/', include(router.urls)),
 | 
			
		||||
]
 | 
			
		||||
 
 | 
			
		||||
@@ -38,6 +38,7 @@ INSTALLED_APPS = [
 | 
			
		||||
    'django.contrib.sessions',
 | 
			
		||||
    'django.contrib.messages',
 | 
			
		||||
    'django.contrib.staticfiles',
 | 
			
		||||
    'rest_framework',
 | 
			
		||||
    # 'don_confiao'
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user