fix(api): remove SaleWithLinesViewSet.
This commit is contained in:
		@@ -2,18 +2,18 @@ from rest_framework import viewsets
 | 
				
			|||||||
from rest_framework.response import Response
 | 
					from rest_framework.response import Response
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from .models import Sale, SaleLine, Customer, Product
 | 
					from .models import Sale, SaleLine, Customer, Product
 | 
				
			||||||
from .serializers import SaleSerializer, SaleWithLinesSerializer, ProductSerializer, CustomerSerializer
 | 
					from .serializers import SaleSerializer, ProductSerializer, CustomerSerializer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SaleWithLinesViewSet(viewsets.ModelViewSet):
 | 
					class SaleView(viewsets.ModelViewSet):
 | 
				
			||||||
    queryset = Sale.objects.all()
 | 
					    queryset = Sale.objects.all()
 | 
				
			||||||
    serializer_class = SaleWithLinesSerializer
 | 
					    serializer_class = SaleSerializer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def create(self, request):
 | 
					    def create(self, request):
 | 
				
			||||||
        data = request.data
 | 
					        data = request.data
 | 
				
			||||||
        customer = Customer.objects.get(pk=data['customer'])
 | 
					        customer = Customer.objects.get(pk=data['customer'])
 | 
				
			||||||
        date = data['date']
 | 
					        date = data['date']
 | 
				
			||||||
        lines = data['lines']
 | 
					        lines = data['saleline_set']
 | 
				
			||||||
        sale = Sale.objects.create(customer=customer, date=date)
 | 
					        sale = Sale.objects.create(customer=customer, date=date)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for line in lines:
 | 
					        for line in lines:
 | 
				
			||||||
@@ -30,11 +30,6 @@ class SaleWithLinesViewSet(viewsets.ModelViewSet):
 | 
				
			|||||||
        return Response({'message': 'Venta creada con exito'}, status=201)
 | 
					        return Response({'message': 'Venta creada con exito'}, status=201)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SaleView(viewsets.ModelViewSet):
 | 
					 | 
				
			||||||
    queryset = Sale.objects.all()
 | 
					 | 
				
			||||||
    serializer_class = SaleSerializer
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class ProductView(viewsets.ModelViewSet):
 | 
					class ProductView(viewsets.ModelViewSet):
 | 
				
			||||||
    queryset = Product.objects.all()
 | 
					    queryset = Product.objects.all()
 | 
				
			||||||
    serializer_class = ProductSerializer
 | 
					    serializer_class = ProductSerializer
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,23 +9,10 @@ class SaleLineSerializer(serializers.ModelSerializer):
 | 
				
			|||||||
        fields = ['id', 'sale', 'product', 'unit_price', 'quantity']
 | 
					        fields = ['id', 'sale', 'product', 'unit_price', 'quantity']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SaleWithLinesSerializer(serializers.ModelSerializer):
 | 
					 | 
				
			||||||
    lines = SaleLineSerializer(many=True)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    class Meta:
 | 
					 | 
				
			||||||
        model = Sale
 | 
					 | 
				
			||||||
        fields = ['id', 'customer', 'date', 'lines']
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class SaleSerializer(serializers.ModelSerializer):
 | 
					class SaleSerializer(serializers.ModelSerializer):
 | 
				
			||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        model = Sale
 | 
					        model = Sale
 | 
				
			||||||
        fields = ['id', 'customer', 'date']
 | 
					        fields = ['id', 'customer', 'date', 'saleline_set']
 | 
				
			||||||
 | 
					 | 
				
			||||||
    def to_representation(self, instance):
 | 
					 | 
				
			||||||
        representation = super().to_representation(instance)
 | 
					 | 
				
			||||||
        representation['lines'] = [line for line in instance.saleline_set.all().values()]
 | 
					 | 
				
			||||||
        return representation
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ProductSerializer(serializers.ModelSerializer):
 | 
					class ProductSerializer(serializers.ModelSerializer):
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,7 +21,7 @@ class TestAPI(APITestCase):
 | 
				
			|||||||
        data = {
 | 
					        data = {
 | 
				
			||||||
            'customer': self.customer.id,
 | 
					            'customer': self.customer.id,
 | 
				
			||||||
            'date': '2024-09-02',
 | 
					            'date': '2024-09-02',
 | 
				
			||||||
            'lines': [
 | 
					            'saleline_set': [
 | 
				
			||||||
                {'product': self.product.id, 'quantity': 2, 'unit_price': 3000},
 | 
					                {'product': self.product.id, 'quantity': 2, 'unit_price': 3000},
 | 
				
			||||||
                {'product': self.product.id, 'quantity': 3, 'unit_price': 5000}
 | 
					                {'product': self.product.id, 'quantity': 3, 'unit_price': 5000}
 | 
				
			||||||
            ],
 | 
					            ],
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,6 +22,5 @@ urlpatterns = [
 | 
				
			|||||||
    path("cuadrar_tarro", views.reconciliate_jar, name="reconciliate_jar"),
 | 
					    path("cuadrar_tarro", views.reconciliate_jar, name="reconciliate_jar"),
 | 
				
			||||||
    path("cuadres", views.reconciliate_jar, name="reconciliations"),
 | 
					    path("cuadres", views.reconciliate_jar, name="reconciliations"),
 | 
				
			||||||
    path("resumen_compra/<int:id>", views.purchase_summary, name="purchase_summary"),
 | 
					    path("resumen_compra/<int:id>", views.purchase_summary, name="purchase_summary"),
 | 
				
			||||||
    path('api/sales_with_lines/', api_views.SaleWithLinesViewSet.as_view({'post': 'create'}), name='sale-create'),
 | 
					 | 
				
			||||||
    path('api/', include(router.urls)),
 | 
					    path('api/', include(router.urls)),
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user