# Path: irish_townlands/utils.py
# def m2_to_arp(area_m2):
#     area_acres = area_m2 / 4046.85642
# 
#     acres = int(area_acres)
#     subacres = area_acres - acres
# 
#     roods = int(subacres * 4)
#     perches = int((subacres * 4 - roods) * 40)
# 
#     return (acres, roods, perches)
# 
# def remove_prefixes(string, prefixes):
#     for i in range(1000):
#         for prefix in prefixes:
#             if string.startswith(prefix):
#                 string = string[len(prefix):]
#                 continue
#         else:
#             break
# 
#     return string
# 
# def remove_accents(string):
#     for a, b in [ (u"Á", "A"), (u"É", "E"), (u"Í", "I"), (u"Ó", "O"), (u"Ú", "U"),
#                  (u"á", "a"), (u"é", "e"), (u"í", "i"), (u"ó", "o"), (u"ú", "u") ]:
# 
#         string = string.replace(a, b)
# 
#     return string
# 
# def historic_names(tags):
#     table = []
#     for k, v in tags.items():
#         if not k.startswith("name:"):
#             continue
#         k = k[5:]
# 
#         period = parse_period(k)
#         if period is None:
#             continue
# 
#         # FIXME include source, and source url link
# 
#         table.append((period, v))
# 
#     table.sort()
# 
#     table = {'header': ['When', 'Name'], 'body': table, 'has_results': len(table) > 0 }
# 
#     # TODO add other tags, not just 'name'
#     return table


from collections import defaultdict
from django.db import models
from django.db.models import Sum, Q
from django.template.defaultfilters import slugify
from django.utils.translation import ungettext, ugettext
from django.utils import translation
from django.utils.html import format_html, mark_safe
from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db import models
from .utils import m2_to_arp, remove_prefixes, remove_accents, historic_names
import math
import json
	    ed_name = ElectoralDivision.objects.filter(id=self.townland.ed_id).values_list("name_tag", flat=True)[0] if self.townland.ed_id else None

            self.url_path = pathify(county_name, barony_name, civil_parish_name, ed_name, self.townland.name, name)

        else:
            self.url_path = "{0}".format(name)

        if self.unique_suffix:
            self.url_path += str(self.unique_suffix)

    @property
    def centre_pretty(self):
        return location_pretty(x=self.location_x, y=self.location_y)

    @property
    def osm_type(self):
        return "node"

    def __unicode__(self):
        return "{0} ({1})".format(self.name, self.osm_id)

    @property
    def tags(self):
        if self._tags_json == '':
            return {}
        else:
            return json.loads(self._tags_json)

    @property
    def old_names(self):

# Path: irish_townlands/utils.py
# def m2_to_arp(area_m2):
#     area_acres = area_m2 / 4046.85642
# 
#     acres = int(area_acres)
#     subacres = area_acres - acres
# 
#     roods = int(subacres * 4)
#     perches = int((subacres * 4 - roods) * 40)
# 
#     return (acres, roods, perches)
# 
# def remove_prefixes(string, prefixes):
#     for i in range(1000):
#         for prefix in prefixes:
#             if string.startswith(prefix):
#                 string = string[len(prefix):]
#                 continue
#         else:
#             break
# 
#     return string
# 
# def remove_accents(string):
#     for a, b in [ (u"Á", "A"), (u"É", "E"), (u"Í", "I"), (u"Ó", "O"), (u"Ú", "U"),
#                  (u"á", "a"), (u"é", "e"), (u"í", "i"), (u"ó", "o"), (u"ú", "u") ]:
# 
#         string = string.replace(a, b)
# 
#     return string
# 
# def historic_names(tags):
#     table = []
#     for k, v in tags.items():
#         if not k.startswith("name:"):
#             continue
#         k = k[5:]
# 
#         period = parse_period(k)
#         if period is None:
#             continue
# 
#         # FIXME include source, and source url link
# 
#         table.append((period, v))
# 
#     table.sort()
# 
#     table = {'header': ['When', 'Name'], 'body': table, 'has_results': len(table) > 0 }
# 
#     # TODO add other tags, not just 'name'
#     return table


# Path: irish_townlands/models.py
from collections import defaultdict
from django.db import models
from django.db.models import Sum, Q
from django.template.defaultfilters import slugify
from django.utils.translation import ungettext, ugettext
from django.utils import translation
from django.utils.html import format_html, mark_safe
from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db import models
from .utils import m2_to_arp, remove_prefixes, remove_accents, historic_names
import math
import json

	    ed_name = ElectoralDivision.objects.filter(id=self.townland.ed_id).values_list("name_tag", flat=True)[0] if self.townland.ed_id else None

            self.url_path = pathify(county_name, barony_name, civil_parish_name, ed_name, self.townland.name, name)

        else:
            self.url_path = "{0}".format(name)

        if self.unique_suffix:
            self.url_path += str(self.unique_suffix)

    @property
    def centre_pretty(self):
        return location_pretty(x=self.location_x, y=self.location_y)

    @property
    def osm_type(self):
        return "node"

    def __unicode__(self):
        return "{0} ({1})".format(self.name, self.osm_id)

    @property
    def tags(self):
        if self._tags_json == '':
            return {}
        else:
            return json.loads(self._tags_json)

    @property
    def old_names(self):

        return historic_names(self.tags)