# Path: dplace_app/models.py
# class Society(models.Model):
#     ext_id = models.CharField('External ID', db_index=True, unique=True, max_length=20)
#     xd_id = models.CharField(
#         'Cross ID', db_index=True, default=None, null=True, max_length=10
#     )
#     name = models.CharField('Name', db_index=True, max_length=200)
#     latitude = models.FloatField('Latitude', null=True)
#     longitude = models.FloatField('Longitude', null=True)
#     focal_year = models.CharField('Focal Year', null=True, blank=True, max_length=100)
#     alternate_names = models.TextField(default="")
#     original_name = models.CharField('ORIG_name', max_length=200, default=None, null=True)
#     original_latitude = models.FloatField('ORIG_latitude', null=True)
#     original_longitude = models.FloatField('ORIG_longitude', null=True)
#     
#     region = models.ForeignKey('GeographicRegion', null=True)
#     source = models.ForeignKey('Source', null=True, related_name="societies")
#     language = models.ForeignKey('Language', null=True, related_name="societies")
#     
#     hraf_link = models.CharField('HRAF', null=True, default=None, max_length=200)
#     chirila_link = models.CharField('CHIRILA', default=None, null=True, max_length=200)
#     relationships = models.ManyToManyField(
#         'self', through='SocietyRelation', symmetrical=False)
# 
#     @property
#     def related(self):
#         return list(self.relationships.all())
# 
#     @property
#     def location(self):
#         return dict(coordinates=[self.longitude, self.latitude])
#         
#     @property
#     def original_location(self):
#         return dict(coordinates=[self.original_longitude, self.original_latitude])
# 
#     def get_environmental_data(self):
#         """Returns environmental data for the given society"""
#         valueDict = defaultdict(list)
#         for value in self.value_set\
#                 .select_related('variable').order_by('variable__name').all():
#             if value.variable.type == 'environmental':
#                 categories = value.variable.index_categories.all()
#                 valueDict[str(categories[0])].append({
#                     'name': value.variable.name,
#                     'value': '{0}'.format(value),
#                     'units': value.variable.units,
#                     'comment': value.comment
#                 })
#         return valueDict
# 
#     def get_cultural_trait_data(self):
#         """Returns the data for the given society"""
#         valueDict = defaultdict(list)
#         for value in self.value_set\
#                 .select_related('code').\
#                 select_related('variable').order_by('variable__label').all():
#             if value.variable.type == 'cultural':
#                 categories = value.variable.index_categories.all()
#                 for c in categories:
#                     valueDict[str(c)].append({
#                         'id': value.id,
#                         'label': value.variable.label,
#                         'name': value.variable.name,
#                         'code': value.coded_value,
#                         'description': value.get_description(),
#                         'year': value.focal_year,
#                         'comment': value.comment,
#                         'sources': value.references.all(),
#                     })
#         return valueDict
# 
#     def get_data_references(self):
#         """Returns the references for the cultural trait data"""
#         refs = []
#         qset = self.value_set
#         for value in qset.all():
#             for r in value.references.all():
#                 if r not in refs:
#                     refs.append(r)
#         return sorted(refs, key=lambda r: r.author)
# 
#     def __unicode__(self):
#         return "%s - %s" % (self.ext_id, self.name)
#     
#     def get_absolute_url(self):
#         return reverse("view_society", args=[self.ext_id])
#     
#     class Meta(object):
#         verbose_name_plural = "Societies"
#         ordering = ('name', )
# 
# class GeographicRegion(models.Model):
#     level_2_re = models.FloatField()
#     count = models.FloatField()
#     region_nam = models.CharField(max_length=254, db_index=True)
#     continent = models.CharField(max_length=254,  db_index=True)
#     tdwg_code = models.IntegerField()
# 
#     def __unicode__(self):
#         return "Region: %s, Continent %s" % (self.region_nam, self.continent)
# 
#     class Meta(object):
#         ordering = ('region_nam', )
# 
# class SocietyRelation(models.Model):
#     from_society = models.ForeignKey(Society, related_name='from_societies')
#     to_society = models.ForeignKey(Society, related_name='to_societies')
#     type = models.CharField(max_length=100, default='similar')


import logging
from dplace_app.models import Society, GeographicRegion, SocietyRelation
from sources import get_source
# coding: utf8
from __future__ import unicode_literals




def society_locations(repos):
    societies = {s.ext_id: s for s in Society.objects.all()}
    regions = {r.region_nam: r for r in GeographicRegion.objects.all()}

    count = 0
    for soc_id, spec in repos.read_json('geo', 'societies_tdwg.json').items():
        society = societies.get(soc_id)
        if not society:
            logging.warn("No matching society found for %s" % soc_id)
            continue

        region = regions.get(spec['name'])
        if not region:
            logging.warn("No matching region found for %s" % spec['name'])
        else:
            society.region = region
        society.save()
        count += 1
    return count


def load_society_relations(repos):
    count = 0

