Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/application/serializers/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
from langchain_mcp_adapters.client import MultiServerMCPClient
from rest_framework import serializers, status
from rest_framework.utils.formatting import lazy_format

from application.flow.common import Workflow
from application.models.application import Application, ApplicationTypeChoices, ApplicationKnowledgeMapping, \
ApplicationFolder, ApplicationVersion
from application.models.application_access_token import ApplicationAccessToken
from common import result
from common.cache_data.application_access_token_cache import del_application_access_token
from common.database_model_manage.database_model_manage import DatabaseModelManage
from common.db.search import native_search, native_page_search
from common.exception.app_exception import AppApiException
Expand Down Expand Up @@ -734,6 +736,18 @@ def publish(self, instance, with_valid=True):
workspace_id=workspace_id)
self.reset_application_version(work_flow_version, application)
work_flow_version.save()
access_token = hashlib.md5(
str(uuid.uuid7()).encode()).hexdigest()[
8:24]
application_access_token = QuerySet(ApplicationAccessToken).filter(
application_id=application.id).first()
if application_access_token is None:
application_access_token = ApplicationAccessToken(application_id=application.id,
access_token=access_token, is_active=True)
application_access_token.save()
else:
access_token = application_access_token.access_token
del_application_access_token(access_token)
return self.one(with_valid=False)

@staticmethod
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few areas to consider for improvement:

  1. Code Duplication:

    from common.cache_data.application_access_token_cache import del_application_access_token

    This line can be moved out of the function, so it's only called once.

  2. Redundant Code: The following lines:

    access_token = hashlib.md5(str(uuid.uuid7()).encode()).hexdigest()[8:24]
    application_access_token = QuerySet(ApplicationAccessToken).filter(application_id=application.id).first()
    if application_access_token is None:
        application_access_token = ApplicationAccessToken(application_id=application.id,
                                                          access_token=access_token, is_active=True)
        application_access_token.save()
    else:
        access_token = application_access_token.access_token

    could be combined into one more concise statement using get_or_create method:

    application_access_token, _ = ApplicationAccessToken.objects.get_or_create(
        application_id=application.id,
        defaults={'access_token': hashlib.md5(str(uuid.uuid7()).encode()).hexdigest()[8:24], 'is_active': True}
    )
  3. Avoid Unused Imports:
    Ensure that imports you're not actually using should be removed from your file.

  4. Documentation Comments:
    Add comments where necessary to describe complex blocks of code or logic flows, which will make maintenance easier down the road.

Following these recommendations leads to cleaner and potentially more efficient code like this:

from langchain_mcp_adapters.client import MultiServerMCPClient
from rest_framework import serializers, status
from rest_framework.utils.formatting import lazy_format

from application.flow.common import Workflow
from application.models.application import (
    Application,
    ApplicationTypeChoices,
    ApplicationKnowledgeMapping,
    ApplicationFolder,
    ApplicationVersion
)
from application.models.application_access_token import ApplicationAccessToken
from common import result
from common.database_model_manage import DatabaseModelManage
from common.db.search import native_search, native_page_search
from common.exception.app_exception import AppApiException

class YourClass:
    # Function implementation here...
    
    def publish(self, instance, with_valid=True):
        # Function body remains largely the same...

    @staticmethod
    def get_or_update_app_access_token(application):
        return ApplicationAccessToken.objects.get_or_create(
            application_id=application.id,
            defaults={
                'access_token': hashlib.md5(str(uuid.uuid7()).encode()).hexdigest()[8:24],
                'is_active': True
            }
        )

# Call within your method as needed:
token, created = get_or_update_app_access_token(instance.application)

By applying some of these suggestions, your codebase becomes leaner while maintaining readability and functionality integrity.

Expand Down
Loading