-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1874] Enable selection of company branch at eHerkenning login
- Loading branch information
Showing
17 changed files
with
268 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django import forms | ||
|
||
|
||
class CompanyBranchChoiceForm(forms.Form): | ||
branch_number = forms.CharField(widget=forms.HiddenInput()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import logging | ||
|
||
from django.http import HttpResponseRedirect | ||
from django.urls import NoReverseMatch, reverse | ||
|
||
from furl import furl | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class KvKLoginMiddleware: | ||
"""Redirect authenticated eHerkenning users to select a company branch""" | ||
|
||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
user = request.user | ||
|
||
# pass through | ||
if ( | ||
not user.is_authenticated | ||
or not user.kvk | ||
or request.session.get("KVK_BRANCH_NUMBER") | ||
): | ||
return self.get_response(request) | ||
|
||
# let the user logout and avoid redirect circles | ||
try: | ||
logout = reverse("logout") | ||
eherkenning_logout = reverse("eherkenning:logout") | ||
registration = reverse("profile:registration_necessary") | ||
branches = reverse("kvk:branches") | ||
except NoReverseMatch: | ||
logout = "/logout/" | ||
eherkenning_logout = "/eherkenning/logout/" | ||
registration = "/mijn-profiel/register/necessary/" | ||
branches = "/kvk/branches/" | ||
|
||
if request.path.startswith( | ||
(logout, eherkenning_logout, registration, branches) | ||
): | ||
return self.get_response(request) | ||
|
||
# redirect to company branch choice, passing on next url | ||
redirect = furl(reverse("kvk:branches")) | ||
redirect.args.update(request.GET) | ||
|
||
return HttpResponseRedirect(redirect.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.urls import path | ||
|
||
from .views import CompanyBranchChoiceView | ||
|
||
app_name = "kvk" | ||
|
||
urlpatterns = [ | ||
path("branches/", CompanyBranchChoiceView.as_view(), name="branches"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from django.http import HttpResponseRedirect | ||
from django.shortcuts import render | ||
from django.urls import reverse | ||
from django.views.generic import FormView | ||
|
||
from .client import KvKClient | ||
from .forms import CompanyBranchChoiceForm | ||
|
||
|
||
class CompanyBranchChoiceView(FormView): | ||
"""Choose the branch ("vestiging") of a company""" | ||
|
||
template_name = "pages/kvk/branches.html" | ||
form_class = CompanyBranchChoiceForm | ||
|
||
def get_context_data(self, **kwargs): | ||
context = super().get_context_data(**kwargs) | ||
|
||
kvk_client = KvKClient() | ||
|
||
company_branches = kvk_client.get_all_company_branches( | ||
kvk=self.request.user.kvk | ||
) | ||
form = self.get_form() | ||
target_url = self.request.GET.get("next") or reverse("pages-root") | ||
|
||
context.update( | ||
company_branches=company_branches, | ||
form=form, | ||
target_url=target_url, | ||
) | ||
|
||
return context | ||
|
||
def get(self, request): | ||
context = self.get_context_data() | ||
|
||
target_url = context["target_url"] | ||
company_branches = context["company_branches"] | ||
|
||
if not company_branches: | ||
return HttpResponseRedirect(target_url) | ||
|
||
if len(company_branches) == 1: | ||
request.session["KVK_BRANCH_NUMBER"] = ( | ||
company_branches[0].get("vestigingsnummer") or request.user.kvk | ||
) | ||
return HttpResponseRedirect(target_url) | ||
|
||
form = context["form"] | ||
|
||
return render( | ||
request, | ||
self.template_name, | ||
{ | ||
"company_branches": company_branches, | ||
"form": form, | ||
}, | ||
) | ||
|
||
def post(self, request): | ||
form = self.get_form() | ||
|
||
if form.is_valid(): | ||
context = self.get_context_data() | ||
target_url = context["target_url"] | ||
|
||
cleaned = form.cleaned_data | ||
branch_number = cleaned["branch_number"] | ||
|
||
request.session["KVK_BRANCH_NUMBER"] = branch_number | ||
|
||
return HttpResponseRedirect(target_url) | ||
|
||
return super().form_invalid(form) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.