package.py 4.04 KB
Newer Older
1 2 3
'''
Author: your name
Date: 2021-07-15 09:33:39
4
LastEditTime: 2021-07-27 16:41:32
5 6 7 8
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \evm-store\tools\build_out\views\package.py
'''
9 10 11
#!/usr/bin/env python
# -*- coding: utf_8 -*-

12 13 14
import os
import json
import traceback
wanli's avatar
wanli committed
15 16 17 18 19
from flask import current_app, jsonify, request
from flask_restful import Resource
from flask_restful.reqparse import RequestParser
from flask_jwt_extended import ( jwt_required, get_jwt_identity )
from application.signal_manager import signalManager
wanli's avatar
wanli committed
20
from models.package import  getListPackageSchema, getListPackagesSchema, getPackageSchema
wanli's avatar
wanli committed
21
from webcreator.log import logger
wanli's avatar
wanli committed
22 23
from webcreator.response import ResponseCode, response_result

wanli's avatar
wanli committed
24
class PackageResourceList(Resource):
wanli's avatar
wanli committed
25 26
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
27
        self.parser = RequestParser()
wanli's avatar
wanli committed
28

29
    @jwt_required(locations=["headers"])
wanli's avatar
wanli committed
30 31
    def get(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
32 33 34 35
        self.parser.add_argument("app", type=str, location="args", nullable=True, required=False)
        self.parser.add_argument("page", type=int, location="args", default=1)
        self.parser.add_argument("pageSize", type=int, location="args", default=15)
        args = self.parser.parse_args()
wanli's avatar
wanli committed
36 37

        try:
38 39 40 41 42 43
            jwt = get_jwt_identity()
            data = dict()
            for key, value in args.items():
                if value != None:
                    data[key] = value

44
            result, message = signalManager.actionGetlistPackage.emit(data, jwt)
45
            if result:
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
                # json_dumps = getListPackagesSchema.dump(result.items)
                replace_key = '******'
                response = []
                for p, a in result.items:
                    t = p.to_dict()
                    if p.geo_location != "" and isinstance(p.geo_location, str):
                        t['geo_location'] = json.loads(p.geo_location)
                    if p.remarks != "" and isinstance(p.remarks, str):
                        t['remarks'] = json.loads(p.remarks)
                        t['remarks']['appDir'] = replace_key + t['remarks']['appDir'][len(replace_key):]
                    t['app_name'] = a.app_name
                    t['file_dir'] = os.path.dirname(p.file_path)
                    t['application'] = a.to_dict()
                    t['package_info'] = json.loads(p.package_info)
                    t['package_info']['epkfile'] = replace_key + t['package_info']['epkfile'][len(replace_key):]
                    response.append(t)
                return response_result(message, data=response, total=result.total, pageSize=args.pageSize)
63
            return response_result(message)
wanli's avatar
wanli committed
64
        except Exception as e:
65
            traceback.print_exc()
wanli's avatar
wanli committed
66
            current_app.logger.error(e)
67
            return response_result(ResponseCode.HTTP_SERVER_ERROR)
wanli's avatar
wanli committed
68

wanli's avatar
wanli committed
69
class PackageResource(Resource):
wanli's avatar
wanli committed
70 71
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
72
        self.parser = RequestParser()
wanli's avatar
wanli committed
73

74
    @jwt_required(locations=["headers"])
wanli's avatar
wanli committed
75
    def get(self, uuid):
wanli's avatar
wanli committed
76 77 78 79 80 81
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        # self.parser.add_argument("page", type=int, location="args", default=1)
        # self.parser.add_argument("pageSize", type=int, location="args", default=15)
        # args = self.parser.parse_args()

        try:
82
            jwt = get_jwt_identity()
wanli's avatar
wanli committed
83
            json_payload = request.json
wanli's avatar
wanli committed
84
            print("========>", uuid, json_payload)
85
            result, message = signalManager.actionGetPackage.emit(uuid, jwt)
86 87 88 89
            if result:
                json_dumps = getPackageSchema.dump(result)
                return response_result(message, data=json_dumps)
            return response_result(message)
wanli's avatar
wanli committed
90
        except Exception as e:
91
            traceback.print_exc()
wanli's avatar
wanli committed
92
            current_app.logger.error(e)
93
            return response_result(ResponseCode.HTTP_SERVER_ERROR)