# -*- coding: utf-8 -*-
from urllib.parse import urlparse

from rest_framework import serializers
from server.models import Booklet

from server.models import Reference


class BookletListSerializer(serializers.ModelSerializer):

    class Meta:
        model = Booklet
        fields = ("id", "status")
        read_only_fields = ("status",)


class BookletRetrieveSerializer(BookletListSerializer):

    booklet = serializers.SerializerMethodField(read_only=True)

    def get_booklet(self, obj):
        if obj.status == Booklet.STATUS_DONE:
            request = self.context.get('request')
            if request:
                url = request.build_absolute_uri()
                parts = urlparse(url)
                return "{}://{}/booklets/{}.pdf".format(parts.scheme, parts.netloc, obj.id)
            else:
                return None
        else:
            return None

    class Meta(BookletListSerializer.Meta):
        fields = ("id", "status", "booklet")
        read_only_fields = ("status", "booklet")


class BookletSerializer(BookletRetrieveSerializer):

    subHeader = serializers.CharField(source='sub_header', allow_blank=True, default='', label='Untertitel', help_text="Das ist der Untertitel für das Titelblatt")
    mainHeader = serializers.CharField(source='main_header', allow_blank=True, default='', label='Hauptitel', help_text='Das ist der Haupttitel für das Titelblatt')

    class Meta(BookletRetrieveSerializer.Meta):
        fields = ("id", "references", "format", "header", "subHeader", "mainHeader", "status", "booklet")

    def validate_references(self, value):
        invalid_codes = set()
        for code in value:
            try:
                reference = Reference.get_reference(code)
                if reference.deprecated:
                    invalid_codes.add(code)
            except Reference.DoesNotExist:
                invalid_codes.add(code)
        if invalid_codes:
            n = len(invalid_codes)
            raise serializers.ValidationError(f"Reference{'s' if n>1 else ''} '{', '.join(invalid_codes)}' {'are' if n>1 else 'is'} not valid.")
        return value

@@ -1101,32 +1101,41 @@
 izers.CharField(
+%0A        
 source='sub_head
@@ -1130,32 +1130,64 @@
 ce='sub_header',
+%0A        max_length=125,%0A       
  allow_blank=Tru
@@ -1192,32 +1192,40 @@
 rue, default='',
+%0A       
  label='Untertit
@@ -1224,24 +1224,32 @@
 Untertitel',
+%0A       
  help_text=%22
@@ -1290,17 +1290,23 @@
 elblatt%22
-)
+%0A    )%0A
 %0A    mai
@@ -1337,16 +1337,25 @@
 arField(
+%0A        
 source='
@@ -1367,16 +1367,48 @@
 header',
+%0A        max_length=125,%0A       
  allow_b
@@ -1429,16 +1429,24 @@
 ault='',
+%0A       
  label='
@@ -1456,16 +1456,24 @@
 ptitel',
+%0A       
  help_te
@@ -1518,16 +1518,21 @@
 elblatt'
+%0A    
 )%0A%0A    c
@@ -1723,16 +1723,16 @@
 value):%0A
-
         
@@ -1753,16 +1753,43 @@
 = set()%0A
+        value = set(value)%0A
         

Improve Booklet serializer