Implementando api #32
@@ -1 +1,2 @@
 | 
			
		||||
Django==5.0.6
 | 
			
		||||
djangorestframework
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										30
									
								
								tienda_ilusion/don_confiao/api_views.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								tienda_ilusion/don_confiao/api_views.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
from rest_framework import viewsets
 | 
			
		||||
from rest_framework.response import Response
 | 
			
		||||
 | 
			
		||||
from .models import Sale, SaleLine, Customer, Product
 | 
			
		||||
from .serializers import SaleSerializer
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SaleViewSet(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['lines']
 | 
			
		||||
        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)
 | 
			
		||||
							
								
								
									
										17
									
								
								tienda_ilusion/don_confiao/serializers.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								tienda_ilusion/don_confiao/serializers.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
from rest_framework import serializers
 | 
			
		||||
 | 
			
		||||
from .models import Sale, SaleLine
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SaleLineSerializer(serializers.ModelSerializer):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = SaleLine
 | 
			
		||||
        fields = ['id', 'sale', 'product', 'unit_price', 'quantity']
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SaleSerializer(serializers.ModelSerializer):
 | 
			
		||||
    lines = SaleLineSerializer(many=True)
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = Sale
 | 
			
		||||
        fields = ['id', 'customer', 'date', 'lines']
 | 
			
		||||
							
								
								
									
										32
									
								
								tienda_ilusion/don_confiao/tests/test_api.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								tienda_ilusion/don_confiao/tests/test_api.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
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',
 | 
			
		||||
            'lines': [
 | 
			
		||||
                {'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)
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
from django.urls import path
 | 
			
		||||
 | 
			
		||||
from . import views
 | 
			
		||||
from . import api_views
 | 
			
		||||
 | 
			
		||||
app_name = 'don_confiao'
 | 
			
		||||
urlpatterns = [
 | 
			
		||||
@@ -13,4 +14,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/sales/', api_views.SaleViewSet.as_view({'post': 'create'}), name='sale-create')
 | 
			
		||||
]
 | 
			
		||||
 
 | 
			
		||||
@@ -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