-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: system profile #3193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: system profile #3193
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # coding=utf-8 | ||
| """ | ||
| @project: MaxKB | ||
| @Author:虎虎 | ||
| @file: system_setting.py | ||
| @date:2025/6/4 16:34 | ||
| @desc: | ||
| """ | ||
| from common.mixins.api_mixin import APIMixin | ||
| from common.result import ResultSerializer | ||
| from system_manage.serializers.system import SystemProfileResponseSerializer | ||
|
|
||
|
|
||
| class SystemProfileResult(ResultSerializer): | ||
| def get_data(self): | ||
| return SystemProfileResponseSerializer() | ||
|
|
||
|
|
||
| class SystemProfileAPI(APIMixin): | ||
| @staticmethod | ||
| def get_response(): | ||
| return SystemProfileResult | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # coding=utf-8 | ||
| """ | ||
| @project: MaxKB | ||
| @Author:虎虎 | ||
| @file: system.py | ||
| @date:2025/6/4 16:01 | ||
| @desc: | ||
| """ | ||
| import os | ||
|
|
||
| from django.db import models | ||
| from rest_framework import serializers | ||
| from django.core.cache import cache | ||
|
|
||
| from common.constants.cache_version import Cache_Version | ||
| from maxkb import settings | ||
|
|
||
|
|
||
| class SettingType(models.CharField): | ||
| # Community Edition | ||
| CE = "CE", "社区" | ||
| # Enterprise Edition | ||
| PE = "PE", "专业版" | ||
| # Professional Edition | ||
| EE = "EE", '企业版' | ||
|
|
||
|
|
||
| class SystemProfileResponseSerializer(serializers.Serializer): | ||
| version = serializers.CharField(required=True, label="version") | ||
| edition = serializers.CharField(required=True, label="edition") | ||
| license_is_valid = serializers.BooleanField(required=True, label="License is valid") | ||
|
|
||
|
|
||
| class SystemProfileSerializer(serializers.Serializer): | ||
| @staticmethod | ||
| def profile(): | ||
| version = os.environ.get('MAXKB_VERSION') | ||
| license_is_valid = cache.get(Cache_Version.SYSTEM.get_key(key='license_is_valid'), | ||
| version=Cache_Version.SYSTEM.get_version()) | ||
| return {'version': version, 'edition': settings.edition, | ||
| 'license_is_valid': license_is_valid if license_is_valid is not None else False} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code looks well-formed and appears to be correctly structured for use with Django Rest Framework in a MaxKB project. There are no critical issues detected, but there are some optimizations that could be considered:
Here's the optimized version of the provided function and class definitions with suggested improvements: import os
from django.db import models
from rest_framework import serializers
from django.core.cache import cache
from common.constants.cache_version import Cache_Version
from maxkb import settings
class SettingType(models.CharField):
# Community Edition
CE = "CE", "社区"
# Enterprise Edition
PE = "PE", "专业版"
# Professional Edition
EE = "EE", '企业版'
CACHED_LICENSE_VALID_KEY = f"system_license_validation_{settings.CACHE_PREFIX}"
def system_profile():
"""Retrieve and prepare the System Profile."""
version = os.getenv('MAXKB_VERSION', '') # Default empty string if MAXKB_VERSION isn't set
license_is_valid_cached = cache.get(CACHED_LICENSE_VALID_KEY)
if license_is_valid_cached is None:
licensee_key = Cache_Version.SYSTEM.get_key('license_is_valid')
licensed_value = settings.get(licensee_key).get("default") # Assuming settings have a getter method
cache.set(CACHED_LICENSE_VALID_KEY, licensed_value, version=Cache_Version.SYSTEM.get_version())
return {
'version': version,
'edition': settings.edition,
'license_is_valid': True if (license_is_valid_cached) else False
}
class SystemProfileSerializer(serializers.Serializer):
versionserializer = serializers.CharField(label="version")
editionserializer = serializers.CharField(label="edition")
licenseserializer = serializers.BooleanField(label="License is valid")
@staticmethod
def profile():
"""
Retrieve and format the System Profile data.
This method uses caching to minimize repeated database queries
and improves performance.
"""
return system_profile()Key Changes Made:
These changes make the code both efficient and maintainable while adhering to best practices for Python development and using Django and REST framework effectively. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,4 @@ | |
| """ | ||
| from .user_resource_permission import * | ||
| from .email_setting import * | ||
| from .system_profile import * | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # coding=utf-8 | ||
| """ | ||
| @project: MaxKB | ||
| @Author:虎虎 | ||
| @file: system_profile.py | ||
| @date:2025/6/4 15:59 | ||
| @desc: | ||
| """ | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from drf_spectacular.utils import extend_schema | ||
| from rest_framework.request import Request | ||
| from rest_framework.views import APIView | ||
|
|
||
| from common import result | ||
| from system_manage.api.system import SystemProfileAPI | ||
| from system_manage.serializers.system import SystemProfileSerializer | ||
|
|
||
|
|
||
| class SystemProfile(APIView): | ||
| @extend_schema( | ||
| methods=['GET'], | ||
| description=_('Get MaxKB related information'), | ||
| operation_id=_('Get MaxKB related information'), # type: ignore | ||
| responses=SystemProfileAPI.get_response(), | ||
| tags=[_('System parameters')] # type: ignore | ||
| ) | ||
| def get(self, request: Request): | ||
| return result.success(SystemProfileSerializer.profile()) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's a review of your code snippet for potential issues and suggestions: Potential Issues:
Optimization Suggestions:
In general, your initial code looks well-structured and follows best practices for DRFAPIView classes and Swagger/OpenAPI extensions. Just make sure all configuration matches your specific project settings and requirements. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your provided code is well formatted and follows Python best practices. Here's an assessment with suggestions:
Irregularities and Issues:
# coding=utf-8) is consistent across all*.pyfiles to prevent encoding mismatches when running scripts.Optimization Suggestions:
SystemProfileResult). Consider using a consistent naming convention throughout the file for clarity.Here's the optimized version of your script with these improvements made:
Additional Recommendations:
This should address any minor issues while keeping your code clean and functional.