"""
Import Enfield
"""
from time import sleep

from django.contrib.gis.geos import Point

from data_collection.management.commands import BaseCsvStationsCsvAddressesImporter
from data_finder.helpers import geocode, geocode_point_only, PostcodeError
from addressbase.models import Address


class Command(BaseCsvStationsCsvAddressesImporter):
    """
    Imports the Polling Station data from Enfield Council
    """
    council_id      = 'E09000010'
    addresses_name  = 'PropertyPostCodePollingStationWebLookup-2016-06-09.TSV'
    stations_name   = 'PropertyPostCodePollingStationWebLookup-2016-06-09.TSV'
    csv_delimiter   = '\t'
    elections       = [
        'ref.2016-06-23'
    ]

    def get_station_hash(self, record):
        return "-".join([
            record.pollingplaceaddress7,
            record.pollingplaceid,
        ])

    def station_record_to_dict(self, record):
        # format address
        address = "\n".join([
            record.pollingplaceaddress1,
            record.pollingplaceaddress2,
            record.pollingplaceaddress3,
            record.pollingplaceaddress4,
            record.pollingplaceaddress5,
            record.pollingplaceaddress6,
            record.pollingplaceaddress7,
        ])
        while "\n\n" in address:
            address = address.replace("\n\n", "\n").strip()

        location = None
        if int(record.pollingplaceeasting) and int(record.pollingplacenorthing):
            location = Point(
                float(record.pollingplaceeasting),
                float(record.pollingplacenorthing),
                srid=27700)
        else:
            # no points supplied, so attempt to attach them by geocoding
            try:
                location_data = geocode_point_only(record.pollingplaceaddress7)
            except PostcodeError:
                pass

            if location_data:
                location = Point(
                    location_data['wgs84_lon'],
                    location_data['wgs84_lat'],
                    srid=4326)

        return {
            'internal_council_id': record.pollingplaceid,
            'postcode'           : record.pollingplaceaddress7,
            'address'            : address,
            'location'           : location
        }

    def address_record_to_dict(self, record):
        if record.propertynumber.strip() == '0':
            address = record.streetname.strip()
        else:
            address = '%s %s' % (
                record.propertynumber.strip(), record.streetname.strip())

        return {
            'address'           : address,
            'postcode'          : record.postcode.strip(),
            'polling_station_id': record.pollingplaceid
        }

@@ -1,419 +1,138 @@
-%22%22%22%0AImport Enfield%0A%22%22%22%0Afrom time import sleep%0A%0Afrom django.contrib.gis.geos import Point%0A%0Afrom data_collection.management.commands import BaseCsvStationsCsvAddressesImporter%0Afrom data_finder.helpers import geocode, geocode_point_only, PostcodeError%0Afrom addressbase.models import Address%0A%0A%0Aclass Command(BaseCsvStationsCsvAddressesImporter):%0A    %22%22%22%0A    Imports the Polling Station data from Enfield Council%0A    %22%22%22
+from data_collection.management.commands import BaseXpressDemocracyClubCsvImporter%0A%0Aclass Command(BaseXpressDemocracyClubCsvImporter):
 %0A   
@@ -143,21 +143,16 @@
 ncil_id 
-     
 = 'E0900
@@ -180,2260 +180,215 @@
 ame 
- 
 = '
-PropertyPostCodePollingStationWebLookup-2016
+parl.2017
 -06-0
-9.TSV'%0A    stations_name   = 'PropertyPostCodePollingStationWebLookup-2016-06-09.TSV'%0A    csv_delimiter   = '%5Ct'%0A    elections       = %5B%0A        'ref.2016-06-23'%0A    %5D%0A%0A    def get_station_hash(self, record):%0A        return %22-%22.join(%5B%0A            record.pollingplaceaddress7,%0A            record.pollingplaceid,%0A        %5D)%0A%0A    def station_record_to_dict(self, record):%0A        # format address%0A        address = %22%5Cn%22.join(%5B%0A            record.pollingplaceaddress1,%0A            record.pollingplaceaddress2,%0A            record.pollingplaceaddress3,%0A            record.pollingplaceaddress4,%0A            record.pollingplaceaddress5,%0A            record.pollingplaceaddress6,%0A            record.pollingplaceaddress7,%0A        %5D)%0A        while %22%5Cn%5Cn%22 in address:%0A            address = address.replace(%22%5Cn%5Cn%22, %22%5Cn%22).strip()%0A%0A        location = None%0A        if int(record.pollingplaceeasting) and int(record.pollingplacenorthing):%0A            location = Point(%0A                float(record.pollingplaceeasting),%0A                float(record.pollingplacenorthing),%0A                srid=27700)%0A        else:%0A            # no points supplied, so attempt to attach them by geocoding%0A            try:%0A                location_data = geocode_point_only(record.pollingplaceaddress7)%0A            except PostcodeError:%0A                pass%0A%0A            if location_data:%0A                location = Point(%0A                    location_data%5B'wgs84_lon'%5D,%0A                    location_data%5B'wgs84_lat'%5D,%0A                    srid=4326)%0A%0A        return %7B%0A            'internal_council_id': record.pollingplaceid,%0A            'postcode'           : record.pollingplaceaddress7,%0A            'address'            : address,%0A            'location'           : location%0A        %7D%0A%0A    def address_record_to_dict(self, record):%0A        if record.propertynumber.strip() == '0':%0A            address = record.streetname.strip()%0A        else:%0A            address = '%25s %25s' %25 (%0A                record.propertynumber.strip(), record.streetname.strip())%0A%0A        return %7B%0A            'address'           : address,%0A            'postcode'          : record.postcode.strip(),%0A            'polling_station_id': record.pollingplaceid%0A        %7D
+8/Version 1/Democracy_Club__08June2017 (20).tsv'%0A    stations_name = 'parl.2017-06-08/Version 1/Democracy_Club__08June2017 (20).tsv'%0A    elections = %5B'parl.2017-06-08'%5D%0A    csv_delimiter = '%5Ct'
 %0A

update Enfield import script for parl.2017-06-08