from os import listdir

from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers

MAX_DURATION = 120


class MusicSessionSerializer(serializers.Serializer):
    source = serializers.CharField()
    duration = serializers.IntegerField(min_value=0, max_value=MAX_DURATION)

    def sources(self):
        return listdir(settings.DEFAULT_SONGS_FOLDER)

    def validate_source(self, value):
        if value not in self.sources():
            raise serializers.ValidationError(_(
                'You must choose an existing source'
            ))
        return value
