views.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from itertools import chain
  2. from django.shortcuts import render
  3. from .models import Service, Cost, Good, CostUse, GoodUse
  4. def index(request):
  5. return render(request, 'costs/index.html')
  6. def list_services(request):
  7. context = {
  8. 'services': Service.objects.all(),
  9. }
  10. return render(request, 'costs/services_list.html', context)
  11. def list_resources(request):
  12. context = {
  13. 'goods': Good.objects.all(),
  14. 'costs': Cost.objects.all(),
  15. }
  16. return render(request, 'costs/resources_list.html', context)
  17. def detail_service(request, pk):
  18. service = Service.objects.get(pk=pk)
  19. costs_uses = CostUse.objects.filter(service=service)
  20. goods_uses = GoodUse.objects.filter(service=service)
  21. total_costs_price = sum(chain(
  22. (i.monthly_provision_share() for i in goods_uses),
  23. (i.cost_share() for i in costs_uses),
  24. ))
  25. if service.subscriptions_count == 0:
  26. unit_costs_price = 0
  27. else:
  28. unit_costs_price = total_costs_price/service.subscriptions_count
  29. context = {
  30. 'service': service,
  31. 'costs_uses': costs_uses,
  32. 'goods_uses': goods_uses,
  33. 'total_costs_price': total_costs_price,
  34. 'unit_costs_price': unit_costs_price,
  35. }
  36. return render(request, 'costs/service_detail.html', context)