# Path: db/campaigns/models.py
# class Parameter(vera.BaseParameter):
#     campaign = models.ForeignKey(Campaign, related_name='parameters')
#     description = models.TextField()
# 
#     # Inherited from vera's params.BaseParameter:
#     #   name = models.CharField()
#     #   slug = models.CharField()
#     #   is_numeric = models.BooleanField()
#     #   units = models.CharField()
# 
#     class Meta:
#         ordering = ['pk']
# 
# class Report(vera.BaseReport):
#     photo = models.ImageField(
#         null=True, blank=True, upload_to='reports'
#     )
# 
#     # Inherited from vera's series.BaseReport:
#     #   event = models.ForeignKey(Event)
#     #   entered = models.DateTimeField()
#     #   user = models.ForeignKey('auth.User')
#     #   status = models.ForeignKey('vera.ReportStatus')


from rest_framework import serializers
from wq.db.rest.serializers import ModelSerializer
from wq.db.patterns import serializers as patterns
from vera.models import Site
from .models import Parameter, Report
from vera.results.serializers import ResultSerializer as VeraResultSerializer
from vera.series.serializers import ReportSerializer as VeraReportSerializer
class ResultSerializer(VeraResultSerializer):
    class Meta(VeraResultSerializer.Meta):
        type_filter = {
            'campaign_id': '{{campaign_id}}',
        }


class ReportSerializer(VeraReportSerializer):
    results = ResultSerializer(many=True)

    def to_representation(self, obj):
        # FIXME: See https://github.com/wq/wq.db/issues/61
        result = super(ReportSerializer, self).to_representation(obj)
        if result.get('results', None):
            return result

        campaign_id = (
            'request' in self.context and
            self.context['request'].GET.get('campaign_id', None)
        )
        if not campaign_id:
            return result

        result['results'] = [
            {
                '@index': i,
                'type': self.router.serialize(parameter),
                'type_label': str(parameter),
                'type_id': parameter.slug,
            }

# Path: db/campaigns/models.py
# class Parameter(vera.BaseParameter):
#     campaign = models.ForeignKey(Campaign, related_name='parameters')
#     description = models.TextField()
# 
#     # Inherited from vera's params.BaseParameter:
#     #   name = models.CharField()
#     #   slug = models.CharField()
#     #   is_numeric = models.BooleanField()
#     #   units = models.CharField()
# 
#     class Meta:
#         ordering = ['pk']
# 
# class Report(vera.BaseReport):
#     photo = models.ImageField(
#         null=True, blank=True, upload_to='reports'
#     )
# 
#     # Inherited from vera's series.BaseReport:
#     #   event = models.ForeignKey(Event)
#     #   entered = models.DateTimeField()
#     #   user = models.ForeignKey('auth.User')
#     #   status = models.ForeignKey('vera.ReportStatus')


# Path: db/campaigns/serializers.py
from rest_framework import serializers
from wq.db.rest.serializers import ModelSerializer
from wq.db.patterns import serializers as patterns
from vera.models import Site
from .models import Parameter, Report
from vera.results.serializers import ResultSerializer as VeraResultSerializer
from vera.series.serializers import ReportSerializer as VeraReportSerializer

class ResultSerializer(VeraResultSerializer):
    class Meta(VeraResultSerializer.Meta):
        type_filter = {
            'campaign_id': '{{campaign_id}}',
        }


class ReportSerializer(VeraReportSerializer):
    results = ResultSerializer(many=True)

    def to_representation(self, obj):
        # FIXME: See https://github.com/wq/wq.db/issues/61
        result = super(ReportSerializer, self).to_representation(obj)
        if result.get('results', None):
            return result

        campaign_id = (
            'request' in self.context and
            self.context['request'].GET.get('campaign_id', None)
        )
        if not campaign_id:
            return result

        result['results'] = [
            {
                '@index': i,
                'type': self.router.serialize(parameter),
                'type_label': str(parameter),
                'type_id': parameter.slug,
            }

            for i, parameter in enumerate(Parameter.objects.filter(