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

[style] impose black code style #1292

Merged
merged 3 commits into from
Feb 26, 2020
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
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
import-order-style = google
max-line-length = 90
exclude = .tox,build,docs,bin,examples,flask_appbuilder/templates,flask_appbuilder/static,venv
ignore = E203,W503,W605
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ matrix:
include:
- python: 3.6
env: TOXENV=flake8

- python: 3.6
env: TOXENV=black
- python: 3.6
env: TOXENV=mysql
services:
Expand All @@ -14,7 +15,6 @@ matrix:
- mysql -u root -e "DROP DATABASE IF EXISTS app; CREATE DATABASE app DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"
- mysql -u root -e "CREATE USER 'mysqluser'@'localhost' IDENTIFIED BY 'mysqluserpassword';"
- mysql -u root -e "GRANT ALL ON app.* TO 'mysqluser'@'localhost';"

- python: 3.6
env: TOXENV=mssql
services:
Expand Down
51 changes: 20 additions & 31 deletions flask_appbuilder/api/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Tree:
"""

def __init__(self):
self.root = TreeNode('+')
self.root = TreeNode("+")

def add(self, data):
node = TreeNode(data)
Expand All @@ -45,18 +45,14 @@ def __repr__(self):
def columns2Tree(columns):
tree = Tree()
for column in columns:
if '.' in column:
tree.add_child(
column.split('.')[0],
column.split('.')[1]
)
if "." in column:
tree.add_child(column.split(".")[0], column.split(".")[1])
else:
tree.add(column)
return tree


class BaseModel2SchemaConverter(object):

def __init__(self, datamodel, validators_columns):
"""
:param datamodel: SQLAInterface
Expand Down Expand Up @@ -95,18 +91,22 @@ def _meta_schema_factory(self, columns, model, class_mixin):
"""
_model = model
if columns:

class MetaSchema(ModelSchema, class_mixin):
class Meta:
model = _model
fields = columns
strict = True
sqla_session = self.datamodel.session

else:

class MetaSchema(ModelSchema, class_mixin):
class Meta:
model = _model
strict = True
sqla_session = self.datamodel.session

return MetaSchema

def _column2field(self, datamodel, column, nested=True, enum_dump_by_name=False):
Expand All @@ -124,11 +124,7 @@ def _column2field(self, datamodel, column, nested=True, enum_dump_by_name=False)
required = not datamodel.is_nullable(column.data)
nested_model = datamodel.get_related_model(column.data)
lst = [item.data for item in column.childs]
nested_schema = self.convert(
lst,
nested_model,
nested=False
)
nested_schema = self.convert(lst, nested_model, nested=False)
if datamodel.is_relation_many_to_one(column.data):
many = False
elif datamodel.is_relation_many_to_many(column.data):
Expand All @@ -141,9 +137,10 @@ def _column2field(self, datamodel, column, nested=True, enum_dump_by_name=False)
return field
# Handle bug on marshmallow-sqlalchemy #163
elif datamodel.is_relation(column.data):
if (datamodel.is_relation_many_to_many(column.data) or
datamodel.is_relation_one_to_many(column.data)):
if datamodel.get_info(column.data).get('required', False):
if datamodel.is_relation_many_to_many(
column.data
) or datamodel.is_relation_one_to_many(column.data):
if datamodel.get_info(column.data).get("required", False):
required = True
else:
required = False
Expand All @@ -157,8 +154,7 @@ def _column2field(self, datamodel, column, nested=True, enum_dump_by_name=False)
elif datamodel.is_enum(column.data):
required = not datamodel.is_nullable(column.data)
enum_class = datamodel.list_columns[column.data].info.get(
'enum_class',
datamodel.list_columns[column.data].type
"enum_class", datamodel.list_columns[column.data].type
)
if enum_dump_by_name:
enum_dump_by = EnumField.NAME
Expand All @@ -168,10 +164,10 @@ def _column2field(self, datamodel, column, nested=True, enum_dump_by_name=False)
field.unique = datamodel.is_unique(column.data)
return field
# is custom property method field?
if hasattr(getattr(_model, column.data), 'fget'):
if hasattr(getattr(_model, column.data), "fget"):
return fields.Raw(dump_only=True)
# is a normal model field not a function?
if not hasattr(getattr(_model, column.data), '__call__'):
if not hasattr(getattr(_model, column.data), "__call__"):
field = field_for(_model, column.data)
field.unique = datamodel.is_unique(column.data)
if column.data in self.validators_columns:
Expand All @@ -180,13 +176,13 @@ def _column2field(self, datamodel, column, nested=True, enum_dump_by_name=False)

@staticmethod
def get_column_child_model(column):
if '.' in column:
return column.split('.')[0]
if "." in column:
return column.split(".")[0]
return column

@staticmethod
def is_column_dotted(column):
return '.' in column
return "." in column

def convert(self, columns, model=None, nested=True, enum_dump_by_name=False):
"""
Expand All @@ -198,11 +194,7 @@ def convert(self, columns, model=None, nested=True, enum_dump_by_name=False):
:param nested: Generate relation with nested schemas
:return: ModelSchema object
"""
super(Model2SchemaConverter, self).convert(
columns,
model=model,
nested=nested
)
super(Model2SchemaConverter, self).convert(columns, model=model, nested=nested)

class SchemaMixin:
pass
Expand All @@ -217,10 +209,7 @@ class SchemaMixin:
for column in tree_columns.root.childs:
# Get child model is column is dotted notation
ma_sqla_fields_override[column.data] = self._column2field(
_datamodel,
column,
nested,
enum_dump_by_name
_datamodel, column, nested, enum_dump_by_name
)
_columns.append(column.data)
for k, v in ma_sqla_fields_override.items():
Expand Down
20 changes: 10 additions & 10 deletions flask_appbuilder/api/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@


class OpenApi(BaseApi):
route_base = '/api'
route_base = "/api"
allow_browser_login = True

@expose('/<version>/_openapi')
@expose("/<version>/_openapi")
@protect()
@safe
def get(self, version):
Expand Down Expand Up @@ -56,28 +56,28 @@ def _create_api_spec(version):
openapi_version="3.0.2",
info=dict(description=current_app.appbuilder.app_name),
plugins=[MarshmallowPlugin()],
servers=[{'url': "/api/{}".format(version)}]
servers=[{"url": "/api/{}".format(version)}],
)


class SwaggerView(BaseView):

default_view = 'ui'
openapi_uri = '/api/{}/_openapi'
default_view = "ui"
openapi_uri = "/api/{}/_openapi"

@expose('/<version>')
@expose("/<version>")
@has_access
def show(self, version):
return self.render_template(
'appbuilder/swagger/swagger.html',
openapi_uri=self.openapi_uri.format(version)
"appbuilder/swagger/swagger.html",
openapi_uri=self.openapi_uri.format(version),
)


class OpenApiManager(BaseManager):
def register_views(self):
if not self.appbuilder.app.config.get('FAB_ADD_SECURITY_VIEWS', True):
if not self.appbuilder.app.config.get("FAB_ADD_SECURITY_VIEWS", True):
return
if self.appbuilder.get_app.config.get('FAB_API_SWAGGER_UI', False):
if self.appbuilder.get_app.config.get("FAB_API_SWAGGER_UI", False):
self.appbuilder.add_api(OpenApi)
self.appbuilder.add_view_no_menu(SwaggerView)
8 changes: 4 additions & 4 deletions flask_appbuilder/babel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@


class LocaleView(BaseView):
route_base = '/lang'
route_base = "/lang"

default_view = 'index'
default_view = "index"

@expose('/<string:locale>')
@expose("/<string:locale>")
def index(self, locale):
session['locale'] = locale
session["locale"] = locale
refresh()
self.update_redirect()
return redirect(self.get_redirect())
40 changes: 16 additions & 24 deletions flask_appbuilder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
LOGMSG_ERR_FAB_ADDON_PROCESS,
LOGMSG_INF_FAB_ADD_VIEW,
LOGMSG_INF_FAB_ADDON_ADDED,
LOGMSG_WAR_FAB_VIEW_EXISTS
LOGMSG_WAR_FAB_VIEW_EXISTS,
)
from .filters import TemplateFilters
from .menu import Menu, MenuApiManager
Expand Down Expand Up @@ -109,7 +109,7 @@ def __init__(
static_folder="static/appbuilder",
static_url_path="/appbuilder",
security_manager_class=None,
update_perms=True
update_perms=True,
):
"""
AppBuilder constructor
Expand Down Expand Up @@ -167,42 +167,34 @@ def init_app(self, app, session):

