from django.http import JsonResponse from django.db.models import Q from rest_framework import viewsets, permissions, status, filters from rest_framework.filters import OrderingFilter from update.models import Cinema from update.serializers import * from rest_framework.views import APIView from rest_framework.response import Response from rest_framework_extensions.cache.mixins import CacheResponseMixin from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_page from django_filters.rest_framework import DjangoFilterBackend from django.core.cache import cache from update.utils.get_version import GetVersion from rest_framework.decorators import action from update.utils.git_util import GitUtil from update.utils.cmd_extcute import UpdateCommandUtil from update.utils.db_compare import DbCompare from update.utils.client_util import ClientUtil # CacheResponseMixin 一定要放第一位 class CinemaViewSet(CacheResponseMixin, viewsets.ModelViewSet): # 接口文档的中文注释 """ create: 添加测试影院 list: 获取测试影院列表 retrieve: 获取某个影院的信息 update: 更新某个影院的信息 delete: 删除指定影院 """ queryset = Cinema.objects.all() serializer_class = CinemaSerializer permission_classes = (permissions.IsAuthenticated,) filter_backends = (filters.SearchFilter, DjangoFilterBackend, OrderingFilter) ordering_fields = ('ip',) search_fields = ('sys_ver',) # filter_fields = ('ip',) filterset_fields = ('ip',) GetVersion().main_process() @action(methods=['get'], detail=False) @method_decorator(cache_page(60 * 1)) def refresh(self, request, *args, **kwargs): GetVersion().main_process() queryset = Cinema.objects.all().order_by('ip') serializer = self.get_serializer(instance=queryset, many=True) return Response(serializer.data) class CinemaUserViewSet(CacheResponseMixin, viewsets.ModelViewSet): queryset = CinemaUser.objects.order_by('pinyin').all() serializer_class = CinemaUserSerializer # class CinemaSearchAPIView(APIView, CacheResponseMixin): # @method_decorator(cache_page(60 * 5)) # def get(self, request, *args, **kwargs): # query_params = request.query_params.dict() # print(query_params) # query_data = Cinema.objects.filter( # Q(ip__contains=query_params.get('ip')) & Q(sys_ver__icontains=query_params.get('version'))) # serializer = CinemaSerializer(instance=query_data, many=True) # return Response(serializer.data, status=status.HTTP_200_OK) def get_git_version(request): git_util = GitUtil() git_ver = git_util.get_short_version() serializer = ReleaseSerializer(instance=git_ver, many=True) return JsonResponse(serializer.data, safe=False) def get_operation_cmd(request): update_cmd = UpdateCommandUtil() no_sys_cmd = update_cmd.get_no_sys_cmd() serializer = UpdateCommandSerializer(instance=no_sys_cmd, many=True) return JsonResponse(serializer.data, safe=False) # 测试用接口 def write_git_version_to_db(request): git_util = GitUtil('') git_util.set_release_to_db() return JsonResponse({'result': 'success'}) def write_cine_sql(request): git_util = GitUtil('2.0.33.0338_Release') git_util.handle_create_cine() return JsonResponse({'result': 'success'}) def compare_db_structor(request): db_compare = DbCompare('172.16.3.88', '2.0.33.0338_Release') db_compare.exec_diff_sql() return JsonResponse({'result': 'success'}) def get_cmd(request): update_cmd = UpdateCommandUtil() update_cmd.connect('172.16.3.112') update_cmd.disconnect() # cmd = update_cmd.get_all_cmd() # cmd_ser = UpdateCommandSerializer(instance=cmd, many=True) # return JsonResponse(data=cmd_ser.data, safe=False) cmd_list = [10, 12, 13, 14, 15, 17, 19, 20] r = update_cmd.get_checked_cmd(cmd_list) print(r) return JsonResponse({'result': 'success'}) def setup_cmd(request): update_cmd = UpdateCommandUtil() cmd_list = [10, 11, 12, 13, 14, 15, 16, 17, 19, 20] # exec_cmd_data = update_cmd.get_checked_cmd(cmd_list) update_cmd.exec_cmd_by_type('172.16.3.88', 'setup', cmd_list, '2.0.33.0338_Release') return JsonResponse({'result': 'success'}) def teardown_cmd(request): update_cmd = UpdateCommandUtil() cmd_list = [10, 11, 12, 13, 14, 15, 16, 17, 19, 20] # exec_cmd_data = update_cmd.get_checked_cmd(cmd_list) update_cmd.exec_cmd_by_type('172.16.3.88', 'teardown', cmd_list, '2.0.33.0338_Release') return JsonResponse({'result': 'success'}) def get_client(request): client_release = ClientUtil() client_release.client_process('172.16.3.88', '2.0.33.0338_Release') return JsonResponse({'result': 'success'}) def update_cine(request): req = request.GET.dict() cinema_ip = req.get('cinema_ip') short_release = req.get('short_release') print(cinema_ip, short_release) # cinema_ip = '172.16.3.88' # short_release = '2.0.33.0338_Release' cmd_list = [10, 11, 12, 13, 14, 15, 16, 17, 19, 20] # 获取并写入cine.sql git_util = GitUtil(short_release) git_util.handle_create_cine() # 数据库对比 db_compare = DbCompare(cinema_ip, short_release) db_compare.exec_diff_sql() # 执行setup update_cmd = UpdateCommandUtil() update_cmd.exec_cmd_by_type(cinema_ip, 'setup', cmd_list, short_release) # 执行teardown update_cmd.exec_cmd_by_type(cinema_ip, 'teardown', cmd_list, short_release) # 传输客户端 client_release = ClientUtil() client_release.client_process(cinema_ip, short_release) return JsonResponse({'result': 'success'})