portier/source/config/views/stream.py

82 lines
2.6 KiB
Python

import logging
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.admin.utils import NestedObjects
from django.utils.decorators import method_decorator
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_stream'),
name='dispatch')
class StreamList(ListView):
model = models.Stream
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.view_stream',
(models.Stream, 'pk', 'pk')),
name='dispatch')
class StreamDetail(DetailView):
model = models.Stream
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["srs_nodes"] = models.SRSNode.objects.all()
return context
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.change_stream',
(models.Stream, 'pk', 'pk')),
name='dispatch')
class StreamChange(UpdateView):
model = models.Stream
template_name_suffix = '_update_form'
fields = ['name']
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.add_stream'),
name='dispatch')
class StreamCreate(CreateView):
model = models.Stream
fields = ['name']
def form_valid(self, form):
valid = super().form_valid(form)
if valid:
user = self.request.user
assign_perm('view_stream', user, self.object)
assign_perm('change_stream', user, self.object)
assign_perm('delete_stream', user, self.object)
return valid
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.delete_stream',
(models.Stream, 'pk', 'pk')),
name='dispatch')
class StreamDelete(DeleteView):
model = models.Stream
success_url = reverse_lazy('config:stream_list')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
collector = NestedObjects(using='default')
collector.collect([self.object])
context['to_delete'] = collector.nested()
print(context['to_delete'])
return context