@@ -77,26 +77,26 @@ class {{ class_name }}:
77
77
additional_properties: Dict[str, {{ additional_property_type }}] = _attrs_field(init=False, factory=dict)
78
78
{% endif %}
79
79
80
- {% macro _to_dict (multipart =False ) %}
81
- {% for property in model .required_properties + model .optional_properties %}
80
+ {% macro _transform_property (property , content , multipart =False ) %}
82
81
{% import "property_templates/" + property .template as prop_template %}
83
- {% if prop_template .transform %}
84
- {{ prop_template.transform(property, "self." + property.python_name , property.python_name, multipart=multipart) }}
85
- {% elif multipart %}
86
- {{ property.python_name }} = self. {{ property.python_name }} if isinstance(self. {{ property.python_name }}, Unset) else (None, str(self. {{ property.python_name }}).encode(), "text/plain")
87
- {% else %}
88
- {{ property.python_name }} = self. {{ property.python_name }}
89
- {% endif %}
82
+ {% - if prop_template .transform - %}
83
+ {{ prop_template.transform(property, content , property.python_name, multipart=multipart) }}
84
+ {% - elif multipart - %}
85
+ {{ property.python_name }} = {{ content }} if isinstance({{ content }}, Unset) else (None, str({{ content }}).encode(), "text/plain")
86
+ {% - else - %}
87
+ {{ property.python_name }} = {{ content }}
88
+ {% - endif - %}
90
89
91
- {% endfor %}
90
+ {% - endmacro - %}
92
91
92
+ {% macro _prepare_field_dict (multipart =False ) %}
93
93
field_dict: Dict[str, Any] = {}
94
94
{% if model .additional_properties %}
95
- {% if model .additional_properties .template %} {# Can be a bool instead of an object #}
96
- {% import "property_templates/" + model .additional_properties .template as prop_template %}
97
- {% else %}
98
- {% set prop_template = None %}
99
- {% endif %}
95
+ {% - if model .additional_properties .template - %} {# Can be a bool instead of an object #}
96
+ {% - import "property_templates/" + model .additional_properties .template as prop_template - %}
97
+ {% - else - %}
98
+ {% - set prop_template = None - %}
99
+ {% - endif - %}
100
100
{% if prop_template and prop_template .transform %}
101
101
for prop_name, prop in self.additional_properties.items():
102
102
{{ prop_template.transform(model.additional_properties, "prop", "field_dict[prop_name] ", multipart=multipart, declare_type=false) | indent(4) }}
@@ -107,8 +107,16 @@ field_dict.update({
107
107
})
108
108
{% else %}
109
109
field_dict.update(self.additional_properties)
110
- {% endif %}
111
- {% endif %}
110
+ {% - endif -%}
111
+ {% - endif -%}
112
+ {% endmacro %}
113
+
114
+ {% macro _to_dict () %}
115
+ {% for property in model .required_properties + model .optional_properties -%}
116
+ {{ _transform_property(property, "self." + property.python_name) }}
117
+ {% endfor %}
118
+
119
+ {{ _prepare_field_dict() }}
112
120
field_dict.update({
113
121
{% for property in model .required_properties + model .optional_properties %}
114
122
{% if property .required %}
@@ -133,8 +141,35 @@ return field_dict
133
141
{{ _to_dict() | indent(8) }}
134
142
135
143
{% if model .is_multipart_body %}
136
- def to_multipart(self) -> Dict[str, Any]:
137
- {{ _to_dict(multipart=True) | indent(8) }}
144
+ def to_multipart(self) -> List[Tuple[str, Any]]:
145
+ field_list: List[Tuple[str, Any]] = []
146
+ {% for property in model .required_properties + model .optional_properties %}
147
+ {% if property .__class__ .__name__ == 'ListProperty' %}
148
+ {% if not property .required %}
149
+ for cont in self.{{ property.python_name }} or []:
150
+ {% else %}
151
+ for cont in self.{{ property.python_name }}:
152
+ {% endif %}
153
+ {{ _transform_property(property.inner_property, "cont", True) | indent(12) }}
154
+ field_list.append(("{{ property.python_name }}", {{property.inner_property.python_name}}))
155
+
156
+ {% else %}
157
+ {{ _transform_property(property, "self." + property.python_name, True) | indent(8) }}
158
+ {% if not property .required %}
159
+ if {{ property.python_name }} is not UNSET:
160
+ field_list.append(("{{ property.python_name }}", {{property.python_name}}))
161
+ {% else %}
162
+ field_list.append(("{{ property.python_name }}", {{property.python_name}}))
163
+ {% endif %}
164
+ {% endif %}
165
+ {% endfor %}
166
+
167
+ {{ _prepare_field_dict(True) | indent(8) }}
168
+
169
+ field_list += list(field_dict.items())
170
+
171
+ return field_list
172
+
138
173
{% endif %}
139
174
140
175
@classmethod
0 commit comments