From 40a26176eb241453bf51031a072b84e54aa8a58f Mon Sep 17 00:00:00 2001 From: Jonathan Baecker Date: Wed, 29 Jul 2020 09:54:19 +0200 Subject: [PATCH] save playlist only when data is different --- ffplayout/apps/api_player/utils.py | 13 +++++++++++++ ffplayout/apps/api_player/views.py | 9 +++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/ffplayout/apps/api_player/utils.py b/ffplayout/apps/api_player/utils.py index 48905307..2412f2df 100644 --- a/ffplayout/apps/api_player/utils.py +++ b/ffplayout/apps/api_player/utils.py @@ -14,6 +14,8 @@ import zmq from apps.api_player.models import GuiSettings from django.conf import settings from natsort import natsorted +from rest_framework import status +from rest_framework.response import Response def read_yaml(): @@ -53,9 +55,20 @@ def write_json(data): os.makedirs(_path, exist_ok=True) output = os.path.join(_path, '{}.json'.format(data['date'])) + + if os.path.isfile(output) and data == read_json(data['date']): + return Response({ + 'text': 'Playlist from {} already exists'.format(data['date']) + }, status.HTTP_204_NO_CONTENT) + + with open(output, "w") as outfile: json.dump(data, outfile, indent=4) + return Response({ + 'text': 'Playlist from {} saved'.format(data['date']) + }, status.HTTP_201_CREATED) + def read_log(type, _date): config = read_yaml() diff --git a/ffplayout/apps/api_player/views.py b/ffplayout/apps/api_player/views.py index 6f654ae2..2cf267fa 100644 --- a/ffplayout/apps/api_player/views.py +++ b/ffplayout/apps/api_player/views.py @@ -7,7 +7,7 @@ from apps.api_player.serializers import (GuiSettingsSerializer, MessengerSerializer, UserSerializer) from django.contrib.auth.models import User from django_filters import rest_framework as filters -from rest_framework import viewsets +from rest_framework import status, viewsets from rest_framework.parsers import FileUploadParser, JSONParser from rest_framework.response import Response from rest_framework.views import APIView @@ -169,10 +169,11 @@ class Playlist(APIView): def post(self, request, *args, **kwargs): if 'data' in request.data: - write_json(request.data['data']) - return Response(status=200) + _respose = write_json(request.data['data']) + return _respose - return Response(status=400) + return Response({'text': 'Unspecified save error'}, + status=status.HTTP_400_BAD_REQUEST) class Statistics(APIView):