Skip to content

Commit

Permalink
Merge pull request #2 from opennorth/upgrade
Browse files Browse the repository at this point in the history
Upgrade Python and Django
  • Loading branch information
jpmckinney authored Jun 26, 2024
2 parents af96882 + c92f195 commit c0b2fd9
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 61 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Lint
on: [push, pull_request]
jobs:
build:
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'
- run: pip install --upgrade flake8 isort
- run: flake8 .
- run: isort .
8 changes: 2 additions & 6 deletions postcodes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
from postcodes.models import Postcode, PostcodeConcordance


@admin.register(Postcode)
class PostcodeAdmin(admin.OSMGeoAdmin):
list_display = ('code', 'city', 'province')


admin.site.register(Postcode, PostcodeAdmin)


@admin.register(PostcodeConcordance)
class PostcodeConcordanceAdmin(admin.ModelAdmin):
list_display = ('code', 'boundary', 'source')


admin.site.register(PostcodeConcordance, PostcodeConcordanceAdmin)
21 changes: 14 additions & 7 deletions postcodes/management/commands/loadpostcodeconcordance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import sys

from boundaries.models import BoundarySet, Boundary
from boundaries.models import Boundary, BoundarySet
from django.core.exceptions import ValidationError
from django.core.management.base import BaseCommand
from django.db import transaction
Expand All @@ -26,9 +26,16 @@ def add_arguments(self, parser):
parser.add_argument('slug', nargs=1)
parser.add_argument('source', nargs=1)
parser.add_argument('filename', nargs='?')
parser.add_argument('--searchfield', action='store', dest='search-field',
parser.add_argument(
'--searchfield',
action='store',
dest='search-field',
default='external_id',
help="Set the SQL column to which the second column of the CSV corresponds. One of 'external_id' (default), 'name' or 'slug'.")
help=(
"Set the SQL column to which the second column of the CSV corresponds. "
"One of 'external_id' (default), 'name' or 'slug'."
),
)

@transaction.atomic
def handle(self, *args, **options):
Expand All @@ -51,7 +58,7 @@ def handle(self, *args, **options):
try:
(postcode, created) = Postcode.objects.get_or_create(code=code)
except ValidationError as e:
log.error("%s: %r" % (code, e))
log.error("%s: %s", code, repr(e))
continue

try:
Expand All @@ -63,12 +70,12 @@ def handle(self, *args, **options):
boundary = boundaries.get(**{options['search-field']: term})
boundaries_seen[term] = boundary
except Boundary.DoesNotExist:
log.error("No boundary %s matches %s" % (options['search-field'], term))
log.error("No boundary %s matches %s", options['search-field'], term)
continue

path = '%s/%s' % (boundary_set.slug, boundary.slug)
path = f'{boundary_set.slug}/{boundary.slug}'
if PostcodeConcordance.objects.filter(code=postcode, boundary=path).exists():
log.warning("Concordance already exists between %s and %s" % (code, path))
log.warning("Concordance already exists between %s and %s", code, path)
continue

PostcodeConcordance.objects.create(
Expand Down
9 changes: 6 additions & 3 deletions postcodes/management/commands/loadpostcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@


class Command(BaseCommand):
help = 'Imports a headerless CSV file with columns for code,latitude,longitude,locality,region. If no filename is given, reads from standard input.'
help = (
'Imports a headerless CSV file with columns for code,latitude,longitude,locality,region. '
'If no filename is given, reads from standard input.'
)

def add_arguments(self, parser):
parser.add_argument('filename', nargs='?')
Expand All @@ -27,10 +30,10 @@ def handle(self, *args, **options):
for row in reader:
try:
Postcode(
code=row['code'].upper(),
code=row['code'].upper().replace(' ', ''),
centroid=Point(float(row['longitude']), float(row['latitude'])),
city=row['locality'],
province=row['region'],
).save()
except ValidationError as e:
log.error("%s: %r" % (row['code'], e))
log.error("%s: %s", row['code'], repr(e))
10 changes: 4 additions & 6 deletions postcodes/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import re

from django.db import models, migrations
import django.contrib.gis.db.models.fields
import django.core.validators
import re
from django.db import migrations, models


class Migration(migrations.Migration):
Expand All @@ -31,14 +29,14 @@ class Migration(migrations.Migration):
('id', models.AutoField(primary_key=True, verbose_name='ID', serialize=False, auto_created=True)),
('boundary', models.TextField()),
('source', models.CharField(help_text='A description of the data source.', max_length=30)),
('code', models.ForeignKey(to='postcodes.Postcode')),
('code', models.ForeignKey(on_delete=models.CASCADE, to='postcodes.Postcode')),
],
options={
},
bases=(models.Model,),
),
migrations.AlterUniqueTogether(
name='postcodeconcordance',
unique_together=set([('code', 'boundary')]),
unique_together={('code', 'boundary')},
),
]
15 changes: 10 additions & 5 deletions postcodes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def save(self, *args, **kwargs):
'V': 'BC',
'Y': 'YT',
}.get(self.code[0])
super(Postcode, self).save(*args, **kwargs)
super().save(*args, **kwargs)

