Skip to content

Commit f0ff4ec

Browse files
committed
Update template rendering
1 parent b99f3df commit f0ff4ec

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
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 %}
7-
{% if 'Decimal' in models %}
13+
{% if model_types|select('in', DECIMAL_TYPES)|first %}
814
from decimal import Decimal
9-
1015
{% endif %}
11-
{% if 'Uuid' or 'UUID' in models %}
16+
{% if 'Uuid' in model_types or 'UUID' in model_types %}
1217
from uuid import UUID
1318
{% endif %}
1419

@@ -37,14 +42,9 @@ class {{ class_name }}({% if default_datetime_column %}Base{% else %}DataClassBa
3742
{%- endif %} = mapped_column(
3843
{%- if model.type in ['NVARCHAR', 'String', 'Unicode', 'VARCHAR'] -%}
3944
sa.String({{ model.length }})
40-
{%- elif database_type == 'mysql' and model.type in ['BIT', 'ENUM', 'LONGBLOB', 'LONGTEXT', 'MEDIUMBLOB',
41-
'MEDIUMINT', 'MEDIUMTEXT', 'SET', 'TINYBLOB', 'TINYINT', 'TINYTEXT', 'YEAR'] -%}
45+
{%- elif database_type == 'mysql' and model.type in MYSQL_TYPES -%}
4246
mysql.{{ model.type }}()
43-
{%- elif database_type == 'postgresql' and model.type in [
44-
'ARRAY', 'BIT', 'BYTEA', 'CIDR', 'CITEXT', 'DATEMULTIRANGE', 'DATERANGE', 'DOMAIN', 'ENUM', 'HSTORE', 'INET',
45-
'INT4MULTIRANGE', 'INT4RANGE', 'INT8MULTIRANGE', 'INT8RANGE', 'INTERVAL', 'JSONB', 'JSONPATH', 'MACADDR',
46-
'MACADDR8', 'MONEY', 'NUMMULTIRANGE', 'NUMRANGE', 'OID', 'REGCLASS', 'REGCONFIG', 'TSMULTIRANGE', 'TSQUERY',
47-
'TSRANGE', 'TSTZMULTIRANGE', 'TSTZRANGE', 'TSVECTOR'] -%}
47+
{%- elif database_type == 'postgresql' and model.type in POSTGRESQL_TYPES -%}
4848
{%- else -%}
4949
sa.{{ model.type }}()
5050
{%- 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)