# Path: dplace_app/models.py
# class Society(models.Model):
#     ext_id = models.CharField('External ID', db_index=True, unique=True, max_length=20)
#     xd_id = models.CharField(
#         'Cross ID', db_index=True, default=None, null=True, max_length=10
#     )
#     name = models.CharField('Name', db_index=True, max_length=200)
#     latitude = models.FloatField('Latitude', null=True)
#     longitude = models.FloatField('Longitude', null=True)
#     focal_year = models.CharField('Focal Year', null=True, blank=True, max_length=100)
#     alternate_names = models.TextField(default="")
#     original_name = models.CharField('ORIG_name', max_length=200, default=None, null=True)
#     original_latitude = models.FloatField('ORIG_latitude', null=True)
#     original_longitude = models.FloatField('ORIG_longitude', null=True)
#     
#     region = models.ForeignKey('GeographicRegion', null=True)
#     source = models.ForeignKey('Source', null=True, related_name="societies")
#     language = models.ForeignKey('Language', null=True, related_name="societies")
#     
#     hraf_link = models.CharField('HRAF', null=True, default=None, max_length=200)
#     chirila_link = models.CharField('CHIRILA', default=None, null=True, max_length=200)
#     relationships = models.ManyToManyField(
#         'self', through='SocietyRelation', symmetrical=False)
# 
#     @property
#     def related(self):
#         return list(self.relationships.all())
# 
#     @property
#     def location(self):
#         return dict(coordinates=[self.longitude, self.latitude])
#         
#     @property
#     def original_location(self):
#         return dict(coordinates=[self.original_longitude, self.original_latitude])
# 
#     def get_environmental_data(self):
#         """Returns environmental data for the given society"""
#         valueDict = defaultdict(list)
#         for value in self.value_set\
#                 .select_related('variable').order_by('variable__name').all():
#             if value.variable.type == 'environmental':
#                 categories = value.variable.index_categories.all()
#                 valueDict[str(categories[0])].append({
#                     'name': value.variable.name,
#                     'value': '{0}'.format(value),
#                     'units': value.variable.units,
#                     'comment': value.comment
#                 })
#         return valueDict
# 
#     def get_cultural_trait_data(self):
#         """Returns the data for the given society"""
#         valueDict = defaultdict(list)
#         for value in self.value_set\
#                 .select_related('code').\
#                 select_related('variable').order_by('variable__label').all():
#             if value.variable.type == 'cultural':
#                 categories = value.variable.index_categories.all()
#                 for c in categories:
#                     valueDict[str(c)].append({
#                         'id': value.id,
#                         'label': value.variable.label,
#                         'name': value.variable.name,
#                         'code': value.coded_value,
#                         'description': value.get_description(),
#                         'year': value.focal_year,
#                         'comment': value.comment,
#                         'sources': value.references.all(),
#                     })
#         return valueDict
# 
#     def get_data_references(self):
#         """Returns the references for the cultural trait data"""
#         refs = []
#         qset = self.value_set
#         for value in qset.all():
#             for r in value.references.all():
#                 if r not in refs:
#                     refs.append(r)
#         return sorted(refs, key=lambda r: r.author)
# 
#     def __unicode__(self):
#         return "%s - %s" % (self.ext_id, self.name)
#     
#     def get_absolute_url(self):
#         return reverse("view_society", args=[self.ext_id])
#     
#     class Meta(object):
#         verbose_name_plural = "Societies"
#         ordering = ('name', )
# 
# class GeographicRegion(models.Model):
#     level_2_re = models.FloatField()
#     count = models.FloatField()
#     region_nam = models.CharField(max_length=254, db_index=True)
#     continent = models.CharField(max_length=254,  db_index=True)
#     tdwg_code = models.IntegerField()
# 
#     def __unicode__(self):
#         return "Region: %s, Continent %s" % (self.region_nam, self.continent)
# 
#     class Meta(object):
#         ordering = ('region_nam', )
# 
# class SocietyRelation(models.Model):
#     from_society = models.ForeignKey(Society, related_name='from_societies')
#     to_society = models.ForeignKey(Society, related_name='to_societies')
#     type = models.CharField(max_length=100, default='similar')


# Path: dplace_app/loader/society.py
import logging
from dplace_app.models import Society, GeographicRegion, SocietyRelation
from sources import get_source

# coding: utf8
from __future__ import unicode_literals




def society_locations(repos):
    societies = {s.ext_id: s for s in Society.objects.all()}
    regions = {r.region_nam: r for r in GeographicRegion.objects.all()}

    count = 0
    for soc_id, spec in repos.read_json('geo', 'societies_tdwg.json').items():
        society = societies.get(soc_id)
        if not society:
            logging.warn("No matching society found for %s" % soc_id)
            continue

        region = regions.get(spec['name'])
        if not region:
            logging.warn("No matching region found for %s" % spec['name'])
        else:
            society.region = region
        society.save()
        count += 1
    return count


def load_society_relations(repos):
    count = 0

    societies = {s.ext_id: s for s in Society.objects.all()}