Skip to content

Commit 6c029b9

Browse files
authored
Fix code generation template missing Decimal (#830)
* Fix code generation template missing Decimal * Update template rendering * Update version number * Fix lint
1 parent df12a0c commit 6c029b9

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

backend/plugin/code_generator/plugin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[plugin]
22
summary = '代码生成'
3-
version = '0.0.4'
3+
version = '0.0.5'
44
description = '生成通用业务代码'
55
author = 'wu-clan'
66

backend/plugin/code_generator/templates/python/model.jinja

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
{% set DECIMAL_TYPES = ['DECIMAL', 'NUMERIC', 'MONEY', 'NUMMULTIRANGE', 'NUMRANGE'] %}
4+
{% set MYSQL_TYPES = ['BIT', 'ENUM', 'LONGBLOB', 'LONGTEXT', 'MEDIUMBLOB', 'MEDIUMINT', 'MEDIUMTEXT', 'SET',
5+
'TINYBLOB', 'TINYINT', 'TINYTEXT', 'YEAR'] %}
6+
{% set POSTGRESQL_TYPES = ['ARRAY', 'BIT', 'BYTEA', 'CIDR', 'CITEXT', 'DATEMULTIRANGE', 'DATERANGE', 'DOMAIN', 'ENUM',
7+
'HSTORE', 'INET', 'INT4MULTIRANGE', 'INT4RANGE', 'INT8MULTIRANGE', 'INT8RANGE', 'INTERVAL', 'JSONB', 'JSONPATH',
8+
'MACADDR', 'MACADDR8', 'MONEY', 'NUMMULTIRANGE', 'NUMRANGE', 'OID', 'REGCLASS', 'REGCONFIG', 'TSMULTIRANGE', 'TSQUERY',
9+
'TSRANGE', 'TSTZMULTIRANGE', 'TSTZRANGE', 'TSVECTOR'] %}
310
{% if default_datetime_column %}
411
from datetime import datetime
5-
612
{% endif %}
13+
{% if model_types|select('in', DECIMAL_TYPES)|first %}
14+
from decimal import Decimal
15+
{% endif %}
16+
{% if 'Uuid' in model_types or 'UUID' in model_types %}
717
from uuid import UUID
18+
{% endif %}
819

920
import sqlalchemy as sa
1021

@@ -31,14 +42,9 @@ class {{ class_name }}({% if default_datetime_column %}Base{% else %}DataClassBa
3142
{%- endif %} = mapped_column(
3243
{%- if model.type in ['NVARCHAR', 'String', 'Unicode', 'VARCHAR'] -%}
3344
sa.String({{ model.length }})
34-
{%- elif database_type == 'mysql' and model.type in ['BIT', 'ENUM', 'LONGBLOB', 'LONGTEXT', 'MEDIUMBLOB',
35-
'MEDIUMINT', 'MEDIUMTEXT', 'SET', 'TINYBLOB', 'TINYINT', 'TINYTEXT', 'YEAR'] -%}
45+
{%- elif database_type == 'mysql' and model.type in MYSQL_TYPES -%}
3646
mysql.{{ model.type }}()
37-
{%- elif database_type == 'postgresql' and model.type in [
38-
'ARRAY', 'BIT', 'BYTEA', 'CIDR', 'CITEXT', 'DATEMULTIRANGE', 'DATERANGE', 'DOMAIN', 'ENUM', 'HSTORE', 'INET',
39-
'INT4MULTIRANGE', 'INT4RANGE', 'INT8MULTIRANGE', 'INT8RANGE', 'INTERVAL', 'JSONB', 'JSONPATH', 'MACADDR',
40-
'MACADDR8', 'MONEY', 'NUMMULTIRANGE', 'NUMRANGE', 'OID', 'REGCLASS', 'REGCONFIG', 'TSMULTIRANGE', 'TSQUERY',
41-
'TSRANGE', 'TSTZMULTIRANGE', 'TSTZRANGE', 'TSVECTOR'] -%}
47+
{%- elif database_type == 'postgresql' and model.type in POSTGRESQL_TYPES -%}
4248
{%- else -%}
4349
sa.{{ model.type }}()
4450
{%- endif -%}, default=

backend/plugin/code_generator/utils/code_template.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def get_vars(business: GenBusiness, models: Sequence[GenColumn]) -> dict[str, st
9595
'permission': str(business.table_name.replace('_', ':')),
9696
'database_type': settings.DATABASE_TYPE,
9797
'models': models,
98+
'model_types': [model.type for model in models],
9899
}
99100

100101

0 commit comments

Comments
 (0)