github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/python/model.mustache (about)

     1  # coding: utf-8
     2  
     3  {{>partial_header}}
     4  
     5  import pprint
     6  import re  # noqa: F401
     7  
     8  import six
     9  
    10  from {{packageName}}.configuration import Configuration
    11  
    12  
    13  {{#models}}
    14  {{#model}}
    15  class {{classname}}(object):
    16      """NOTE: This class is auto generated by OpenAPI Generator.
    17      Ref: https://openapi-generator.tech
    18  
    19      Do not edit the class manually.
    20      """{{#allowableValues}}
    21  
    22      """
    23      allowed enum values
    24      """
    25  {{#enumVars}}
    26      {{name}} = {{{value}}}{{^-last}}
    27  {{/-last}}
    28  {{/enumVars}}{{/allowableValues}}
    29  
    30  {{#allowableValues}}
    31      allowable_values = [{{#enumVars}}{{name}}{{^-last}}, {{/-last}}{{/enumVars}}]  # noqa: E501
    32  
    33  {{/allowableValues}}
    34      """
    35      Attributes:
    36        openapi_types (dict): The key is attribute name
    37                              and the value is attribute type.
    38        attribute_map (dict): The key is attribute name
    39                              and the value is json key in definition.
    40      """
    41      openapi_types = {
    42  {{#vars}}
    43          '{{name}}': '{{{dataType}}}'{{^-last}},{{/-last}}
    44  {{/vars}}
    45      }
    46  
    47      attribute_map = {
    48  {{#vars}}
    49          '{{name}}': '{{baseName}}'{{^-last}},{{/-last}}
    50  {{/vars}}
    51      }
    52  {{#discriminator}}
    53  
    54      discriminator_value_class_map = {
    55          {{#children}}'{{^vendorExtensions.x-discriminator-value}}{{name}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}': '{{{classname}}}'{{^-last}},
    56          {{/-last}}{{/children}}
    57      }
    58  {{/discriminator}}
    59  
    60      def __init__(self{{#vars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}, local_vars_configuration=None):  # noqa: E501
    61          """{{classname}} - a model defined in OpenAPI"""  # noqa: E501
    62          if local_vars_configuration is None:
    63              local_vars_configuration = Configuration()
    64          self.local_vars_configuration = local_vars_configuration
    65  {{#vars}}{{#-first}}
    66  {{/-first}}
    67          self._{{name}} = None
    68  {{/vars}}
    69          self.discriminator = {{#discriminator}}'{{{discriminatorName}}}'{{/discriminator}}{{^discriminator}}None{{/discriminator}}
    70  {{#vars}}{{#-first}}
    71  {{/-first}}
    72  {{#required}}
    73          self.{{name}} = {{name}}
    74  {{/required}}
    75  {{^required}}
    76  {{#isNullable}}
    77          self.{{name}} = {{name}}
    78  {{/isNullable}}
    79  {{^isNullable}}
    80          if {{name}} is not None:
    81              self.{{name}} = {{name}}
    82  {{/isNullable}}
    83  {{/required}}
    84  {{/vars}}
    85  
    86  {{#vars}}
    87      @property
    88      def {{name}}(self):
    89          """Gets the {{name}} of this {{classname}}.  # noqa: E501
    90  
    91  {{#description}}
    92          {{{description}}}  # noqa: E501
    93  {{/description}}
    94  
    95          :return: The {{name}} of this {{classname}}.  # noqa: E501
    96          :rtype: {{dataType}}
    97          """
    98          return self._{{name}}
    99  
   100      @{{name}}.setter
   101      def {{name}}(self, {{name}}):
   102          """Sets the {{name}} of this {{classname}}.
   103  
   104  {{#description}}
   105          {{{description}}}  # noqa: E501
   106  {{/description}}
   107  
   108          :param {{name}}: The {{name}} of this {{classname}}.  # noqa: E501
   109          :type: {{dataType}}
   110          """
   111  {{^isNullable}}
   112  {{#required}}
   113          if self.local_vars_configuration.client_side_validation and {{name}} is None:  # noqa: E501
   114              raise ValueError("Invalid value for `{{name}}`, must not be `None`")  # noqa: E501
   115  {{/required}}
   116  {{/isNullable}}
   117  {{#isEnum}}
   118  {{#isContainer}}
   119          allowed_values = [{{#isNullable}}None,{{/isNullable}}{{#allowableValues}}{{#values}}{{#items.isString}}"{{/items.isString}}{{{this}}}{{#items.isString}}"{{/items.isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}]  # noqa: E501
   120  {{#isArray}}
   121          if (self.local_vars_configuration.client_side_validation and
   122                  not set({{{name}}}).issubset(set(allowed_values))):  # noqa: E501
   123              raise ValueError(
   124                  "Invalid values for `{{{name}}}` [{0}], must be a subset of [{1}]"  # noqa: E501
   125                  .format(", ".join(map(str, set({{{name}}}) - set(allowed_values))),  # noqa: E501
   126                          ", ".join(map(str, allowed_values)))
   127              )
   128  {{/isArray}}
   129  {{#isMap}}
   130          if (self.local_vars_configuration.client_side_validation and
   131                  not set({{{name}}}.keys()).issubset(set(allowed_values))):  # noqa: E501
   132              raise ValueError(
   133                  "Invalid keys in `{{{name}}}` [{0}], must be a subset of [{1}]"  # noqa: E501
   134                  .format(", ".join(map(str, set({{{name}}}.keys()) - set(allowed_values))),  # noqa: E501
   135                          ", ".join(map(str, allowed_values)))
   136              )
   137  {{/isMap}}
   138  {{/isContainer}}
   139  {{^isContainer}}
   140          allowed_values = [{{#isNullable}}None,{{/isNullable}}{{#allowableValues}}{{#values}}{{#isString}}"{{/isString}}{{{this}}}{{#isString}}"{{/isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}]  # noqa: E501
   141          if self.local_vars_configuration.client_side_validation and {{{name}}} not in allowed_values:  # noqa: E501
   142              raise ValueError(
   143                  "Invalid value for `{{{name}}}` ({0}), must be one of {1}"  # noqa: E501
   144                  .format({{{name}}}, allowed_values)
   145              )
   146  {{/isContainer}}
   147  {{/isEnum}}
   148  {{^isEnum}}
   149  {{#hasValidation}}
   150  {{#maxLength}}
   151          if (self.local_vars_configuration.client_side_validation and
   152                  {{name}} is not None and len({{name}}) > {{maxLength}}):
   153              raise ValueError("Invalid value for `{{name}}`, length must be less than or equal to `{{maxLength}}`")  # noqa: E501
   154  {{/maxLength}}
   155  {{#minLength}}
   156          if (self.local_vars_configuration.client_side_validation and
   157                  {{name}} is not None and len({{name}}) < {{minLength}}):
   158              raise ValueError("Invalid value for `{{name}}`, length must be greater than or equal to `{{minLength}}`")  # noqa: E501
   159  {{/minLength}}
   160  {{#maximum}}
   161          if (self.local_vars_configuration.client_side_validation and
   162                  {{name}} is not None and {{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}):  # noqa: E501
   163              raise ValueError("Invalid value for `{{name}}`, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}`{{maximum}}`")  # noqa: E501
   164  {{/maximum}}
   165  {{#minimum}}
   166          if (self.local_vars_configuration.client_side_validation and
   167                  {{name}} is not None and {{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}):  # noqa: E501
   168              raise ValueError("Invalid value for `{{name}}`, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}`{{minimum}}`")  # noqa: E501
   169  {{/minimum}}
   170  {{#pattern}}
   171          if (self.local_vars_configuration.client_side_validation and
   172                  {{name}} is not None and not re.search(r'{{{vendorExtensions.x-regex}}}', {{name}}{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}})):  # noqa: E501
   173              raise ValueError(r"Invalid value for `{{name}}`, must be a follow pattern or equal to `{{{pattern}}}`")  # noqa: E501
   174  {{/pattern}}
   175  {{#maxItems}}
   176          if (self.local_vars_configuration.client_side_validation and
   177                  {{name}} is not None and len({{name}}) > {{maxItems}}):
   178              raise ValueError("Invalid value for `{{name}}`, number of items must be less than or equal to `{{maxItems}}`")  # noqa: E501
   179  {{/maxItems}}
   180  {{#minItems}}
   181          if (self.local_vars_configuration.client_side_validation and
   182                  {{name}} is not None and len({{name}}) < {{minItems}}):
   183              raise ValueError("Invalid value for `{{name}}`, number of items must be greater than or equal to `{{minItems}}`")  # noqa: E501
   184  {{/minItems}}
   185  {{/hasValidation}}
   186  {{/isEnum}}
   187  
   188          self._{{name}} = {{name}}
   189  
   190  {{/vars}}
   191  {{#discriminator}}
   192      def get_real_child_model(self, data):
   193          """Returns the real base class specified by the discriminator"""
   194          discriminator_key = self.attribute_map[self.discriminator]
   195          discriminator_value = data[discriminator_key]
   196          return self.discriminator_value_class_map.get(discriminator_value)
   197  
   198  {{/discriminator}}
   199      def to_dict(self):
   200          """Returns the model properties as a dict"""
   201          result = {}
   202  
   203          for attr, _ in six.iteritems(self.openapi_types):
   204              value = getattr(self, attr)
   205              if isinstance(value, list):
   206                  result[attr] = list(map(
   207                      lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
   208                      value
   209                  ))
   210              elif hasattr(value, "to_dict"):
   211                  result[attr] = value.to_dict()
   212              elif isinstance(value, dict):
   213                  result[attr] = dict(map(
   214                      lambda item: (item[0], item[1].to_dict())
   215                      if hasattr(item[1], "to_dict") else item,
   216                      value.items()
   217                  ))
   218              else:
   219                  result[attr] = value
   220  
   221          return result
   222  
   223      def to_str(self):
   224          """Returns the string representation of the model"""
   225          return pprint.pformat(self.to_dict())
   226  
   227      def __repr__(self):
   228          """For `print` and `pprint`"""
   229          return self.to_str()
   230  
   231      def __eq__(self, other):
   232          """Returns true if both objects are equal"""
   233          if not isinstance(other, {{classname}}):
   234              return False
   235  
   236          return self.to_dict() == other.to_dict()
   237  
   238      def __ne__(self, other):
   239          """Returns true if both objects are not equal"""
   240          if not isinstance(other, {{classname}}):
   241              return True
   242  
   243          return self.to_dict() != other.to_dict()
   244  {{/model}}
   245  {{/models}}