Skip to content

Commit

Permalink
DRY and optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro committed Mar 5, 2023
1 parent 5b96269 commit d566dc8
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

Base = declarative_base()

ORIENTATION = "horizontal"
CHART_TYPE = "echarts_timeseries_bar"


class Slice(Base):
"""Declarative class to do query in upgrade"""
Expand All @@ -55,9 +58,10 @@ def upgrade():
session.query(Slice)
.filter(
and_(
Slice.viz_type == "echarts_timeseries_bar",
Slice.viz_type == CHART_TYPE,
Slice.params.like("%x_axis_sort%"),
Slice.params.like("%x_axis_sort_asc%"),
Slice.params.like(f"%{ORIENTATION}%"),
)
)
.all()
Expand All @@ -69,9 +73,9 @@ def upgrade():
orientation = params.get("orientation")
x_axis_sort = params.get("x_axis_sort")
x_axis_sort_asc = params.get("x_axis_sort_asc", None)
if orientation == "horizontal" and x_axis_sort:
if orientation == ORIENTATION and x_axis_sort:
changes += 1
params["x_axis_sort_asc"] = False if x_axis_sort else True
params["x_axis_sort_asc"] = not x_axis_sort_asc
slc.params = json.dumps(params, sort_keys=True)
except Exception as e:
print(e)
Expand All @@ -92,9 +96,10 @@ def downgrade():
session.query(Slice)
.filter(
and_(
Slice.viz_type == "echarts_timeseries_bar",
Slice.viz_type == CHART_TYPE,
Slice.params.like("%x_axis_sort%"),
Slice.params.like("%x_axis_sort_asc%"),
Slice.params.like(f"%{ORIENTATION}%"),
)
)
.all()
Expand All @@ -106,9 +111,9 @@ def downgrade():
orientation = params.get("orientation")
x_axis_sort = params.get("x_axis_sort")
x_axis_sort_asc = params.pop("x_axis_sort_asc", None)
if orientation == "horizontal" and x_axis_sort:
if orientation == ORIENTATION and x_axis_sort:
changes += 1
params["x_axis_sort_asc"] = False if x_axis_sort else True
params["x_axis_sort_asc"] = not x_axis_sort_asc
slc.params = json.dumps(params, sort_keys=True)
except Exception as e:
print(e)
Expand Down

0 comments on commit d566dc8

Please sign in to comment.