From 4da64862ae360341930ef22f3b779eac611cac82 Mon Sep 17 00:00:00 2001 From: ydzhang Date: Tue, 27 Dec 2022 17:36:26 +0800 Subject: [PATCH 1/4] upload decode/sysom_decode.py --- .../sysom_hotfix/lib/decode/sysom_decode.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 sysom_server/sysom_hotfix/lib/decode/sysom_decode.py diff --git a/sysom_server/sysom_hotfix/lib/decode/sysom_decode.py b/sysom_server/sysom_hotfix/lib/decode/sysom_decode.py new file mode 100644 index 00000000..12c7c1a0 --- /dev/null +++ b/sysom_server/sysom_hotfix/lib/decode/sysom_decode.py @@ -0,0 +1,17 @@ +import jwt +from django.conf import settings + + +class JWTTokenDecode: + """SYSOM TOken解析认证""" + def decode(self, token): + r, s = None, False + try: + r, s = jwt.decode(token, key=settings.SECRET_KEY, algorithms='HS256'), True + except jwt.exceptions.ExpiredSignatureError as e: + r = f'令牌失效: {e}' + except jwt.exceptions.DecodeError as e: + r = f'令牌校验失败: {e}' + except jwt.exceptions.InvalidAlgorithmError as e: + r = f'令牌不合法: {e}' + return r, s \ No newline at end of file -- Gitee From fdac24f6428195d22dcf6b76a99bfebce259f8f5 Mon Sep 17 00:00:00 2001 From: ydzhang Date: Tue, 27 Dec 2022 17:37:27 +0800 Subject: [PATCH 2/4] upload __init.py of sysom_server lib and the authentications.py --- sysom_server/sysom_hotfix/lib/__init__.py | 8 +++ .../sysom_hotfix/lib/authentications.py | 50 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 sysom_server/sysom_hotfix/lib/__init__.py create mode 100644 sysom_server/sysom_hotfix/lib/authentications.py diff --git a/sysom_server/sysom_hotfix/lib/__init__.py b/sysom_server/sysom_hotfix/lib/__init__.py new file mode 100644 index 00000000..331893ca --- /dev/null +++ b/sysom_server/sysom_hotfix/lib/__init__.py @@ -0,0 +1,8 @@ +# -*- encoding: utf-8 -*- +""" +@File : __init__.py +@Time : 2022/12/27 17:30 +@Author : ydjohn +@Email : ydzhang@linux.alibaba.com +@Software: VSCode +""" \ No newline at end of file diff --git a/sysom_server/sysom_hotfix/lib/authentications.py b/sysom_server/sysom_hotfix/lib/authentications.py new file mode 100644 index 00000000..582821a2 --- /dev/null +++ b/sysom_server/sysom_hotfix/lib/authentications.py @@ -0,0 +1,50 @@ +import logging +import os +from typing import List +from django.conf import settings +from django.utils.translation import ugettext as _ +from rest_framework.exceptions import AuthenticationFailed +from rest_framework.request import Request +from rest_framework.authentication import BaseAuthentication +from .utils import import_module + + +logger = logging.getLogger(__name__) + + +def get_jwt_decode_classes() -> List[BaseAuthentication]: + jwt_decode_classes = [] + import_strings = [ + f'lib.decode.{f.replace(".py", "")}' for f in os.listdir(settings.JWT_TOKEN_DECODE_DIR) + ] + for string in import_strings: + module = import_module(string) + try: + m = getattr(module, 'JWTTokenDecode') + jwt_decode_classes.append(m) + except Exception as exc: + logger.warn(exc) + return jwt_decode_classes + + +def decode_token(token: str) -> dict: + error_message, success, result = "", False, {} + for auth_class in get_jwt_decode_classes(): + result, success = auth_class().decode(token) + if not success: + error_message += result + else: + break + if not success: + raise AuthenticationFailed(error_message) + return result + + +class TaskAuthentication(BaseAuthentication): + def authenticate(self, request: Request): + token = request.META.get('HTTP_AUTHORIZATION') + payload = decode_token(token) + payload['token'] = token + if 'sub' in payload: + payload['id'] = int(payload['sub']) + return payload, _ -- Gitee From 78bd595b3b21ccb92e5bf477c173e7bd23439802 Mon Sep 17 00:00:00 2001 From: ydzhang Date: Tue, 27 Dec 2022 17:38:05 +0800 Subject: [PATCH 3/4] Upload base_model of sysom_hotfix server --- sysom_server/sysom_hotfix/lib/base_model.py | 50 +++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 sysom_server/sysom_hotfix/lib/base_model.py diff --git a/sysom_server/sysom_hotfix/lib/base_model.py b/sysom_server/sysom_hotfix/lib/base_model.py new file mode 100644 index 00000000..582821a2 --- /dev/null +++ b/sysom_server/sysom_hotfix/lib/base_model.py @@ -0,0 +1,50 @@ +import logging +import os +from typing import List +from django.conf import settings +from django.utils.translation import ugettext as _ +from rest_framework.exceptions import AuthenticationFailed +from rest_framework.request import Request +from rest_framework.authentication import BaseAuthentication +from .utils import import_module + + +logger = logging.getLogger(__name__) + + +def get_jwt_decode_classes() -> List[BaseAuthentication]: + jwt_decode_classes = [] + import_strings = [ + f'lib.decode.{f.replace(".py", "")}' for f in os.listdir(settings.JWT_TOKEN_DECODE_DIR) + ] + for string in import_strings: + module = import_module(string) + try: + m = getattr(module, 'JWTTokenDecode') + jwt_decode_classes.append(m) + except Exception as exc: + logger.warn(exc) + return jwt_decode_classes + + +def decode_token(token: str) -> dict: + error_message, success, result = "", False, {} + for auth_class in get_jwt_decode_classes(): + result, success = auth_class().decode(token) + if not success: + error_message += result + else: + break + if not success: + raise AuthenticationFailed(error_message) + return result + + +class TaskAuthentication(BaseAuthentication): + def authenticate(self, request: Request): + token = request.META.get('HTTP_AUTHORIZATION') + payload = decode_token(token) + payload['token'] = token + if 'sub' in payload: + payload['id'] = int(payload['sub']) + return payload, _ -- Gitee From 57a8fcfb6c309ea689b2b78571b4a32a30ada013 Mon Sep 17 00:00:00 2001 From: ydzhang Date: Tue, 27 Dec 2022 17:38:30 +0800 Subject: [PATCH 4/4] uploaded excel.py file --- sysom_server/sysom_hotfix/lib/excel.py | 55 ++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 sysom_server/sysom_hotfix/lib/excel.py diff --git a/sysom_server/sysom_hotfix/lib/excel.py b/sysom_server/sysom_hotfix/lib/excel.py new file mode 100644 index 00000000..96f57e7c --- /dev/null +++ b/sysom_server/sysom_hotfix/lib/excel.py @@ -0,0 +1,55 @@ +import logging +from io import BytesIO +import pandas as pd +from xlwt import Workbook +from django.http import StreamingHttpResponse, HttpResponse + + +logger = logging.getLogger(__name__) + + +class Excel: + def __init__(self, file, row_dict: dict) -> None: + self.file = file + self.row_dict = row_dict + self._file_io = None + self.open() + + def open(self): + if not self._file_io: + self._file_io = pd.read_excel(self.file) + + def values(self): + content = list() + for _, row in self._file_io.iterrows(): + item = {} + for k, v in self.row_dict.items(): + item[k] = row[v] + content.append(item) + return content + + @staticmethod + def export(datalist: list = ..., sheetname: str = 'sheet1', excelname: str = 'host'): + """ + Export excel + Type xlsx + """ + response = HttpResponse(content_type='application/vnd.ms-excel') + response['Content-Disposition'] = 'attachment; filename=%s' % excelname+'.xlsx' + + workbook = Workbook(encoding='utf-8') + sheet = workbook.add_sheet(sheetname) + + for i, k in enumerate([k for k in datalist[0].keys()]): + sheet.write(0, i, k) + + for r, data in enumerate(datalist): + for i, key in enumerate([k for k in data.keys()]): + sheet.write(r+1, i, data[key]) + + io = BytesIO() + workbook.save(io) + io.seek(0) + + response.write(io.getvalue()) + return response -- Gitee