PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/polls/views.py

https://gitlab.com/lidorle/Django-Assignment1
Python | 109 lines | 79 code | 27 blank | 3 comment | 10 complexity | 75f2cd27daf506cfc4037895c57ad58d MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import pandas as pd
  4. from django.shortcuts import render
  5. from .models import Car,Company,PriceList
  6. from django.http import Http404
  7. import json
  8. from django.core import serializers
  9. from django.core.serializers.json import DjangoJSONEncoder
  10. from django.views.decorators.csrf import csrf_exempt
  11. import urlparse
  12. from django.http import HttpResponse
  13. # Create your views here.
  14. def make_excel_file(name,obj):
  15. df = pd.DataFrame(obj)
  16. df.to_excel(name,engine='openpyxl',index=False)
  17. return name
  18. @csrf_exempt
  19. def cars_from_active_company(request):
  20. obj = list(Car.objects.cars_company_is_active())
  21. if not obj:
  22. raise Http404("The company: There are no company that i avtive.")
  23. print obj
  24. if request.is_ajax():
  25. return HttpResponse(json.dumps(obj, cls=DjangoJSONEncoder), content_type='application/json')
  26. file_name = "cars_from_active_company.xlsx"
  27. file = open(make_excel_file(file_name,obj),'rb')
  28. response = HttpResponse(file,content_type='application/vnd.ms-excel')
  29. response['Content-Disposition'] = 'attachment; filename= '+ file_name
  30. return response
  31. @csrf_exempt
  32. def companys_name_start_with(request,letter):
  33. obj = list(Company.objects.companys_name_start_with(letter))
  34. if not obj:
  35. raise Http404("No sech company that start the latter:.".format(letter))
  36. if request.GET.get('type')=='Ajax':
  37. return HttpResponse(json.dumps(obj), content_type='application/json')
  38. file_name = make_excel_file("price_list_for_car_{0}.xlsx".format(letter),obj)
  39. file = open(make_excel_file(file_name,obj),'rb')
  40. response = HttpResponse(file,content_type='application/vnd.ms-excel')
  41. response['Content-Disposition'] = 'attachment; filename= '+ file_name
  42. return response
  43. @csrf_exempt
  44. def PriceList_by_company(request,company):
  45. obj = list(PriceList.objects.sort_by_company(company))
  46. if not obj:
  47. raise Http404("The company:{0} may not in the data base or there are no cars.".format(company))
  48. if request.GET.get('type')=='Ajax':
  49. return HttpResponse(json.dumps(obj), content_type='application/json')
  50. file_name = "PriceList_by_company_{0}.xlsx".format(company)
  51. file = open(make_excel_file(file_name,obj),'rb')
  52. response = HttpResponse(file,content_type='application/vnd.ms-excel')
  53. response['Content-Disposition'] = 'attachment; filename= '+ file_name
  54. return response
  55. @csrf_exempt
  56. def price_list_for_car(request,car):
  57. obj = list(PriceList.objects.car_price_in_all_companys(car))
  58. print obj
  59. if not obj:
  60. raise Http404("The car:{0} may not be in the data base or there there no relations to companys.".format(car))
  61. if request.GET.get('type')=='Ajax':
  62. return HttpResponse(json.dumps(obj), content_type='application/json')
  63. file_name = "price_list_for_car_{0}.xlsx".format(car)
  64. file = open(make_excel_file(file_name,obj),'rb')
  65. response = HttpResponse(file,content_type='application/vnd.ms-excel')
  66. response['Content-Disposition'] = 'attachment; filename= '+ file_name
  67. return response
  68. @csrf_exempt
  69. def price_list_by_minmun_price(request):
  70. obj = list(PriceList.objects.comany_minmum_price())
  71. if not obj:
  72. raise Http404("The car:{0} may not be in the data base or there there no relations to companys.".format())
  73. if request.GET.get('type')=='Ajax':
  74. # return HttpResponse(serializers.serialize('json',obj,use_natural_foreign_keys=True ), content_type='application/json')
  75. return HttpResponse(json.dumps(obj), content_type='application/json')
  76. file_name = "price_list_by_minmun_price.xlsx"
  77. file = open(make_excel_file(file_name,obj),'rb')
  78. response = HttpResponse(file,content_type='application/vnd.ms-excel')
  79. response['Content-Disposition'] = 'attachment; filename= '+ file_name
  80. return response
  81. def home(request):
  82. return render(request, 'home.html')