From deb2022216add9c5fb881728881d4e8a39ddfaee Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Wed, 25 May 2016 15:58:35 +0100 Subject: [PATCH 1/5] Use more modern way of having custom query set. --- mapit/models.py | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/mapit/models.py b/mapit/models.py index a189de9a..71aa9b91 100644 --- a/mapit/models.py +++ b/mapit/models.py @@ -411,12 +411,20 @@ def __str__(self): # Postcodes -class PostcodeManager(models.GeoManager): - def get_queryset(self): - return self.model.QuerySet(self.model) - - def __getattr__(self, attr, *args): - return getattr(self.get_queryset(), attr, *args) +class PostcodeQuerySet(models.query.GeoQuerySet): + # ST_CoveredBy on its own does not appear to use the index. + # Plus this way we can keep the polygons in the database + # without pulling out in a giant WKB string + def filter_by_area(self, area): + collect = '''ST_Transform((select ST_Collect(polygon) from mapit_geometry + where area_id=%s group by area_id), 4326)''' + return self.extra( + where=[ + 'location && %s' % collect, + 'ST_CoveredBy(location, %s)' % collect + ], + params=[area.id, area.id] + ) @python_2_unicode_compatible @@ -426,26 +434,11 @@ class Postcode(models.Model): # Will hopefully use PostGIS point-in-polygon tests, but if we don't have the polygons... areas = models.ManyToManyField(Area, related_name='postcodes', blank=True) - objects = PostcodeManager() + objects = PostcodeQuerySet.as_manager() class Meta: ordering = ('postcode',) - class QuerySet(models.query.GeoQuerySet): - # ST_CoveredBy on its own does not appear to use the index. - # Plus this way we can keep the polygons in the database - # without pulling out in a giant WKB string - def filter_by_area(self, area): - collect = '''ST_Transform((select ST_Collect(polygon) from mapit_geometry - where area_id=%s group by area_id), 4326)''' - return self.extra( - where=[ - 'location && %s' % collect, - 'ST_CoveredBy(location, %s)' % collect - ], - params=[area.id, area.id] - ) - def __str__(self): return self.get_postcode_display() From bad882e61cbd1c530dd236c0d934ed33b8979779 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Wed, 25 May 2016 16:15:19 +0100 Subject: [PATCH 2/5] Specify default on_delete argument to ForeignKeys. --- mapit/migrations/0001_initial.py | 20 ++++++++++---------- mapit/models.py | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/mapit/migrations/0001_initial.py b/mapit/migrations/0001_initial.py index e26786c7..eebbf5ad 100644 --- a/mapit/migrations/0001_initial.py +++ b/mapit/migrations/0001_initial.py @@ -28,7 +28,7 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('code', models.CharField(max_length=500)), - ('area', models.ForeignKey(related_name='codes', to='mapit.Area')), + ('area', models.ForeignKey(related_name='codes', to='mapit.Area', on_delete=models.CASCADE)), ], options={ }, @@ -74,7 +74,7 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('polygon', django.contrib.gis.db.models.fields.PolygonField(srid=settings.MAPIT_AREA_SRID)), - ('area', models.ForeignKey(related_name='polygons', to='mapit.Area')), + ('area', models.ForeignKey(related_name='polygons', to='mapit.Area', on_delete=models.CASCADE)), ], options={ 'verbose_name_plural': 'geometries', @@ -86,7 +86,7 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('name', models.CharField(max_length=2000)), - ('area', models.ForeignKey(related_name='names', to='mapit.Area')), + ('area', models.ForeignKey(related_name='names', to='mapit.Area', on_delete=models.CASCADE)), ], options={ }, @@ -130,7 +130,7 @@ class Migration(migrations.Migration): migrations.AddField( model_name='name', name='type', - field=models.ForeignKey(related_name='names', to='mapit.NameType'), + field=models.ForeignKey(related_name='names', to='mapit.NameType', on_delete=models.CASCADE), preserve_default=True, ), migrations.AlterUniqueTogether( @@ -140,7 +140,7 @@ class Migration(migrations.Migration): migrations.AddField( model_name='code', name='type', - field=models.ForeignKey(related_name='codes', to='mapit.CodeType'), + field=models.ForeignKey(related_name='codes', to='mapit.CodeType', on_delete=models.CASCADE), preserve_default=True, ), migrations.AlterUniqueTogether( @@ -150,31 +150,31 @@ class Migration(migrations.Migration): migrations.AddField( model_name='area', name='country', - field=models.ForeignKey(related_name='areas', blank=True, to='mapit.Country', null=True), + field=models.ForeignKey(related_name='areas', blank=True, to='mapit.Country', null=True, on_delete=models.CASCADE), preserve_default=True, ), migrations.AddField( model_name='area', name='generation_high', - field=models.ForeignKey(related_name='final_areas', to='mapit.Generation', null=True), + field=models.ForeignKey(related_name='final_areas', to='mapit.Generation', null=True, on_delete=models.CASCADE), preserve_default=True, ), migrations.AddField( model_name='area', name='generation_low', - field=models.ForeignKey(related_name='new_areas', to='mapit.Generation', null=True), + field=models.ForeignKey(related_name='new_areas', to='mapit.Generation', null=True, on_delete=models.CASCADE), preserve_default=True, ), migrations.AddField( model_name='area', name='parent_area', - field=models.ForeignKey(related_name='children', blank=True, to='mapit.Area', null=True), + field=models.ForeignKey(related_name='children', blank=True, to='mapit.Area', null=True, on_delete=models.CASCADE), preserve_default=True, ), migrations.AddField( model_name='area', name='type', - field=models.ForeignKey(related_name='areas', to='mapit.Type'), + field=models.ForeignKey(related_name='areas', to='mapit.Type', on_delete=models.CASCADE), preserve_default=True, ), ] diff --git a/mapit/models.py b/mapit/models.py index 71aa9b91..358637a8 100644 --- a/mapit/models.py +++ b/mapit/models.py @@ -221,11 +221,11 @@ def get_or_create_with_code(self, country=None, type=None, code_type='', code='' @python_2_unicode_compatible class Area(models.Model): name = models.CharField(max_length=2000, blank=True) - parent_area = models.ForeignKey('self', related_name='children', null=True, blank=True) - type = models.ForeignKey(Type, related_name='areas') - country = models.ForeignKey(Country, related_name='areas', null=True, blank=True) - generation_low = models.ForeignKey(Generation, related_name='new_areas', null=True) - generation_high = models.ForeignKey(Generation, related_name='final_areas', null=True) + parent_area = models.ForeignKey('self', related_name='children', null=True, blank=True, on_delete=models.CASCADE) + type = models.ForeignKey(Type, related_name='areas', on_delete=models.CASCADE) + country = models.ForeignKey(Country, related_name='areas', null=True, blank=True, on_delete=models.CASCADE) + generation_low = models.ForeignKey(Generation, related_name='new_areas', null=True, on_delete=models.CASCADE) + generation_high = models.ForeignKey(Generation, related_name='final_areas', null=True, on_delete=models.CASCADE) objects = AreaManager() @@ -325,7 +325,7 @@ def export(self, @python_2_unicode_compatible class Geometry(models.Model): - area = models.ForeignKey(Area, related_name='polygons') + area = models.ForeignKey(Area, related_name='polygons', on_delete=models.CASCADE) polygon = models.PolygonField(srid=settings.MAPIT_AREA_SRID) objects = models.GeoManager() @@ -357,8 +357,8 @@ def __str__(self): @python_2_unicode_compatible class Name(models.Model): - area = models.ForeignKey(Area, related_name='names') - type = models.ForeignKey(NameType, related_name='names') + area = models.ForeignKey(Area, related_name='names', on_delete=models.CASCADE) + type = models.ForeignKey(NameType, related_name='names', on_delete=models.CASCADE) name = models.CharField(max_length=2000) objects = models.Manager() @@ -397,8 +397,8 @@ def __str__(self): @python_2_unicode_compatible class Code(models.Model): - area = models.ForeignKey(Area, related_name='codes') - type = models.ForeignKey(CodeType, related_name='codes') + area = models.ForeignKey(Area, related_name='codes', on_delete=models.CASCADE) + type = models.ForeignKey(CodeType, related_name='codes', on_delete=models.CASCADE) code = models.CharField(max_length=500) objects = models.Manager() From 5f0f33c48aff5aed24ac6c37762b4693cd1523ed Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Mon, 15 Aug 2016 10:31:42 +0100 Subject: [PATCH 3/5] Move custom management commands to argparse. --- .../mapit_delete_areas_from_new_generation.py | 11 ++- .../commands/mapit_generation_activate.py | 11 ++- .../commands/mapit_generation_create.py | 13 ++-- .../commands/mapit_generation_deactivate.py | 11 ++- ...mapit_generation_raise_on_current_areas.py | 15 ++-- mapit/management/commands/mapit_import.py | 70 +++++++++---------- .../commands/mapit_import_area_unions.py | 8 +-- .../commands/mapit_import_postal_codes.py | 38 +++++----- .../commands/mapit_make_fusion_csv.py | 23 +++--- .../management/commands/mapit_print_areas.py | 6 +- mapit/management/find_parents.py | 13 ++-- .../mapit_UK_add_2016_forward_dated_codes.py | 6 +- .../mapit_UK_add_gss_codes_to_ni_areas.py | 6 +- .../mapit_UK_add_names_to_ni_areas.py | 6 +- .../commands/mapit_UK_add_ons_to_gss.py | 6 +- .../commands/mapit_UK_fix_2011-10.py | 8 +-- .../commands/mapit_UK_fix_2012-05.py | 8 +-- .../mapit_UK_fix_2013-10-south-tynedale.py | 13 ++-- .../commands/mapit_UK_fix_2013-10.py | 8 +-- .../commands/mapit_UK_fix_2014-05-torfaen.py | 13 ++-- .../commands/mapit_UK_fix_2014-05.py | 13 ++-- .../commands/mapit_UK_fix_2016-05.py | 13 ++-- .../commands/mapit_UK_import_2011_scotparl.py | 8 +-- .../commands/mapit_UK_import_boundary_line.py | 12 ++-- .../mapit_UK_import_ni_output_areas.py | 9 ++- .../commands/mapit_UK_import_nspd_ni_areas.py | 6 +- .../commands/mapit_UK_import_onspd.py | 29 ++++---- .../commands/mapit_UK_import_osni.py | 57 ++++++++------- .../mapit_UK_import_police_force_areas.py | 39 +++++------ .../mapit_UK_ni_consolidate_boundaries.py | 13 ++-- .../management/commands/mapit_UK_scilly.py | 14 ++-- .../commands/mapit_UK_update_ons_ids.py | 6 +- .../commands/mapit_UK_update_ons_ids2.py | 6 +- .../commands/mapit_global_import.py | 8 +-- .../commands/mapit_global_oneoff_fix_gen4.py | 8 +-- .../commands/mapit_use_osm_place_name.py | 8 +-- .../commands/mapit_NO_import_osm.py | 8 +-- .../commands/mapit_za_import_boundaries.py | 33 +++------ 38 files changed, 275 insertions(+), 308 deletions(-) diff --git a/mapit/management/commands/mapit_delete_areas_from_new_generation.py b/mapit/management/commands/mapit_delete_areas_from_new_generation.py index a7038f01..3750bb9c 100644 --- a/mapit/management/commands/mapit_delete_areas_from_new_generation.py +++ b/mapit/management/commands/mapit_delete_areas_from_new_generation.py @@ -3,17 +3,16 @@ from __future__ import print_function -from optparse import make_option -from django.core.management.base import NoArgsCommand, CommandError +from django.core.management.base import BaseCommand, CommandError from mapit.models import Generation, Area -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Remove all areas from the new (inactive) generation' args = '' - option_list = NoArgsCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', - help='Actually update the database'),) + + def add_arguments(self, parser): + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') def handle(self, **options): new = Generation.objects.new() diff --git a/mapit/management/commands/mapit_generation_activate.py b/mapit/management/commands/mapit_generation_activate.py index 5155cd9d..aeec6351 100644 --- a/mapit/management/commands/mapit_generation_activate.py +++ b/mapit/management/commands/mapit_generation_activate.py @@ -1,15 +1,14 @@ # This script activates the currently inactive generation. -from optparse import make_option -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Generation -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Actives the inactive generation' - option_list = NoArgsCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') def handle(self, **options): new = Generation.objects.new() diff --git a/mapit/management/commands/mapit_generation_create.py b/mapit/management/commands/mapit_generation_create.py index 5d698929..93d56eda 100644 --- a/mapit/management/commands/mapit_generation_create.py +++ b/mapit/management/commands/mapit_generation_create.py @@ -1,17 +1,16 @@ # This script is used to create a new inactive generation for # inputting new boundaries of some sort. -from optparse import make_option -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Generation -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Create a new generation' - option_list = NoArgsCommand.option_list + ( - make_option('--desc', action='store', dest='desc', help='Description of this generation'), - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + parser.add_argument('--desc', action='store', dest='desc', help='Description of this generation') + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') def handle(self, **options): new_generation = Generation.objects.new() diff --git a/mapit/management/commands/mapit_generation_deactivate.py b/mapit/management/commands/mapit_generation_deactivate.py index ced00ce0..2511d7b2 100644 --- a/mapit/management/commands/mapit_generation_deactivate.py +++ b/mapit/management/commands/mapit_generation_deactivate.py @@ -1,6 +1,5 @@ # This script deactivates a particular generation -from optparse import make_option from django.core.management.base import BaseCommand, CommandError from mapit.models import Generation @@ -8,11 +7,11 @@ class Command(BaseCommand): help = 'Deactivate a generation' args = '' - option_list = BaseCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', - help='Actually update the database'), - make_option('--force', action='store_true', dest='force', - help='Force deactivation, even if it would leave no active generations')) + + def add_arguments(self, parser): + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') + parser.add_argument('--force', action='store_true', dest='force', + help='Force deactivation, even if it would leave no active generations') def handle(self, generation_id, **options): generation_to_deactivate = Generation.objects.get(id=int(generation_id, 10)) diff --git a/mapit/management/commands/mapit_generation_raise_on_current_areas.py b/mapit/management/commands/mapit_generation_raise_on_current_areas.py index 6c2afcae..513ce0ec 100644 --- a/mapit/management/commands/mapit_generation_raise_on_current_areas.py +++ b/mapit/management/commands/mapit_generation_raise_on_current_areas.py @@ -6,8 +6,6 @@ # safe way of raising the generation_high of all active areas to the # ID of the new inactive generation. -from optparse import make_option - from django.core.management.base import BaseCommand, CommandError from mapit.models import Area, Generation, Type, Country @@ -36,14 +34,11 @@ def check_option(option_name, options, model_class): class Command(BaseCommand): help = "Raise generation_high on active areas to the new generation's ID" - option_list = BaseCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', - help='Actually update the database'), - make_option('--country', - help='Only raise the generation on areas with this country code'), - make_option('--type', - help='Only raise the generation on areas with this area type code'), - ) + + def add_arguments(self, parser): + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') + parser.add_argument('--country', help='Only raise the generation on areas with this country code') + parser.add_argument('--type', help='Only raise the generation on areas with this area type code') def handle(self, **options): area_type = check_option('type', options, Type) diff --git a/mapit/management/commands/mapit_import.py b/mapit/management/commands/mapit_import.py index fccbe884..9c86a2fa 100644 --- a/mapit/management/commands/mapit_import.py +++ b/mapit/management/commands/mapit_import.py @@ -5,7 +5,6 @@ # Email: matthew@mysociety.org; WWW: http://www.mysociety.org import re -from optparse import make_option from django.core.management.base import LabelCommand, CommandError # Not using LayerMapping as want more control, but what it does is what this does @@ -22,103 +21,104 @@ class Command(LabelCommand): help = 'Import geometry data from .shp, .kml or .geojson files' args = '' - option_list = LabelCommand.option_list + ( - make_option( + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument( '--commit', action='store_true', dest='commit', help='Actually update the database' - ), - make_option( + ) + parser.add_argument( '--generation_id', action="store", dest='generation_id', help='Which generation ID should be used', - ), - make_option( + ) + parser.add_argument( '--country_code', action="store", dest='country_code', help='Which country should be used', - ), - make_option( + ) + parser.add_argument( '--area_type_code', action="store", dest='area_type_code', help='Which area type should be used (specify using code)', - ), - make_option( + ) + parser.add_argument( '--name_type_code', action="store", dest='name_type_code', help='Which name type should be used (specify using code)', - ), - make_option( + ) + parser.add_argument( '--code_type', action="store", dest='code_type', help='Which code type should be used (specify using its code)', - ), - make_option( + ) + parser.add_argument( '--name_field', action="store", dest='name_field', help="The field name (or names separated by comma) to look at for the area's name" - ), - make_option( + ) + parser.add_argument( '--override_name', action="store", dest='override_name', help="The name to use for the area" - ), - make_option( + ) + parser.add_argument( '--code_field', action="store", dest='code_field', help="The field name containing the area's ID code" - ), - make_option( + ) + parser.add_argument( '--override_code', action="store", dest='override_code', help="The ID code to use for the area" - ), - make_option( + ) + parser.add_argument( '--use_code_as_id', action="store_true", dest='use_code_as_id', help="Set to use the code from code_field as the MapIt ID" - ), - make_option( + ) + parser.add_argument( '--preserve', action="store_true", dest='preserve', help="Create a new area if the name's the same but polygons differ" - ), - make_option( + ) + parser.add_argument( '--new', action="store_true", dest='new', help="Don't look for existing areas at all, just import everything as new areas" - ), - make_option( + ) + parser.add_argument( '--encoding', action="store", dest='encoding', help="The encoding of names in this dataset" - ), - make_option( + ) + parser.add_argument( '--fix_invalid_polygons', action="store_true", dest='fix_invalid_polygons', help="Try to fix any invalid polygons and multipolygons found" - ), - make_option( + ) + parser.add_argument( '--ignore_blank', action="store_true", help="Skip over any entry with an empty name, rather than abort" - ), - ) + ) def handle_label(self, filename, **options): diff --git a/mapit/management/commands/mapit_import_area_unions.py b/mapit/management/commands/mapit_import_area_unions.py index 64f5e1fe..26426227 100644 --- a/mapit/management/commands/mapit_import_area_unions.py +++ b/mapit/management/commands/mapit_import_area_unions.py @@ -6,7 +6,6 @@ # the GPL. Based on import_norway_osm.py by Matthew Somerville import csv -from optparse import make_option from django.core.management.base import LabelCommand from django.contrib.gis.geos import GEOSGeometry @@ -39,9 +38,10 @@ def __iter__(self): class Command(LabelCommand): help = 'Import region data' args = '' - option_list = LabelCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') def handle_label(self, filename, **options): current_generation = Generation.objects.current() diff --git a/mapit/management/commands/mapit_import_postal_codes.py b/mapit/management/commands/mapit_import_postal_codes.py index a7768c2e..ee66489d 100644 --- a/mapit/management/commands/mapit_import_postal_codes.py +++ b/mapit/management/commands/mapit_import_postal_codes.py @@ -5,7 +5,6 @@ # the command line import csv -from optparse import make_option from django.db import transaction from django.contrib.gis.geos import Point from django.core.management.base import LabelCommand @@ -20,64 +19,65 @@ class Command(LabelCommand): often = 1000 option_defaults = {} - option_list = LabelCommand.option_list + ( - make_option( + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument( '--code-field', action='store', dest='code-field', default=1, help='The column of the CSV containing the postal code (default 1, first)' - ), - make_option( + ) + parser.add_argument( '--coord-field-lat', action='store', dest='coord-field-lat', default=2, help='The column of the CSV containing the lat/y co-ordinate (default 2)' - ), - make_option( + ) + parser.add_argument( '--coord-field-lon', action='store', dest='coord-field-lon', default=None, help='The column of the CSV containing the lon/x co-ordinate (default --coord-field-lat + 1)' - ), - make_option( + ) + parser.add_argument( '--header-row', action='store_true', dest='header-row', default=False, help='Set if the CSV file has a header row' - ), - make_option( + ) + parser.add_argument( '--no-location', action="store_false", dest='location', default=True, help='Set if the postal codes have no associated location (still useful for existence checks)' - ), - make_option( + ) + parser.add_argument( '--srid', action="store", dest='srid', default=4326, help='The SRID of the projection for the data given (default 4326 WGS-84)' - ), - make_option( + ) + parser.add_argument( '--strip', action="store_true", dest='strip', default=False, help='Whether to strip all spaces from the postal code before import' - ), - make_option( + ) + parser.add_argument( '--tabs', action="store_true", dest='tabs', default=False, help='If the CSV file actually uses tab as its separator' - ), - ) + ) def handle_label(self, file, **options): self.process(file, options) diff --git a/mapit/management/commands/mapit_make_fusion_csv.py b/mapit/management/commands/mapit_make_fusion_csv.py index 0b8231ff..571f59fa 100644 --- a/mapit/management/commands/mapit_make_fusion_csv.py +++ b/mapit/management/commands/mapit_make_fusion_csv.py @@ -55,7 +55,6 @@ import sys import csv -from optparse import make_option from random import random, seed import colorsys @@ -85,26 +84,26 @@ def all_equal(iterator): class Command(BaseCommand): help = 'Generate a CSV file for Google Fusion Tables from MapIt' - option_list = BaseCommand.option_list + ( - make_option( + + def add_arguments(self, parser): + parser.add_argument( "--types", dest="types", help="The comma-separated types of the areas to return", - metavar="TYPES"), - make_option( + metavar="TYPES") + parser.add_argument( "--coveredby", dest="coveredby", type="int", help="Only include areas covered by AREA-ID", - metavar="AREA-ID"), - make_option( + metavar="AREA-ID") + parser.add_argument( "--generation", dest="generation", - help="Specify the generation number", metavar="AREA-ID"), - make_option( + help="Specify the generation number", metavar="AREA-ID") + parser.add_argument( "--tolerance", dest="tolerance", type="float", default=0.0001, - help="Specify the simplifiy tolerance (default: 0.0001)", metavar="TOLERANCE"), - ) + help="Specify the simplifiy tolerance (default: 0.0001)", metavar="TOLERANCE") def handle(self, *args, **options): - # To add a new query type, add it to this tuple, and option_list above: + # To add a new query type, add it to this tuple, and add_arguments above: possible_query_types = ('coveredby',) if len(args) != 1: diff --git a/mapit/management/commands/mapit_print_areas.py b/mapit/management/commands/mapit_print_areas.py index b66691fe..5dec78bf 100644 --- a/mapit/management/commands/mapit_print_areas.py +++ b/mapit/management/commands/mapit_print_areas.py @@ -1,13 +1,13 @@ # For each generation, show every area, grouped by type -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, Generation, Type -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Show all areas by generation and area type' - def handle_noargs(self, **options): + def handle(self, **options): for g in Generation.objects.all().order_by('id'): print(g) for t in Type.objects.all().order_by('code'): diff --git a/mapit/management/find_parents.py b/mapit/management/find_parents.py index 153d913c..109268b0 100644 --- a/mapit/management/find_parents.py +++ b/mapit/management/find_parents.py @@ -2,22 +2,21 @@ # their "parents". Provide a "parentmap" in your subclass mapping child area # type to parent area type. -from optparse import make_option -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, Generation -class FindParentsCommand(NoArgsCommand): +class FindParentsCommand(BaseCommand): help = 'Find parents for shapes' - option_list = NoArgsCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') @property def parentmap(self): raise NotImplementedError("You must specify a parentmap attribute in your subclass") - def handle_noargs(self, **options): + def handle(self, **options): new_generation = Generation.objects.new() if not new_generation: raise Exception("No new generation to be used for import!") diff --git a/mapit_gb/management/commands/mapit_UK_add_2016_forward_dated_codes.py b/mapit_gb/management/commands/mapit_UK_add_2016_forward_dated_codes.py index 11781837..5e005cff 100644 --- a/mapit_gb/management/commands/mapit_UK_add_2016_forward_dated_codes.py +++ b/mapit_gb/management/commands/mapit_UK_add_2016_forward_dated_codes.py @@ -2,14 +2,14 @@ import re import sys -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, Generation, CodeType -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Adds GSS codes to new wards available in 2016 OS forward dated polygons' - def handle_noargs(self, **options): + def handle(self, **options): code_type = CodeType.objects.get(code='gss') generation = Generation.objects.current() diff --git a/mapit_gb/management/commands/mapit_UK_add_gss_codes_to_ni_areas.py b/mapit_gb/management/commands/mapit_UK_add_gss_codes_to_ni_areas.py index 67acca54..63e8133f 100644 --- a/mapit_gb/management/commands/mapit_UK_add_gss_codes_to_ni_areas.py +++ b/mapit_gb/management/commands/mapit_UK_add_gss_codes_to_ni_areas.py @@ -5,15 +5,15 @@ import csv import os.path -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, Generation, Country, CodeType from django.utils import six -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Uses fixtures to find NI areas and add any missing GSS codes to them' - def handle_noargs(self, **options): + def handle(self, **options): try: self.latest_generation = Generation.objects.order_by('-id')[0] except IndexError: diff --git a/mapit_gb/management/commands/mapit_UK_add_names_to_ni_areas.py b/mapit_gb/management/commands/mapit_UK_add_names_to_ni_areas.py index 44a33e2f..49b1ec17 100644 --- a/mapit_gb/management/commands/mapit_UK_add_names_to_ni_areas.py +++ b/mapit_gb/management/commands/mapit_UK_add_names_to_ni_areas.py @@ -5,15 +5,15 @@ import csv import os.path -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, Generation, Country, NameType from django.utils import six -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Uses fixtures to find NI areas and add any missing names to them' - def handle_noargs(self, **options): + def handle(self, **options): try: self.latest_generation = Generation.objects.order_by('-id')[0] except IndexError: diff --git a/mapit_gb/management/commands/mapit_UK_add_ons_to_gss.py b/mapit_gb/management/commands/mapit_UK_add_ons_to_gss.py index c038a8f6..ad63a5c3 100644 --- a/mapit_gb/management/commands/mapit_UK_add_ons_to_gss.py +++ b/mapit_gb/management/commands/mapit_UK_add_ons_to_gss.py @@ -4,7 +4,7 @@ import codecs import csv import os.path -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, CodeType from psycopg2 import IntegrityError @@ -26,10 +26,10 @@ def process(new_code, old_code): raise Exception("Key already exists for %s, can't give it %s" % (area, old_code)) -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Inserts the old ONS codes into mapit' - def handle_noargs(self, **options): + def handle(self, **options): code_changes = codecs.open(os.path.join( os.path.dirname(__file__), '../../data/BL-2010-10-code-change.csv' diff --git a/mapit_gb/management/commands/mapit_UK_fix_2011-10.py b/mapit_gb/management/commands/mapit_UK_fix_2011-10.py index f7e5d82e..f08ea209 100644 --- a/mapit_gb/management/commands/mapit_UK_fix_2011-10.py +++ b/mapit_gb/management/commands/mapit_UK_fix_2011-10.py @@ -2,7 +2,6 @@ # one-off after that import in order to get the two old boundaries back in that # were removed due to a mistake in the 2011-05 Boundary-Line. -from optparse import make_option from django.core.management.base import LabelCommand from django.contrib.gis.gdal import DataSource from django.utils import six @@ -14,9 +13,10 @@ class Command(LabelCommand): help = 'Import OS Boundary-Line' args = '' - option_list = LabelCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') def handle_label(self, filename, **options): code_version = CodeType.objects.get(code='gss') diff --git a/mapit_gb/management/commands/mapit_UK_fix_2012-05.py b/mapit_gb/management/commands/mapit_UK_fix_2012-05.py index efac5019..603e6a09 100644 --- a/mapit_gb/management/commands/mapit_UK_fix_2012-05.py +++ b/mapit_gb/management/commands/mapit_UK_fix_2012-05.py @@ -5,7 +5,6 @@ from __future__ import print_function import re -from optparse import make_option from django.core.management.base import LabelCommand from django.contrib.gis.gdal import DataSource from django.utils import six @@ -17,9 +16,10 @@ class Command(LabelCommand): help = 'Import OS Boundary-Line' args = '' - option_list = LabelCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') def handle_label(self, filename, **options): code_version = CodeType.objects.get(code='gss') diff --git a/mapit_gb/management/commands/mapit_UK_fix_2013-10-south-tynedale.py b/mapit_gb/management/commands/mapit_UK_fix_2013-10-south-tynedale.py index e6cfc061..a7fbc166 100644 --- a/mapit_gb/management/commands/mapit_UK_fix_2013-10-south-tynedale.py +++ b/mapit_gb/management/commands/mapit_UK_fix_2013-10-south-tynedale.py @@ -2,18 +2,17 @@ # the Ordnance survey gave the wrong code too in the 2013-10 edition of the # boundary line. It must be run *before* importing the 2014-05 edition. -from optparse import make_option -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, CodeType -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Fix the GSS code of UTE South Tynedale so that we can import the May 2014 boundary line' - option_list = NoArgsCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) - def handle_noargs(self, **options): + def add_arguments(self, parser): + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') + + def handle(self, **options): code_version = CodeType.objects.get(code='gss') # We need to remove the code E05009154 from the South Tynedale area diff --git a/mapit_gb/management/commands/mapit_UK_fix_2013-10.py b/mapit_gb/management/commands/mapit_UK_fix_2013-10.py index be2324a5..381c9d71 100644 --- a/mapit_gb/management/commands/mapit_UK_fix_2013-10.py +++ b/mapit_gb/management/commands/mapit_UK_fix_2013-10.py @@ -5,7 +5,6 @@ from __future__ import print_function import re -from optparse import make_option from django.core.management.base import LabelCommand from django.contrib.gis.gdal import DataSource @@ -18,9 +17,10 @@ class Command(LabelCommand): help = 'Import OS Boundary-Line' args = '' - option_list = LabelCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') def handle_label(self, filename, **options): code_version = CodeType.objects.get(code='gss') diff --git a/mapit_gb/management/commands/mapit_UK_fix_2014-05-torfaen.py b/mapit_gb/management/commands/mapit_UK_fix_2014-05-torfaen.py index 7ab4eac5..020f6451 100644 --- a/mapit_gb/management/commands/mapit_UK_fix_2014-05-torfaen.py +++ b/mapit_gb/management/commands/mapit_UK_fix_2014-05-torfaen.py @@ -11,8 +11,7 @@ # (originally this script only revoked 5 and 6, leaving 10 as it overlapped # with 8/9). -from optparse import make_option -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area @@ -22,11 +21,11 @@ def disp(areas): return ', '.join(a.all_codes['gss'] for a in areas) -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Fix the Torfaen wards in the May 2014 UK Boundary-Line import' - option_list = NoArgsCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') def move(self, name, code, starts=False): areas = Area.objects.filter(parent_area=self.torfaen, type__code=code) @@ -42,7 +41,7 @@ def move(self, name, code, starts=False): else: print('Would delete %s and reinstate %s' % (disp(new), disp(old))) - def handle_noargs(self, **options): + def handle(self, **options): self.commit = options['commit'] self.torfaen = Area.objects.get(name='Torfaen Council', type__code='UTA') diff --git a/mapit_gb/management/commands/mapit_UK_fix_2014-05.py b/mapit_gb/management/commands/mapit_UK_fix_2014-05.py index 03da2a09..e72c8b5c 100644 --- a/mapit_gb/management/commands/mapit_UK_fix_2014-05.py +++ b/mapit_gb/management/commands/mapit_UK_fix_2014-05.py @@ -1,19 +1,18 @@ # This script is to be run as a one-off to fix up some geometries in the May # 2014 edition of boundary line that are invalid. -from optparse import make_option -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, CodeType from mapit.management.command_utils import fix_invalid_geos_geometry -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Fix the UK boundary line import for May 2014' - option_list = NoArgsCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) - def handle_noargs(self, **options): + def add_arguments(self, parser): + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') + + def handle(self, **options): code_version = CodeType.objects.get(code='gss') # Get the polygons that we want to fix diff --git a/mapit_gb/management/commands/mapit_UK_fix_2016-05.py b/mapit_gb/management/commands/mapit_UK_fix_2016-05.py index f019daf5..efc64f97 100644 --- a/mapit_gb/management/commands/mapit_UK_fix_2016-05.py +++ b/mapit_gb/management/commands/mapit_UK_fix_2016-05.py @@ -1,16 +1,15 @@ # This script is to be run as a one-off to fix up some geometries in the May # 2016 edition of Boundary-Line that are incorrect. -from optparse import make_option -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, CodeType, Generation, Geometry -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Fix the UK Boundary-Line import for May 2016' - option_list = NoArgsCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') code_version = CodeType.objects.get(code='gss') @@ -25,7 +24,7 @@ def get_generation_prior_to_current(self): return latest_on[1] return None - def handle_noargs(self, **options): + def handle(self, **options): # The area that has been included as Cheriton and Bishops Sutton should # be part of Alresford & Itchen Valley. area_to_add_to = self.get_area('E05010995') # Alresford & Itchen Valley diff --git a/mapit_gb/management/commands/mapit_UK_import_2011_scotparl.py b/mapit_gb/management/commands/mapit_UK_import_2011_scotparl.py index 701a1fe4..6c4fa490 100644 --- a/mapit_gb/management/commands/mapit_UK_import_2011_scotparl.py +++ b/mapit_gb/management/commands/mapit_UK_import_2011_scotparl.py @@ -1,7 +1,6 @@ # This script is used to import the 2011 Scottish Parliament from OS Boundary-Line. import re -from optparse import make_option from django.core.management.base import LabelCommand from django.contrib.gis.gdal import DataSource @@ -98,9 +97,10 @@ class Command(LabelCommand): help = 'Import OS Boundary-Line Scottish Parliament 2011 in advance' args = '' - option_list = LabelCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') ons_code_to_shape = {} diff --git a/mapit_gb/management/commands/mapit_UK_import_boundary_line.py b/mapit_gb/management/commands/mapit_UK_import_boundary_line.py index 926ef8d2..c71847de 100644 --- a/mapit_gb/management/commands/mapit_UK_import_boundary_line.py +++ b/mapit_gb/management/commands/mapit_UK_import_boundary_line.py @@ -5,7 +5,6 @@ import re import sys -from optparse import make_option from django.core.management.base import LabelCommand # Not using LayerMapping as want more control, but what it does is what this does @@ -20,12 +19,13 @@ class Command(LabelCommand): help = 'Import OS Boundary-Line' args = '' - option_list = LabelCommand.option_list + ( - make_option( + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument( '--control', action='store', dest='control', - help='Refer to a Python module that can tell us what has changed'), - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + help='Refer to a Python module that can tell us what has changed') + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') ons_code_to_shape = {} unit_id_to_shape = {} diff --git a/mapit_gb/management/commands/mapit_UK_import_ni_output_areas.py b/mapit_gb/management/commands/mapit_UK_import_ni_output_areas.py index c39ebfcf..ea542827 100644 --- a/mapit_gb/management/commands/mapit_UK_import_ni_output_areas.py +++ b/mapit_gb/management/commands/mapit_UK_import_ni_output_areas.py @@ -1,8 +1,6 @@ # This script is used to import information from the Northern Ireland # Output Areas, available from http://www.nisra.gov.uk/geography/default.asp2.htm -from optparse import make_option - from django.core.management.base import LabelCommand # Not using LayerMapping as want more control, but what it does is what this does # from django.contrib.gis.utils import LayerMapping @@ -15,9 +13,10 @@ class Command(LabelCommand): help = 'Import NI Output Areas' args = ' ' - option_list = LabelCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') ons_code_to_shape = {} councils = [] diff --git a/mapit_gb/management/commands/mapit_UK_import_nspd_ni_areas.py b/mapit_gb/management/commands/mapit_UK_import_nspd_ni_areas.py index fc5fa78e..46321e72 100644 --- a/mapit_gb/management/commands/mapit_UK_import_nspd_ni_areas.py +++ b/mapit_gb/management/commands/mapit_UK_import_nspd_ni_areas.py @@ -7,14 +7,14 @@ import csv import re import os.path -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, Generation, Country, Type, CodeType, NameType -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Creates/updates Northern Ireland areas' - def handle_noargs(self, **options): + def handle(self, **options): current_generation = Generation.objects.current() new_generation = Generation.objects.new() country = Country.objects.get(code='N') diff --git a/mapit_gb/management/commands/mapit_UK_import_onspd.py b/mapit_gb/management/commands/mapit_UK_import_onspd.py index e313aa7d..b7f09610 100644 --- a/mapit_gb/management/commands/mapit_UK_import_onspd.py +++ b/mapit_gb/management/commands/mapit_UK_import_onspd.py @@ -84,7 +84,6 @@ # 52. Decimal degrees latitude # 53. Decimal degrees longitude -from optparse import make_option from mapit.management.commands.mapit_import_postal_codes import Command @@ -95,16 +94,17 @@ class Command(Command): 'without locations.') args = '' option_defaults = {'header-row': True, 'strip': True, 'srid': 27700, 'coord-field-lon': 10, 'coord-field-lat': 11} - option_list = Command.option_list + ( - make_option( + + def add_arguments(self, parser): + parser.add_argument( '--allow-terminated-postcodes', action='store_true', dest='include-terminated', default=False, help=('Set if you want to import terminated postcodes. Affects all ' 'postcodes: GB, NI, and Crown Dependencies') - ), - make_option( + ) + parser.add_argument( '--allow-no-location-postcodes', action='store_true', dest='include-no-location', @@ -114,8 +114,8 @@ class Command(Command): 'Dependency postcodes have no location and will be imported ' 'based on the value of --crown-dependencies, regardless of ' 'your choice for this option.') - ), - make_option( + ) + parser.add_argument( '--crown-dependencies', action='store', dest='crown-dependencies', @@ -126,8 +126,8 @@ class Command(Command): 'Crown Dependency postcodes have no location info and are ' 'imported solely based on this option, regardless of the ' 'presence of --allow-no-location-postcodes.') - ), - make_option( + ) + parser.add_argument( '--northern-ireland', action='store', dest='northern-ireland', @@ -137,24 +137,23 @@ class Command(Command): '"only" to import only these. (Default: exclude). You should' ' run mapit_UK_import_onspd_ni_areas before trying to import ' 'any NI postcodes.') - ), - make_option( + ) + parser.add_argument( '--gb-srid', action='store', dest='gb-srid', default=27700, help=('SRID for GB & Crown Dependency postcodes. Overrides --srid ' 'value. (Default: 27700).') - ), - make_option( + ) + parser.add_argument( '--ni-srid', action='store', dest='ni-srid', default=29902, help=('SRID for NI postcodes. Overrides --srid value for. (Default:' ' 29902).') - ), - ) + ) def handle_label(self, file, **options): self.check_options_are_valid(options) diff --git a/mapit_gb/management/commands/mapit_UK_import_osni.py b/mapit_gb/management/commands/mapit_UK_import_osni.py index 7188f82c..ba3b3d2e 100644 --- a/mapit_gb/management/commands/mapit_UK_import_osni.py +++ b/mapit_gb/management/commands/mapit_UK_import_osni.py @@ -6,9 +6,8 @@ import sys import csv import os -from optparse import make_option -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand # Not using LayerMapping as want more control, but what it does is what this does # from django.contrib.gis.utils import LayerMapping from django.contrib.gis.gdal import DataSource @@ -19,33 +18,34 @@ from mapit.management.command_utils import save_polygons, fix_invalid_geos_geometry -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Import OSNI releases' - option_list = NoArgsCommand.option_list + ( - make_option( + + def add_arguments(self, parser): + parser.add_argument( '--control', action='store', dest='control', - help='Refer to a Python module that can tell us what has changed'), - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - make_option( + help='Refer to a Python module that can tell us what has changed') + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') + parser.add_argument( '--lgw', action='store', dest='lgw_file', help='Name of OSNI shapefile that contains Ward boundary information' - ), - make_option( + ) + parser.add_argument( '--wmc', action='store', dest='wmc_file', help=( 'Name of OSNI shapefile that contains Westminister Parliamentary constituency boundary' 'information (also used for Northern Ireland Assembly constituencies)' ) - ), - make_option( + ) + parser.add_argument( '--lgd', action='store', dest='lgd_file', - help='Name of OSNI shapefile that contains Council boundary information'), - make_option( + help='Name of OSNI shapefile that contains Council boundary information') + parser.add_argument( '--lge', action='store', dest='lge_file', - help='Name of OSNI shapefile that contains Electoral Area boundary information'), - make_option( + help='Name of OSNI shapefile that contains Electoral Area boundary information') + parser.add_argument( '--eur', action='store', dest='eur_file', - help='Name of OSNI shapefile that contains European Region boundary information'), + help='Name of OSNI shapefile that contains European Region boundary information') # OSNI datasets are exported in either 29902 or 102100 projections. # PostGIS doesn't support 102100, but it is mathematically equivalent @@ -59,27 +59,26 @@ class Command(NoArgsCommand): # to 29902. This suggests it's safe to use 4326 as a replacement for # 102100. The defaults here are based on the srids of the data # downloaded in Dec 2015 - they may change over time. - make_option( + parser.add_argument( '--lgw-srid', action='store', type='int', dest='lgw_srid', default=4326, - help='SRID of Ward boundary information shapefile (default 4326)'), - make_option( + help='SRID of Ward boundary information shapefile (default 4326)') + parser.add_argument( '--wmc-srid', action='store', type='int', dest='wmc_srid', default=4326, - help='SRID of Westminister Parliamentery constituency boundary information shapefile (default 4326)'), - make_option( + help='SRID of Westminister Parliamentery constituency boundary information shapefile (default 4326)') + parser.add_argument( '--lgd-srid', action='store', type='int', dest='lgd_srid', default=4326, - help='SRID of Council boundary information shapefile (default 4326)'), - make_option( + help='SRID of Council boundary information shapefile (default 4326)') + parser.add_argument( '--lge-srid', action='store', type='int', dest='lge_srid', default=29902, - help='SRID of Electoral Area boundary information shapefile (default 29902)'), - make_option( + help='SRID of Electoral Area boundary information shapefile (default 29902)') + parser.add_argument( '--eur-srid', action='store', type='int', dest='eur_srid', default=29902, - help='SRID of European Region boundary information shapefile (default 29902)'), - ) + help='SRID of European Region boundary information shapefile (default 29902)') ons_code_to_shape = {} osni_object_id_to_shape = {} - def handle_noargs(self, **options): + def handle(self, **options): if not options['control']: raise Exception("You must specify a control file") __import__(options['control']) diff --git a/mapit_gb/management/commands/mapit_UK_import_police_force_areas.py b/mapit_gb/management/commands/mapit_UK_import_police_force_areas.py index 14af8822..7b9b20cd 100644 --- a/mapit_gb/management/commands/mapit_UK_import_police_force_areas.py +++ b/mapit_gb/management/commands/mapit_UK_import_police_force_areas.py @@ -15,8 +15,6 @@ import os import sys -from optparse import make_option - from django.core.management import call_command from django.core.management.base import LabelCommand from django.utils.six.moves import urllib @@ -30,56 +28,57 @@ class Command(LabelCommand): help = 'Import England, Wales and Northern Ireland police force area boundaries from .kml files' args = '' - option_list = LabelCommand.option_list + ( - make_option( + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument( '--commit', action='store_true', dest='commit', help='Actually update the database' - ), - make_option( + ) + parser.add_argument( '--generation_id', action="store", dest='generation_id', help='Which generation ID should be used', - ), - make_option( + ) + parser.add_argument( '--area_type_code', action="store", dest='area_type_code', help='Which area type should be used (specify using code)', - ), - make_option( + ) + parser.add_argument( '--name_type_code', action="store", dest='name_type_code', help='Which name type should be used (specify using code)', - ), - make_option( + ) + parser.add_argument( '--code_type', action="store", dest='code_type', help='Which code type should be used (specify using its code)', - ), - make_option( + ) + parser.add_argument( '--preserve', action="store_true", dest='preserve', help="Create a new area if the name's the same but polygons differ" - ), - make_option( + ) + parser.add_argument( '--new', action="store_true", dest='new', help="Don't look for existing areas at all, just import everything as new areas" - ), - make_option( + ) + parser.add_argument( '--fix_invalid_polygons', action="store_true", dest='fix_invalid_polygons', help="Try to fix any invalid polygons and multipolygons found" - ), - ) + ) def handle_label(self, directory, **options): diff --git a/mapit_gb/management/commands/mapit_UK_ni_consolidate_boundaries.py b/mapit_gb/management/commands/mapit_UK_ni_consolidate_boundaries.py index 6a4c021b..bab975e6 100644 --- a/mapit_gb/management/commands/mapit_UK_ni_consolidate_boundaries.py +++ b/mapit_gb/management/commands/mapit_UK_ni_consolidate_boundaries.py @@ -3,18 +3,17 @@ from __future__ import print_function -from optparse import make_option -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, Type, Geometry -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Puts the boundaries on the LGDs, LGWs and LGEs from the Output Areas' - option_list = NoArgsCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) - def handle_noargs(self, **options): + def add_arguments(self, parser): + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') + + def handle(self, **options): area_type = Type.objects.get(code='OUA') done = [] diff --git a/mapit_gb/management/commands/mapit_UK_scilly.py b/mapit_gb/management/commands/mapit_UK_scilly.py index 507b4aa8..7c94db3d 100644 --- a/mapit_gb/management/commands/mapit_UK_scilly.py +++ b/mapit_gb/management/commands/mapit_UK_scilly.py @@ -5,7 +5,6 @@ import csv import re -from optparse import make_option from django.core.management.base import LabelCommand from mapit.models import Postcode, Area, Country, Type, CodeType, NameType @@ -13,8 +12,10 @@ class Command(LabelCommand): help = 'Sort out the Isles of Scilly' args = ' or ' - option_list = LabelCommand.option_list + ( - make_option( + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument( '--allow-terminated-postcodes', action='store_true', dest='include-terminated', @@ -22,16 +23,15 @@ class Command(LabelCommand): help=('Set if you want to fix wards for terminated postcodes (only ' 'relevant for ONSPD files, Code-Point Open contains no ' 'terminated postcodes)') - ), - make_option( + ) + parser.add_argument( '--allow-no-location-postcodes', action='store_true', dest='include-no-location', default=False, help=('Set if you want to fix wards for postcodes with no location ' '(quality 9 in ONSPD, quality 90 in Code Point Open)') - ), - ) + ) def handle_label(self, file, **options): # The Isles of Scilly have changed their code in B-L, but Code-Point still has the old code currently diff --git a/mapit_gb/management/commands/mapit_UK_update_ons_ids.py b/mapit_gb/management/commands/mapit_UK_update_ons_ids.py index 63fafb98..e56884f6 100644 --- a/mapit_gb/management/commands/mapit_UK_update_ons_ids.py +++ b/mapit_gb/management/commands/mapit_UK_update_ons_ids.py @@ -1,16 +1,16 @@ # This script is for a one off import of all the new GSS codes. import csv -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, Generation, CodeType from psycopg2 import IntegrityError -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Inserts all the new GSS codes into mapit' args = '' - def handle_noargs(self, **options): + def handle(self, **options): current_generation = Generation.objects.current() # Read in ward name -> electoral area name/area diff --git a/mapit_gb/management/commands/mapit_UK_update_ons_ids2.py b/mapit_gb/management/commands/mapit_UK_update_ons_ids2.py index 55470d22..6312b878 100644 --- a/mapit_gb/management/commands/mapit_UK_update_ons_ids2.py +++ b/mapit_gb/management/commands/mapit_UK_update_ons_ids2.py @@ -2,16 +2,16 @@ # To include the ones not in the file from Ordnance Survey. import csv -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Area, Generation, CodeType from psycopg2 import IntegrityError -class Command(NoArgsCommand): +class Command(BaseCommand): help = 'Inserts all the new GSS codes into mapit' args = '' - def handle_noargs(self, **options): + def handle(self, **options): current_generation = Generation.objects.current() # Read in ward name -> electoral area name/area diff --git a/mapit_global/management/commands/mapit_global_import.py b/mapit_global/management/commands/mapit_global_import.py index 76c6971b..84bca3dd 100644 --- a/mapit_global/management/commands/mapit_global_import.py +++ b/mapit_global/management/commands/mapit_global_import.py @@ -17,7 +17,6 @@ import csv from glob import glob import json -from optparse import make_option import os import re import xml.sax @@ -75,9 +74,10 @@ def get_iso639_2_table(): class Command(LabelCommand): help = 'Import OSM boundary data from KML files' args = '' - option_list = LabelCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') def handle_label(self, directory_name, **options): current_generation = Generation.objects.current() diff --git a/mapit_global/management/commands/mapit_global_oneoff_fix_gen4.py b/mapit_global/management/commands/mapit_global_oneoff_fix_gen4.py index 9a4611c5..2a189173 100644 --- a/mapit_global/management/commands/mapit_global_oneoff_fix_gen4.py +++ b/mapit_global/management/commands/mapit_global_oneoff_fix_gen4.py @@ -2,7 +2,6 @@ # boundaries of the import that was Generation 4 of global MapIt. from glob import glob -from optparse import make_option import os import re @@ -19,9 +18,10 @@ class Command(LabelCommand): help = 'Update OSM boundary data from KML files' args = '' - option_list = LabelCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') def handle_label(self, directory_name, **options): current_generation = Generation.objects.current() diff --git a/mapit_global/management/commands/mapit_use_osm_place_name.py b/mapit_global/management/commands/mapit_use_osm_place_name.py index 2f67b2e8..9842209d 100644 --- a/mapit_global/management/commands/mapit_use_osm_place_name.py +++ b/mapit_global/management/commands/mapit_use_osm_place_name.py @@ -9,7 +9,6 @@ import os import re -from optparse import make_option from django.core.management.base import LabelCommand from django.utils.encoding import smart_str, smart_text @@ -21,9 +20,10 @@ class Command(LabelCommand): help = 'Find any "Unknown" names, and use place_name instead, if possible' args = '' - option_list = LabelCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') def handle_label(self, directory_name, **options): diff --git a/mapit_no/management/commands/mapit_NO_import_osm.py b/mapit_no/management/commands/mapit_NO_import_osm.py index 592b8588..ef30151a 100644 --- a/mapit_no/management/commands/mapit_NO_import_osm.py +++ b/mapit_no/management/commands/mapit_NO_import_osm.py @@ -7,7 +7,6 @@ import re import xml.sax -from optparse import make_option from django.core.management.base import LabelCommand # Not using LayerMapping as want more control, but what it does is what this does @@ -23,9 +22,10 @@ class Command(LabelCommand): help = 'Import OSM data' args = '' - option_list = LabelCommand.option_list + ( - make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), - ) + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') def handle_label(self, filename, **options): current_generation = Generation.objects.current() diff --git a/mapit_za/management/commands/mapit_za_import_boundaries.py b/mapit_za/management/commands/mapit_za_import_boundaries.py index 018fa07e..89877cdb 100644 --- a/mapit_za/management/commands/mapit_za_import_boundaries.py +++ b/mapit_za/management/commands/mapit_za_import_boundaries.py @@ -22,39 +22,26 @@ import sys from collections import namedtuple -from optparse import make_option from django.core.management import call_command -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from mapit.models import Generation, NameType, Country -class Command(NoArgsCommand): +class Command(BaseCommand): """Import South African boundaries""" help = 'Import shapefiles with South African boundary data' - option_list = NoArgsCommand.option_list + ( - make_option( - '--wards', '-w', - help="The wards shapefile"), - make_option( - '--districts', '-d', - help="The district municipalities shapefile"), - make_option( - '--provinces', '-p', - help="The provinces shapefile"), - make_option( - '--locals', '-l', - help="The local municipalities shapefile"), - make_option( - '--commit', - action='store_true', - dest='commit', - help='Actually update the database'),) - - def handle_noargs(self, **options): + def add_arguments(self, parser): + parser.add_argument('--wards', '-w', help="The wards shapefile") + parser.add_argument('--districts', '-d', help="The district municipalities shapefile") + parser.add_argument('--provinces', '-p', help="The provinces shapefile") + parser.add_argument('--locals', '-l', help="The local municipalities shapefile") + parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') + + def handle(self, **options): stop = False for k in ('wards', From 4011cca3a71b198666ef597b28d0b7d2b678ffab Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Mon, 15 Aug 2016 11:28:03 +0100 Subject: [PATCH 4/5] Don't show deprecation warnings by default. * 'PYTHONWARNINGS=ignore tox' will show no warnings. * 'tox' will show no past-next-version deprecation warnings. * 'PYTHONWARNINGS=all tox' will show all warnings. --- tox.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tox.ini b/tox.ini index 7bf10f4c..953c4876 100644 --- a/tox.ini +++ b/tox.ini @@ -4,13 +4,13 @@ envlist = flake8, py{27,32}-1.8, py{27,34}-{1.9,1.10} [testenv] commands = flake8: flake8 mapit mapit_gb mapit_global mapit_it mapit_no mapit_se mapit_za project - py{27,32,34}: python -Wall manage.py test mapit mapit_gb + py{27,32,34}: python -W all -W ignore::PendingDeprecationWarning manage.py test mapit mapit_gb deps = flake8: flake8 1.8: Django>=1.8,<1.9 1.9: Django>=1.9,<1.10 1.10: git+https://github.com/django/django.git@master#egg=django -passenv = CFLAGS +passenv = CFLAGS PYTHONWARNINGS setenv = py{32,34}: DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib:/usr/lib PYTHONDONTWRITEBYTECODE=1 From f8793d13bff7071ea6edafca7ffcbfead8f1a55d Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Mon, 15 Aug 2016 11:28:31 +0100 Subject: [PATCH 5/5] Test released 1.10 (must pass), unreleased 1.11. --- .travis.yml | 6 ++++-- tox.ini | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 03acc512..3f804095 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,14 +18,16 @@ env: - TOXENV=py27-1.8 - TOXENV=py27-1.9 - TOXENV=py27-1.10 + - TOXENV=py27-1.11 - TOXENV=py32-1.8 - TOXENV=py34-1.9 - TOXENV=py34-1.10 + - TOXENV=py34-1.11 matrix: allow_failures: - - env: TOXENV=py27-1.10 - - env: TOXENV=py34-1.10 + - env: TOXENV=py27-1.11 + - env: TOXENV=py34-1.11 install: - pip install tox diff --git a/tox.ini b/tox.ini index 953c4876..0e9443e9 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = flake8, py{27,32}-1.8, py{27,34}-{1.9,1.10} +envlist = flake8, py{27,32}-1.8, py{27,34}-{1.9,1.10,1.11} [testenv] commands = @@ -9,7 +9,8 @@ deps = flake8: flake8 1.8: Django>=1.8,<1.9 1.9: Django>=1.9,<1.10 - 1.10: git+https://github.com/django/django.git@master#egg=django + 1.10: Django>=1.10,<1.11 + 1.11: git+https://github.com/django/django.git@master#egg=django passenv = CFLAGS PYTHONWARNINGS setenv = py{32,34}: DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib:/usr/lib