Skip to content

Commit

Permalink
Merge pull request #8 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 9bff4e3 + 8a03db0 commit 7dade26
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 137 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 .
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"
20 changes: 12 additions & 8 deletions representatives/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@

from django.contrib import admin, messages

from representatives.models import RepresentativeSet, Representative, Election, Candidate, app_settings
from representatives.models import Candidate, Election, Representative, RepresentativeSet, app_settings


@admin.register(RepresentativeSet)
class RepresentativeSetAdmin(admin.ModelAdmin):
actions = ['update_from_data_source']
list_display = ['name', 'last_import_time', 'last_import_successful', 'enabled']
list_filter = ['last_import_successful', 'enabled']

@admin.action(
description="Update from data source"
)
def update_from_data_source(self, request, queryset):
for individual_set in queryset:
try:
count = individual_set.update_from_data_source()
except Exception:
messages.error(request, "Couldn't update individuals in %s: %s" % (individual_set, traceback.format_exc()))
messages.error(request, f"Couldn't update individuals in {individual_set}: {traceback.format_exc()}")
continue
if count is False:
messages.error(request, "Couldn't update individuals in %s." % individual_set)
messages.error(request, f"Couldn't update individuals in {individual_set}.")
else:
message = "Updated %s individuals in %s." % (count, individual_set)
message = f"Updated {count} individuals in {individual_set}."
no_boundaries = individual_set.individuals.filter(boundary='').values_list('name', flat=True)
if no_boundaries:
messages.warning(request, message + " %d match no boundary (%s)." % (len(no_boundaries), ', '.join(no_boundaries)))
messages.warning(
request, message + f" {len(no_boundaries)} match no boundary ({', '.join(no_boundaries)})."
)
else:
messages.success(request, message)
update_from_data_source.short_description = "Update from data source"


@admin.register(Representative)
class RepresentativeAdmin(admin.ModelAdmin):
list_display = ['name', 'representative_set', 'district_name', 'elected_office', 'boundary']
list_filter = ['representative_set']
Expand All @@ -41,8 +47,6 @@ class CandidateAdmin(admin.ModelAdmin):
search_fields = ['name', 'district_name', 'elected_office']


admin.site.register(RepresentativeSet, RepresentativeSetAdmin)
admin.site.register(Representative, RepresentativeAdmin)
if app_settings.ENABLE_CANDIDATES:
admin.site.register(Election, RepresentativeSetAdmin)
admin.site.register(Candidate, CandidateAdmin)
12 changes: 8 additions & 4 deletions representatives/management/commands/updaterepresentatives.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging
import itertools
import logging

from django.core.management.base import BaseCommand

from representatives.models import RepresentativeSet, Election
from representatives.models import Election, RepresentativeSet

log = logging.getLogger(__name__)

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

def handle(self, *args, **options):
for representative_set in itertools.chain(RepresentativeSet.objects.filter(enabled=True), Election.objects.filter(enabled=True)):
for representative_set in itertools.chain(
RepresentativeSet.objects.filter(enabled=True), Election.objects.filter(enabled=True)
):
try:
representative_set.update_from_data_source()
except Exception:
log.error("Couldn't update representatives in %s." % representative_set)
representative_set.__class__.objects.filter(pk=representative_set.pk).update(last_import_successful=False)
representative_set.__class__.objects.filter(pk=representative_set.pk).update(
last_import_successful=False
)
10 changes: 4 additions & 6 deletions representatives/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

from django.db import models, migrations

class JSONField(models.TextField):
"""Mocks jsonfield 0.92's column-type behaviour"""
def db_type(self, connection):
if connection.vendor == 'postgresql' and connection.pg_version >= 90300:
return 'json'
else:
return super(JSONField, self).db_type(connection)
return super().db_type(connection)

class Migration(migrations.Migration):

Expand Down Expand Up @@ -110,13 +108,13 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='representative',
name='representative_set',
field=models.ForeignKey(related_name='individuals', to='representatives.RepresentativeSet'),
field=models.ForeignKey(on_delete=models.CASCADE, related_name='individuals', to='representatives.RepresentativeSet'),
preserve_default=True,
),
migrations.AddField(
model_name='candidate',
name='election',
field=models.ForeignKey(related_name='individuals', to='representatives.Election'),
field=models.ForeignKey(on_delete=models.CASCADE, related_name='individuals', to='representatives.Election'),
preserve_default=True,
),
]
6 changes: 2 additions & 4 deletions representatives/migrations/0002_auto_20141129_1450.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

from django.db import models, migrations

class JSONField(models.TextField):
"""Mocks jsonfield 0.92's column-type behaviour"""
def db_type(self, connection):
if connection.vendor == 'postgresql' and connection.pg_version >= 90300:
return 'json'
else:
return super(JSONField, self).db_type(connection)
return super().db_type(connection)


class Migration(migrations.Migration):
Expand Down
5 changes: 2 additions & 3 deletions representatives/migrations/0003_auto_20170214_1237.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.11 on 2017-02-14 12:37
from __future__ import unicode_literals

from django.db import migrations, models


class JSONField(models.TextField):
"""Mocks jsonfield 0.92's column-type behaviour"""
def db_type(self, connection):
if connection.vendor == 'postgresql' and connection.pg_version >= 90300:
return 'json'
else:
return super(JSONField, self).db_type(connection)
return super().db_type(connection)

class Migration(migrations.Migration):

Expand Down
2 changes: 0 additions & 2 deletions representatives/migrations/0004_switch_to_django_jsonfield.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-02-23 20:05
from __future__ import unicode_literals

import django.contrib.postgres.fields.jsonb
from django.db import migrations
Expand Down
Loading

0 comments on commit 7dade26

Please sign in to comment.