self.app = app

self.base_template = app.config.get(
"FAB_BASE_TEMPLATE",
self.base_template,
)
self.static_folder = app.config.get(
"FAB_STATIC_FOLDER",
self.static_folder,
)
self.base_template = app.config.get("FAB_BASE_TEMPLATE", self.base_template)
self.static_folder = app.config.get("FAB_STATIC_FOLDER", self.static_folder)
self.static_url_path = app.config.get(
"FAB_STATIC_URL_PATH",
self.static_url_path,
"FAB_STATIC_URL_PATH", self.static_url_path
)
_index_view = app.config.get('FAB_INDEX_VIEW', None)
_index_view = app.config.get("FAB_INDEX_VIEW", None)
if _index_view is not None:
self.indexview = dynamic_class_import(
_index_view
)
self.indexview = dynamic_class_import(_index_view)
else:
self.indexview = self.indexview or IndexView
_menu = app.config.get('FAB_MENU', None)
_menu = app.config.get("FAB_MENU", None)
if _menu is not None:
self.menu = dynamic_class_import(
_menu
)
self.menu = dynamic_class_import(_menu)
else:
self.menu = self.menu or Menu()

if self.update_perms: # default is True, if False takes precedence from config
self.update_perms = app.config.get('FAB_UPDATE_PERMS', True)
_security_manager_class_name = app.config.get('FAB_SECURITY_MANAGER_CLASS', None)
self.update_perms = app.config.get("FAB_UPDATE_PERMS", True)
_security_manager_class_name = app.config.get(
"FAB_SECURITY_MANAGER_CLASS", None
)
if _security_manager_class_name is not None:
self.security_manager_class = dynamic_class_import(
_security_manager_class_name
)
if self.security_manager_class is None:
from flask_appbuilder.security.sqla.manager import SecurityManager

