views.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from django.db.models import Max
  2. from django.shortcuts import render
  3. from django.forms.models import model_to_dict
  4. # Create your views here.
  5. from rest_framework import status,filters
  6. from rest_framework.authentication import SessionAuthentication, BasicAuthentication
  7. from rest_framework.permissions import IsAuthenticated,AllowAny
  8. from rest_framework.response import Response
  9. from rest_framework.views import APIView
  10. from rest_framework.viewsets import ModelViewSet
  11. from rest_framework_jwt.authentication import JSONWebTokenAuthentication
  12. from utils import elmentList
  13. from bsdata.models import Company, Navigation, Slideshow, Hmwe, Hmart, Column, Hmcase, Hmgroup, Hmaddress, \
  14. WnComponentCssModel, WnComponentModel, WnComponentKeyModel
  15. from bsdata.serializer import WnComponentCssSerializer, WnComponentSerializer, WnComponentKeyModelSerializer
  16. # 查看公司
  17. class WbCompany(APIView):
  18. # 1,设置局部认证
  19. authentication_classes = [SessionAuthentication, BasicAuthentication]
  20. # 2,设置局部权限
  21. permission_classes = (AllowAny,)
  22. def get(self,request):
  23. id__max = Company.objects.aggregate(Max('id'))
  24. data = Company.objects.get(id=id__max['id__max'])
  25. data = {'id':data.id,'name':data.name}
  26. return Response(data, status=status.HTTP_200_OK)
  27. # 查看导航
  28. class WbNavigation(APIView):
  29. # 1,设置局部认证
  30. authentication_classes = [SessionAuthentication, BasicAuthentication]
  31. # 2,设置局部权限
  32. permission_classes = (AllowAny,)
  33. def get(self,request):
  34. data = Navigation.objects.all().values('id', 'name','path')
  35. data = list(data)
  36. return Response(data, status=status.HTTP_200_OK)
  37. # 获取轮播
  38. class WbSlideshow(APIView):
  39. # 1,设置局部认证
  40. authentication_classes = [SessionAuthentication, BasicAuthentication]
  41. # 2,设置局部权限
  42. permission_classes = (AllowAny,)
  43. def get(self,request):
  44. data = Slideshow.objects.all().values('id', 'imgurl','url','sort').order_by('sort')
  45. data = list(data)
  46. return Response(data, status=status.HTTP_200_OK)
  47. # 关于我们
  48. class WbHmwe(APIView):
  49. # 1,设置局部认证
  50. authentication_classes = [SessionAuthentication, BasicAuthentication]
  51. # 2,设置局部权限
  52. permission_classes = (AllowAny,)
  53. def get(self,request):
  54. data = Hmwe.objects.all().values('id', 'title','content')
  55. data = list(data)
  56. return Response(data, status=status.HTTP_200_OK)
  57. # 技术与服务
  58. class WbHmart(APIView):
  59. # 1,设置局部认证
  60. authentication_classes = [SessionAuthentication, BasicAuthentication]
  61. # 2,设置局部权限
  62. permission_classes = (AllowAny,)
  63. def get(self,request):
  64. data = Hmart.objects.all().values('id', 'title','content','icon')
  65. data = list(data)
  66. return Response(data, status=status.HTTP_200_OK)
  67. # 栏目标签
  68. class WbColumn(APIView):
  69. # 1,设置局部认证
  70. authentication_classes = [SessionAuthentication, BasicAuthentication]
  71. # 2,设置局部权限
  72. permission_classes = (AllowAny,)
  73. def get(self,request):
  74. data = Column.objects.all().values('id', 'title','content')
  75. data = list(data)
  76. return Response(data, status=status.HTTP_200_OK)
  77. # 成功案例
  78. class WbHmcase(APIView):
  79. # 1,设置局部认证
  80. authentication_classes = [SessionAuthentication, BasicAuthentication]
  81. # 2,设置局部权限
  82. permission_classes = (AllowAny,)
  83. def get(self,request):
  84. data = Hmcase.objects.all().values('id', 'company','artisan','signatory','satisfaction','words','signatory_time')
  85. data = list(data)
  86. return Response(data, status=status.HTTP_200_OK)
  87. # 团队介绍
  88. class WbHmgroup(APIView):
  89. # 1,设置局部认证
  90. authentication_classes = [SessionAuthentication, BasicAuthentication]
  91. # 2,设置局部权限
  92. permission_classes = (AllowAny,)
  93. def get(self,request):
  94. data = Hmgroup.objects.all().values('id', 'name','imgurl','intro')
  95. data = list(data)
  96. return Response(data, status=status.HTTP_200_OK)
  97. class WbHmaddress(APIView):
  98. # 1,设置局部认证
  99. authentication_classes = [SessionAuthentication, BasicAuthentication]
  100. # 2,设置局部权限
  101. permission_classes = (AllowAny,)
  102. def get(self,request):
  103. data = Hmaddress.objects.all().values('id', 'content','add','tel')
  104. data = list(data)
  105. return Response(data, status=status.HTTP_200_OK)
  106. # 获取组件使用中的组件
  107. class WnComponentkeyView(ModelViewSet):
  108. # 1,设置局部认证
  109. authentication_classes = [SessionAuthentication, BasicAuthentication]
  110. # 2,设置局部权限
  111. permission_classes = (AllowAny,)
  112. # 1,提供通用的序列化器
  113. serializer_class = WnComponentKeyModelSerializer
  114. # filter_backends = (filters.OrderingFilter,) # 固定写法
  115. filter_backends = (filters.OrderingFilter,) # 固定写法
  116. filterset_fields = ('id', 'sort')
  117. ordering_fields = ('id','sort',)
  118. # 2,提供通用的数据集
  119. queryset = WnComponentKeyModel.objects.all()
  120. import ast
  121. # 获取组件样式
  122. class WnComponentCssView(APIView):
  123. # 1,设置局部认证
  124. authentication_classes = [SessionAuthentication, BasicAuthentication]
  125. # 2,设置局部权限
  126. permission_classes = (AllowAny,)
  127. def get(self, request):
  128. params = request.query_params.get("componentkey")
  129. result = WnComponentCssModel.objects.filter(componentkey = params)
  130. field_list = elmentList.field_list
  131. # field_list = []
  132. if len(result) == 0 :
  133. return Response('没有查询到样式', status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION)
  134. data = model_to_dict(result[0]) # 取第一条记录,转换为字典
  135. new_data = {}
  136. for key,value in data.items():
  137. new_data[key] = value
  138. if key in field_list:
  139. data[key] = ast.literal_eval(value)
  140. return Response(data, status=status.HTTP_200_OK)