diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..ba6ba0a --- /dev/null +++ b/.github/workflows/lint.yml @@ -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 . diff --git a/.gitignore b/.gitignore index 7a4b5f9..c37c114 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ settings.py /paramiko.log /staticfiles +/static /data /static diff --git a/finder/templates/api.html b/finder/templates/api.html index 2a541f2..2286427 100644 --- a/finder/templates/api.html +++ b/finder/templates/api.html @@ -1,5 +1,5 @@ {% extends 'base.html' %} -{% load staticfiles %} +{% load static %} {% block title %}API Reference | Represent Elected Officials and Electoral Districts API for Canada{% endblock %} {% block bodyattributes %}id="api"{% endblock %} diff --git a/finder/templates/data.html b/finder/templates/data.html index b6968ae..82149d7 100644 --- a/finder/templates/data.html +++ b/finder/templates/data.html @@ -1,6 +1,6 @@ {% extends 'base.html' %} {% load i18n %} -{% load staticfiles %} +{% load static %} {% block title %}{% trans "CSV Downloads" %} | {% trans "Represent Elected Officials and Electoral Districts API for Canada" %}{% endblock %} {% block bodyattributes %}id="data"{% endblock %} diff --git a/finder/templates/demo.html b/finder/templates/demo.html index 0c1c382..447a47e 100644 --- a/finder/templates/demo.html +++ b/finder/templates/demo.html @@ -1,6 +1,6 @@ {% extends 'base.html' %} {% load i18n %} -{% load staticfiles %} +{% load static %} {% block title %}{% trans "Find Your Canadian Representatives with the Represent API" %}{% endblock %} diff --git a/finder/templates/government.html b/finder/templates/government.html index 5bd6a73..77401c4 100644 --- a/finder/templates/government.html +++ b/finder/templates/government.html @@ -1,6 +1,6 @@ {% extends 'base.html' %} {% load i18n %} -{% load staticfiles %} +{% load static %} {% block title %}{% trans "For Government" %} | {% trans "Represent Elected Officials and Electoral Districts API for Canada" %}{% endblock %} {% block bodyattributes %}id="government"{% endblock %} diff --git a/finder/templates/index.html b/finder/templates/index.html index d62e21b..fbce6d6 100644 --- a/finder/templates/index.html +++ b/finder/templates/index.html @@ -1,6 +1,6 @@ {% extends 'base.html' %} {% load i18n %} -{% load staticfiles %} +{% load static %} {% block title %}{% trans "Represent Elected Officials and Electoral Districts API for Canada" %}{% endblock %} diff --git a/finder/urls.py b/finder/urls.py index 4eb3c33..12a5032 100644 --- a/finder/urls.py +++ b/finder/urls.py @@ -1,15 +1,15 @@ -from django.conf.urls import url +from django.urls import path from django.views.i18n import JavaScriptCatalog from finder import views urlpatterns = [ - url(r'^jsi18n/$', JavaScriptCatalog.as_view(packages=['finder']), name='javascript-catalog'), - url(r'^setlang/$', views.set_language, name='set_language'), - url(r'^api/$', views.api, name='apidoc'), - url(r'^data/$', views.data, name='data'), - url(r'^demo/$', views.demo, name='demo'), - url(r'^government/$', views.government, name='government'), - url(r'^privacy/$', views.privacy, name='privacy'), - url(r'^$', views.index), + path('jsi18n/', JavaScriptCatalog.as_view(packages=['finder']), name='javascript-catalog'), + path('setlang/', views.set_language, name='set_language'), + path('api/', views.api, name='apidoc'), + path('data/', views.data, name='data'), + path('demo/', views.demo, name='demo'), + path('government/', views.government, name='government'), + path('privacy/', views.privacy, name='privacy'), + path('', views.index), ] diff --git a/finder/views.py b/finder/views.py index 498d5c8..99ba5a3 100644 --- a/finder/views.py +++ b/finder/views.py @@ -1,8 +1,8 @@ from django import http from django.conf import settings from django.shortcuts import render +from django.utils.http import url_has_allowed_host_and_scheme from django.utils.translation import check_for_language -from django.utils.http import is_safe_url def index(request): @@ -36,9 +36,9 @@ def _render(template_name, request): # @see django/views/i18n.py def set_language(request): next = request.GET.get('next') - if not is_safe_url(url=next, host=request.get_host()): - next = request.META.get('HTTP_REFERER') - if not is_safe_url(url=next, host=request.get_host()): + if not url_has_allowed_host_and_scheme(url=next, host=request.get_host()): + next = request.headers.get('referer') + if not url_has_allowed_host_and_scheme(url=next, host=request.get_host()): next = '/' response = http.HttpResponseRedirect(next) lang_code = request.GET.get('language') diff --git a/represent/default_settings.py b/represent/default_settings.py index fb9ed83..e05e5b2 100644 --- a/represent/default_settings.py +++ b/represent/default_settings.py @@ -1,4 +1,3 @@ -# coding: utf-8 import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) @@ -63,6 +62,11 @@ ROOT_URLCONF = 'represent.urls' WSGI_APPLICATION = 'represent.wsgi.application' +# Default primary key field type +# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + LANGUAGES = ( ('en', 'English'), ('fr', 'Français'), @@ -116,3 +120,8 @@ 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', }, } + +if 'GDAL_LIBRARY_PATH' in os.environ: + GDAL_LIBRARY_PATH = os.getenv('GDAL_LIBRARY_PATH') +if 'GEOS_LIBRARY_PATH' in os.environ: + GEOS_LIBRARY_PATH = os.getenv('GEOS_LIBRARY_PATH') diff --git a/represent/urls.py b/represent/urls.py index 326248d..055ee8d 100644 --- a/represent/urls.py +++ b/represent/urls.py @@ -1,12 +1,19 @@ -from django.conf.urls import include, url +from django.conf import settings from django.contrib import admin +from django.contrib.staticfiles import views +from django.urls import include, path, re_path admin.autodiscover() urlpatterns = [ - url(r'^admin/', admin.site.urls), - url('', include('boundaries.urls')), - url('', include('representatives.urls')), - url('', include('postcodes.urls')), - url('', include('finder.urls')), + re_path(r'^admin/', admin.site.urls), + path('', include('boundaries.urls')), + path('', include('representatives.urls')), + path('', include('postcodes.urls')), + path('', include('finder.urls')), ] + +if settings.DEBUG: + urlpatterns += [ + re_path(r"^static/(?P.*)$", views.serve), + ] diff --git a/represent/wsgi.py b/represent/wsgi.py index 630f717..53f3c83 100644 --- a/represent/wsgi.py +++ b/represent/wsgi.py @@ -8,7 +8,9 @@ """ import os + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "represent.settings") -from django.core.wsgi import get_wsgi_application +from django.core.wsgi import get_wsgi_application # noqa: E402 + application = get_wsgi_application() diff --git a/requirements.txt b/requirements.txt index 61751aa..7202c12 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,10 @@ -Django==1.11.29 -psycopg2-binary==2.7.4 +Django<5 +psycopg2-binary==2.9.9 python3-memcached==1.51 -represent-boundaries==0.9.3 --e git+https://github.com/opennorth/represent-reps.git#egg=represent-representatives --e git+https://github.com/opennorth/represent-postcodes.git#egg=represent-postcodes +# represent-boundaries==0.9.3 +-e git+https://github.com/opennorth/represent-boundaries.git@upgrade#egg=represent-boundaries +-e git+https://github.com/opennorth/represent-reps.git@upgrade#egg=represent-representatives +-e git+https://github.com/opennorth/represent-postcodes.git@upgrade#egg=represent-postcodes # Server eventlet==0.35.2 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..119a32a --- /dev/null +++ b/setup.cfg @@ -0,0 +1,6 @@ +[isort] +line_length = 119 +profile = black + +[flake8] +max-line-length = 119 diff --git a/templates/base.html b/templates/base.html index 0bedfdb..8cb029f 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,4 +1,4 @@ -{% load i18n %}{% load staticfiles %} +{% load i18n %}{% load static %} diff --git a/templates/boundaries/apibrowser.html b/templates/boundaries/apibrowser.html index 7077a04..b7562d4 100644 --- a/templates/boundaries/apibrowser.html +++ b/templates/boundaries/apibrowser.html @@ -1,5 +1,5 @@ {% extends 'base.html' %} -{% load staticfiles %} +{% load static %} {% block title %}{{ title.capitalize }} | Represent API{% endblock %} diff --git a/tox.ini b/tox.ini deleted file mode 100644 index 9a06d47..0000000 --- a/tox.ini +++ /dev/null @@ -1,3 +0,0 @@ -[flake8] -ignore=E501 -# E501 line too long (X > 79 characters)