Skip to content

Commit 7dade26

Browse files
authored
Merge pull request #8 from opennorth/upgrade
Upgrade Python and Django
2 parents 9bff4e3 + 8a03db0 commit 7dade26

14 files changed

+201
-137
lines changed

.github/workflows/lint.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Lint
2+
on: [push, pull_request]
3+
jobs:
4+
build:
5+
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v4
9+
- uses: actions/setup-python@v5
10+
with:
11+
python-version: '3.10'
12+
- run: pip install --upgrade flake8 isort
13+
- run: flake8 .
14+
- run: isort .

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["setuptools>=61.2"]
3+
build-backend = "setuptools.build_meta"

representatives/admin.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,39 @@
22

33
from django.contrib import admin, messages
44

5-
from representatives.models import RepresentativeSet, Representative, Election, Candidate, app_settings
5+
from representatives.models import Candidate, Election, Representative, RepresentativeSet, app_settings
66

77

8+
@admin.register(RepresentativeSet)
89
class RepresentativeSetAdmin(admin.ModelAdmin):
910
actions = ['update_from_data_source']
1011
list_display = ['name', 'last_import_time', 'last_import_successful', 'enabled']
1112
list_filter = ['last_import_successful', 'enabled']
1213

14+
@admin.action(
15+
description="Update from data source"
16+
)
1317
def update_from_data_source(self, request, queryset):
1418
for individual_set in queryset:
1519
try:
1620
count = individual_set.update_from_data_source()
1721
except Exception:
18-
messages.error(request, "Couldn't update individuals in %s: %s" % (individual_set, traceback.format_exc()))
22+
messages.error(request, f"Couldn't update individuals in {individual_set}: {traceback.format_exc()}")
1923
continue
2024
if count is False:
21-
messages.error(request, "Couldn't update individuals in %s." % individual_set)
25+
messages.error(request, f"Couldn't update individuals in {individual_set}.")
2226
else:
23-
message = "Updated %s individuals in %s." % (count, individual_set)
27+
message = f"Updated {count} individuals in {individual_set}."
2428
no_boundaries = individual_set.individuals.filter(boundary='').values_list('name', flat=True)
2529
if no_boundaries:
26-
messages.warning(request, message + " %d match no boundary (%s)." % (len(no_boundaries), ', '.join(no_boundaries)))
30+
messages.warning(
31+
request, message + f" {len(no_boundaries)} match no boundary ({', '.join(no_boundaries)})."
32+
)
2733
else:
2834
messages.success(request, message)
29-
update_from_data_source.short_description = "Update from data source"
3035

3136

37+
@admin.register(Representative)
3238
class RepresentativeAdmin(admin.ModelAdmin):
3339
list_display = ['name', 'representative_set', 'district_name', 'elected_office', 'boundary']
3440
list_filter = ['representative_set']
@@ -41,8 +47,6 @@ class CandidateAdmin(admin.ModelAdmin):
4147
search_fields = ['name', 'district_name', 'elected_office']
4248

4349

44-
admin.site.register(RepresentativeSet, RepresentativeSetAdmin)
45-
admin.site.register(Representative, RepresentativeAdmin)
4650
if app_settings.ENABLE_CANDIDATES:
4751
admin.site.register(Election, RepresentativeSetAdmin)
4852
admin.site.register(Candidate, CandidateAdmin)
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import logging
21
import itertools
2+
import logging
33

44
from django.core.management.base import BaseCommand
55

6-
from representatives.models import RepresentativeSet, Election
6+
from representatives.models import Election, RepresentativeSet
77

88
log = logging.getLogger(__name__)
99

@@ -12,9 +12,13 @@ class Command(BaseCommand):
1212
help = 'Updates representatives from sources.'
1313

1414
def handle(self, *args, **options):
15-
for representative_set in itertools.chain(RepresentativeSet.objects.filter(enabled=True), Election.objects.filter(enabled=True)):
15+
for representative_set in itertools.chain(
16+
RepresentativeSet.objects.filter(enabled=True), Election.objects.filter(enabled=True)
17+
):
1618
try:
1719
representative_set.update_from_data_source()
1820
except Exception:
1921
log.error("Couldn't update representatives in %s." % representative_set)
20-
representative_set.__class__.objects.filter(pk=representative_set.pk).update(last_import_successful=False)
22+
representative_set.__class__.objects.filter(pk=representative_set.pk).update(
23+
last_import_successful=False
24+
)

representatives/migrations/0001_initial.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
# -*- coding: utf-8 -*-
2-
from __future__ import unicode_literals
1+
from django.db import migrations, models
32

4-
from django.db import models, migrations
53

64
class JSONField(models.TextField):
75
"""Mocks jsonfield 0.92's column-type behaviour"""
86
def db_type(self, connection):
97
if connection.vendor == 'postgresql' and connection.pg_version >= 90300:
108
return 'json'
119
else:
12-
return super(JSONField, self).db_type(connection)
10+
return super().db_type(connection)
1311

1412
class Migration(migrations.Migration):
1513

@@ -110,13 +108,13 @@ class Migration(migrations.Migration):
110108
migrations.AddField(
111109
model_name='representative',
112110
name='representative_set',
113-
field=models.ForeignKey(related_name='individuals', to='representatives.RepresentativeSet'),
111+
field=models.ForeignKey(on_delete=models.CASCADE, related_name='individuals', to='representatives.RepresentativeSet'),
114112
preserve_default=True,
115113
),
116114
migrations.AddField(
117115
model_name='candidate',
118116
name='election',
119-
field=models.ForeignKey(related_name='individuals', to='representatives.Election'),
117+
field=models.ForeignKey(on_delete=models.CASCADE, related_name='individuals', to='representatives.Election'),
120118
preserve_default=True,
121119
),
122120
]

representatives/migrations/0002_auto_20141129_1450.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
# -*- coding: utf-8 -*-
2-
from __future__ import unicode_literals
1+
from django.db import migrations, models
32

4-
from django.db import models, migrations
53

64
class JSONField(models.TextField):
75
"""Mocks jsonfield 0.92's column-type behaviour"""
86
def db_type(self, connection):
97
if connection.vendor == 'postgresql' and connection.pg_version >= 90300:
108
return 'json'
119
else:
12-
return super(JSONField, self).db_type(connection)
10+
return super().db_type(connection)
1311

1412

1513
class Migration(migrations.Migration):

representatives/migrations/0003_auto_20170214_1237.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
# -*- coding: utf-8 -*-
21
# Generated by Django 1.9.11 on 2017-02-14 12:37
3-
from __future__ import unicode_literals
42

53
from django.db import migrations, models
64

5+
76
class JSONField(models.TextField):
87
"""Mocks jsonfield 0.92's column-type behaviour"""
98
def db_type(self, connection):
109
if connection.vendor == 'postgresql' and connection.pg_version >= 90300:
1110
return 'json'
1211
else:
13-
return super(JSONField, self).db_type(connection)
12+
return super().db_type(connection)
1413

1514
class Migration(migrations.Migration):
1615

representatives/migrations/0004_switch_to_django_jsonfield.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# -*- coding: utf-8 -*-
21
# Generated by Django 1.10.5 on 2017-02-23 20:05
3-
from __future__ import unicode_literals
42

53
import django.contrib.postgres.fields.jsonb
64
from django.db import migrations

0 commit comments

Comments
 (0)