from django.contrib.auth.models import User
from .models import Patient
from rest_framework import serializers
import datetime


class SimpleUserSerializer(serializers.ModelSerializer):
    full_name = serializers.SerializerMethodField('get_full_name')

    class Meta:
        model = User
        fields = [
            'full_name',
        ]

    def get_full_name(self, obj):
        return obj.get_full_name()


class PatientListSerializer(serializers.ModelSerializer):
    user = SimpleUserSerializer()
    birth_date = serializers.SerializerMethodField('get_birth_date')
    age = serializers.SerializerMethodField('get_age')

    def get_birth_date(self, obj):
        return obj.national_identification_number[0:2] + "."\
            + obj.national_identification_number[2:4] + "."\
            + obj.national_identification_number[4:6]

    def get_age(self, obj):
        today = datetime.datetime.today()
        ddmm = obj.national_identification_number[0:4]
        yyyy = "20" + obj.national_identification_number[4:6]
        if int(yyyy) >= today.year:
            yyyy = str(int(yyyy) - 100)
        birth_date = datetime.datetime.strptime(ddmm + yyyy, "%d%m%Y")
        diff = today - birth_date
        num_years = int(diff.days / 365.2425)  # rough estimate, can be wrong in some edge cases
        return num_years

    class Meta:
        model = Patient
        fields = (
            'id',
            'user',
            'birth_date',
            'age',
            'national_identification_number',
            'telephone'
        )

class PatientDetailSerializer(PatientListSerializer):
    user = SimpleUserSerializer()
    birth_date = serializers.SerializerMethodField('get_birth_date')
    age = serializers.SerializerMethodField('get_age')

    class Meta:
        model = Patient
        fields = (
            'id',
            'user',
            'birth_date',
            'age',
            'national_identification_number',
            'telephone',
            'address',
            'pulse_max',
            'pulse_min',
            'o2_max',
            'o2_min',
            'temperature_max',
            'temperature_min',
            'activity_access',
            'pulse_access',
            'o2_access',
            'temperature_access'
        )

@@ -718,24 +718,25 @@
 r%5B0:2%5D + %22.%22
+ 
 %5C%0A          
@@ -784,16 +784,17 @@
 4%5D + %22.%22
+ 
 %5C%0A      
@@ -1556,16 +1556,17 @@
      )%0A%0A
+%0A
 class Pa

Fix whitespace