# Path: washmap/stats/models.py
# class StatDescription(Sortable):
#     class Meta(Sortable.Meta):
#         pass
# 
#     description = models.TextField()
#     code = models.SlugField()
# 
#     def __unicode__(self):
#         return self.description
# 
# 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
# class StatisticResource(ModelResource):
#     """
#         StatDescription
# 
#         StatValue
#             Year
#             @Country
#             @StatDescription
# 
#         Stat Description
#         Country | Year | Year | Year ... |
#         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)
#         years = values_qs.values_list('year', flat=True).distinct()
# 
#         for year in sorted(years):
#             year_fields['year-%d' % year] = YearField(year, column_name='%s' % year)  # nopep8
# 
#         return year_fields
# 
#     def get_queryset(self):
#         countries = Country.objects.all().order_by('name')
#         statvalues = []
#         for country in countries:
#             inst = StatValueRowInstance(self.statistic, country)
#             statvalues.append(inst)
# 
#         class dummy_queryset(object):
#             def iterator(self):
#                 return statvalues
# 
#         return dummy_queryset()


from django.core.urlresolvers import reverse
from custom_importer.forms import ImportFormBase
from custom_importer.views import ImporterBase, ExportTemplateBase
from country.models import Country
from .models import StatDescription, StatValue
from .statsimporter import StatisticResource




class CountryStatsForm(ImportFormBase):
    def get_choice_field_name(self):
        return 'statistic'

    def get_choice_field_queryset(self):
        return StatDescription.objects.all()


class ImportCountryStats(ImporterBase):
    form_class = CountryStatsForm

    def get_success_url(self):
        return reverse('admin:country-stats')

    def get_export_url(self, statistic):
        return reverse('admin:country-stats-template',
                       kwargs={'statistic': statistic.id})

    def get_import_resource_class(self):
        return StatisticResource


class ExportCountryStats(ExportTemplateBase):
    model = StatDescription

    def get_export_resource_class(self):

# Path: washmap/stats/models.py
# class StatDescription(Sortable):
#     class Meta(Sortable.Meta):
#         pass
# 
#     description = models.TextField()
#     code = models.SlugField()
# 
#     def __unicode__(self):
#         return self.description
# 
# 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
# class StatisticResource(ModelResource):
#     """
#         StatDescription
# 
#         StatValue
#             Year
#             @Country
#             @StatDescription
# 
#         Stat Description
#         Country | Year | Year | Year ... |
#         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)
#         years = values_qs.values_list('year', flat=True).distinct()
# 
#         for year in sorted(years):
#             year_fields['year-%d' % year] = YearField(year, column_name='%s' % year)  # nopep8
# 
#         return year_fields
# 
#     def get_queryset(self):
#         countries = Country.objects.all().order_by('name')
#         statvalues = []
#         for country in countries:
#             inst = StatValueRowInstance(self.statistic, country)
#             statvalues.append(inst)
# 
#         class dummy_queryset(object):
#             def iterator(self):
#                 return statvalues
# 
#         return dummy_queryset()


# Path: washmap/stats/views.py
from django.core.urlresolvers import reverse
from custom_importer.forms import ImportFormBase
from custom_importer.views import ImporterBase, ExportTemplateBase
from country.models import Country
from .models import StatDescription, StatValue
from .statsimporter import StatisticResource





class CountryStatsForm(ImportFormBase):
    def get_choice_field_name(self):
        return 'statistic'

    def get_choice_field_queryset(self):
        return StatDescription.objects.all()


class ImportCountryStats(ImporterBase):
    form_class = CountryStatsForm

    def get_success_url(self):
        return reverse('admin:country-stats')

    def get_export_url(self, statistic):
        return reverse('admin:country-stats-template',
                       kwargs={'statistic': statistic.id})

    def get_import_resource_class(self):
        return StatisticResource


class ExportCountryStats(ExportTemplateBase):
    model = StatDescription

    def get_export_resource_class(self):

        return StatisticResource