# 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


class StatValueRowInstance(object):
    """
    Maps an import row onto a collection of StatValue objects.

    Values for 'statistic' (StatDescription) and 'country' are initialized
    with the instance loader (called per row).

    YearFields are added for each non-country column.

    Implements ORM like interface on row:
        .save
        .delete

    """
    years = None
    pk = None  # Has to be faked to keep import-export happy

    def __init__(self, statistic, country):
        self.statistic = statistic
        self.country = country

        self.years = SortedDict()
        self.load_statvalues()

    def get_country(self):
        return self.country

    def load_statvalues(self):

# 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



class StatValueRowInstance(object):
    """
    Maps an import row onto a collection of StatValue objects.

    Values for 'statistic' (StatDescription) and 'country' are initialized
    with the instance loader (called per row).

    YearFields are added for each non-country column.

    Implements ORM like interface on row:
        .save
        .delete

    """
    years = None
    pk = None  # Has to be faked to keep import-export happy

    def __init__(self, statistic, country):
        self.statistic = statistic
        self.country = country

        self.years = SortedDict()
        self.load_statvalues()

    def get_country(self):
        return self.country

    def load_statvalues(self):

        values = StatValue.objects.filter(description=self.statistic,