|
| 1 | +# coding=utf-8 |
| 2 | +""" |
| 3 | + @project: MaxKB |
| 4 | + @Author:虎虎 |
| 5 | + @file: application_access_token.py |
| 6 | + @date:2025/6/9 17:49 |
| 7 | + @desc: |
| 8 | +""" |
| 9 | +import hashlib |
| 10 | +import uuid |
| 11 | + |
| 12 | +from django.core.cache import cache |
| 13 | +from django.db.models import QuerySet |
| 14 | +from django.utils.translation import gettext_lazy as _ |
| 15 | +from rest_framework import serializers |
| 16 | + |
| 17 | +from application.models import ApplicationAccessToken |
| 18 | +from common.constants.cache_version import Cache_Version |
| 19 | +from common.database_model_manage.database_model_manage import DatabaseModelManage |
| 20 | + |
| 21 | + |
| 22 | +class AccessTokenEditSerializer(serializers.Serializer): |
| 23 | + access_token_reset = serializers.BooleanField(required=False, |
| 24 | + label=_("Reset Token")) |
| 25 | + is_active = serializers.BooleanField(required=False, label=_("Is it enabled")) |
| 26 | + access_num = serializers.IntegerField(required=False, max_value=10000, |
| 27 | + min_value=0, |
| 28 | + label=_("Number of visits")) |
| 29 | + white_active = serializers.BooleanField(required=False, |
| 30 | + label=_("Whether to enable whitelist")) |
| 31 | + white_list = serializers.ListSerializer(required=False, child=serializers.CharField(required=True, |
| 32 | + label=_("Whitelist")), |
| 33 | + label=_("Whitelist")), |
| 34 | + show_source = serializers.BooleanField(required=False, |
| 35 | + label=_("Whether to display knowledge sources")) |
| 36 | + language = serializers.CharField(required=False, allow_blank=True, allow_null=True, |
| 37 | + label=_("language")) |
| 38 | + authentication = serializers.BooleanField(default=False, label="Do you need authentication") |
| 39 | + |
| 40 | + authentication_value = serializers.JSONField(required=False, label="Certified value", default=dict) |
| 41 | + |
| 42 | + |
| 43 | +class AccessTokenSerializer(serializers.Serializer): |
| 44 | + application_id = serializers.UUIDField(required=True, label=_("Application ID")) |
| 45 | + |
| 46 | + def edit(self, instance): |
| 47 | + self.is_valid(raise_exception=True) |
| 48 | + AccessTokenEditSerializer(data=instance).is_valid(raise_exception=True) |
| 49 | + application_access_token = QuerySet(ApplicationAccessToken).get( |
| 50 | + application_id=self.data.get('application_id')) |
| 51 | + if 'is_active' in instance: |
| 52 | + application_access_token.is_active = instance.get("is_active") |
| 53 | + if 'access_token_reset' in instance and instance.get('access_token_reset'): |
| 54 | + application_access_token.access_token = hashlib.md5(str(uuid.uuid1()).encode()).hexdigest()[8:24] |
| 55 | + if 'access_num' in instance and instance.get('access_num') is not None: |
| 56 | + application_access_token.access_num = instance.get("access_num") |
| 57 | + if 'white_active' in instance and instance.get('white_active') is not None: |
| 58 | + application_access_token.white_active = instance.get("white_active") |
| 59 | + if 'white_list' in instance and instance.get('white_list') is not None: |
| 60 | + application_access_token.white_list = instance.get('white_list') |
| 61 | + if 'show_source' in instance and instance.get('show_source') is not None: |
| 62 | + application_access_token.show_source = instance.get('show_source') |
| 63 | + if 'language' in instance and instance.get('language') is not None: |
| 64 | + application_access_token.language = instance.get('language') |
| 65 | + if 'language' not in instance or instance.get('language') is None: |
| 66 | + application_access_token.language = None |
| 67 | + application_access_token.save() |
| 68 | + application_setting_model = DatabaseModelManage.get_model('application_setting') |
| 69 | + license_is_valid = cache.get(Cache_Version.SYSTEM.get_key(key='license_is_valid'), |
| 70 | + version=Cache_Version.SYSTEM.get_version()) |
| 71 | + if application_setting_model is not None and license_is_valid: |
| 72 | + application_setting, _ = application_setting_model.objects.get_or_create( |
| 73 | + application_id=self.data.get('application_id')) |
| 74 | + if application_setting is not None and instance.get('authentication') is not None and instance.get( |
| 75 | + 'authentication_value') is not None: |
| 76 | + application_setting.authentication = instance.get('authentication') |
| 77 | + application_setting.authentication_value = instance.get('authentication_value') |
| 78 | + application_setting.save() |
| 79 | + return self.one(with_valid=False) |
| 80 | + |
| 81 | + def one(self, with_valid=True): |
| 82 | + if with_valid: |
| 83 | + self.is_valid(raise_exception=True) |
| 84 | + application_id = self.data.get("application_id") |
| 85 | + application_access_token = QuerySet(ApplicationAccessToken).filter( |
| 86 | + application_id=application_id).first() |
| 87 | + if application_access_token is None: |
| 88 | + application_access_token = ApplicationAccessToken(application_id=application_id, |
| 89 | + access_token=hashlib.md5( |
| 90 | + str(uuid.uuid1()).encode()).hexdigest()[ |
| 91 | + 8:24], is_active=True) |
| 92 | + application_access_token.save() |
| 93 | + application_setting_model = DatabaseModelManage.get_model('application_setting') |
| 94 | + other = {} |
| 95 | + if application_setting_model is not None: |
| 96 | + application_setting, _ = application_setting_model.objects.get_or_create( |
| 97 | + application_id=self.data.get('application_id')) |
| 98 | + if application_setting is not None: |
| 99 | + other = {'authentication': application_setting.authentication, |
| 100 | + 'authentication_value': application_setting.authentication_value} |
| 101 | + |
| 102 | + return {'application_id': application_access_token.application_id, |
| 103 | + 'access_token': application_access_token.access_token, |
| 104 | + "is_active": application_access_token.is_active, |
| 105 | + 'access_num': application_access_token.access_num, |
| 106 | + 'white_active': application_access_token.white_active, |
| 107 | + 'white_list': application_access_token.white_list, |
| 108 | + 'show_source': application_access_token.show_source, |
| 109 | + 'language': application_access_token.language, |
| 110 | + **other, |
| 111 | + } |
0 commit comments