from django.shortcuts import render # Create your views here. # 查询菜单栏 from django_filters.rest_framework import DjangoFilterBackend from rest_framework import status,filters from rest_framework.authentication import SessionAuthentication from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView from rest_framework.viewsets import ModelViewSet from rest_framework_jwt.authentication import JSONWebTokenAuthentication from utils import elmentList from .models import Bar,Navigation,Company,Slideshow,EditorImg,EditorContens,Hmwe,Hmart,Hmcase,Hmgroup,Hmaddress,Column,WnComponentModel,WnComponentCssModel,\ WnComponentKeyModel,WnComponentCssGroupModel from .serializer import WebNavigationModelSerializer,WebCompanyModelSerializer,WebSlideshowModelSerializer,WebEditorContensModelSerializer,\ WebHmweModelSerializer,WebHmartModelSerializer,WebHmcaseModelSerializer,WebHmgroupModelSerializer,WebHmaddressModelSerializer,WebColumnModelSerializer,\ WnComponentSerializer,WnComponentCssSerializer,WnComponentKeyModelSerializer,WnComponentCssGroupSerializer from .utils import display import ast # 富文本编辑器 # 富文本编辑器文件上传 class EditorImgView(APIView): def post(self,request): params = request.data img = params.get('img') if img is None: return Response('参数不全', status=status.HTTP_304_NOT_MODIFIED) img_url = EditorImg(imgurl = img) img_url.save() url = '/static/uploads/'+str(img_url.imgurl) return Response(url,status=status.HTTP_200_OK) class EditorContensView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WebEditorContensModelSerializer # 2,提供通用的数据集 queryset = EditorContens.objects.all() class Bars(APIView): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) def get(self,request): bar_list = Bar.objects.filter(partment_id=None) bar = display(bar_list) data = { 'bar':bar } return Response(data,status=status.HTTP_200_OK) # 前台菜单视图 class NavigationView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WebNavigationModelSerializer # 2,提供通用的数据集 queryset = Navigation.objects.all() # 公司接口 class CompanyView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WebCompanyModelSerializer # 2,提供通用的数据集 queryset = Company.objects.all() # 公司接口 class SlideshowView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WebSlideshowModelSerializer filter_backends = (filters.OrderingFilter,) # 固定写法 ordering_fields = ('sort',) # 2,提供通用的数据集 queryset = Slideshow.objects.all() def get_serializer_context(self): """ Extra context provided to the serializer class. """ return { 'format': self.format_kwarg, 'view': self } # 关于我们 class HmweView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WebHmweModelSerializer # filter_backends = (filters.OrderingFilter,) # 固定写法 # ordering_fields = ('sort',) # 2,提供通用的数据集 queryset = Hmwe.objects.all() # 技术与服务 class HmartView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WebHmartModelSerializer # filter_backends = (filters.OrderingFilter,) # 固定写法 # ordering_fields = ('sort',) # 2,提供通用的数据集 queryset = Hmart.objects.all() # 标签描述 class HmColumnView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WebColumnModelSerializer # filter_backends = (filters.OrderingFilter,) # 固定写法 # ordering_fields = ('sort',) # 2,提供通用的数据集 queryset = Column.objects.all() # 成功案例 class HmcaseView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WebHmcaseModelSerializer # filter_backends = (filters.OrderingFilter,) # 固定写法 # ordering_fields = ('sort',) # 2,提供通用的数据集 queryset = Hmcase.objects.all() # 团队介绍 class HmgroupView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WebHmgroupModelSerializer # filter_backends = (filters.OrderingFilter,) # 固定写法 # ordering_fields = ('sort',) # 2,提供通用的数据集 queryset = Hmgroup.objects.all() def get_serializer_context(self): """ Extra context provided to the serializer class. """ return { 'format': self.format_kwarg, 'view': self } # 联系我们 class HmaddressView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WebHmaddressModelSerializer # filter_backends = (filters.OrderingFilter,) # 固定写法 # ordering_fields = ('sort',) # 2,提供通用的数据集 queryset = Hmaddress.objects.all() # 全部组件 class WnComponentView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WnComponentSerializer # filter_backends = (filters.OrderingFilter,) # 固定写法 # ordering_fields = ('sort',) # 2,提供通用的数据集 queryset = WnComponentModel.objects.all() def get_serializer_context(self): """ Extra context provided to the serializer class. """ return { 'format': self.format_kwarg, 'view': self } # 使用中的组件 class WnComponentKeyView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WnComponentKeyModelSerializer filter_backends = (filters.OrderingFilter,) # 固定写法 filterset_fields = ('id', 'coding','sort') ordering_fields = ('id', 'coding','sort') # 2,提供通用的数据集 queryset = WnComponentKeyModel.objects.all() def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) # 插入对应的默认的组件样式 styleValue = elmentList.styleValue styleValue['coding'] = serializer.data['coding'] styleValue['component'] = serializer.data['name'] styleValue['componentkey_id'] = serializer.data['id'] styleValue['create_time'] = serializer.data['create_time'] styleValue['create_user'] = serializer.data['create_user'] WnComponentCssModel.objects.create(**styleValue) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) # 组件样式分组 class WnComponentCssGroupView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WnComponentCssGroupSerializer filter_backends = (filters.OrderingFilter,DjangoFilterBackend) # 固定写法 filterset_fields = ('id', 'coding') ordering_fields = ('id', 'coding') # 2,提供通用的数据集 queryset = WnComponentCssGroupModel.objects.all() def get_serializer_context(self): """ Extra context provided to the serializer class. """ return { 'format': self.format_kwarg, 'view': self } import ast from django.forms.models import model_to_dict # 使用组件样式 class WnComponentCssView(ModelViewSet): # 1,设置局部认证 authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,) # authentication_classes = [SessionAuthentication, BasicAuthentication] # 2,设置局部权限 permission_classes = (IsAuthenticated,) # 1,提供通用的序列化器 serializer_class = WnComponentCssSerializer # filter_backends = (filters.OrderingFilter,) # 固定写法 filterset_fields = ('id', 'coding','componentkey_id',) # ordering_fields = ('sort',) # 2,提供通用的数据集 queryset = WnComponentCssModel.objects.all() def functionGet(self, request): print(33333333333333333333333333) params = request.query_params.get("componentkey_id") print(params) result = WnComponentCssModel.objects.filter(componentkey_id = params) field_list = elmentList.field_list if len(result) == 0 : return Response('没有查询到样式', status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION) data = model_to_dict(result[0]) # 取第一条记录,转换为字典 new_data = {} for key,value in data.items(): new_data[key] = value if key in field_list: data[key] = ast.literal_eval(value) return Response(data, status=status.HTTP_200_OK) def update(self, request, *args, **kwargs): print(111111) data = {} for key,value in request.data.items(): data[key] = str(value) partial = kwargs.pop('partial', False) instance = self.get_object() serializer = self.get_serializer(instance, data=data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): # If 'prefetch_related' has been applied to a queryset, we need to # forcibly invalidate the prefetch cache on the instance. instance._prefetched_objects_cache = {} return Response(serializer.data)