portier/source/config/api/transcodingprofile.py

64 lines
2.1 KiB
Python
Raw Permalink Normal View History

from ninja import Router, ModelSchema, Schema
from ninja.errors import HttpError
from config import models
from typing import List
from guardian.shortcuts import get_objects_for_user, assign_perm
from django.shortcuts import get_object_or_404
router = Router()
class TranscodingProfile(ModelSchema):
class Meta:
model = models.TranscodingProfile
fields = "__all__"
class TranscodingProfilePatch(ModelSchema):
class Meta:
model = models.TranscodingProfile
exclude = ["id"]
fields_optional = "__all__"
extra = "forbid"
@router.get('/', response=List[TranscodingProfile])
def list_transcodingprofile(request):
return get_objects_for_user(request.user, 'view_transcodingprofile', models.TranscodingProfile.objects.all())
@router.get('/{id}', response=TranscodingProfile)
def get_transcodingprofile(request, id: int):
obj = get_object_or_404(models.TranscodingProfile, id=id)
if not request.user.has_perm('view_transcodingprofile', obj):
raise HttpError(401, "unauthorized")
return obj
@router.post('/', response=TranscodingProfile)
def create_transcodingprofile(request, payload: TranscodingProfilePatch):
obj = models.TranscodingProfile.objects.create(**payload.dict())
assign_perm( 'view_transcodingprofile', request.user, obj)
assign_perm('change_transcodingprofile', request.user, obj)
assign_perm('delete_transcodingprofile', request.user, obj)
return obj
@router.patch('/{id}', response=TranscodingProfile)
def patch_transcodingprofile(request, id: int, payload: TranscodingProfilePatch):
obj = get_object_or_404(models.TranscodingProfile, id=id)
if not request.user.has_perm('change_transcodingprofile', obj):
raise HttpError(401, "unauthorized")
for key, value in payload.dict(exclude_unset=True).items():
setattr(obj, key, value)
obj.save()
return obj
@router.delete('/{id}', response=None)
def delete_transcodingprofile(request, id: int):
obj = get_object_or_404(models.TranscodingProfile, id=id)
if not request.user.has_perm('delete_transcodingprofile', obj):
raise HttpError(401, "unauthorized")
obj.delete()