123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- 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)
|