save playlist only when data is different

This commit is contained in:
Jonathan Baecker 2020-07-29 09:54:19 +02:00
parent 21ca4b7d11
commit 40a26176eb
2 changed files with 18 additions and 4 deletions

View File

@ -14,6 +14,8 @@ import zmq
from apps.api_player.models import GuiSettings from apps.api_player.models import GuiSettings
from django.conf import settings from django.conf import settings
from natsort import natsorted from natsort import natsorted
from rest_framework import status
from rest_framework.response import Response
def read_yaml(): def read_yaml():
@ -53,9 +55,20 @@ def write_json(data):
os.makedirs(_path, exist_ok=True) os.makedirs(_path, exist_ok=True)
output = os.path.join(_path, '{}.json'.format(data['date'])) 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: with open(output, "w") as outfile:
json.dump(data, outfile, indent=4) json.dump(data, outfile, indent=4)
return Response({
'text': 'Playlist from {} saved'.format(data['date'])
}, status.HTTP_201_CREATED)
def read_log(type, _date): def read_log(type, _date):
config = read_yaml() config = read_yaml()

View File

@ -7,7 +7,7 @@ from apps.api_player.serializers import (GuiSettingsSerializer,
MessengerSerializer, UserSerializer) MessengerSerializer, UserSerializer)
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django_filters import rest_framework as filters 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.parsers import FileUploadParser, JSONParser
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.views import APIView from rest_framework.views import APIView
@ -169,10 +169,11 @@ class Playlist(APIView):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
if 'data' in request.data: if 'data' in request.data:
write_json(request.data['data']) _respose = write_json(request.data['data'])
return Response(status=200) return _respose
return Response(status=400) return Response({'text': 'Unspecified save error'},
status=status.HTTP_400_BAD_REQUEST)
class Statistics(APIView): class Statistics(APIView):