Files
RuoYi-Vue3-FastAPI/ruoyi-fastapi-backend/module_generator/templates/python/controller.py.jinja2
T

160 lines
7.7 KiB
Django/Jinja
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{% set pkField = pkColumn.python_field %}
{% set pk_field = pkColumn.python_field | camel_to_snake %}
{% set pkParentheseIndex = pkColumn.column_comment.find("") %}
{% set pk_field_comment = pkColumn.column_comment[:pkParentheseIndex] if pkParentheseIndex != -1 else pkColumn.column_comment %}
{% set need_import_datetime = namespace(has_datetime=False) %}
{% for column in columns %}
{% if column.python_field in ["createTime", "updatetime"] %}
{% set need_import_datetime.has_datetime = True %}
{% endif %}
{% endfor %}
{% if need_import_datetime.has_datetime %}
from datetime import datetime
{% endif %}
from typing import Annotated
from fastapi import APIRouter, Form, Path, Query, Request, Response
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from common.annotation.log_annotation import Log
from common.aspect.db_seesion import DBSessionDependency
from common.aspect.interface_auth import UserInterfaceAuthDependency
from common.aspect.pre_auth import CurrentUserDependency, PreAuthDependency
from common.enums import BusinessType
from module_admin.entity.vo.user_vo import CurrentUserModel
from {{ packageName }}.service.{{ businessName }}_service import {{ BusinessName }}Service
from {{ packageName }}.entity.vo.{{ businessName }}_vo import Delete{{ BusinessName }}Model, {{ BusinessName }}Model, {{ BusinessName }}PageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil
{{ businessName }}_controller = APIRouter(prefix='/{{ moduleName }}/{{ businessName }}', dependencies=[PreAuthDependency()])
@{{ businessName }}_controller.get(
'/list',
response_model=PageResponseModel,
dependencies=[UserInterfaceAuthDependency('{{ permissionPrefix }}:list')],
)
async def get_{{ moduleName }}_{{ businessName }}_list(
request: Request,
{% if table.crud or table.sub %}{{ businessName }}_page_query{% elif table.tree %}{{ businessName }}_query{% endif %}: Annotated[{{ BusinessName }}PageQueryModel, Query()],
query_db: Annotated[AsyncSession, DBSessionDependency()],
) -> Response:
{% if table.crud or table.sub %}
# 获取分页数据
{{ businessName }}_page_query_result = await {{ BusinessName }}Service.get_{{ businessName }}_list_services(query_db, {{ businessName }}_page_query, is_page=True)
logger.info('获取成功')
return ResponseUtil.success(model_content={{ businessName }}_page_query_result)
{% elif table.tree %}
{{ businessName }}_query_result = await {{ BusinessName }}Service.get_{{ businessName }}_list_services(query_db, {{ businessName }}_query)
logger.info('获取成功')
return ResponseUtil.success(data={{ businessName }}_query_result)
{% endif %}
@{{ businessName }}_controller.post(
'',
dependencies=[UserInterfaceAuthDependency('{{ permissionPrefix }}:add')]),
@ValidateFields(validate_model='add_{{ businessName }}')
@Log(title='{{ functionName }}', business_type=BusinessType.INSERT)
async def add_{{ moduleName }}_{{ businessName }}(
request: Request,
add_{{ businessName }}: {{ BusinessName }}Model,
query_db: Annotated[AsyncSession, DBSessionDependency()],
current_user: Annotated[CurrentUserModel, CurrentUserDependency()],
) -> Response:
{% for column in columns %}
{% if column.python_field == "createBy" %}
add_{{ businessName }}.create_by = current_user.user.user_name
{% elif column.python_field == "createTime" %}
add_{{ businessName }}.create_time = datetime.now()
{% elif column.python_field == "updateBy" %}
add_{{ businessName }}.update_by = current_user.user.user_name
{% elif column.python_field == "updateTime" %}
add_{{ businessName }}.update_time = datetime.now()
{% endif %}
{% endfor %}
add_{{ businessName }}_result = await {{ BusinessName }}Service.add_{{ businessName }}_services(query_db, add_{{ businessName }})
logger.info(add_{{ businessName }}_result.message)
return ResponseUtil.success(msg=add_{{ businessName }}_result.message)
@{{ businessName }}_controller.put(
'',
dependencies=[UserInterfaceAuthDependency('{{ permissionPrefix }}:edit')]),
@ValidateFields(validate_model='edit_{{ businessName }}')
@Log(title='{{ functionName }}', business_type=BusinessType.UPDATE)
async def edit_{{ moduleName }}_{{ businessName }}(
request: Request,
edit_{{ businessName }}: {{ BusinessName }}Model,
query_db: Annotated[AsyncSession, DBSessionDependency()],
current_user: Annotated[CurrentUserModel, CurrentUserDependency()],
) -> Response:
{% for column in columns %}
{% if column.python_field == "updateBy" %}
edit_{{ businessName }}.update_by = current_user.user.user_name
{% elif column.python_field == "updateTime" %}
edit_{{ businessName }}.update_time = datetime.now()
{% endif %}
{% endfor %}
edit_{{ businessName }}_result = await {{ BusinessName }}Service.edit_{{ businessName }}_services(query_db, edit_{{ businessName }})
logger.info(edit_{{ businessName }}_result.message)
return ResponseUtil.success(msg=edit_{{ businessName }}_result.message)
@{{ businessName }}_controller.delete(
'/{% raw %}{{% endraw %}{{ pk_field }}s{% raw %}}{% endraw %}',
dependencies=[UserInterfaceAuthDependency('{{ permissionPrefix }}:remove')]),
@Log(title='{{ functionName }}', business_type=BusinessType.DELETE)
async def delete_{{ moduleName }}_{{ businessName }}(
request: Request,
{{ pk_field }}s: Annotated[str, Path(description='需要删除的{{ pk_field_comment }}')],
Annotated[AsyncSession, DBSessionDependency()],
) -> Response:
delete_{{ businessName }} = Delete{{ BusinessName }}Model({{ pkField }}s={{ pk_field }}s)
delete_{{ businessName }}_result = await {{ BusinessName }}Service.delete_{{ businessName }}_services(query_db, delete_{{ businessName }})
logger.info(delete_{{ businessName }}_result.message)
return ResponseUtil.success(msg=delete_{{ businessName }}_result.message)
@{{ businessName }}_controller.get(
'/{% raw %}{{% endraw %}{{ pk_field }}{% raw %}}{% endraw %}',
response_model={{ BusinessName }}Model,
dependencies=[UserInterfaceAuthDependency('{{ permissionPrefix }}:query')]
)
async def query_detail_{{ moduleName }}_{{ businessName }}(
request: Request,
{{ pk_field }}: Annotated[{{ pkColumn.python_type }}, Path(description='{{ pk_field_comment }}')],
query_db: Annotated[AsyncSession, DBSessionDependency()],
) -> Response:
{{ businessName }}_detail_result = await {{ BusinessName }}Service.{{ businessName }}_detail_services(query_db, {{ pk_field }})
logger.info(f'获取{{ pk_field }}{% raw %}{{% endraw %}{{ pk_field }}{% raw %}}{% endraw %}的信息成功')
return ResponseUtil.success(data={{ businessName }}_detail_result)
@{{ businessName }}_controller.post(
'/export',
dependencies=[UserInterfaceAuthDependency('{{ permissionPrefix }}:export')])
@Log(title='{{ functionName }}', business_type=BusinessType.EXPORT)
async def export_{{ moduleName }}_{{ businessName }}_list(
request: Request,
{{ businessName }}_page_query: Annotated[{{ BusinessName }}PageQueryModel, Form()],
query_db: Annotated[AsyncSession, DBSessionDependency()],
) -> Response:
# 获取全量数据
{{ businessName }}_query_result = await {{ BusinessName }}Service.get_{{ businessName }}_list_services(query_db, {{ businessName }}_page_query, is_page=False)
{{ businessName }}_export_result = await {{ BusinessName }}Service.export_{{ businessName }}_list_services({% if dicts %}request, {% endif %}{{ businessName }}_query_result)
logger.info('导出成功')
return ResponseUtil.streaming(data=bytes2file_response({{ businessName }}_export_result))