portier/source/config/views.py

182 lines
5.9 KiB
Python
Raw Normal View History

2020-04-15 20:29:59 +02:00
import json
import logging
from django.core.exceptions import ObjectDoesNotExist
2020-04-15 20:29:59 +02:00
from django.http import HttpResponse
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.decorators.csrf import csrf_exempt, 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
2020-04-15 20:29:59 +02:00
from . import models, forms
2020-04-15 20:29:59 +02:00
logger = logging.getLogger(__name__)
2020-04-20 14:51:44 +02:00
2020-04-15 20:29:59 +02:00
@csrf_exempt
def callback_srs(request):
2020-04-15 20:29:59 +02:00
if request.method != 'POST':
return HttpResponse('1', status=405)
try:
json_data = json.loads(request.body)
except json.decoder.JSONDecodeError:
return HttpResponse('1', status=400)
2020-04-15 20:29:59 +02:00
try:
app_name = json_data['app']
# QUIRK this is a weird bug when pushing from OME to SRS. wtf.
# for some reason srs interprets the incoming app as app/stream, and passes this on to portier.
# only keep the stuff infront of a (potential) slash, and throw away the rest. problem solved^tm
app_name = app_name.split('/')[0]
# ENDQUIRK
2020-04-15 20:29:59 +02:00
stream_name = json_data['stream']
param = json_data['param']
2020-04-15 20:29:59 +02:00
except KeyError:
return HttpResponse('1', status=401)
try:
stream = models.Stream.objects.get(stream=stream_name)
2020-04-15 20:29:59 +02:00
except ObjectDoesNotExist:
return HttpResponse('1', status=401)
if json_data.get('action') == 'on_publish':
stream.on_publish(param=param)
2020-04-15 20:29:59 +02:00
if json_data.get('action') == 'on_unpublish':
stream.on_unpublish(param=param)
2020-04-15 20:29:59 +02:00
return HttpResponse('0')
@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
@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'
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_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
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.add_restream'),
name='dispatch')
@method_decorator(ensure_csrf_cookie, name='dispatch')
class RestreamList(ListView):
model = models.Restream
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.view_restream',
(models.Restream, 'pk', 'pk')),
name='dispatch')
class RestreamDetail(DetailView):
model = models.Restream
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.change_restream',
(models.Restream, 'pk', 'pk')),
name='dispatch')
class RestreamUpdate(UpdateView):
model = models.Restream
form_class = forms.RestreamFilteredStreamForm
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_restream'),
name='dispatch')
class RestreamCreate(CreateView):
model = models.Restream
form_class = forms.RestreamFilteredStreamForm
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_restream', user, self.object)
assign_perm('change_restream', user, self.object)
assign_perm('delete_restream', user, self.object)
return valid
@method_decorator(login_required, name='dispatch')
@method_decorator(permission_required_or_403('config.delete_restream',
(models.Restream, 'pk', 'pk')),
name='dispatch')
class RestreamDelete(DeleteView):
model = models.Restream
success_url = reverse_lazy('config:restream_list')