self.security_manager_class = SecurityManager

self._addon_managers = app.config["ADDON_MANAGERS"]
Expand Down Expand Up @@ -635,7 +627,7 @@ def _process_inner_views(self):
for inner_class in view.get_uninit_inner_views():
for v in self.baseviews:
if (
isinstance(v, inner_class) and
v not in view.get_init_inner_views()
isinstance(v, inner_class)
and v not in view.get_init_inner_views()
):
view.get_init_inner_views().append(v)
26 changes: 13 additions & 13 deletions flask_appbuilder/charts/jsontools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ def dict_to_json(xcol, ycols, labels, value_columns): # pragma: no cover
"""
json_data = dict()

json_data['cols'] = [{'id': xcol,
'label': as_unicode(labels[xcol]),
'type': 'string'}]
json_data["cols"] = [
{"id": xcol, "label": as_unicode(labels[xcol]), "type": "string"}
]
for ycol in ycols:
json_data['cols'].append({'id': ycol,
'label': as_unicode(labels[ycol]),
'type': 'number'})
json_data['rows'] = []
json_data["cols"].append(
{"id": ycol, "label": as_unicode(labels[ycol]), "type": "number"}
)
json_data["rows"] = []
for value in value_columns:
row = {'c': []}
row = {"c": []}
if isinstance(value[xcol], datetime.date):
row['c'].append({'v': (str(value[xcol]))})
row["c"].append({"v": (str(value[xcol]))})
else:
row['c'].append({'v': (value[xcol])})
row["c"].append({"v": (value[xcol])})
for ycol in ycols:
if value[ycol]:
row['c'].append({'v': (value[ycol])})
row["c"].append({"v": (value[ycol])})
else:
row['c'].append({'v': 0})
json_data['rows'].append(row)
row["c"].append({"v": 0})
json_data["rows"].append(row)
return json_data
6 changes: 3 additions & 3 deletions flask_appbuilder/charts/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@


class ChartWidget(RenderTemplateWidget):
template = 'appbuilder/general/widgets/chart.html'
template = "appbuilder/general/widgets/chart.html"


class DirectChartWidget(RenderTemplateWidget):
template = 'appbuilder/general/widgets/direct_chart.html'
template = "appbuilder/general/widgets/direct_chart.html"


class MultipleChartWidget(RenderTemplateWidget):
template = 'appbuilder/general/widgets/multiple_chart.html'
template = "appbuilder/general/widgets/multiple_chart.html"
Loading