Make "state_url" optional in the "extended_json" function since creating an app context causes SQLAlchemy issues on the backend

This commit is contained in:
mprahl
2017-11-17 12:00:33 -05:00
parent 88d655d099
commit 9890277620
3 changed files with 28 additions and 8 deletions

View File

@@ -399,8 +399,17 @@ class ModuleBuild(MBSBase):
'tasks': self.tasks(),
}
def extended_json(self):
def extended_json(self, show_state_url=False):
"""
:kwarg show_state_url: this will determine if `get_url_for` should be run to determine
what the `state_url` is. This should be set to `False` when extended_json is called from
the backend because it forces an app context to be created, which causes issues with
SQLAlchemy sessions.
"""
json = self.json()
state_url = None
if show_state_url:
state_url = get_url_for('module_build', id=self.id)
json.update({
# TODO, show their entire .json() ?
'component_builds': [build.id for build in self.component_builds],
@@ -411,7 +420,7 @@ class ModuleBuild(MBSBase):
'reason': record.state_reason}
for record
in self.state_trace(self.id)],
'state_url': get_url_for('module_build', id=self.id)
'state_url': state_url
})
return json
@@ -553,15 +562,25 @@ class ComponentBuild(MBSBase):
return retval
def extended_json(self):
def extended_json(self, show_state_url=False):
"""
:kwarg show_state_url: this will determine if `get_url_for` should be run to determine
what the `state_url` is. This should be set to `False` when extended_json is called from
the backend because it forces an app context to be created, which causes issues with
SQLAlchemy sessions.
"""
json = self.json()
state_url = None
if show_state_url:
state_url = get_url_for('component_build', id=self.id)
json.update({
'state_trace': [{'time': _utc_datetime_to_iso(record.state_time),
'state': record.state,
'state_name': INVERSE_BUILD_STATES[record.state],
'reason': record.state_reason}
for record
in self.state_trace(self.id)]
in self.state_trace(self.id)],
'state_url': state_url
})
return json

View File

@@ -103,7 +103,7 @@ class AbstractQueryableBuildAPI(MethodView):
}
if verbose_flag == 'true' or verbose_flag == '1':
json_data['items'] = [item.extended_json() for item in p_query.items]
json_data['items'] = [item.extended_json(True) for item in p_query.items]
else:
json_data['items'] = [item.json() for item in p_query.items]
@@ -113,7 +113,7 @@ class AbstractQueryableBuildAPI(MethodView):
instance = self.model.query.filter_by(id=id).first()
if instance:
if verbose_flag == 'true' or verbose_flag == '1':
return jsonify(instance.extended_json()), 200
return jsonify(instance.extended_json(True)), 200
else:
return jsonify(instance.json()), 200
else:
@@ -148,7 +148,7 @@ class ModuleBuildAPI(AbstractQueryableBuildAPI):
handler.validate()
module = handler.post()
return jsonify(module.extended_json()), 201
return jsonify(module.extended_json(True)), 201
def patch(self, id):
username, groups = module_build_service.auth.get_user(request)
@@ -196,7 +196,7 @@ class ModuleBuildAPI(AbstractQueryableBuildAPI):
db.session.add(module)
db.session.commit()
return jsonify(module.extended_json()), 200
return jsonify(module.extended_json(True)), 200
class AboutAPI(MethodView):

View File

@@ -324,6 +324,7 @@ class TestViews(unittest.TestCase):
self.assertTrue(data['state_trace'][0]['time'] is not None)
self.assertEquals(data['state_trace'][0]['state'], 1)
self.assertEquals(data['state_trace'][0]['state_name'], 'wait')
self.assertEquals(data['state_url'], '/module-build-service/1/component-builds/3')
component_builds_filters = ['tagged', 'ref', 'format']