From e92a131e4cd1a0cc29bceaf457e28e027d4bebdc Mon Sep 17 00:00:00 2001 From: Jan Koppe Date: Fri, 1 May 2020 16:06:32 +0200 Subject: [PATCH] add default rtmp application, filter by rtmp application permissions --- portier/settings.py | 1 + rtmp/forms.py | 16 ++++++++++++++++ .../commands/createdefaultapplication.py | 16 ++++++++++++++++ rtmp/views.py | 15 +++++++++++++-- 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 rtmp/forms.py create mode 100644 rtmp/management/commands/createdefaultapplication.py diff --git a/portier/settings.py b/portier/settings.py index 76b1b8e..c2aa042 100644 --- a/portier/settings.py +++ b/portier/settings.py @@ -28,6 +28,7 @@ ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", default="*").split(" ") DEFAULT_GROUP = 'default' +DEFAULT_RTMP_APPPLICATION = 'live' # Application definition diff --git a/rtmp/forms.py b/rtmp/forms.py new file mode 100644 index 0000000..80e381b --- /dev/null +++ b/rtmp/forms.py @@ -0,0 +1,16 @@ +from django.forms import ModelForm +from guardian.shortcuts import get_objects_for_user +from . import models + + +class StreamFilteredApplicationForm(ModelForm): + class Meta: + model = models.Stream + fields = ['name', 'application'] + + def __init__(self, *args, **kwargs): + user = kwargs.pop('user', None) + super().__init__(*args, **kwargs) + + # limit the stream selection to user-accessible streams + self.fields['application'].queryset = get_objects_for_user(user, 'rtmp.view_application') diff --git a/rtmp/management/commands/createdefaultapplication.py b/rtmp/management/commands/createdefaultapplication.py new file mode 100644 index 0000000..0e77a56 --- /dev/null +++ b/rtmp/management/commands/createdefaultapplication.py @@ -0,0 +1,16 @@ +from django.core.management.base import BaseCommand +from django.contrib.auth.models import Group +from django.conf import settings +from guardian.shortcuts import assign_perm + +from rtmp import models + + +class Command(BaseCommand): + help = 'Creates a default RTMP application that is available to all users in the default group' + + def handle(self, *args, **options): + default_group, _ = Group.objects.get_or_create(name=settings.DEFAULT_GROUP) + default_app, _ = models.Application.objects.get_or_create(name=settings.DEFAULT_RTMP_APPPLICATION) + + assign_perm('view_application', default_group, default_app) diff --git a/rtmp/views.py b/rtmp/views.py index afc9df2..d96ab60 100644 --- a/rtmp/views.py +++ b/rtmp/views.py @@ -13,6 +13,7 @@ from guardian.decorators import permission_required_or_403 from guardian.shortcuts import assign_perm from . import models +from . import forms logger = logging.getLogger(__name__) @@ -70,16 +71,26 @@ class StreamDetail(DetailView): name='dispatch') class StreamChange(UpdateView): model = models.Stream - fields = ['application', 'name'] + form_class = forms.StreamFilteredApplicationForm 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('rtmp.add_stream'), name='dispatch') class StreamCreate(CreateView): model = models.Stream - fields = ["name", "application"] + form_class = forms.StreamFilteredApplicationForm + + 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)