admin: add ?provider for ApplicationCreateView

This commit is contained in:
Jens Langhammer 2021-02-16 20:00:52 +01:00
parent 416d949d80
commit 6bcdf36ca6
4 changed files with 21 additions and 3 deletions

View File

@ -1,4 +1,6 @@
"""authentik Application administration""" """authentik Application administration"""
from typing import Any
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.mixins import ( from django.contrib.auth.mixins import (
PermissionRequiredMixin as DjangoPermissionRequiredMixin, PermissionRequiredMixin as DjangoPermissionRequiredMixin,
@ -7,6 +9,7 @@ from django.contrib.messages.views import SuccessMessageMixin
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.views.generic import UpdateView from django.views.generic import UpdateView
from guardian.mixins import PermissionRequiredMixin from guardian.mixins import PermissionRequiredMixin
from guardian.shortcuts import get_objects_for_user
from authentik.admin.views.utils import BackSuccessUrlMixin, DeleteMessageView from authentik.admin.views.utils import BackSuccessUrlMixin, DeleteMessageView
from authentik.core.forms.applications import ApplicationForm from authentik.core.forms.applications import ApplicationForm
@ -30,6 +33,22 @@ class ApplicationCreateView(
template_name = "generic/create.html" template_name = "generic/create.html"
success_message = _("Successfully created Application") success_message = _("Successfully created Application")
def get_initial(self) -> dict[str, Any]:
if "provider" in self.request.GET:
try:
initial_provider_pk = int(self.request.GET["provider"])
except ValueError:
return super().get_initial()
providers = (
get_objects_for_user(self.request.user, "authentik_core.view_provider")
.filter(pk=initial_provider_pk)
.select_subclasses()
)
if not providers.exists():
return {}
return {"provider": providers.first()}
return super().get_initial()
class ApplicationUpdateView( class ApplicationUpdateView(
SuccessMessageMixin, SuccessMessageMixin,

View File

@ -9,7 +9,7 @@ from authentik.lib.widgets import GroupedModelChoiceField
class ApplicationForm(forms.ModelForm): class ApplicationForm(forms.ModelForm):
"""Application Form""" """Application Form"""
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs): # pragma: no cover
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.fields["provider"].queryset = ( self.fields["provider"].queryset = (
Provider.objects.all().order_by("pk").select_subclasses() Provider.objects.all().order_by("pk").select_subclasses()

View File

@ -64,7 +64,7 @@ class SAMLProviderViewSet(ModelViewSet):
try: try:
metadata = DescriptorDownloadView.get_metadata(request, provider) metadata = DescriptorDownloadView.get_metadata(request, provider)
return Response({"metadata": metadata}) return Response({"metadata": metadata})
except Provider.application.RelatedObjectDoesNotExist: except Provider.application.RelatedObjectDoesNotExist: # pylint: disable=no-member
return Response({"metadata": ""}) return Response({"metadata": ""})

View File

@ -4,7 +4,6 @@ from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.urls.base import reverse_lazy
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views import View from django.views import View
from django.views.generic.edit import FormView from django.views.generic.edit import FormView