X Tutup
Skip to content

fix: [Application] The application node uses {{}} to reference variables, resulting in a parsing failure.#4858

Merged
shaohuzhang1 merged 1 commit intov1from
pr@v1@fix_application_template
Mar 10, 2026
Merged

fix: [Application] The application node uses {{}} to reference variables, resulting in a parsing failure.#4858
shaohuzhang1 merged 1 commit intov1from
pr@v1@fix_application_template

Conversation

@shaohuzhang1
Copy link
Contributor

fix: [Application] The application node uses {{}} to reference variables, resulting in a parsing failure.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Mar 10, 2026

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Mar 10, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment



def run():
DEFAULT_FORMATTER_MAPPING['jinja2'] = jinja2_formatter
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The provided Python script seems to define a custom Jinja2 formatter for formatting strings using the SandboxedEnvironment. This environment has been configured to block all attribute/method access except for simple variable lookups, which is generally good security practice.

Key Points:

  1. Import Statements: Ensures that necessary libraries are imported at the beginning (import jinja2, from langchain_core.prompts.string import DEFAULT_FORMATTER_MAPPING).

  2. Function Definition:

    • The function jinja2_formatter takes a template string and optional keyword arguments (**kwargs).
    • It checks if Jinja2 is installed; otherwise, it raises an error, advising against using untrusted templates.
    • A SandboxedEnvironment is used with restrictions on attribute accesses beyond simple variable lookup, preventing code execution through templates.
  3. Main Function:

    • In the main section, it attempts to add the custom formatter jinja2_formatter to the DEFAULT_FORMATTER_MAPPING, which would typically allow other parts of your program to use this formatter seamlessly.

Overall, the code appears functional and secure given its sandboxing mechanisms. However, it might be worth noting:

  • Ensure you handle cases where Jinja2 might not be available during development.
  • Make sure that any untrusted inputs passed to templates are sanitized to prevent malicious code injection attacks.


handler404 = page_not_found
init_doc(urlpatterns, application_urlpatterns)
init_template.run()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The provided Python code looks mostly clean and well-structured for setting up a basic Django project. However, there are a few minor improvements that can be made:

  1. Remove duplicate imports: The Result class is imported twice at the beginning of the file. You can remove one occurrence.

  2. Consistent formatting: Ensure consistent indentation throughout the file. While it's correct already, you can adjust any missing indentations to match other lines.

  3. Avoid unnecessary space between functions: There is an extra space after importing smartdoc.

  4. Use absolute imports if possible: While not strictly necessary, using absolute imports (import xxx) is considered best practice compared to relative ones.

  5. Consider adding error handling in page_not_found function: This will improve robustness by logging exceptions or providing meaningful error messages.

Here’s the updated version with these considerations:

# Removed duplication
from application.urls import urlpatterns as application_urlpatterns
from common.cache_data.static_resource_cache import get_index_html, get_cache
from common.constants.cache_code_constants import CacheCodeConstants
from common.doc.init import init_doc
from common.response.result import Result
from common.util.cache_util import get_cache

# Used strict paths for better clarity (absolute)
from smartdoc import settings, conf

PROJECT_DIR = getattr(conf, 'PROJECT_DIR')  # Assuming conf has a PROJECT_DIR attribute

def page_not_found(request, exception):
    # Consider adding try-except block for error handling
    return "Error 404"

handler404 = page_not_found
init_doc(urlpatterns, application_urlpatterns)
init_template.run()

Additional Suggestions

  • If you have multiple files with similar imports, consider creating an __init__.py file in a directory containing those files to manage imports centrally.
  • If smartdoc.settings does not use attributes directly from conf, ensure that the configuration loading logic is correct.
  • Review the usage of get_project_dir() if it refers to something different than what I assumed based on the comments.

These adjustments should make the code slightly more readable, maintainable, and robust.

init_template.run()


def delete_files(directory):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The provided code looks generally well-written, but here are some suggested improvements for clarity, efficiency, and readability:

  1. Docstring Update: Add a docstring to describe the purpose of on_app_ready function.

  2. Import Order Consistency: Ensure consistent import order. Typically, first go with third-party imports like logging, then standard library imports (os), and lastly custom imports (subtask, worker_ready, etc.).

  3. Code Block Formatting: While clean, consider adding spacing around operators inside expressions when they're used within parentheses, e.g., (x > y) rather than ( x>y).

  4. Comments and Naming conventions: Consider improving comments to explain complex logic or important sections. Also ensure that variable names follow PEP 8 guidelines (snake_case).

  5. Optional Imports: If init_template might not be always needed, consider checking if it's available before calling run().

Here's an updated version incorporating these suggestions:

#
import os

from .common import init_template
from celery import subtask

# Function documentation explaining its purpose
def on_app_ready():
    """
    Handles tasks based on environment variables and schedules periodic tasks.
    
    This function goes through all scheduled tasks defined in environment variables,
    enables them using celery, and optionally initializes template-related components.
    """
    tasks = os.environ.get('SCHEDULED_TASKS', '').split(',')
    
    # Iterate over each enabled task
    for task in tasks:
        if task in ['TASK_A', 'DISABLED_TASK']:
            logger.debug(f"Periodic task [{task}] is disabled!")
            continue
        
        # Schedule the task using Celery
        subtask(task).delay()

    # Optionally run initialization script
    try:
        init_template.run()
    except ImportError:
        logger.warning("Could not initialize template-related components.")

# Example usage of delete_files function which could also benefit from comments or improved design choices
def delete_files(directory):
    pass

These changes enhance both readability and maintainability while ensuring functionality.

@shaohuzhang1 shaohuzhang1 merged commit e78c764 into v1 Mar 10, 2026
3 of 4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@v1@fix_application_template branch March 10, 2026 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

X Tutup