mozilla-releng/balrog

stop using MethodViews in flask apps

Closed this issue · 3 comments

Plain functions are just fine.

(Imported from https://bugzilla.mozilla.org/show_bug.cgi?id=1508137, filed by @allan-silva)

@bhearsum I have some clarifying questions for this issue.

  1. Does this mean that in a function such as:

def scheduled_changes():
view = ScheduledChangesView("emergency_shutoff", dbo.emergencyShutoffs)
return view.get()

Instead we would have the get function that is in the ScheduledChangeView:

def get(self, where=None):
if where is None:
where = {}
if connexion.request.args.get("all") is None:
where["complete"] = False
rows = self.sc_table.select(where=where)
ret = {"count": len(rows), "scheduled_changes": []}
for row in rows:
ret["scheduled_changes"].append(add_signoff_information(row, self.table, self.sc_table))
return jsonify(ret)

  1. How would we deal with the base views that the views inherit from i.e. AdminView and HistoryView

To answer the second question first -- any code that is shared by multiple views would need to be refactored to be a plain function (not a class method), which the new view functions could then call. This is actually what is already happening in emergency_shutoffs.py. It is not happening in files like

class RulesAPIView(AdminView):
, where we have a number of classes with methods on them that implement various endpoints.

(This code is connected to an endpoint name through

operationId: auslib.web.admin.views.mapper.scheduled_change_rules_get_by_id
via https://github.com/mozilla-releng/balrog/blob/ab3882c1c040770dfb779155092fe3d468571ea5/src/auslib/web/admin/views/mapper.py.)

@bhearsum Thank you for the detailed explanation on my second question. After going through the linked files again, I now understand how plain functions would work as the base in place of the base views.