# Path: washmap/stats/models.py
# class StatValue(models.Model):
#     class Meta:
#         unique_together = ('description', 'country', 'year')
# 
#     description = models.ForeignKey(StatDescription)
#     country = models.ForeignKey(Country)
#     year = models.IntegerField()
#     value = models.DecimalField(max_digits=20, decimal_places=10, null=True, blank=True)  # nopep8
# 
#     def __unicode__(self):
#         return "%s, %s, %s" % (
#             self.country,
#             self.year,
#             self.value
#         )


from copy import deepcopy
from django.core.exceptions import ValidationError
from django.utils.datastructures import SortedDict
from decimal import Decimal
from import_export.fields import Field
from import_export.instance_loaders import ModelInstanceLoader
from import_export.resources import ModelResource
from import_export.widgets import DecimalWidget
from country.models import Country
from .models import StatValue
        A..     |
        B..     |

    """

    class Meta:
        model = None  # see StatValueRowInstance instead
        fields = ()
        instance_loader_class = StatValueInstanceLoader
        import_id_fields = ['country']

    country = CountryField(attribute='name', readonly=True)

    def __init__(self, statistic, *args, **kwargs):
        self.statistic = statistic
        super(StatisticResource, self).__init__(*args, **kwargs)

    def before_import(self, dataset, real_dry_run):
        # Presume first column is country column
        year_headers = dataset.headers[1:]

        # check they are valid, unique, etc

        for year_header in year_headers:
            year = int(year_header)
            self.fields['year-%d' % year] = YearField(year, column_name='%s' % year)  # nopep8

    def get_dynamic_fields(self):
        year_fields = SortedDict()


# Path: washmap/stats/models.py
# class StatValue(models.Model):
#     class Meta:
#         unique_together = ('description', 'country', 'year')
# 
#     description = models.ForeignKey(StatDescription)
#     country = models.ForeignKey(Country)
#     year = models.IntegerField()
#     value = models.DecimalField(max_digits=20, decimal_places=10, null=True, blank=True)  # nopep8
# 
#     def __unicode__(self):
#         return "%s, %s, %s" % (
#             self.country,
#             self.year,
#             self.value
#         )


# Path: washmap/stats/statsimporter.py
from copy import deepcopy
from django.core.exceptions import ValidationError
from django.utils.datastructures import SortedDict
from decimal import Decimal
from import_export.fields import Field
from import_export.instance_loaders import ModelInstanceLoader
from import_export.resources import ModelResource
from import_export.widgets import DecimalWidget
from country.models import Country
from .models import StatValue

        A..     |
        B..     |

    """

    class Meta:
        model = None  # see StatValueRowInstance instead
        fields = ()
        instance_loader_class = StatValueInstanceLoader
        import_id_fields = ['country']

    country = CountryField(attribute='name', readonly=True)

    def __init__(self, statistic, *args, **kwargs):
        self.statistic = statistic
        super(StatisticResource, self).__init__(*args, **kwargs)

    def before_import(self, dataset, real_dry_run):
        # Presume first column is country column
        year_headers = dataset.headers[1:]

        # check they are valid, unique, etc

        for year_header in year_headers:
            year = int(year_header)
            self.fields['year-%d' % year] = YearField(year, column_name='%s' % year)  # nopep8

    def get_dynamic_fields(self):
        year_fields = SortedDict()


        values_qs = StatValue.objects.filter(description=self.statistic)