# -*- coding: utf-8 -*-

# Akvo RSR is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.

from akvo.rest.serializers.rsr_serializer import BaseRSRSerializer
from akvo.rest.serializers.indicator_period_data import IndicatorPeriodDataFrameworkSerializer
from akvo.rsr.models import IndicatorPeriod, IndicatorPeriodData

from rest_framework import serializers


class IndicatorPeriodSerializer(BaseRSRSerializer):

    indicator_unicode = serializers.ReadOnlyField(source='indicator.__unicode__')
    percent_accomplishment = serializers.ReadOnlyField()

    class Meta:
        model = IndicatorPeriod

    # TODO: add validation for parent_period


class IndicatorPeriodFrameworkSerializer(BaseRSRSerializer):

    data = serializers.SerializerMethodField('get_updates')
    parent_period = serializers.ReadOnlyField(source='parent_period.pk')
    percent_accomplishment = serializers.ReadOnlyField()

    class Meta:
        model = IndicatorPeriod

    def get_updates(self, obj):
        user = self.context['request'].user
        updates = IndicatorPeriodData.objects.filter(period=obj).select_related(
            'period',
            'user',
            'approved_by',
        ).prefetch_related(
            'comments',
            'comments__user'
        )
        updates = IndicatorPeriodData.get_user_viewable_updates(updates, user)
        serializer = IndicatorPeriodDataFrameworkSerializer(updates, many=True)
        return serializer.data

@@ -780,24 +780,51 @@
 icatorPeriod
+%0A        fields = '__all__'
 %0A%0A    # TODO
@@ -1160,16 +1160,43 @@
 orPeriod
+%0A        fields = '__all__'
 %0A%0A    de

Fix rest.test_indicator tests