Пример #1
0
    def create(self, validated_data):
        self._validate_disaggregations(
            self._disaggregations_data,
            value=ensure_decimal(validated_data.get('value', 0)),
            numerator=ensure_decimal(validated_data.get('numerator', None)),
            denominator=ensure_decimal(validated_data.get('denominator',
                                                          None)))
        """Over-ridden to handle nested writes."""
        files = validated_data.pop('files', [])
        photos = validated_data.pop('photos', [])
        comments = validated_data.pop('comments', [])
        update = super(IndicatorPeriodDataFrameworkSerializer,
                       self).create(validated_data)
        for disaggregation in self._disaggregations_data:
            disaggregation['update'] = update.id
            if 'type_id' in disaggregation and 'dimension_value' not in disaggregation:
                disaggregation['dimension_value'] = disaggregation['type_id']
            serializer = DisaggregationSerializer(data=disaggregation)
            serializer.is_valid(raise_exception=True)
            serializer.create(serializer.validated_data)
        for file in files:
            IndicatorPeriodDataFile.objects.create(update=update, file=file)
        for photo in photos:
            IndicatorPeriodDataPhoto.objects.create(update=update, photo=photo)
        for comment in comments:
            IndicatorPeriodDataComment.objects.create(
                data=update, user=update.user, comment=comment['comment'])

        return update
Пример #2
0
    def create(self, validated_data):
        """Over-ridden to handle nested writes."""
        update = super(IndicatorPeriodDataFrameworkSerializer, self).create(validated_data)
        for disaggregation in self._disaggregations_data:
            disaggregation['update'] = update.id
            serializer = DisaggregationSerializer(data=disaggregation)
            serializer.is_valid(raise_exception=True)
            serializer.create(serializer.validated_data)

        return update
Пример #3
0
    def update(self, instance, validated_data):
        self._validate_disaggregations(
            self._disaggregations_data,
            value=ensure_decimal(validated_data.get('value', instance.value)),
            numerator=ensure_decimal(
                validated_data.get('numerator', instance.numerator)),
            denominator=ensure_decimal(
                validated_data.get('denominator', instance.denominator)),
            update=instance)
        """Over-ridden to handle nested updates."""
        files = validated_data.pop('files', [])
        photos = validated_data.pop('photos', [])
        comments = validated_data.pop('comments', [])
        super(IndicatorPeriodDataFrameworkSerializer,
              self).update(instance, validated_data)
        for disaggregation in self._disaggregations_data:
            disaggregation['update'] = instance.id
            serializer = DisaggregationSerializer(data=disaggregation)
            serializer.is_valid(raise_exception=True)
            disaggregation_instance, _ = instance.disaggregations.get_or_create(
                update=instance,
                dimension_value=serializer.validated_data['dimension_value'],
            )
            serializer.update(disaggregation_instance,
                              serializer.validated_data)
        for file in files:
            IndicatorPeriodDataFile.objects.create(update=instance, file=file)
        for photo in photos:
            IndicatorPeriodDataPhoto.objects.create(update=instance,
                                                    photo=photo)
        for comment in comments:
            comment_id = int(comment.get('id', 0))
            comment_txt = str(comment.get('comment', ''))
            if not comment_id:
                IndicatorPeriodDataComment.objects.create(
                    data=instance,
                    user=instance.user,
                    comment=comment['comment'])
            else:
                comment_obj = IndicatorPeriodDataComment.objects.get(
                    id=comment_id)
                if not comment_txt:
                    comment_obj.delete()
                else:
                    comment_obj.comment = comment_txt
                    comment_obj.save()

        return instance._meta.model.objects.select_related(
            'period',
            'user',
            'approved_by',
        ).prefetch_related(
            'comments',
            'disaggregations',
        ).get(id=instance.id)
Пример #4
0
    def create(self, validated_data):
        """Over-ridden to handle nested writes."""
        update = super(IndicatorPeriodDataFrameworkSerializer,
                       self).create(validated_data)
        for disaggregation in self._disaggregations_data:
            disaggregation['update'] = update.id
            serializer = DisaggregationSerializer(data=disaggregation)
            serializer.is_valid(raise_exception=True)
            serializer.create(serializer.validated_data)

        return update
Пример #5
0
    def update(self, instance, validated_data):
        """Over-ridden to handle nested updates."""
        super(IndicatorPeriodDataFrameworkSerializer, self).update(instance, validated_data)
        for disaggregation in self._disaggregations_data:
            disaggregation['update'] = instance.id
            serializer = DisaggregationSerializer(data=disaggregation)
            serializer.is_valid(raise_exception=True)
            disaggregation_instance, _ = instance.disaggregations.get_or_create(
                update=instance,
                dimension=serializer.validated_data['dimension'],
            )
            serializer.update(disaggregation_instance, serializer.validated_data)

        return instance._meta.model.objects.select_related(
            'period',
            'user',
            'approved_by',
        ).prefetch_related(
            'comments',
            'disaggregations',
        ).get(id=instance.id)
Пример #6
0
    def update(self, instance, validated_data):
        """Over-ridden to handle nested updates."""
        super(IndicatorPeriodDataFrameworkSerializer,
              self).update(instance, validated_data)
        for disaggregation in self._disaggregations_data:
            disaggregation['update'] = instance.id
            serializer = DisaggregationSerializer(data=disaggregation)
            serializer.is_valid(raise_exception=True)
            disaggregation_instance, _ = instance.disaggregations.get_or_create(
                update=instance,
                dimension=serializer.validated_data['dimension'],
            )
            serializer.update(disaggregation_instance,
                              serializer.validated_data)

        return instance._meta.model.objects.select_related(
            'period',
            'user',
            'approved_by',
        ).prefetch_related(
            'comments',
            'disaggregations',
        ).get(id=instance.id)