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