Skip to content

Commit

Permalink
removed querystring parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-wm-arthur committed Jun 7, 2021
1 parent 1d2e829 commit b010b49
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
34 changes: 3 additions & 31 deletions dolt/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,21 @@

from dolt.constants import (
DOLT_BRANCH_KEYWORD,
DOLT_VERSIONED_URL_PREFIXES,
DOLT_DEFAULT_BRANCH,
)
from dolt.context_managers import AutoDoltCommit
from dolt.models import Branch


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)
Expand All @@ -62,9 +35,8 @@ def process_view(self, request, view_func, view_args, view_kwargs):
f"""<div class="text-center">branch not found: {branch}</div>"""
),
)
# verify the active branch
active = Branch.active_branch()
# inject the "active branch" banner
active = Branch.active_branch()
messages.info(
request,
mark_safe(f"""<div class="text-center">active branch: {active}</div>"""),
Expand Down
2 changes: 1 addition & 1 deletion dolt/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Active
</div>
{% else %}
<a href="/?branch={{ record.name }}" class="btn btn-xs btn-primary" title="checkout">
<a href="{% url 'plugins:dolt:branch_checkout' pk=record.pk %}" class="btn btn-xs btn-primary" title="checkout">
Checkout
</a>
{% endif %}
Expand Down
5 changes: 5 additions & 0 deletions dolt/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
name="branch_merge_preview",
),
path("branches/<str:pk>/", views.BranchView.as_view(), name="branch"),
path(
"branches/<str:pk>/checkout/",
views.BranchCheckoutView.as_view(),
name="branch_checkout",
),
path("branches/<str:pk>/edit/", views.BranchEditView.as_view(), name="branch_edit"),
path(
"branches/<str:pk>/delete/",
Expand Down
12 changes: 11 additions & 1 deletion dolt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b010b49

Please sign in to comment.