<commit_before>"""
Import Cardiff
"""
from django.db import transaction
from django.db import connection

from data_collection.management.commands import BaseShpShpImporter
from pollingstations.models import PollingDistrict


class Command(BaseShpShpImporter):
    """
    Imports the Polling Station data from Braintree
    """
    council_id     = 'W06000015'
    districts_name = 'Polling Districts_region'
    stations_name  = 'Polling Stations_font_point.shp'

    def district_record_to_dict(self, record):
        if type(record[1]) == str:
            name = record[1]
        else:
            name = record[1].decode('latin-1')
        return {
            'internal_council_id': record[0].encode('utf8'),
            'name': name
        }

    def station_record_to_dict(self, record):
        try:
            return {
                'internal_council_id': record[0],
                'postcode'           : "",
                'address'            : str(record[2]),
                'polling_district_id': str(record[1]),
            }
        except TypeError:
            # there is a single bad row, it seems
            pass

    @transaction.atomic
    def post_import(self):
        """
        This data isn't great – the polygons seem to be corrupt in some way.

        PostGIS can fix them though!
        """
        print("running fixup SQL")
        table_name = PollingDistrict()._meta.db_table

        cursor = connection.cursor()
        cursor.execute("""
        UPDATE {0}
         SET area=ST_Multi(ST_CollectionExtract(ST_MakeValid(area), 3))
         WHERE NOT ST_IsValid(area);
        """.format(table_name))
        # Note the delibarate use of `.format` above – we don't want the table
        # names in quotes.  Use `%s` and a list as a 2nd arg to execute
        # if you're adding values at all.
<commit_msg>The bad line isn't there any more !<commit_after>"""
Import Cardiff
"""
from django.db import transaction
from django.db import connection

from data_collection.management.commands import BaseShpShpImporter
from pollingstations.models import PollingDistrict


class Command(BaseShpShpImporter):
    """
    Imports the Polling Station data from Braintree
    """
    council_id     = 'W06000015'
    districts_name = 'Polling Districts_region'
    stations_name  = 'Polling Stations_font_point.shp'

    def district_record_to_dict(self, record):
        if type(record[1]) == str:
            name = record[1]
        else:
            name = record[1].decode('latin-1')
        return {
            'internal_council_id': record[0].encode('utf8'),
            'name': name
        }

    def station_record_to_dict(self, record):
        return {
            'internal_council_id': record[0],
            'postcode'           : "",
            'address'            : str(record[2]),
            'polling_district_id': str(record[1]),
        }

    @transaction.atomic
    def post_import(self):
        """
        This data isn't great – the polygons seem to be corrupt in some way.

        PostGIS can fix them though!
        """
        print("running fixup SQL")
        table_name = PollingDistrict()._meta.db_table

        cursor = connection.cursor()
        cursor.execute("""
        UPDATE {0}
         SET area=ST_Multi(ST_CollectionExtract(ST_MakeValid(area), 3))
         WHERE NOT ST_IsValid(area);
        """.format(table_name))
        # Note the delibarate use of `.format` above – we don't want the table
        # names in quotes.  Use `%s` and a list as a 2nd arg to execute
        # if you're adding values at all.

{'fraction_non_alphanumeric': 0.09844271412680757, 'fraction_numerical': 0.011123470522803115, 'mean_word_length': 2.208742194469224, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '19227', 'n_tokens_mistral': 1083, 'n_tokens_neox': 1027, 'n_words': 289}