2019-10-10 09:45:51 +01:00
|
|
|
"""passbook helper views"""
|
|
|
|
|
|
|
|
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 = [
|
|
|
|
'%s.view_%s',
|
|
|
|
'%s.change_%s',
|
|
|
|
'%s.delete_%s',
|
|
|
|
]
|
2019-10-10 09:45:51 +01: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 % (
|
|
|
|
self.object._meta.app_label, self.object._meta.model_name)
|
|
|
|
print(full_permission)
|
|
|
|
assign_perm(full_permission, self.request.user, self.object)
|
2019-10-10 09:45:51 +01:00
|
|
|
return response
|