"""
Imports Mid Sussex
"""
import sys

from django.contrib.gis.geos import Point, GEOSGeometry

from data_collection.management.commands import BaseKamlImporter

class Command(BaseKamlImporter):
    """
    Imports the Polling Station data from Mid Sussex
    """
    council_id     = 'E07000228'
    districts_name = 'msdc_3830_pollingdistricts_polygon.kmz'
    stations_name  = 'R3900_pollingstations.csv'

    def station_record_to_dict(self, record):
        location = Point(float(record.xcoord), float(record.ycoord), srid=self.srid)
        address = "\n".join([record.venue, record.street, record.town])
        return {
            'internal_council_id': record.statnum,
            'postcode':            record.postcode,
            'address':             address,
            'location':            location
        }

"""
Imports Mid Sussex
"""
import sys
from lxml import etree
from django.contrib.gis.geos import Point, GEOSGeometry
from data_collection.management.commands import BaseKamlImporter

class Command(BaseKamlImporter):
    """
    Imports the Polling Station data from Mid Sussex
    """
    council_id     = 'E07000228'
    districts_name = 'msdc_3830_pollingdistricts_polygon.kmz'
    stations_name  = 'R3900_pollingstations.csv'

    def extract_msercode_from_description(self, description):
        html = etree.HTML(str(description).replace('&', '&amp;'))
        rows = html.xpath("//td")
        return rows[7].text

    def district_record_to_dict(self, record):
        msercode = self.extract_msercode_from_description(record['description'])
        geojson = self.strip_z_values(record.geom.geojson)
        poly = self.clean_poly(GEOSGeometry(geojson, srid=self.get_srid('districts')))
        return {
            'internal_council_id': msercode,
            'name'               : record['Name'].value,
            'area'               : poly
        }

    def station_record_to_dict(self, record):
        location = Point(float(record.xcoord), float(record.ycoord), srid=self.srid)
        address = "\n".join([record.venue, record.street, record.town])
        return {
            'internal_council_id': record.msercode,
            'postcode':            record.postcode,
            'address':             address,
            'location':            location,
            'polling_district_id': record.msercode
        }

Fix Mid Sussex Import script
Fix Mid Sussex Import script

Set polling_district_id
Use mserid as internal_council_id to avoid importing duplicate points
