116 lines
3.6 KiB
Python
116 lines
3.6 KiB
Python
import json
|
||
import shutil
|
||
|
||
|
||
def generate_view_code(view):
|
||
module = view['module']
|
||
view_name = view['name']
|
||
view_func = view['name'] + '_view'
|
||
template = view['template']
|
||
model = view['model']
|
||
filter_order = view['filter_order']
|
||
form_action_url = view['form_action_url']
|
||
|
||
filters = view['filters']
|
||
breadcrumb_list = view['breadcrumb_list']
|
||
table_columns = view['table_columns']
|
||
show_modify_button = view['show_modify_button']
|
||
show_add_button = view['show_add_button']
|
||
show_download_button = view['show_download_button']
|
||
show_upload_button = view['show_upload_button']
|
||
|
||
filter_conditions = "\n ".join([
|
||
f"if request.GET.get('{f['name']}', ''):\n query_set = query_set.filter({f['name']}__icontains=request.GET.get('{f['name']}', ''))"
|
||
for f in filters])
|
||
query_params_assignments = "\n ".join([f"{f['name']} = request.GET.get('{f['name']}', '')" for f in filters])
|
||
query_params_string = " + ".join([f"'&{f['name']}={{}}' + format({f['name']})" for f in filters])
|
||
|
||
view_code = f"""
|
||
def {view_func}(request):
|
||
# 声明查询集
|
||
query_set = {model}.objects.filter().order_by('-{filter_order}')
|
||
|
||
# 获取查询参数
|
||
{query_params_assignments}
|
||
|
||
# 根据提供的参数进行筛选
|
||
{filter_conditions}
|
||
|
||
# 对查询结果进行分页,每页10条记录
|
||
items = paginate_query_and_assign_numbers(
|
||
request=request,
|
||
queryset=query_set,
|
||
per_page=10
|
||
)
|
||
|
||
# 构建上下文查询参数字符串
|
||
query_params = {query_params_string}
|
||
|
||
# 准备上下文
|
||
context = {{
|
||
'items': items,
|
||
'filters': {json.dumps(filters, ensure_ascii=False)},
|
||
'form_action_url': '{form_action_url}',
|
||
'breadcrumb_list': {json.dumps(breadcrumb_list, ensure_ascii=False)},
|
||
'query_params': query_params,
|
||
'table_columns': {json.dumps(table_columns, ensure_ascii=False)},
|
||
'show_modify_button': {show_modify_button},
|
||
'show_add_button': {show_add_button},
|
||
'show_download_button': {show_download_button},
|
||
'show_upload_button': {show_upload_button},
|
||
}}
|
||
|
||
return render(request, '{module}/{template}', context)
|
||
"""
|
||
return view_code
|
||
|
||
|
||
def copy_template(path):
|
||
source_html = 'comom_list.html' # 修改为实际模板文件路径
|
||
destination_html = path
|
||
|
||
shutil.copy(source_html, destination_html)
|
||
|
||
|
||
def add_url_pattern(view):
|
||
view_name = view['name']
|
||
urls_path = view['urls_path']
|
||
view_def = view['name'] + '_view'
|
||
url_pattern = f" path('{view_name}/', {view_def}, name='{view_name}'),\n"
|
||
|
||
with open(urls_path, 'r', encoding='utf-8') as file:
|
||
content = file.readlines()
|
||
|
||
for index, line in enumerate(content):
|
||
if line.strip() == "urlpatterns = [":
|
||
content.insert(index + 1, url_pattern)
|
||
break
|
||
|
||
with open(urls_path, 'w', encoding='utf-8') as file:
|
||
file.writelines(content)
|
||
|
||
|
||
def main_func():
|
||
config_file = 'config.json'
|
||
with open(config_file, 'r', encoding='utf-8') as file:
|
||
config = json.load(file)
|
||
|
||
content = ['asset_mgnt', 'pjt_mgnt']
|
||
for key, value in config.items():
|
||
if key in content:
|
||
for view in value:
|
||
view_code = generate_view_code(view)
|
||
insert_file_path = view['insert_file_path']
|
||
|
||
with open(insert_file_path, 'a', encoding='utf-8') as file:
|
||
file.write(view_code)
|
||
|
||
# Copy template file and rename
|
||
html_file_path = view.get('html_file_path')
|
||
copy_template(html_file_path)
|
||
add_url_pattern(view)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main_func()
|