def get_tutorial_list(request, fossid, langid): """ Retrieve, update or delete a code snippet. """ try: tuts = TutorialResource.objects.filter(Q(status=1) | Q(status=2), language_id=langid, tutorial_detail_id__foss=fossid) except ObjectDoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = VideoSerializer(tuts, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = VideoSerializer(tut, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': tut.delete() return HttpResponse(status=204)
def post(self, request, *args, **kwargs): def create_video_thumbnail(video_obj): video_path = os.path.join(MEDIA_ROOT, video_obj.video.name) with VideoFileClip(video_path, audio=False) as clip: max_duration = int(clip.duration) + 1 print(max_duration) frame_at_second = 3 thumbnail_frame = clip.get_frame(frame_at_second) video_thumbnail = Image.fromarray(thumbnail_frame) thumbnail_path = os.path.join(AZURE_STATIC_ROOT, f"{video}.jpg") video_thumbnail.save(thumbnail_path) # create an ImageFile compatable with Django's ORM/Postgres try: thumbnail_buffer = open(thumbnail_path, "rb") except FileExistsError: raise Exception( "thumbnail file not captured from video properly") thumbnail = File(thumbnail_buffer) video.video_thumbnail.save(f"{video}.jpg", thumbnail) serializer = VideoSerializer(data=request.data) if serializer.is_valid(): video = serializer.save(creator=request.user) serializer.save() create_video_thumbnail(video) return Response(data=serializer.data, status=status.HTTP_201_CREATED) return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def video_list(request): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': snippets = TutorialResource.objects.filter(status=1, id=1) serializer = VideoSerializer(snippets, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = VideoSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def get_tutorial_list(request, fossid, langid): """ Retrieve, update or delete a code snippet. """ try: tuts = TutorialResource.objects.filter(language_id=langid, tutorial_detail_id__foss=fossid,status=1) except ObjectDoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = VideoSerializer(tuts, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = VideoSerializer(tut, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': tut.delete() return HttpResponse(status=204)