From 7240dbce3300ff30999e3730ef9330d6ff93b06c Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 2 Apr 2019 16:26:54 -0700 Subject: refactor release+container editing routes --- python/fatcat_web/editing_routes.py | 122 +++++++++++++++++------------------- 1 file changed, 58 insertions(+), 64 deletions(-) diff --git a/python/fatcat_web/editing_routes.py b/python/fatcat_web/editing_routes.py index db4e22ad..fe2c0722 100644 --- a/python/fatcat_web/editing_routes.py +++ b/python/fatcat_web/editing_routes.py @@ -15,6 +15,32 @@ from fatcat_web.search import * from fatcat_web.forms import * +### Helper Methods ########################################################## + +def form_editgroup_get_or_create(api, edit_form): + """ + This function expects a submitted, validated + """ + if edit_form.editgroup_id.data: + try: + eg = api.get_editgroup(edit_form.editgroup_id.data) + except ApiException as ae: + app.logging.warn(ae) + abort(ae.status) + else: + # if no editgroup, create one from description + try: + eg = api.create_editgroup( + Editgroup(description=edit_form.editgroup_description.data or None)) + except ApiException as ae: + app.logging.warn(ae) + abort(ae.status) + # set this session editgroup_id + session['active_editgroup_id'] = eg.editgroup_id + flash('Started new editgroup {}' \ + .format(eg.editgroup_id, eg.editgroup_id)) + return eg + ### Views ################################################################### @app.route('/container/create', methods=['GET', 'POST']) @@ -25,27 +51,19 @@ def container_create(): if form.validate_on_submit(): # API on behalf of user user_api = auth_api(session['api_token']) - if form.editgroup_id.data: - # TODO: error handling - eg = user_api.get_editgroup(form.editgroup_id.data) - else: - # if no editgroup, create one from description - eg = user_api.create_editgroup( - Editgroup(description=form.editgroup_description.data or None)) - # set this session editgroup_id - session['active_editgroup_id'] = eg.editgroup_id - print(eg.editgroup_id) # XXX: debug - flash('Started new editgroup {}' \ - .format(eg.editgroup_id, eg.editgroup_id)) + eg = form_editgroup_get_or_create(user_api, form) # no merge or anything hard to do; just create the entity entity = form.to_entity() - edit = user_api.create_container(entity, editgroup_id=eg.editgroup_id) + try: + edit = user_api.create_container(entity, editgroup_id=eg.editgroup_id) + except ApiException as ae: + app.logging.warn(ae) + abort(ae.status) # redirect to new entity return redirect('/container/{}'.format(edit.ident)) elif form.errors: - print("user form errors: {}".format(form.errors)) - print("didn't validate...") - if not form.is_submitted(): + app.logger.info("form errors (did not validate): {}".format(form.errors)) + else: editgroup_id = session.get('active_editgroup_id', None) form.editgroup_id.data = editgroup_id return render_template('container_create.html', @@ -55,7 +73,6 @@ def container_create(): @app.route('/container//edit', methods=['GET', 'POST']) def container_edit(ident): # TODO: prev_rev interlock - # TODO: factor out editgroup active/creation stuff try: entity = api.get_container(ident) except ApiException as ae: @@ -65,27 +82,20 @@ def container_edit(ident): if form.validate_on_submit(): # API on behalf of user user_api = auth_api(session['api_token']) - if form.editgroup_id.data: - # TODO: error handling - eg = user_api.get_editgroup(form.editgroup_id.data) - else: - # if no editgroup, create one from description - eg = user_api.create_editgroup( - Editgroup(description=form.editgroup_description.data or None)) - # set this session editgroup_id - session['active_editgroup_id'] = eg.editgroup_id - print(eg.editgroup_id) # XXX: debug - flash('Started new editgroup {}' \ - .format(eg.editgroup_id, eg.editgroup_id)) + eg = form_editgroup_get_or_create(user_api, form) # all the tricky logic is in the update method form.update_entity(entity) - edit = user_api.update_container(entity.ident, entity, - editgroup_id=eg.editgroup_id) + try: + edit = user_api.update_container(entity.ident, entity, + editgroup_id=eg.editgroup_id) + except ApiException as ae: + app.logging.warn(ae) + abort(ae.status) # redirect to entity revision # TODO: container_rev_view return redirect('/container/{}'.format(edit.ident)) elif form.errors: - print("user form errors (didn't validate): {}".format(form.errors)) + app.logger.info("form errors (did not validate): {}".format(form.errors)) else: form = ContainerEntityForm.from_entity(entity) if not form.is_submitted(): @@ -134,25 +144,18 @@ def release_create(): if form.validate_on_submit(): # API on behalf of user user_api = auth_api(session['api_token']) - if form.editgroup_id.data: - # TODO: error handling - eg = user_api.get_editgroup(form.editgroup_id.data) - else: - # if no editgroup, create one from description - eg = user_api.create_editgroup( - Editgroup(description=form.editgroup_description.data or None)) - # set this session editgroup_id - session['active_editgroup_id'] = eg.editgroup_id - flash('Started new editgroup {}' \ - .format(eg.editgroup_id, eg.editgroup_id)) + eg = form_editgroup_get_or_create(user_api, form) # no merge or anything hard to do; just create the entity entity = form.to_entity() - edit = user_api.create_release(entity, editgroup_id=eg.editgroup_id) + try: + edit = user_api.create_release(entity, editgroup_id=eg.editgroup_id) + except ApiException as ae: + app.logging.warn(ae) + abort(ae.status) # redirect to new release return redirect('/release/{}'.format(edit.ident)) elif form.errors: - print("user form errors: {}".format(form.errors)) - print("didn't validate...") + app.logger.info("form errors (did not validate): {}".format(form.errors)) elif len(form.contribs) == 0: form.contribs.append_entry() if not form.is_submitted(): @@ -165,7 +168,6 @@ def release_create(): @app.route('/release//edit', methods=['GET', 'POST']) def release_edit(ident): # TODO: prev_rev interlock - # TODO: factor out editgroup active/creation stuff try: entity = api.get_release(ident) except ApiException as ae: @@ -175,30 +177,22 @@ def release_edit(ident): if form.validate_on_submit(): # API on behalf of user user_api = auth_api(session['api_token']) - if form.editgroup_id.data: - # TODO: error handling - eg = user_api.get_editgroup(form.editgroup_id.data) - else: - # if no editgroup, create one from description - eg = user_api.create_editgroup( - Editgroup(description=form.editgroup_description.data or None)) - # set this session editgroup_id - session['active_editgroup_id'] = eg.editgroup_id - print(eg.editgroup_id) # XXX: debug - flash('Started new editgroup {}' \ - .format(eg.editgroup_id, eg.editgroup_id)) + eg = form_editgroup_get_or_create(user_api, form) # all the tricky logic is in the update method form.update_entity(entity) - edit = user_api.update_release(entity.ident, entity, - editgroup_id=eg.editgroup_id) + try: + edit = user_api.update_release(entity.ident, entity, + editgroup_id=eg.editgroup_id) + except ApiException as ae: + app.logging.warn(ae) + abort(ae.status) # redirect to entity revision # TODO: release_rev_view return redirect('/release/{}'.format(edit.ident)) elif form.errors: - print("user form errors (didn't validate): {}".format(form.errors)) - else: + app.logger.info("form errors (did not validate): {}".format(form.errors)) + else: # not submitted form = ReleaseEntityForm.from_entity(entity) - if not form.is_submitted(): editgroup_id = session.get('active_editgroup_id', None) form.editgroup_id.data = editgroup_id return render_template('release_edit.html', -- cgit v1.2.3