lenticular_cloud2/specs/api_template/property_templates/datetime_property.py.jinja

40 lines
1.4 KiB
Plaintext
Raw Normal View History

{% macro construct_function(property, source) %}
isoparse({{ source }})
{% endmacro %}
{% from "property_templates/property_macros.py.jinja" import construct_template %}
{% macro construct(property, source, initial_value=None) %}
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
{% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, str){% endmacro %}
{% macro transform(property, source, destination, declare_type=True, multipart=False) %}
{% set transformed = source + ".isoformat()" %}
{% if multipart %}{# Multipart data must be bytes, not str #}
{% set transformed = transformed + ".encode()" %}
{% endif %}
{% if property.required %}
{% if property.nullable %}
{{ destination }} = {{ transformed }} if {{ source }} else None
{% else %}
{{ destination }} = {{ transformed }}
{% endif %}
{% else %}
{% if declare_type %}
{% set type_annotation = property.get_type_string(json=True) %}
{% if multipart %}{% set type_annotation = type_annotation | replace("str", "bytes") %}{% endif %}
{{ destination }}: {{ type_annotation }} = UNSET
{% else %}
{{ destination }} = UNSET
{% endif %}
if not isinstance({{ source }}, Unset):
{% if property.nullable %}
{{ destination }} = {{ transformed }} if {{ source }} else None
{% else %}
{{ destination }} = {{ transformed }}
{% endif %}
{% endif %}
{% endmacro %}