From b7e2ce2a4d44cd7c392c2d30f863b6556f90cc21 Mon Sep 17 00:00:00 2001 From: Ben Sturmfels Date: Fri, 20 Oct 2023 09:44:24 +1100 Subject: [PATCH] Switch to relative imports The relative imports make it clearer what code is within the project and where it's coming from. --- www/conservancy/__init__.py | 4 ---- .../migrations/0004_auto_20230127_0602.py | 5 ++--- www/conservancy/apps/blog/admin.py | 5 ++++- www/conservancy/apps/blog/models.py | 9 ++++++--- www/conservancy/apps/blog/urls.py | 10 ++++++---- www/conservancy/apps/blog/views.py | 12 +++++++----- www/conservancy/apps/contacts/admin.py | 4 +++- www/conservancy/apps/contacts/views.py | 4 +++- www/conservancy/apps/contractpatch/urls.py | 3 ++- www/conservancy/apps/contractpatch/views.py | 11 +++++++---- www/conservancy/apps/events/admin.py | 3 ++- www/conservancy/apps/events/models.py | 9 ++++++--- www/conservancy/apps/events/urls.py | 3 ++- www/conservancy/apps/events/views.py | 6 +++--- www/conservancy/apps/fundgoal/admin.py | 9 ++++++--- www/conservancy/apps/fundgoal/views.py | 5 +++-- www/conservancy/apps/news/admin.py | 4 +++- www/conservancy/apps/news/models.py | 11 +++++++---- www/conservancy/apps/news/urls.py | 5 +++-- www/conservancy/apps/news/views.py | 12 ++++++------ www/conservancy/apps/staff/admin.py | 4 +++- .../apps/summit_registration/admin.py | 4 +++- www/conservancy/apps/supporter/urls.py | 5 +++-- www/conservancy/apps/supporter/views.py | 5 ++++- www/conservancy/apps/supporters/admin.py | 4 +++- www/conservancy/apps/worldmap/admin.py | 4 +++- www/conservancy/feeds.py | 18 ++++++++++-------- www/conservancy/frontpage.py | 16 +++++++++------- www/conservancy/local_context_processors.py | 8 ++++---- www/conservancy/sponsors.py | 7 +++++-- www/conservancy/urls.py | 6 +++--- 31 files changed, 131 insertions(+), 84 deletions(-) diff --git a/www/conservancy/__init__.py b/www/conservancy/__init__.py index 979a2cd2..a1dc7528 100644 --- a/www/conservancy/__init__.py +++ b/www/conservancy/__init__.py @@ -2,10 +2,6 @@ import hashlib from django.conf import settings -# This is backwards compatibilty support for a custom function we wrote -# ourselves that is no longer necessary in modern Django. -from django.shortcuts import render as render_template_with_context - class ParameterValidator: def __init__(self, given_hash_or_params, params_hash_key=None): diff --git a/www/conservancy/apps/assignment/migrations/0004_auto_20230127_0602.py b/www/conservancy/apps/assignment/migrations/0004_auto_20230127_0602.py index c00a2936..625d9dbc 100644 --- a/www/conservancy/apps/assignment/migrations/0004_auto_20230127_0602.py +++ b/www/conservancy/apps/assignment/migrations/0004_auto_20230127_0602.py @@ -1,8 +1,7 @@ # Generated by Django 1.11.29 on 2023-01-27 06:02 -import conservancy.apps.assignment.models from django.db import migrations, models - +from ..models import validate_mutiple_urls class Migration(migrations.Migration): @@ -24,6 +23,6 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='assignment', name='repositories', - field=models.TextField(help_text='List of URLs, one per line', validators=[conservancy.apps.assignment.models.validate_mutiple_urls], verbose_name='Code repositories containing contributions of yours whose copyright you are assigning'), + field=models.TextField(help_text='List of URLs, one per line', validators=[validate_mutiple_urls], verbose_name='Code repositories containing contributions of yours whose copyright you are assigning'), ), ] diff --git a/www/conservancy/apps/blog/admin.py b/www/conservancy/apps/blog/admin.py index 18a4aa93..539ed502 100644 --- a/www/conservancy/apps/blog/admin.py +++ b/www/conservancy/apps/blog/admin.py @@ -1,11 +1,14 @@ from django.contrib import admin -from conservancy.apps.blog.models import EntryTag, Entry + +from .models import EntryTag, Entry + class EntryTagAdmin(admin.ModelAdmin): prepopulated_fields = {'slug': ('label',)} admin.site.register(EntryTag, EntryTagAdmin) + class EntryAdmin(admin.ModelAdmin): list_display = ('pub_date', 'headline', 'author') list_filter = ['pub_date'] diff --git a/www/conservancy/apps/blog/models.py b/www/conservancy/apps/blog/models.py index 01b3be0c..f37e4e26 100644 --- a/www/conservancy/apps/blog/models.py +++ b/www/conservancy/apps/blog/models.py @@ -1,8 +1,11 @@ +from datetime import datetime, timedelta + from django.db import models from django.conf import settings -from conservancy import bsoup -from conservancy.apps.staff.models import Person -from datetime import datetime, timedelta + +from ... import bsoup +from ..staff.models import Person + class EntryTag(models.Model): """Tagging for blog entries""" diff --git a/www/conservancy/apps/blog/urls.py b/www/conservancy/apps/blog/urls.py index 128afbfc..1c46610c 100644 --- a/www/conservancy/apps/blog/urls.py +++ b/www/conservancy/apps/blog/urls.py @@ -1,8 +1,10 @@ -from django.conf.urls import url, include -from conservancy.apps.blog.models import Entry, EntryTag # relative import -from conservancy.apps.staff.models import Person from datetime import datetime -from conservancy.apps.blog.views import last_name, BlogYearArchiveView, BlogMonthArchiveView, BlogDayArchiveView, BlogDateDetailView, custom_index, query + +from django.conf.urls import url, include + +from .models import Entry, EntryTag +from ..staff.models import Person +from .views import last_name, BlogYearArchiveView, BlogMonthArchiveView, BlogDayArchiveView, BlogDateDetailView, custom_index, query extra_context = {} diff --git a/www/conservancy/apps/blog/views.py b/www/conservancy/apps/blog/views.py index 4c7f9db9..06f4c65a 100644 --- a/www/conservancy/apps/blog/views.py +++ b/www/conservancy/apps/blog/views.py @@ -1,12 +1,14 @@ -from conservancy.apps.blog.models import Entry, EntryTag # relative import -# from django.views.generic.list_detail import object_list +from datetime import datetime +from functools import reduce + from django.views.generic import ListView from django.views.generic.dates import YearArchiveView, MonthArchiveView, DayArchiveView, DateDetailView from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger -from conservancy.apps.staff.models import Person from django.shortcuts import get_object_or_404, render -from datetime import datetime -from functools import reduce + +from .models import Entry, EntryTag +from ..staff.models import Person + def OR_filter(field_name, objs): from django.db.models import Q diff --git a/www/conservancy/apps/contacts/admin.py b/www/conservancy/apps/contacts/admin.py index e52b6aa5..adc2adf5 100644 --- a/www/conservancy/apps/contacts/admin.py +++ b/www/conservancy/apps/contacts/admin.py @@ -1,5 +1,7 @@ from django.contrib import admin -from conservancy.apps.contacts.models import ContactEntry + +from .models import ContactEntry + class ContactEntryAdmin(admin.ModelAdmin): list_display = ('email', 'subscribe_conservancy') diff --git a/www/conservancy/apps/contacts/views.py b/www/conservancy/apps/contacts/views.py index 88337793..a727bdfa 100644 --- a/www/conservancy/apps/contacts/views.py +++ b/www/conservancy/apps/contacts/views.py @@ -1,8 +1,10 @@ from django.shortcuts import render from django import forms -from conservancy.apps.contacts.models import ContactEntry from django.forms import ModelForm +from .models import ContactEntry + + def subscribe(request): """Mailing list subscription form """ diff --git a/www/conservancy/apps/contractpatch/urls.py b/www/conservancy/apps/contractpatch/urls.py index 6ab389f9..fc1be6ec 100644 --- a/www/conservancy/apps/contractpatch/urls.py +++ b/www/conservancy/apps/contractpatch/urls.py @@ -1,5 +1,6 @@ from django.conf.urls import url, include -from conservancy.apps.contractpatch import views as cpatch_views + +from . import views as cpatch_views urlpatterns = [ url(r'', cpatch_views.index), diff --git a/www/conservancy/apps/contractpatch/views.py b/www/conservancy/apps/contractpatch/views.py index 4202552e..e4ee73ee 100644 --- a/www/conservancy/apps/contractpatch/views.py +++ b/www/conservancy/apps/contractpatch/views.py @@ -1,13 +1,16 @@ -from conservancy import render_template_with_context -from conservancy.apps.blog.models import Entry as BlogEntry from datetime import datetime +from django.shortcuts import render + +from ..blog.models import Entry + + def index(request): filters = { 'pub_date__lte': datetime.now(), 'tags__slug': 'ContractPatch', } context = { - 'blog_entries': BlogEntry.objects.filter(**filters)[:3], + 'blog_entries': Entry.objects.filter(**filters)[:3], } - return render_template_with_context(request, "contractpatch/index.html", context) + return render(request, "contractpatch/index.html", context) diff --git a/www/conservancy/apps/events/admin.py b/www/conservancy/apps/events/admin.py index 519891bb..6a2a95f9 100644 --- a/www/conservancy/apps/events/admin.py +++ b/www/conservancy/apps/events/admin.py @@ -1,5 +1,6 @@ from django.contrib import admin -from conservancy.apps.events.models import EventTag, Event, EventMedia + +from .models import EventTag, Event, EventMedia admin.site.register(EventTag) diff --git a/www/conservancy/apps/events/models.py b/www/conservancy/apps/events/models.py index 1b116820..03bed907 100644 --- a/www/conservancy/apps/events/models.py +++ b/www/conservancy/apps/events/models.py @@ -1,8 +1,11 @@ -from django.db import models -from conservancy.apps.staff.models import Person -from conservancy.apps.worldmap.models import EarthLocation from datetime import datetime, timedelta +from django.db import models + +from ..staff.models import Person +from ..worldmap.models import EarthLocation + + class EventTag(models.Model): """Tagging for events diff --git a/www/conservancy/apps/events/urls.py b/www/conservancy/apps/events/urls.py index e6802e48..ab146e48 100644 --- a/www/conservancy/apps/events/urls.py +++ b/www/conservancy/apps/events/urls.py @@ -1,5 +1,6 @@ from django.conf.urls import patterns, url, include -from conservancy.apps.events.models import Event + +from .models import Event info_dict = { 'queryset': Event.objects.all(), diff --git a/www/conservancy/apps/events/views.py b/www/conservancy/apps/events/views.py index b04254c8..5e10b813 100644 --- a/www/conservancy/apps/events/views.py +++ b/www/conservancy/apps/events/views.py @@ -3,9 +3,9 @@ from django.shortcuts import render from django.http import Http404, HttpResponse from django.template import loader from django.core.exceptions import ObjectDoesNotExist -from conservancy.apps.events.models import Event -# for debugging... -from django.http import HttpResponse + +from .models import Event + def event_detail(request, year, slug, queryset, **kwargs): """This view shows event detail. diff --git a/www/conservancy/apps/fundgoal/admin.py b/www/conservancy/apps/fundgoal/admin.py index c5f36715..3186f343 100644 --- a/www/conservancy/apps/fundgoal/admin.py +++ b/www/conservancy/apps/fundgoal/admin.py @@ -1,14 +1,17 @@ from django.contrib import admin -from conservancy.apps.fundgoal import models as fundgoal_models + +from . import models + class FundraisingGoalAdmin(admin.ModelAdmin): list_display = ('fundraiser_code_name', 'fundraiser_goal_amount') + class GoalProviderAdmin(admin.ModelAdmin): fields = [ 'fundraising_goal', 'provider_name', ] -admin.site.register(fundgoal_models.FundraisingGoal, FundraisingGoalAdmin) -admin.site.register(fundgoal_models.GoalProvider, GoalProviderAdmin) +admin.site.register(models.FundraisingGoal, FundraisingGoalAdmin) +admin.site.register(models.GoalProvider, GoalProviderAdmin) diff --git a/www/conservancy/apps/fundgoal/views.py b/www/conservancy/apps/fundgoal/views.py index 027b1831..b2f6673e 100644 --- a/www/conservancy/apps/fundgoal/views.py +++ b/www/conservancy/apps/fundgoal/views.py @@ -1,6 +1,7 @@ -from conservancy.apps.fundgoal.models import FundraisingGoal from django.http import JsonResponse +from .models import FundraisingGoal + def view(request): """JSON version of request @@ -20,4 +21,4 @@ def view(request): if hasattr(fundGoal, kk): returnDict[codeName][kk] = getattr(fundGoal, kk) - return JsonResponse( returnDict) + return JsonResponse(returnDict) diff --git a/www/conservancy/apps/news/admin.py b/www/conservancy/apps/news/admin.py index 7c554b4c..fbecace3 100644 --- a/www/conservancy/apps/news/admin.py +++ b/www/conservancy/apps/news/admin.py @@ -1,5 +1,7 @@ from django.contrib import admin -from conservancy.apps.news.models import PressRelease, ExternalArticleTag, ExternalArticle + +from .models import PressRelease, ExternalArticleTag, ExternalArticle + class PressReleaseAdmin(admin.ModelAdmin): list_display = ("headline", "pub_date") diff --git a/www/conservancy/apps/news/models.py b/www/conservancy/apps/news/models.py index f4031b14..c51ea69b 100644 --- a/www/conservancy/apps/news/models.py +++ b/www/conservancy/apps/news/models.py @@ -1,10 +1,13 @@ +from datetime import datetime, timedelta + from django.db import models from django.conf import settings -from conservancy import bsoup -from conservancy.apps.staff.models import Person -from conservancy.apps.events.models import Event from django.contrib.sites.models import Site -from datetime import datetime, timedelta + +from ... import bsoup +from ..staff.models import Person +from ..events.models import Event + class PressRelease(models.Model, bsoup.SoupModelMixin): """News release model""" diff --git a/www/conservancy/apps/news/urls.py b/www/conservancy/apps/news/urls.py index abe8ba1e..f7ac8e7c 100644 --- a/www/conservancy/apps/news/urls.py +++ b/www/conservancy/apps/news/urls.py @@ -19,8 +19,9 @@ from django.conf.urls import url, include from django.conf import settings -from conservancy.apps.news.models import PressRelease, ExternalArticle -from conservancy.apps.news.views import NewsYearArchiveView, NewsMonthArchiveView, NewsDayArchiveView, NewsDateDetailView, listing + +from .models import PressRelease, ExternalArticle +from .views import NewsYearArchiveView, NewsMonthArchiveView, NewsDayArchiveView, NewsDateDetailView, listing info_dict = { 'queryset': PressRelease.objects.all().filter(sites__id__exact=settings.SITE_ID), diff --git a/www/conservancy/apps/news/views.py b/www/conservancy/apps/news/views.py index 6c95a230..bae3cefb 100644 --- a/www/conservancy/apps/news/views.py +++ b/www/conservancy/apps/news/views.py @@ -1,15 +1,15 @@ -# from django.views.generic.list_detail import object_list +from datetime import datetime + from django.views.generic import ListView from django.shortcuts import render from django.views.generic.dates import YearArchiveView, MonthArchiveView, DayArchiveView, DateDetailView from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger -from conservancy.apps.news.models import ExternalArticle -from conservancy.apps.news.models import PressRelease -from conservancy.apps.events.models import Event -from datetime import datetime -# for debugging... from django.http import HttpResponse +from .models import ExternalArticle +from .models import PressRelease +from ..events.models import Event + class NewsListView(ListView): extra_context = {} diff --git a/www/conservancy/apps/staff/admin.py b/www/conservancy/apps/staff/admin.py index a661b7e9..d2626a1d 100644 --- a/www/conservancy/apps/staff/admin.py +++ b/www/conservancy/apps/staff/admin.py @@ -1,5 +1,7 @@ from django.contrib import admin -from conservancy.apps.staff.models import Person + +from .models import Person + class PersonAdmin(admin.ModelAdmin): list_display = ("username", "formal_name", "casual_name", diff --git a/www/conservancy/apps/summit_registration/admin.py b/www/conservancy/apps/summit_registration/admin.py index 84d4f8e7..1f50b69b 100644 --- a/www/conservancy/apps/summit_registration/admin.py +++ b/www/conservancy/apps/summit_registration/admin.py @@ -1,5 +1,7 @@ from django.contrib import admin -from conservancy.apps.summit_registration.models import SummitRegistration + +from .models import SummitRegistration + class SummitRegistrationAdmin(admin.ModelAdmin): list_display = ('name', 'email', 'affiliation', 'cle_credit') diff --git a/www/conservancy/apps/supporter/urls.py b/www/conservancy/apps/supporter/urls.py index cf24c03c..9c75f89c 100644 --- a/www/conservancy/apps/supporter/urls.py +++ b/www/conservancy/apps/supporter/urls.py @@ -1,7 +1,8 @@ from django.conf.urls import url from django.views.generic import TemplateView -from conservancy.apps.supporter import views as supp_views -from conservancy.static import views as static_views + +from . import views as supp_views +from ...static import views as static_views INDEX_VIEW = supp_views.index urlpatterns = [ diff --git a/www/conservancy/apps/supporter/views.py b/www/conservancy/apps/supporter/views.py index 8c4b768e..b00f6e56 100644 --- a/www/conservancy/apps/supporter/views.py +++ b/www/conservancy/apps/supporter/views.py @@ -1,5 +1,8 @@ +from django.shortcuts import render + import conservancy + def index(request): with conservancy.ParameterValidator(request.GET, 'upgrade_id') as validator: try: @@ -13,4 +16,4 @@ def index(request): 'partial_amount': partial_amount, 'minimum_amount': 120 - partial_amount, } - return conservancy.render_template_with_context(request, "supporter/index.html", context) + return render(request, "supporter/index.html", context) diff --git a/www/conservancy/apps/supporters/admin.py b/www/conservancy/apps/supporters/admin.py index 0c9026a9..8d174af9 100644 --- a/www/conservancy/apps/supporters/admin.py +++ b/www/conservancy/apps/supporters/admin.py @@ -1,5 +1,7 @@ from django.contrib import admin -from conservancy.apps.supporters.models import Supporter + +from .models import Supporter + class SupporterAdmin(admin.ModelAdmin): list_display = ('display_name', 'display_until_date') diff --git a/www/conservancy/apps/worldmap/admin.py b/www/conservancy/apps/worldmap/admin.py index 11e238d3..d4a528bf 100644 --- a/www/conservancy/apps/worldmap/admin.py +++ b/www/conservancy/apps/worldmap/admin.py @@ -1,5 +1,7 @@ from django.contrib import admin -from conservancy.apps.worldmap.models import EarthLocation + +from .models import EarthLocation + class EarthLocationAdmin(admin.ModelAdmin): list_display = ("label", "html_map_link") diff --git a/www/conservancy/feeds.py b/www/conservancy/feeds.py index df9f2051..1dc04ec9 100644 --- a/www/conservancy/feeds.py +++ b/www/conservancy/feeds.py @@ -1,14 +1,16 @@ -from django.contrib.syndication.views import Feed -from django.utils.feedgenerator import Rss201rev2Feed -from conservancy.apps.news.models import PressRelease -from conservancy.apps.blog.models import Entry as BlogEntry -from django.shortcuts import render -from django.conf import settings from datetime import datetime - +from functools import reduce import itertools import operator -from functools import reduce + +from django.conf import settings +from django.contrib.syndication.views import Feed +from django.shortcuts import render +from django.utils.feedgenerator import Rss201rev2Feed + +from .apps.blog.models import Entry as BlogEntry +from .apps.news.models import PressRelease + class ConservancyFeedBase(Feed): def copyright_holder(self): return "Software Freedom Conservancy" diff --git a/www/conservancy/frontpage.py b/www/conservancy/frontpage.py index 75840392..da7624df 100644 --- a/www/conservancy/frontpage.py +++ b/www/conservancy/frontpage.py @@ -1,19 +1,21 @@ -from conservancy import render_template_with_context -from conservancy.apps.supporters.models import Supporter as Supporter -from conservancy.apps.news.models import PressRelease -from conservancy.apps.blog.models import Entry as BlogEntry from datetime import datetime +from django.shortcuts import render + +from .apps.blog.models import Entry +from .apps.news.models import PressRelease +from .apps.supporters.models import Supporter + + def view(request): """Conservancy front page view Performs all object queries necessary to render the front page. """ - now = datetime.now() context = { 'press_releases': PressRelease.objects.all().filter(pub_date__lte=now, sites=2)[:5], 'supporters_count': Supporter.objects.all().filter(display_until_date__gte=now).count(), - 'blog': BlogEntry.objects.all().filter(pub_date__lte=now)[:5], + 'blog': Entry.objects.all().filter(pub_date__lte=now)[:5], } - return render_template_with_context(request, "frontpage.html", context) + return render(request, "frontpage.html", context) diff --git a/www/conservancy/local_context_processors.py b/www/conservancy/local_context_processors.py index 81688691..5a902426 100644 --- a/www/conservancy/local_context_processors.py +++ b/www/conservancy/local_context_processors.py @@ -1,7 +1,7 @@ from datetime import datetime as DateTime -import conservancy.settings -from conservancy.apps.fundgoal.models import FundraisingGoal as FundraisingGoal +from . import settings +from .apps.fundgoal.models import FundraisingGoal SITE_FUNDGOAL = 'cy2022-end-year-match' @@ -18,8 +18,8 @@ def sitefundraiser(request): 'sitefundgoal': fundgoal_lookup(SITE_FUNDGOAL), } -if conservancy.settings.FORCE_CANONICAL_HOSTNAME: - _HOST_URL_VAR = {'host_url': 'https://' + conservancy.settings.FORCE_CANONICAL_HOSTNAME} +if settings.FORCE_CANONICAL_HOSTNAME: + _HOST_URL_VAR = {'host_url': 'https://' + settings.FORCE_CANONICAL_HOSTNAME} def host_url(request): return _HOST_URL_VAR else: diff --git a/www/conservancy/sponsors.py b/www/conservancy/sponsors.py index 72e0bc41..be0480ff 100644 --- a/www/conservancy/sponsors.py +++ b/www/conservancy/sponsors.py @@ -1,7 +1,10 @@ -from django.shortcuts import render -from conservancy.apps.supporters.models import Supporter as Supporter from datetime import datetime, timedelta +from django.shortcuts import render + +from .apps.supporters.models import Supporter + + def view(request): """Conservancy Sponsors Page view diff --git a/www/conservancy/urls.py b/www/conservancy/urls.py index 8e3b18d9..ab7cb06b 100644 --- a/www/conservancy/urls.py +++ b/www/conservancy/urls.py @@ -20,9 +20,9 @@ from django.conf.urls import url, include from django.contrib import admin -from conservancy import feeds, frontpage, sponsors -import conservancy.apps.fundgoal.views as fundgoal_views -import conservancy.static.views as static_views +from . import feeds, frontpage, sponsors +from .apps.fundgoal import views as fundgoal_views +from .static import views as static_views admin.autodiscover()