# Path: irish_townlands/models.py
# class Townland(Area):
#     county = models.ForeignKey(County, related_name='townlands', null=True, db_index=True)
#     barony = models.ForeignKey(Barony, related_name='townlands', null=True)
#     civil_parish = models.ForeignKey(CivilParish, related_name='townlands', null=True)
#     ed = models.ForeignKey(ElectoralDivision, related_name='townlands', null=True)
#     name_entries = GenericRelation('NameEntry', related_query_name='townlands')
# 
#     def generate_url_path(self):
#         name = slugify(self.name.lower())
# 
#         county_name = County.objects.filter(id=self.county_id).values_list("name_tag", flat=True)[0] if self.county_id else None
#         civil_parish_name = CivilParish.objects.filter(id=self.civil_parish_id).values_list("name_tag", flat=True)[0] if self.civil_parish_id else None
#         barony_name = Barony.objects.filter(id=self.barony_id).values_list("name_tag", flat=True)[0] if self.barony_id else None
#         ed_name = ElectoralDivision.objects.filter(id=self.ed_id).values_list("name_tag", flat=True)[0] if self.ed_id else None
# 
#         self.url_path = pathify(county_name, barony_name, civil_parish_name, ed_name, name)
# 
#         if self.unique_suffix:
#             self.url_path += str(self.unique_suffix)
# 
# 
#     @property
#     def area_rank(self):
#         larger = Townland.objects.filter(area_m2__gt=self.area_m2).count()
#         rank = larger + 1
#         return rank
# 
#     @property
#     def area_rank_county(self):
#         assert self.county
#         larger = Townland.objects.filter(county=self.county, area_m2__gt=self.area_m2).count()
#         rank = larger + 1
#         return rank
# 
#     @property
#     def touching_townlands(self):
#         return self.touching_as_a.order_by("townland_b__name_tag")
# 
#     @property
#     def county_name(self):
#         counties = list(County.objects.filter(townlands__townland=self).values_list("name_tag", flat=True).distinct())
#         if len(counties) == 0:
#             return None
#         else:
#             return counties[0]


from django.conf import settings
from django.db import connection
from irish_townlands.models import Townland
from django.core.management.base import BaseCommand, CommandError
import subprocess
import os, os.path
import csv
import psycopg2
import django.template.loader
import hashlib
import colorsys
import random


def colour_for_user(username):
    random.seed(username)
    hue = random.random()
    rgb = colorsys.hls_to_rgb(hue, 0.66, 1)
    rgb = [int(rgb[0]*255), int(rgb[1]*255), int(rgb[2]*255)]
    rgb_string = "#{:x}{:x}{:x}".format(*rgb)

    return rgb_string

class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument("--write-mapnik-to")
        parser.add_argument("--shapefile")

    def handle(self, *args, **options):
        shapefile = options['shapefile']
        output_file = options['write_mapnik_to']

        # create shapefile
        subprocess.call(["pgsql2shp", "-f", shapefile, "townlands", "select osm_user, (st_dump(geom)).geom as geom from  (select osm_user, st_union(way) as geom from irish_townlands_townland join valid_polygon using (osm_id) group by osm_user) as t;"], stdout=subprocess.PIPE)

        # Create mapnik template

# Path: irish_townlands/models.py
# class Townland(Area):
#     county = models.ForeignKey(County, related_name='townlands', null=True, db_index=True)
#     barony = models.ForeignKey(Barony, related_name='townlands', null=True)
#     civil_parish = models.ForeignKey(CivilParish, related_name='townlands', null=True)
#     ed = models.ForeignKey(ElectoralDivision, related_name='townlands', null=True)
#     name_entries = GenericRelation('NameEntry', related_query_name='townlands')
# 
#     def generate_url_path(self):
#         name = slugify(self.name.lower())
# 
#         county_name = County.objects.filter(id=self.county_id).values_list("name_tag", flat=True)[0] if self.county_id else None
#         civil_parish_name = CivilParish.objects.filter(id=self.civil_parish_id).values_list("name_tag", flat=True)[0] if self.civil_parish_id else None
#         barony_name = Barony.objects.filter(id=self.barony_id).values_list("name_tag", flat=True)[0] if self.barony_id else None
#         ed_name = ElectoralDivision.objects.filter(id=self.ed_id).values_list("name_tag", flat=True)[0] if self.ed_id else None
# 
#         self.url_path = pathify(county_name, barony_name, civil_parish_name, ed_name, name)
# 
#         if self.unique_suffix:
#             self.url_path += str(self.unique_suffix)
# 
# 
#     @property
#     def area_rank(self):
#         larger = Townland.objects.filter(area_m2__gt=self.area_m2).count()
#         rank = larger + 1
#         return rank
# 
#     @property
#     def area_rank_county(self):
#         assert self.county
#         larger = Townland.objects.filter(county=self.county, area_m2__gt=self.area_m2).count()
#         rank = larger + 1
#         return rank
# 
#     @property
#     def touching_townlands(self):
#         return self.touching_as_a.order_by("townland_b__name_tag")
# 
#     @property
#     def county_name(self):
#         counties = list(County.objects.filter(townlands__townland=self).values_list("name_tag", flat=True).distinct())
#         if len(counties) == 0:
#             return None
#         else:
#             return counties[0]


# Path: irish_townlands/management/commands/make_per_user_map_style.py
from django.conf import settings
from django.db import connection
from irish_townlands.models import Townland
from django.core.management.base import BaseCommand, CommandError
import subprocess
import os, os.path
import csv
import psycopg2
import django.template.loader
import hashlib
import colorsys
import random



def colour_for_user(username):
    random.seed(username)
    hue = random.random()
    rgb = colorsys.hls_to_rgb(hue, 0.66, 1)
    rgb = [int(rgb[0]*255), int(rgb[1]*255), int(rgb[2]*255)]
    rgb_string = "#{:x}{:x}{:x}".format(*rgb)

    return rgb_string

class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument("--write-mapnik-to")
        parser.add_argument("--shapefile")

    def handle(self, *args, **options):
        shapefile = options['shapefile']
        output_file = options['write_mapnik_to']

        # create shapefile
        subprocess.call(["pgsql2shp", "-f", shapefile, "townlands", "select osm_user, (st_dump(geom)).geom as geom from  (select osm_user, st_union(way) as geom from irish_townlands_townland join valid_polygon using (osm_id) group by osm_user) as t;"], stdout=subprocess.PIPE)

        # Create mapnik template

        users = list(Townland.objects.values_list("osm_user", flat=True).distinct())