Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python-flask] Apply template tweaks to improve lint results #6826

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding: utf-8

# flake8: noqa
from __future__ import absolute_import
# import models into model package
{{#models}}{{#model}}from .{{classFilename}} import {{classname}}{{/model}}
{{#models}}{{#model}}from {{modelPackage}}.{{classFilename}} import {{classname}}{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from flask_testing import TestCase
from ..encoder import JSONEncoder
import connexion
import logging

import connexion
from flask_testing import TestCase

from {{packageName}}.encoder import JSONEncoder


class BaseTestCase(TestCase):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
{{/supportPython2}}

import connexion
from .encoder import JSONEncoder

from {{packageName}} import encoder


def main():
app = connexion.App(__name__, specification_dir='./swagger/')
app.app.json_encoder = JSONEncoder
app.add_api('swagger.yaml', arguments={'title': '{{appDescription}}'})
app.app.json_encoder = encoder.JSONEncoder
app.add_api('swagger.yaml', arguments={'title': '{{appName}}'})
app.run(port={{serverPort}})


Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
from pprint import pformat
import pprint

import six
{{^supportPython2}}
from typing import TypeVar, Type
import typing
{{/supportPython2}}
from six import iteritems
from ..util import deserialize_model

from {{packageName}} import util
{{^supportPython2}}

T = TypeVar('T')
T = typing.TypeVar('T')
{{/supportPython2}}


class Model(object):
# swaggerTypes: The key is attribute name and the value is attribute type.
# swaggerTypes: The key is attribute name and the
# value is attribute type.
swagger_types = {}

# attributeMap: The key is attribute name and the value is json key in definition.
# attributeMap: The key is attribute name and the
# value is json key in definition.
attribute_map = {}

@classmethod
def from_dict(cls{{^supportPython2}}: Type[T]{{/supportPython2}}, dikt){{^supportPython2}} -> T{{/supportPython2}}:
"""
Returns the dict as a model
"""
return deserialize_model(dikt, cls)
def from_dict(cls{{^supportPython2}}: typing.Type[T]{{/supportPython2}}, dikt){{^supportPython2}} -> T{{/supportPython2}}:
"""Returns the dict as a model"""
return util.deserialize_model(dikt, cls)

def to_dict(self):
"""
Returns the model properties as a dict
"""Returns the model properties as a dict

:rtype: dict
"""
result = {}

for attr, _ in iteritems(self.swagger_types):
for attr, _ in six.iteritems(self.swagger_types):
value = getattr(self, attr)
if isinstance(value, list):
result[attr] = list(map(
Expand All @@ -53,27 +54,20 @@ class Model(object):
return result

def to_str(self):
"""
Returns the string representation of the model
"""Returns the string representation of the model

:rtype: str
"""
return pformat(self.to_dict())
return pprint.pformat(self.to_dict())

def __repr__(self):
"""
For `print` and `pprint`
"""
"""For `print` and `pprint`"""
return self.to_str()

def __eq__(self, other):
"""
Returns true if both objects are equal
"""
"""Returns true if both objects are equal"""
return self.__dict__ == other.__dict__

def __ne__(self, other):
"""
Returns true if both objects are not equal
"""
return not self == other
"""Returns true if both objects are not equal"""
return not self == other
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import connexion
{{#imports}}{{import}}
import six

{{#imports}}{{import}} # noqa: E501
{{/imports}}
from datetime import date, datetime
from typing import List, Dict
from six import iteritems
from ..util import deserialize_date, deserialize_datetime
from {{packageName}} import util
{{#operations}}
{{#operation}}


def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}):
"""
{{#summary}}{{.}}{{/summary}}{{^summary}}{{operationId}}{{/summary}}
{{#notes}}{{.}}{{/notes}}
def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}): # noqa: E501
"""{{#summary}}{{.}}{{/summary}}{{^summary}}{{operationId}}{{/summary}}

{{#notes}}{{.}}{{/notes}} # noqa: E501

{{#allParams}}
:param {{paramName}}: {{description}}
{{^isContainer}}
Expand Down Expand Up @@ -55,47 +55,47 @@ def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{
{{#allParams}}
{{^isContainer}}
{{#isDate}}
{{paramName}} = deserialize_date({{paramName}})
{{paramName}} = util.deserialize_date({{paramName}})
{{/isDate}}
{{#isDateTime}}
{{paramName}} = deserialize_datetime({{paramName}})
{{paramName}} = util.deserialize_datetime({{paramName}})
{{/isDateTime}}
{{^isPrimitiveType}}
{{^isFile}}
if connexion.request.is_json:
{{paramName}} = {{baseType}}.from_dict(connexion.request.get_json())
{{paramName}} = {{baseType}}.from_dict(connexion.request.get_json()) # noqa: E501
{{/isFile}}
{{/isPrimitiveType}}
{{/isContainer}}
{{#isListContainer}}
{{#items}}
{{#isDate}}
if connexion.request.is_json:
{{paramName}} = [deserialize_date(s) for s in connexion.request.get_json()]
{{paramName}} = [util.deserialize_date(s) for s in connexion.request.get_json()] # noqa: E501
{{/isDate}}
{{#isDateTime}}
if connexion.request.is_json:
{{paramName}} = [deserialize_datetime(s) for s in connexion.request.get_json()]
{{paramName}} = [util.deserialize_datetime(s) for s in connexion.request.get_json()] # noqa: E501
{{/isDateTime}}
{{#complexType}}
if connexion.request.is_json:
{{paramName}} = [{{complexType}}.from_dict(d) for d in connexion.request.get_json()]
{{paramName}} = [{{complexType}}.from_dict(d) for d in connexion.request.get_json()] # noqa: E501
{{/complexType}}
{{/items}}
{{/isListContainer}}
{{#isMapContainer}}
{{#items}}
{{#isDate}}
if connexion.request.is_json:
{{paramName}} = {k: deserialize_date(v) for k, v in iteritems(connexion.request.get_json())}
{{paramName}} = {k: util.deserialize_date(v) for k, v in six.iteritems(connexion.request.get_json())} # noqa: E501
{{/isDate}}
{{#isDateTime}}
if connexion.request.is_json:
{{paramName}} = {k: deserialize_datetime(v) for k, v in iteritems(connexion.request.get_json())}
{{paramName}} = {k: util.deserialize_datetime(v) for k, v in six.iteritems(connexion.request.get_json())} # noqa: E501
{{/isDateTime}}
{{#complexType}}
if connexion.request.is_json:
{{paramName}} = {k: {{baseType}}.from_dict(v) for k, v in iteritems(connexion.request.get_json())}
{{paramName}} = {k: {{baseType}}.from_dict(v) for k, v in six.iteritems(connexion.request.get_json())} # noqa: E501
{{/complexType}}
{{/items}}
{{/isMapContainer}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

from __future__ import absolute_import

{{#imports}}{{import}}
{{/imports}}
from . import BaseTestCase
from six import BytesIO
from flask import json
from six import BytesIO

{{#imports}}{{import}} # noqa: E501
{{/imports}}
from {{packageName}}.test import BaseTestCase


class {{#operations}}Test{{classname}}(BaseTestCase):
""" {{classname}} integration test stubs """
"""{{classname}} integration test stubs"""

{{#operation}}
def test_{{operationId}}(self):
"""
Test case for {{{operationId}}}
"""Test case for {{{operationId}}}

{{{summary}}}
"""
Expand All @@ -31,15 +31,17 @@ class {{#operations}}Test{{classname}}(BaseTestCase):
{{#formParams}}
{{#-first}}data = dict({{/-first}}{{^-first}} {{/-first}}{{paramName}}={{{example}}}{{#hasMore}},{{/hasMore}}{{#-last}}){{/-last}}
{{/formParams}}
response = self.client.open('{{#contextPath}}{{{.}}}{{/contextPath}}{{{path}}}'{{#pathParams}}{{#-first}}.format({{/-first}}{{paramName}}={{{example}}}{{#hasMore}}, {{/hasMore}}{{^hasMore}}){{/hasMore}}{{/pathParams}},
method='{{httpMethod}}'{{#bodyParam}},
data=json.dumps({{paramName}}){{^consumes}},
content_type='application/json'{{/consumes}}{{/bodyParam}}{{#headerParams}}{{#-first}},
headers=headers{{/-first}}{{/headerParams}}{{#formParams}}{{#-first}},
data=data{{/-first}}{{/formParams}}{{#consumes}}{{#-first}},
content_type='{{{mediaType}}}'{{/-first}}{{/consumes}}{{#queryParams}}{{#-first}},
query_string=query_string{{/-first}}{{/queryParams}})
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
response = self.client.open(
'{{#contextPath}}{{{.}}}{{/contextPath}}{{{path}}}'{{#pathParams}}{{#-first}}.format({{/-first}}{{paramName}}={{{example}}}{{#hasMore}}, {{/hasMore}}{{^hasMore}}){{/hasMore}}{{/pathParams}},
method='{{httpMethod}}'{{#bodyParam}},
data=json.dumps({{paramName}}){{^consumes}},
content_type='application/json'{{/consumes}}{{/bodyParam}}{{#headerParams}}{{#-first}},
headers=headers{{/-first}}{{/headerParams}}{{#formParams}}{{#-first}},
data=data{{/-first}}{{/formParams}}{{#consumes}}{{#-first}},
content_type='{{{mediaType}}}'{{/-first}}{{/consumes}}{{#queryParams}}{{#-first}},
query_string=query_string{{/-first}}{{/queryParams}})
self.assert200(response,
'Response body is : ' + response.data.decode('utf-8'))

{{/operation}}
{{/operations}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from six import iteritems
from {{modelPackage}}.base_model_ import Model
from connexion.apps.flask_app import FlaskJSONEncoder
import six

from {{modelPackage}}.base_model_ import Model


class JSONEncoder(FlaskJSONEncoder):
include_nulls = False

def default(self, o):
if isinstance(o, Model):
dikt = {}
for attr, _ in iteritems(o.swagger_types):
for attr, _ in six.iteritems(o.swagger_types):
value = getattr(o, attr)
if value is None and not self.include_nulls:
continue
attr = o.attribute_map[attr]
dikt[attr] = value
return dikt
return FlaskJSONEncoder.default(self, o)
return FlaskJSONEncoder.default(self, o)
Loading