2020-04-15 20:29:59 +02:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
2020-04-29 18:48:23 +02:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2020-04-15 20:29:59 +02:00
|
|
|
from django.http import HttpResponse
|
2020-04-29 18:48:23 +02:00
|
|
|
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
|
2020-04-15 20:29:59 +02:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2020-04-29 18:48:23 +02:00
|
|
|
from django.views.generic import ListView, DetailView, CreateView, DeleteView
|
2020-05-01 12:38:57 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-04-20 14:51:44 +02:00
|
|
|
|
2020-04-15 20:29:59 +02:00
|
|
|
@csrf_exempt
|
2020-04-23 21:08:39 +02:00
|
|
|
def callback_srs(request):
|
2020-04-15 20:29:59 +02:00
|
|
|
if request.method != 'POST':
|
|
|
|
return HttpResponse('1', status=405)
|
|
|
|
|
2020-04-23 21:08:39 +02:00
|
|
|
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']
|
|
|
|
stream_name = json_data['stream']
|
2020-04-23 21:08:39 +02:00
|
|
|
param = json_data['param']
|
2020-04-15 20:29:59 +02:00
|
|
|
except KeyError:
|
|
|
|
return HttpResponse('1', status=401)
|
|
|
|
try:
|
2020-04-20 18:55:45 +02:00
|
|
|
application = models.Application.objects.get(name=app_name)
|
2020-04-23 21:08:39 +02:00
|
|
|
stream = models.Stream.objects.get(stream=stream_name, application=application)
|
2020-04-15 20:29:59 +02:00
|
|
|
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
return HttpResponse('1', status=401)
|
|
|
|
|
|
|
|
if json_data.get('action') == 'on_publish':
|
2020-04-23 21:08:39 +02:00
|
|
|
stream.on_publish(param=param)
|
2020-04-15 20:29:59 +02:00
|
|
|
|
|
|
|
if json_data.get('action') == 'on_unpublish':
|
2020-04-23 21:08:39 +02:00
|
|
|
stream.on_unpublish(param=param)
|
2020-04-15 20:29:59 +02:00
|
|
|
|
|
|
|
return HttpResponse('0')
|
2020-04-29 18:48:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
2020-05-01 12:38:57 +02:00
|
|
|
@method_decorator(permission_required_or_403('rtmp.add_stream'),
|
|
|
|
name='dispatch')
|
2020-04-29 18:48:23 +02:00
|
|
|
class StreamList(ListView):
|
|
|
|
model = models.Stream
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
2020-05-01 12:38:57 +02:00
|
|
|
@method_decorator(permission_required_or_403('rtmp.view_stream',
|
|
|
|
(models.Stream, 'pk', 'pk')),
|
|
|
|
name='dispatch')
|
2020-04-29 18:48:23 +02:00
|
|
|
class StreamDetail(DetailView):
|
|
|
|
model = models.Stream
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
2020-05-01 12:38:57 +02:00
|
|
|
@method_decorator(permission_required_or_403('rtmp.add_stream'),
|
|
|
|
name='dispatch')
|
2020-04-29 18:48:23 +02:00
|
|
|
class StreamCreate(CreateView):
|
|
|
|
model = models.Stream
|
|
|
|
fields = ["name", "application"]
|
|
|
|
|
2020-05-01 12:38:57 +02:00
|
|
|
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
|
|
|
|
|
2020-04-29 18:48:23 +02:00
|
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
2020-05-01 12:38:57 +02:00
|
|
|
@method_decorator(permission_required_or_403('rtmp.delete_stream',
|
|
|
|
(models.Stream, 'pk', 'pk')),
|
|
|
|
name='dispatch')
|
2020-04-29 18:48:23 +02:00
|
|
|
class StreamDelete(DeleteView):
|
|
|
|
model = models.Stream
|
|
|
|
success_url = reverse_lazy('rtmp: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
|