django(View): add purchase summary json endpoint.
This commit is contained in:
		@@ -32,3 +32,10 @@ class TestSummaryViewPurchase(TestCase):
 | 
				
			|||||||
        self.assertEqual(response.status_code, 200)
 | 
					        self.assertEqual(response.status_code, 200)
 | 
				
			||||||
        self.assertEqual(response.context["purchase"].customer, self.purchase.customer)
 | 
					        self.assertEqual(response.context["purchase"].customer, self.purchase.customer)
 | 
				
			||||||
        self.assertIn('Alejo Mono', response.content.decode('utf-8'))
 | 
					        self.assertIn('Alejo Mono', response.content.decode('utf-8'))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_json_summary(self):
 | 
				
			||||||
 | 
					        response = self.client.get(f"/don_confiao/resumen_compra_json/{self.purchase.id}")
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, 200)
 | 
				
			||||||
 | 
					        self.assertIn('Alejo Mono', response.content.decode('utf-8'))
 | 
				
			||||||
 | 
					        self.assertIn('cafe', response.content.decode('utf-8'))
 | 
				
			||||||
 | 
					        self.assertIn('72500', response.content.decode('utf-8'))
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,5 +25,6 @@ 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("resumen_compra_json/<int:id>", views.purchase_json_summary, name="purchase_json_summary"),
 | 
				
			||||||
    path('api/', include(router.urls)),
 | 
					    path('api/', include(router.urls)),
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -117,6 +117,7 @@ def reconciliate_jar(request):
 | 
				
			|||||||
def reconciliations(request):
 | 
					def reconciliations(request):
 | 
				
			||||||
    return HttpResponse('<h1>Reconciliaciones</h1>')
 | 
					    return HttpResponse('<h1>Reconciliaciones</h1>')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def purchase_summary(request, id):
 | 
					def purchase_summary(request, id):
 | 
				
			||||||
    purchase = Sale.objects.get(pk=id)
 | 
					    purchase = Sale.objects.get(pk=id)
 | 
				
			||||||
    return render(
 | 
					    return render(
 | 
				
			||||||
@@ -128,6 +129,36 @@ def purchase_summary(request, id):
 | 
				
			|||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def purchase_json_summary(request, id):
 | 
				
			||||||
 | 
					    purchase = Sale.objects.get(pk=id)
 | 
				
			||||||
 | 
					    lines = []
 | 
				
			||||||
 | 
					    for line in purchase.saleline_set.all():
 | 
				
			||||||
 | 
					        lines.append({
 | 
				
			||||||
 | 
					            'product': {
 | 
				
			||||||
 | 
					                'id': line.product.id,
 | 
				
			||||||
 | 
					                'name': line.product.name,
 | 
				
			||||||
 | 
					                'quantity': line.quantity,
 | 
				
			||||||
 | 
					                'unit_price': line.unit_price,
 | 
				
			||||||
 | 
					                'description': line.description,
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    to_response = {
 | 
				
			||||||
 | 
					        'id': purchase.id,
 | 
				
			||||||
 | 
					        'customer': {
 | 
				
			||||||
 | 
					            'id': purchase.customer.id,
 | 
				
			||||||
 | 
					            'name': purchase.customer.name,
 | 
				
			||||||
 | 
					            # 'phone': _mask_phone(purchase.customer.phone)
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        'set_lines': lines,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return JsonResponse(to_response, safe=False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def _mask_phone(phone):
 | 
				
			||||||
 | 
					    digits = str(phone)[-3:] if phone else " " * 3
 | 
				
			||||||
 | 
					    return "X" * 7 + digits
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _categories_from_csv_string(categories_string, separator="&"):
 | 
					def _categories_from_csv_string(categories_string, separator="&"):
 | 
				
			||||||
    categories = categories_string.split(separator)
 | 
					    categories = categories_string.split(separator)
 | 
				
			||||||
    clean_categories = [c.strip() for c in categories]
 | 
					    clean_categories = [c.strip() for c in categories]
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user