portier/source/config/views/pull.py

77 lines
2.4 KiB
Python
Raw Permalink Normal View History

2024-04-01 16:27:31 +02:00
import logging
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic import ListView, DetailView, CreateView, DeleteView, UpdateView
from guardian.decorators import permission_required_or_403
from guardian.shortcuts import assign_perm
from config import models, forms
logger = logging.getLogger(__name__)
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.add_pull'),
name='dispatch')
@method_decorator(ensure_csrf_cookie, name='dispatch')
class PullList(ListView):
model = models.Pull
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.view_pull',
(models.Pull, 'pk', 'pk')),
name='dispatch')
class PullDetail(DetailView):
model = models.Pull
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.change_pull',
(models.Pull, 'pk', 'pk')),
name='dispatch')
class PullUpdate(UpdateView):
model = models.Pull
form_class = forms.PullFilteredStreamForm
template_name_suffix = '_update_form'
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.add_pull'),
name='dispatch')
class PullCreate(CreateView):
model = models.Pull
form_class = forms.PullFilteredStreamForm
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
def form_valid(self, form):
valid = super().form_valid(form)
if valid:
user = self.request.user
assign_perm('view_pull', user, self.object)
assign_perm('change_pull', user, self.object)
assign_perm('delete_pull', user, self.object)
return valid
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.delete_pull',
(models.Pull, 'pk', 'pk')),
name='dispatch')
class PullDelete(DeleteView):
model = models.Pull
success_url = reverse_lazy('config:pull_list')