add django-rest-framework basic implementation
This commit is contained in:
		
							parent
							
								
									cf6373c7cb
								
							
						
					
					
						commit
						2f5c5b20c5
					
				| 
						 | 
					@ -40,6 +40,7 @@ INSTALLED_APPS = [
 | 
				
			||||||
    'django.contrib.sessions',
 | 
					    'django.contrib.sessions',
 | 
				
			||||||
    'django.contrib.messages',
 | 
					    'django.contrib.messages',
 | 
				
			||||||
    'django.contrib.staticfiles',
 | 
					    'django.contrib.staticfiles',
 | 
				
			||||||
 | 
					    'rest_framework',
 | 
				
			||||||
    'guardian',
 | 
					    'guardian',
 | 
				
			||||||
    'django_registration',
 | 
					    'django_registration',
 | 
				
			||||||
    'bootstrap4',
 | 
					    'bootstrap4',
 | 
				
			||||||
| 
						 | 
					@ -155,6 +156,15 @@ STATICFILES_DIRS = [
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# DRF
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					REST_FRAMEWORK = {
 | 
				
			||||||
 | 
					    'DEFAULT_PERMISSION_CLASSES': [
 | 
				
			||||||
 | 
					        'rest_framework.permissions.DjangoObjectPermissions'
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# E-mail
 | 
					# E-mail
 | 
				
			||||||
DEFAULT_FROM_EMAIL = os.environ.get('EMAIL_FROM')
 | 
					DEFAULT_FROM_EMAIL = os.environ.get('EMAIL_FROM')
 | 
				
			||||||
EMAIL_HOST = os.environ.get('EMAIL_HOST')
 | 
					EMAIL_HOST = os.environ.get('EMAIL_HOST')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,5 +24,6 @@ urlpatterns = [
 | 
				
			||||||
    path('rtmp/', include('rtmp.urls')),
 | 
					    path('rtmp/', include('rtmp.urls')),
 | 
				
			||||||
    path('restream/', include('restream.urls')),
 | 
					    path('restream/', include('restream.urls')),
 | 
				
			||||||
    path('concierge/', include('concierge.urls')),
 | 
					    path('concierge/', include('concierge.urls')),
 | 
				
			||||||
 | 
					    path('api/v1/', include('restapi.urls')),
 | 
				
			||||||
    path('', include('portal.urls')),
 | 
					    path('', include('portal.urls')),
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,3 +6,6 @@ django-fontawesome-5
 | 
				
			||||||
celery>=4.4
 | 
					celery>=4.4
 | 
				
			||||||
gunicorn>=20
 | 
					gunicorn>=20
 | 
				
			||||||
psycopg2-binary
 | 
					psycopg2-binary
 | 
				
			||||||
 | 
					djangorestframework
 | 
				
			||||||
 | 
					django-filter
 | 
				
			||||||
 | 
					djangorestframework-guardian
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,5 @@
 | 
				
			||||||
 | 
					from django.apps import AppConfig
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class RestapiConfig(AppConfig):
 | 
				
			||||||
 | 
					    name = 'restapi'
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,17 @@
 | 
				
			||||||
 | 
					from django.urls import path, include
 | 
				
			||||||
 | 
					from rest_framework import routers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from .views import ApplicationViewSet, StreamViewSet, RestreamConfigViewSet
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					router = routers.DefaultRouter()
 | 
				
			||||||
 | 
					router.register(r'applications', ApplicationViewSet)
 | 
				
			||||||
 | 
					router.register(r'streams', StreamViewSet)
 | 
				
			||||||
 | 
					router.register(r'restreamconfigs', RestreamConfigViewSet)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					app_name = 'restapi'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					urlpatterns = [
 | 
				
			||||||
 | 
					    path('', include(router.urls)),
 | 
				
			||||||
 | 
					    path('auth/', include('rest_framework.urls', namespace='rest_framework'))
 | 
				
			||||||
 | 
					]
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,63 @@
 | 
				
			||||||
 | 
					from rest_framework_guardian.serializers import ObjectPermissionsAssignmentMixin
 | 
				
			||||||
 | 
					from rest_framework import serializers, viewsets
 | 
				
			||||||
 | 
					from rest_framework_guardian import filters
 | 
				
			||||||
 | 
					from rtmp.models import Application, Stream
 | 
				
			||||||
 | 
					from restream.models import RestreamConfig
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class ApplicationSerializer(serializers.HyperlinkedModelSerializer):
 | 
				
			||||||
 | 
					    class Meta:
 | 
				
			||||||
 | 
					        model = Application
 | 
				
			||||||
 | 
					        fields = ['id', 'name']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class ApplicationViewSet(viewsets.ReadOnlyModelViewSet):
 | 
				
			||||||
 | 
					    queryset = Application.objects.all()
 | 
				
			||||||
 | 
					    serializer_class = ApplicationSerializer
 | 
				
			||||||
 | 
					    filter_backends = [filters.ObjectPermissionsFilter]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class StreamSerializer(ObjectPermissionsAssignmentMixin, serializers.ModelSerializer):
 | 
				
			||||||
 | 
					    class Meta:
 | 
				
			||||||
 | 
					        model = Stream
 | 
				
			||||||
 | 
					        fields = ['id', 'stream', 'name', 'application']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get_permissions_map(self, created):
 | 
				
			||||||
 | 
					        current_user = self.context['request'].user
 | 
				
			||||||
 | 
					        return {
 | 
				
			||||||
 | 
					            'view_stream': [current_user],
 | 
				
			||||||
 | 
					            'change_stream': [current_user],
 | 
				
			||||||
 | 
					            'delete_stream': [current_user]
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class StreamViewSet(viewsets.ModelViewSet):
 | 
				
			||||||
 | 
					    queryset = Stream.objects.all()
 | 
				
			||||||
 | 
					    serializer_class = StreamSerializer
 | 
				
			||||||
 | 
					    filter_backends = [filters.ObjectPermissionsFilter]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class RestreamConfigSerializer(ObjectPermissionsAssignmentMixin, serializers.ModelSerializer):
 | 
				
			||||||
 | 
					    class Meta:
 | 
				
			||||||
 | 
					        model = RestreamConfig
 | 
				
			||||||
 | 
					        fields = '__all__'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get_permissions_map(self, created):
 | 
				
			||||||
 | 
					        current_user = self.context['request'].user
 | 
				
			||||||
 | 
					        return {
 | 
				
			||||||
 | 
					            'view_restreamconfig': [current_user],
 | 
				
			||||||
 | 
					            'change_restreamconfig': [current_user],
 | 
				
			||||||
 | 
					            'delete_restreamconfig': [current_user]
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def validate_stream(self, value):
 | 
				
			||||||
 | 
					        request = self.context['request']
 | 
				
			||||||
 | 
					        if not request.user.has_perm('rtmp.view_stream', value):
 | 
				
			||||||
 | 
					            raise serializers.ValidationError('Access to stream is not authorized')
 | 
				
			||||||
 | 
					        return value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class RestreamConfigViewSet(viewsets.ModelViewSet):
 | 
				
			||||||
 | 
					    queryset = RestreamConfig.objects.all()
 | 
				
			||||||
 | 
					    serializer_class = RestreamConfigSerializer
 | 
				
			||||||
 | 
					    filter_backends = [filters.ObjectPermissionsFilter]
 | 
				
			||||||
		Loading…
	
		Reference in New Issue