feat(api): implement endpoints to products, customer and sales.
This commit is contained in:
		| @@ -2,12 +2,12 @@ from rest_framework import viewsets | ||||
| from rest_framework.response import Response | ||||
|  | ||||
| from .models import Sale, SaleLine, Customer, Product | ||||
| from .serializers import SaleSerializer | ||||
| from .serializers import SaleSerializer, SaleWithLinesSerializer, ProductSerializer, CustomerSerializer | ||||
|  | ||||
|  | ||||
| class SaleViewSet(viewsets.ModelViewSet): | ||||
| class SaleWithLinesViewSet(viewsets.ModelViewSet): | ||||
|     queryset = Sale.objects.all() | ||||
|     serializer_class = SaleSerializer | ||||
|     serializer_class = SaleWithLinesSerializer | ||||
|  | ||||
|     def create(self, request): | ||||
|         data = request.data | ||||
| @@ -28,3 +28,18 @@ class SaleViewSet(viewsets.ModelViewSet): | ||||
|             ) | ||||
|  | ||||
|         return Response({'message': 'Venta creada con exito'}, status=201) | ||||
|  | ||||
|  | ||||
| class SaleView(viewsets.ModelViewSet): | ||||
|     queryset = Sale.objects.all() | ||||
|     serializer_class = SaleSerializer | ||||
|  | ||||
|  | ||||
| class ProductView(viewsets.ModelViewSet): | ||||
|     queryset = Product.objects.all() | ||||
|     serializer_class = ProductSerializer | ||||
|  | ||||
|  | ||||
| class CustomerView(viewsets.ModelViewSet): | ||||
|     queryset = Customer.objects.all() | ||||
|     serializer_class = CustomerSerializer | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| from rest_framework import serializers | ||||
|  | ||||
| from .models import Sale, SaleLine | ||||
| from .models import Sale, SaleLine, Product, Customer | ||||
|  | ||||
|  | ||||
| class SaleLineSerializer(serializers.ModelSerializer): | ||||
| @@ -9,9 +9,31 @@ class SaleLineSerializer(serializers.ModelSerializer): | ||||
|         fields = ['id', 'sale', 'product', 'unit_price', 'quantity'] | ||||
|  | ||||
|  | ||||
| class SaleSerializer(serializers.ModelSerializer): | ||||
| class SaleWithLinesSerializer(serializers.ModelSerializer): | ||||
|     lines = SaleLineSerializer(many=True) | ||||
|  | ||||
|     class Meta: | ||||
|         model = Sale | ||||
|         fields = ['id', 'customer', 'date', 'lines'] | ||||
|  | ||||
|  | ||||
| class SaleSerializer(serializers.ModelSerializer): | ||||
|     class Meta: | ||||
|         model = Sale | ||||
|         fields = ['id', 'customer', 'date'] | ||||
|  | ||||
|     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 Meta: | ||||
|         model = Product | ||||
|         fields = ['name', 'price', 'measuring_unit', 'categories'] | ||||
|  | ||||
| class CustomerSerializer(serializers.ModelSerializer): | ||||
|     class Meta: | ||||
|         model = Customer | ||||
|         fields = ['name', 'address'] | ||||
|   | ||||
| @@ -29,4 +29,21 @@ class TestAPI(APITestCase): | ||||
|         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) | ||||
|         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,9 +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"), | ||||
| @@ -14,5 +22,6 @@ 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') | ||||
|     path('api/sales_with_lines/', api_views.SaleWithLinesViewSet.as_view({'post': 'create'}), name='sale-create'), | ||||
|     path('api/', include(router.urls)), | ||||
| ] | ||||
|   | ||||
		Reference in New Issue
	
	Block a user