def __str__(self):
return self.code
Expand Down Expand Up @@ -104,7 +104,10 @@ def get_boundaries(self, sets=None):
concordance_sets = set()

if concordances:
q = ((models.Q(set=concordance.split('/')[0]) & models.Q(slug=concordance.split('/')[1])) for concordance in concordances)
q = (
(models.Q(set=concordance.split('/')[0]) & models.Q(slug=concordance.split('/')[1]))
for concordance in concordances
)

boundaries = Boundary.objects.filter(reduce(lambda a, b: a | b, q))
boundaries = Boundary.prepare_queryset_for_get_dicts(boundaries)
Expand All @@ -125,7 +128,9 @@ def get_boundaries(self, sets=None):
boundaries = Boundary.prepare_queryset_for_get_dicts(boundaries)
boundaries = Boundary.get_dicts(boundaries)

r['boundaries_centroid'] = [boundary for boundary in boundaries if boundary['related']['boundary_set_url'] not in concordance_sets]
r['boundaries_centroid'] = [
boundary for boundary in boundaries if boundary['related']['boundary_set_url'] not in concordance_sets
]

return r

Expand All @@ -145,12 +150,12 @@ def get_representatives(self, boundary_names, model):


class PostcodeConcordance(models.Model):
code = models.ForeignKey(Postcode)
code = models.ForeignKey(Postcode, on_delete=models.CASCADE)
boundary = models.TextField()
source = models.CharField(max_length=30, help_text="A description of the data source.")

class Meta:
unique_together = (('code', 'boundary'))

def __str__(self):
return '%s -> %s' % (self.code_id, self.boundary)
return f'{self.code_id} -> {self.boundary}'
8 changes: 4 additions & 4 deletions postcodes/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.conf.urls import url
from django.urls import re_path

from postcodes.views import PostcodeDetailView

urlpatterns = [
url(r'^postcodes/(?P<code>\w{6})/$', PostcodeDetailView.as_view()),
re_path(r'^postcodes/(?P<code>\w{6})/$', PostcodeDetailView.as_view()),

# Backwards compatibility.
url(r'^postcodes/(?P<code>\w{6})/boundaries/$', PostcodeDetailView.as_view()),
url(r'^postcodes/(?P<code>\w{6})/representatives/$', PostcodeDetailView.as_view()),
re_path(r'^postcodes/(?P<code>\w{6})/boundaries/$', PostcodeDetailView.as_view()),
re_path(r'^postcodes/(?P<code>\w{6})/representatives/$', PostcodeDetailView.as_view()),
]
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools>=61.2"]
build-backend = "setuptools.build_meta"
24 changes: 24 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[metadata]
name = represent-postcodes
version = 0.0.1
license = MIT
description = A web API for postal codes associated to electoral districts, packaged as a Django app.
url = https://github.com/opennorth/represent-postcodes
classifiers =
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License
Framework :: Django

[options]
packages = find:
install_requires =
django-appconf
represent-boundaries

[isort]
line_length = 119
profile = black

[flake8]
max-line-length = 119
exclude = postcodes/migrations
25 changes: 0 additions & 25 deletions setup.py

This file was deleted.

5 changes: 0 additions & 5 deletions tox.ini

This file was deleted.

0 comments on commit c0b2fd9

Please sign in to comment.