From 441f04ce97d12fd6b5f6cad9e23afe7080939092 Mon Sep 17 00:00:00 2001 From: Elliot Smith Date: Fri, 4 Sep 2015 10:37:47 +0100 Subject: bitbake: toaster: Simplify redirects when build page parameters are missing A RedirectException is used to redirect the client to the correct page when their original request is missing the required parameters (page, orderby etc.). However, the code is difficult to follow. Rather than catching RedirectExceptions and rethrowing them with different view URLs, ensure that the RedirectException has the correct URL in it when thrown by passing the original page name to the _build_list_helper() method. Modified from an original patch by David Reyna . (Bitbake rev: 38f935647dd768a912b933adebfc9cb225a01a54) Signed-off-by: Elliot Smith Signed-off-by: brian avery Signed-off-by: Richard Purdie --- bitbake/lib/toaster/toastergui/views.py | 49 +++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 17 deletions(-) (limited to 'bitbake') diff --git a/bitbake/lib/toaster/toastergui/views.py b/bitbake/lib/toaster/toastergui/views.py index 67c84b2934..d9802f01b8 100755 --- a/bitbake/lib/toaster/toastergui/views.py +++ b/bitbake/lib/toaster/toastergui/views.py @@ -1897,6 +1897,10 @@ if True: pass # shows the "all builds" page for managed mode; it displays build requests (at least started!) instead of actual builds + # WARNING _build_list_helper() may raise a RedirectException, which + # will set the GET parameters and redirect back to the + # all-builds or projectbuilds page as appropriate; + # TODO don't use exceptions to control program flow @_template_renderer("builds.html") def builds(request): # define here what parameters the view needs in the GET portion in order to @@ -1905,27 +1909,35 @@ if True: queryset = Build.objects.all() - try: - context, pagesize, orderby = _build_list_helper(request, queryset) - # all builds page as a Project column - context['tablecols'].append({'name': 'Project', 'clcalss': 'project_column', }) - except RedirectException as re: - # rewrite the RedirectException - re.view = resolve(request.path_info).url_name - raise re + redirect_page = resolve(request.path_info).url_name + + context, pagesize, orderby = _build_list_helper(request, + queryset, + redirect_page) + # all builds page as a Project column + context['tablecols'].append({ + 'name': 'Project', + 'clclass': 'project_column' + }) _set_parameters_values(pagesize, orderby, request) return context # helper function, to be used on "all builds" and "project builds" pages - def _build_list_helper(request, queryset_all): + def _build_list_helper(request, queryset_all, redirect_page, pid=None): default_orderby = 'completed_on:-' (pagesize, orderby) = _get_parameters_values(request, 10, default_orderby) mandatory_parameters = { 'count': pagesize, 'page' : 1, 'orderby' : orderby } retval = _verify_parameters( request.GET, mandatory_parameters ) if retval: - raise RedirectException( None, request.GET, mandatory_parameters) + params = {} + if pid: + params = {'pid': pid} + raise RedirectException(redirect_page, + request.GET, + mandatory_parameters, + **params) # boilerplate code that takes a request for an object type and returns a queryset # for that object type. copypasta for all needed table searches @@ -2669,6 +2681,10 @@ if True: return context + # WARNING _build_list_helper() may raise a RedirectException, which + # will set the GET parameters and redirect back to the + # all-builds or projectbuilds page as appropriate; + # TODO don't use exceptions to control program flow @_template_renderer('projectbuilds.html') def projectbuilds(request, pid): prj = Project.objects.get(id = pid) @@ -2708,13 +2724,12 @@ if True: queryset = Build.objects.filter(project_id = pid) - try: - context, pagesize, orderby = _build_list_helper(request, queryset) - except RedirectException as re: - # rewrite the RedirectException with our current url information - re.view = resolve(request.path_info).url_name - re.okwargs = {"pid" : pid} - raise re + redirect_page = resolve(request.path_info).url_name + + context, pagesize, orderby = _build_list_helper(request, + queryset, + redirect_page, + pid) context['project'] = prj _set_parameters_values(pagesize, orderby, request) -- cgit v1.2.3-54-g00ecf