diff --git a/dolt/middleware.py b/dolt/middleware.py index 5fb8d5b..6a6bf5d 100644 --- a/dolt/middleware.py +++ b/dolt/middleware.py @@ -7,7 +7,6 @@ from dolt.constants import ( DOLT_BRANCH_KEYWORD, - DOLT_VERSIONED_URL_PREFIXES, DOLT_DEFAULT_BRANCH, ) from dolt.context_managers import AutoDoltCommit @@ -15,40 +14,14 @@ class DoltBranchMiddleware: + # DOLT_BRANCH_KEYWORD = "branch" + def __init__(self, get_response): self.get_response = get_response def __call__(self, request): - # check for a `branch` query string param - branch = request.GET.get(DOLT_BRANCH_KEYWORD, None) - - if branch: - # update the session cookie with the active branch - request.session[DOLT_BRANCH_KEYWORD] = branch - elif self._is_vcs_route(request): - # route is under version control, but no branch was specified, - # lookup the active branch in the session cookie. - branch = request.session.get(DOLT_BRANCH_KEYWORD, DOLT_DEFAULT_BRANCH) - # provide the `branch` query string param and redirect - return redirect(f"{request.path}?{DOLT_BRANCH_KEYWORD}={branch}") - return self.get_response(request) - @staticmethod - def _is_vcs_route(request): - """ - Determines whether the requested page is under version-control - and needs to be redirected to read from a specific branch. - """ - if request.GET.get(DOLT_BRANCH_KEYWORD, None): - # if a branch is already specified in the - # query string, don't redirect - return False - - return ( - request.path.startswith(DOLT_VERSIONED_URL_PREFIXES) or request.path == "/" - ) - def process_view(self, request, view_func, view_args, view_kwargs): # lookup the active branch in the session cookie branch = request.session.get(DOLT_BRANCH_KEYWORD, DOLT_DEFAULT_BRANCH) @@ -62,9 +35,8 @@ def process_view(self, request, view_func, view_args, view_kwargs): f"""
branch not found: {branch}
""" ), ) - # verify the active branch - active = Branch.active_branch() # inject the "active branch" banner + active = Branch.active_branch() messages.info( request, mark_safe(f"""
active branch: {active}
"""), diff --git a/dolt/tables.py b/dolt/tables.py index 7fb47de..a76711a 100644 --- a/dolt/tables.py +++ b/dolt/tables.py @@ -21,7 +21,7 @@ Active {% else %} - + Checkout {% endif %} diff --git a/dolt/urls.py b/dolt/urls.py index a5a77c3..2539f1f 100644 --- a/dolt/urls.py +++ b/dolt/urls.py @@ -23,6 +23,11 @@ name="branch_merge_preview", ), path("branches//", views.BranchView.as_view(), name="branch"), + path( + "branches//checkout/", + views.BranchCheckoutView.as_view(), + name="branch_checkout", + ), path("branches//edit/", views.BranchEditView.as_view(), name="branch_edit"), path( "branches//delete/", diff --git a/dolt/views.py b/dolt/views.py index 4ad1b02..c84806c 100644 --- a/dolt/views.py +++ b/dolt/views.py @@ -6,7 +6,6 @@ from django.views import View from nautobot.core.views import generic -from nautobot.utilities.utils import normalize_querydict from nautobot.utilities.views import GetReturnURLMixin from dolt import filters, forms, tables @@ -75,6 +74,17 @@ class BranchListView(generic.ObjectListView): template_name = "dolt/branch_list.html" +class BranchCheckoutView(View): + queryset = Branch.objects.all() + model_form = forms.BranchForm + template_name = "dolt/branch_edit.html" + + def get(self, request, *args, **kwargs): + # new branch will be checked out on redirect + request.session[DOLT_BRANCH_KEYWORD] = kwargs["pk"] + return redirect("/") + + class BranchEditView(generic.ObjectEditView): queryset = Branch.objects.all() model_form = forms.BranchForm