authentik/passbook/lib/views.py

42 lines
1.2 KiB
Python
Raw Normal View History

"""passbook helper views"""
from django.http import HttpRequest
from django.template.response import TemplateResponse
from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView
from guardian.shortcuts import assign_perm
class CreateAssignPermView(CreateView):
"""Assign permissions to object after creation"""
permissions = [
2019-12-31 11:51:16 +00:00
"%s.view_%s",
"%s.change_%s",
"%s.delete_%s",
]
def form_valid(self, form):
response = super().form_valid(form)
for permission in self.permissions:
full_permission = permission % (
2019-12-31 11:51:16 +00:00
self.object._meta.app_label,
self.object._meta.model_name,
)
assign_perm(full_permission, self.request.user, self.object)
return response
2020-08-19 09:32:44 +01:00
def bad_request_message(
request: HttpRequest,
message: str,
title="Bad Request",
template="error/generic.html",
2020-08-19 09:32:44 +01:00
) -> TemplateResponse:
"""Return generic error page with message, with status code set to 400"""
return TemplateResponse(
2020-09-30 18:34:22 +01:00
request,
template,
{"message": message, "card_title": _(title)},
status=400,
)