views.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. from django.shortcuts import render
  2. # Create your views here.
  3. # 查询菜单栏
  4. from django_filters.rest_framework import DjangoFilterBackend
  5. from rest_framework import status,filters
  6. from rest_framework.authentication import SessionAuthentication
  7. from rest_framework.permissions import IsAuthenticated
  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 .models import Bar,Navigation,Company,Slideshow,EditorImg,EditorContens,Hmwe,Hmart,Hmcase,Hmgroup,Hmaddress,Column,WnComponentModel,WnComponentCssModel,\
  14. WnComponentKeyModel,WnComponentCssGroupModel
  15. from .serializer import WebNavigationModelSerializer,WebCompanyModelSerializer,WebSlideshowModelSerializer,WebEditorContensModelSerializer,\
  16. WebHmweModelSerializer,WebHmartModelSerializer,WebHmcaseModelSerializer,WebHmgroupModelSerializer,WebHmaddressModelSerializer,WebColumnModelSerializer,\
  17. WnComponentSerializer,WnComponentCssSerializer,WnComponentKeyModelSerializer,WnComponentCssGroupSerializer
  18. from .utils import display
  19. import ast
  20. # 富文本编辑器
  21. # 富文本编辑器文件上传
  22. class EditorImgView(APIView):
  23. def post(self,request):
  24. params = request.data
  25. img = params.get('img')
  26. if img is None:
  27. return Response('参数不全', status=status.HTTP_304_NOT_MODIFIED)
  28. img_url = EditorImg(imgurl = img)
  29. img_url.save()
  30. url = '/static/uploads/'+str(img_url.imgurl)
  31. return Response(url,status=status.HTTP_200_OK)
  32. class EditorContensView(ModelViewSet):
  33. # 1,设置局部认证
  34. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  35. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  36. # 2,设置局部权限
  37. permission_classes = (IsAuthenticated,)
  38. # 1,提供通用的序列化器
  39. serializer_class = WebEditorContensModelSerializer
  40. # 2,提供通用的数据集
  41. queryset = EditorContens.objects.all()
  42. class Bars(APIView):
  43. # 1,设置局部认证
  44. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  45. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  46. # 2,设置局部权限
  47. permission_classes = (IsAuthenticated,)
  48. def get(self,request):
  49. bar_list = Bar.objects.filter(partment_id=None)
  50. bar = display(bar_list)
  51. data = {
  52. 'bar':bar
  53. }
  54. return Response(data,status=status.HTTP_200_OK)
  55. # 前台菜单视图
  56. class NavigationView(ModelViewSet):
  57. # 1,设置局部认证
  58. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  59. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  60. # 2,设置局部权限
  61. permission_classes = (IsAuthenticated,)
  62. # 1,提供通用的序列化器
  63. serializer_class = WebNavigationModelSerializer
  64. # 2,提供通用的数据集
  65. queryset = Navigation.objects.all()
  66. # 公司接口
  67. class CompanyView(ModelViewSet):
  68. # 1,设置局部认证
  69. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  70. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  71. # 2,设置局部权限
  72. permission_classes = (IsAuthenticated,)
  73. # 1,提供通用的序列化器
  74. serializer_class = WebCompanyModelSerializer
  75. # 2,提供通用的数据集
  76. queryset = Company.objects.all()
  77. # 公司接口
  78. class SlideshowView(ModelViewSet):
  79. # 1,设置局部认证
  80. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  81. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  82. # 2,设置局部权限
  83. permission_classes = (IsAuthenticated,)
  84. # 1,提供通用的序列化器
  85. serializer_class = WebSlideshowModelSerializer
  86. filter_backends = (filters.OrderingFilter,) # 固定写法
  87. ordering_fields = ('sort',)
  88. # 2,提供通用的数据集
  89. queryset = Slideshow.objects.all()
  90. def get_serializer_context(self):
  91. """
  92. Extra context provided to the serializer class.
  93. """
  94. return {
  95. 'format': self.format_kwarg,
  96. 'view': self
  97. }
  98. # 关于我们
  99. class HmweView(ModelViewSet):
  100. # 1,设置局部认证
  101. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  102. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  103. # 2,设置局部权限
  104. permission_classes = (IsAuthenticated,)
  105. # 1,提供通用的序列化器
  106. serializer_class = WebHmweModelSerializer
  107. # filter_backends = (filters.OrderingFilter,) # 固定写法
  108. # ordering_fields = ('sort',)
  109. # 2,提供通用的数据集
  110. queryset = Hmwe.objects.all()
  111. # 技术与服务
  112. class HmartView(ModelViewSet):
  113. # 1,设置局部认证
  114. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  115. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  116. # 2,设置局部权限
  117. permission_classes = (IsAuthenticated,)
  118. # 1,提供通用的序列化器
  119. serializer_class = WebHmartModelSerializer
  120. # filter_backends = (filters.OrderingFilter,) # 固定写法
  121. # ordering_fields = ('sort',)
  122. # 2,提供通用的数据集
  123. queryset = Hmart.objects.all()
  124. # 标签描述
  125. class HmColumnView(ModelViewSet):
  126. # 1,设置局部认证
  127. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  128. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  129. # 2,设置局部权限
  130. permission_classes = (IsAuthenticated,)
  131. # 1,提供通用的序列化器
  132. serializer_class = WebColumnModelSerializer
  133. # filter_backends = (filters.OrderingFilter,) # 固定写法
  134. # ordering_fields = ('sort',)
  135. # 2,提供通用的数据集
  136. queryset = Column.objects.all()
  137. # 成功案例
  138. class HmcaseView(ModelViewSet):
  139. # 1,设置局部认证
  140. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  141. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  142. # 2,设置局部权限
  143. permission_classes = (IsAuthenticated,)
  144. # 1,提供通用的序列化器
  145. serializer_class = WebHmcaseModelSerializer
  146. # filter_backends = (filters.OrderingFilter,) # 固定写法
  147. # ordering_fields = ('sort',)
  148. # 2,提供通用的数据集
  149. queryset = Hmcase.objects.all()
  150. # 团队介绍
  151. class HmgroupView(ModelViewSet):
  152. # 1,设置局部认证
  153. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  154. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  155. # 2,设置局部权限
  156. permission_classes = (IsAuthenticated,)
  157. # 1,提供通用的序列化器
  158. serializer_class = WebHmgroupModelSerializer
  159. # filter_backends = (filters.OrderingFilter,) # 固定写法
  160. # ordering_fields = ('sort',)
  161. # 2,提供通用的数据集
  162. queryset = Hmgroup.objects.all()
  163. def get_serializer_context(self):
  164. """
  165. Extra context provided to the serializer class.
  166. """
  167. return {
  168. 'format': self.format_kwarg,
  169. 'view': self
  170. }
  171. # 联系我们
  172. class HmaddressView(ModelViewSet):
  173. # 1,设置局部认证
  174. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  175. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  176. # 2,设置局部权限
  177. permission_classes = (IsAuthenticated,)
  178. # 1,提供通用的序列化器
  179. serializer_class = WebHmaddressModelSerializer
  180. # filter_backends = (filters.OrderingFilter,) # 固定写法
  181. # ordering_fields = ('sort',)
  182. # 2,提供通用的数据集
  183. queryset = Hmaddress.objects.all()
  184. # 全部组件
  185. class WnComponentView(ModelViewSet):
  186. # 1,设置局部认证
  187. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  188. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  189. # 2,设置局部权限
  190. permission_classes = (IsAuthenticated,)
  191. # 1,提供通用的序列化器
  192. serializer_class = WnComponentSerializer
  193. # filter_backends = (filters.OrderingFilter,) # 固定写法
  194. # ordering_fields = ('sort',)
  195. # 2,提供通用的数据集
  196. queryset = WnComponentModel.objects.all()
  197. def get_serializer_context(self):
  198. """
  199. Extra context provided to the serializer class.
  200. """
  201. return {
  202. 'format': self.format_kwarg,
  203. 'view': self
  204. }
  205. # 使用中的组件
  206. class WnComponentKeyView(ModelViewSet):
  207. # 1,设置局部认证
  208. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  209. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  210. # 2,设置局部权限
  211. permission_classes = (IsAuthenticated,)
  212. # 1,提供通用的序列化器
  213. serializer_class = WnComponentKeyModelSerializer
  214. filter_backends = (filters.OrderingFilter,) # 固定写法
  215. filterset_fields = ('id', 'coding','sort')
  216. ordering_fields = ('id', 'coding','sort')
  217. # 2,提供通用的数据集
  218. queryset = WnComponentKeyModel.objects.all()
  219. def create(self, request, *args, **kwargs):
  220. serializer = self.get_serializer(data=request.data)
  221. serializer.is_valid(raise_exception=True)
  222. self.perform_create(serializer)
  223. headers = self.get_success_headers(serializer.data)
  224. # 插入对应的默认的组件样式
  225. styleValue = elmentList.styleValue
  226. styleValue['coding'] = serializer.data['coding']
  227. styleValue['component'] = serializer.data['name']
  228. styleValue['componentkey_id'] = serializer.data['id']
  229. styleValue['create_time'] = serializer.data['create_time']
  230. styleValue['create_user'] = serializer.data['create_user']
  231. WnComponentCssModel.objects.create(**styleValue)
  232. return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
  233. # 组件样式分组
  234. class WnComponentCssGroupView(ModelViewSet):
  235. # 1,设置局部认证
  236. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  237. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  238. # 2,设置局部权限
  239. permission_classes = (IsAuthenticated,)
  240. # 1,提供通用的序列化器
  241. serializer_class = WnComponentCssGroupSerializer
  242. filter_backends = (filters.OrderingFilter,DjangoFilterBackend) # 固定写法
  243. filterset_fields = ('id', 'coding')
  244. ordering_fields = ('id', 'coding')
  245. # 2,提供通用的数据集
  246. queryset = WnComponentCssGroupModel.objects.all()
  247. def get_serializer_context(self):
  248. """
  249. Extra context provided to the serializer class.
  250. """
  251. return {
  252. 'format': self.format_kwarg,
  253. 'view': self
  254. }
  255. import ast
  256. from django.forms.models import model_to_dict
  257. # 使用组件样式
  258. class WnComponentCssView(ModelViewSet):
  259. # 1,设置局部认证
  260. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  261. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  262. # 2,设置局部权限
  263. permission_classes = (IsAuthenticated,)
  264. # 1,提供通用的序列化器
  265. serializer_class = WnComponentCssSerializer
  266. # filter_backends = (filters.OrderingFilter,) # 固定写法
  267. filterset_fields = ('id', 'coding','componentkey_id',)
  268. # ordering_fields = ('sort',)
  269. # 2,提供通用的数据集
  270. queryset = WnComponentCssModel.objects.all()
  271. def functionGet(self, request):
  272. print(33333333333333333333333333)
  273. params = request.query_params.get("componentkey_id")
  274. print(params)
  275. result = WnComponentCssModel.objects.filter(componentkey_id = params)
  276. field_list = elmentList.field_list
  277. if len(result) == 0 :
  278. return Response('没有查询到样式', status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION)
  279. data = model_to_dict(result[0]) # 取第一条记录,转换为字典
  280. new_data = {}
  281. for key,value in data.items():
  282. new_data[key] = value
  283. if key in field_list:
  284. data[key] = ast.literal_eval(value)
  285. return Response(data, status=status.HTTP_200_OK)
  286. def update(self, request, *args, **kwargs):
  287. print(111111)
  288. data = {}
  289. for key,value in request.data.items():
  290. data[key] = str(value)
  291. partial = kwargs.pop('partial', False)
  292. instance = self.get_object()
  293. serializer = self.get_serializer(instance, data=data, partial=partial)
  294. serializer.is_valid(raise_exception=True)
  295. self.perform_update(serializer)
  296. if getattr(instance, '_prefetched_objects_cache', None):
  297. # If 'prefetch_related' has been applied to a queryset, we need to
  298. # forcibly invalidate the prefetch cache on the instance.
  299. instance._prefetched_objects_cache = {}
  300. return Response(serializer.data)