views.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from itertools import chain
  2. from django.core.urlresolvers import reverse
  3. from django.shortcuts import render, get_object_or_404
  4. from .models import Document, Service, CostUse, GoodUse
  5. def index(request):
  6. return render(request, 'costs/index.html')
  7. def list_documents(request):
  8. breadcrumbs = (
  9. ('Documents', reverse('list-documents')),
  10. )
  11. docs = Document.objects.all().prefetch_related('service_set')
  12. return render(
  13. request, 'costs/documents_list.html', {
  14. 'documents': docs,
  15. 'breadcrumbs': breadcrumbs,
  16. })
  17. def detail_document(request, pk):
  18. doc = get_object_or_404(Document, pk=pk)
  19. breadcrumbs = (
  20. ('Documents', reverse('list-documents')),
  21. (str(doc), doc.get_absolute_url())
  22. )
  23. return render(
  24. request, 'costs/document_detail.html', {
  25. 'document': doc,
  26. 'breadcrumbs': breadcrumbs,
  27. })
  28. def detail_service(request, pk):
  29. service = Service.objects.get(pk=pk)
  30. doc = service.document
  31. breadcrumbs = (
  32. ('Documents', reverse('list-documents')),
  33. (str(doc), doc.get_absolute_url()),
  34. (service.name, service.get_absolute_url())
  35. )
  36. costs_uses = CostUse.objects.filter(service=service)
  37. goods_uses = GoodUse.objects.filter(service=service)
  38. total_costs_price = sum(chain(
  39. (i.monthly_provision_share() for i in goods_uses),
  40. (i.cost_share() for i in costs_uses),
  41. ))
  42. total_goods_value_share = sum(i.value_share() for i in goods_uses)
  43. if service.subscriptions_count == 0:
  44. unit_costs_price = 0
  45. unit_goods_value_share = 0
  46. else:
  47. unit_costs_price = total_costs_price/service.subscriptions_count
  48. unit_goods_value_share = total_goods_value_share/service.subscriptions_count
  49. context = {
  50. 'breadcrumbs': breadcrumbs,
  51. 'document': doc,
  52. 'service': service,
  53. 'costs_uses': costs_uses,
  54. 'goods_uses': goods_uses,
  55. 'total_costs_price': total_costs_price,
  56. 'unit_costs_price': unit_costs_price,
  57. 'total_goods_value_share': total_goods_value_share,
  58. 'unit_goods_value_share': unit_goods_value_share,
  59. 'monthly_fas': unit_goods_value_share/12,
  60. }
  61. return render(request, 'costs/service_detail.html', context)