diff options
| -rw-r--r-- | pydis_site/apps/content/urls.py | 43 | ||||
| -rw-r--r-- | pydis_site/apps/events/urls.py | 35 | ||||
| -rw-r--r-- | pydis_site/apps/home/urls.py | 6 | ||||
| -rw-r--r-- | pydis_site/apps/home/views/home.py | 26 | ||||
| -rw-r--r-- | pydis_site/apps/resources/urls.py | 22 | ||||
| -rw-r--r-- | pydis_site/urls.py | 11 | 
6 files changed, 122 insertions, 21 deletions
| diff --git a/pydis_site/apps/content/urls.py b/pydis_site/apps/content/urls.py index c11b222a..fe7c2852 100644 --- a/pydis_site/apps/content/urls.py +++ b/pydis_site/apps/content/urls.py @@ -1,9 +1,46 @@ -from django.urls import path +import typing +from pathlib import Path + +from django_distill import distill_path  from . import views  app_name = "content" + + +def __get_all_files(root: Path, folder: typing.Optional[Path] = None) -> list[str]: +    """Find all folders and markdown files recursively starting from `root`.""" +    if not folder: +        folder = root + +    results = [] + +    for item in folder.iterdir(): +        name = item.relative_to(root).__str__().replace("\\", "/") + +        if item.is_dir(): +            results.append(name) +            results.extend(__get_all_files(root, item)) +        else: +            path, extension = name.rsplit(".", maxsplit=1) +            if extension == "md": +                results.append(path) + +    return results + + +def get_all_pages() -> typing.Iterator[dict[str, str]]: +    """Yield a dict of all pag categories.""" +    for location in __get_all_files(Path("pydis_site", "apps", "content", "resources")): +        yield {"location": location} + +  urlpatterns = [ -    path("", views.PageOrCategoryView.as_view(), name='pages'), -    path("<path:location>/", views.PageOrCategoryView.as_view(), name='page_category'), +    distill_path("", views.PageOrCategoryView.as_view(), name='pages'), +    distill_path( +        "<path:location>/", +        views.PageOrCategoryView.as_view(), +        name='page_category', +        distill_func=get_all_pages +    ),  ] diff --git a/pydis_site/apps/events/urls.py b/pydis_site/apps/events/urls.py index 9a65cf1f..7ea65a31 100644 --- a/pydis_site/apps/events/urls.py +++ b/pydis_site/apps/events/urls.py @@ -1,9 +1,38 @@ -from django.urls import path +import typing +from pathlib import Path + +from django_distill import distill_path  from pydis_site.apps.events.views import IndexView, PageView  app_name = "events" + + +def __get_all_files(root: Path, folder: typing.Optional[Path] = None) -> list[str]: +    """Find all folders and HTML files recursively starting from `root`.""" +    if not folder: +        folder = root + +    results = [] + +    for sub_folder in folder.iterdir(): +        results.append( +            sub_folder.relative_to(root).__str__().replace("\\", "/").replace(".html", "") +        ) + +        if sub_folder.is_dir(): +            results.extend(__get_all_files(root, sub_folder)) + +    return results + + +def get_all_events() -> typing.Iterator[dict[str, str]]: +    """Yield a dict of all event pages.""" +    for file in __get_all_files(Path("pydis_site", "templates", "events", "pages")): +        yield {"path": file} + +  urlpatterns = [ -    path("", IndexView.as_view(), name="index"), -    path("<path:path>/", PageView.as_view(), name="page"), +    distill_path("", IndexView.as_view(), name="index"), +    distill_path("<path:path>/", PageView.as_view(), name="page", distill_func=get_all_events),  ] diff --git a/pydis_site/apps/home/urls.py b/pydis_site/apps/home/urls.py index 57abc942..30321ece 100644 --- a/pydis_site/apps/home/urls.py +++ b/pydis_site/apps/home/urls.py @@ -1,9 +1,9 @@ -from django.urls import path +from django_distill import distill_path  from .views import HomeView, timeline  app_name = 'home'  urlpatterns = [ -    path('', HomeView.as_view(), name='home'), -    path('timeline/', timeline, name="timeline"), +    distill_path('', HomeView.as_view(), name='home'), +    distill_path('timeline/', timeline, name="timeline"),  ] diff --git a/pydis_site/apps/home/views/home.py b/pydis_site/apps/home/views/home.py index 401c768f..e28a3a00 100644 --- a/pydis_site/apps/home/views/home.py +++ b/pydis_site/apps/home/views/home.py @@ -8,6 +8,7 @@ from django.shortcuts import render  from django.utils import timezone  from django.views import View +from pydis_site import settings  from pydis_site.apps.home.models import RepositoryMetadata  from pydis_site.constants import GITHUB_TOKEN, TIMEOUT_PERIOD @@ -32,7 +33,10 @@ class HomeView(View):      def __init__(self):          """Clean up stale RepositoryMetadata.""" -        RepositoryMetadata.objects.exclude(repo_name__in=self.repos).delete() +        self._static_build = settings.env("STATIC_BUILD") + +        if not self._static_build: +            RepositoryMetadata.objects.exclude(repo_name__in=self.repos).delete()          # If no token is defined (for example in local development), then          # it does not make sense to pass the Authorization header. More @@ -91,10 +95,13 @@ class HomeView(View):      def _get_repo_data(self) -> List[RepositoryMetadata]:          """Build a list of RepositoryMetadata objects that we can use to populate the front page."""          # First off, load the timestamp of the least recently updated entry. -        last_update = ( -            RepositoryMetadata.objects.values_list("last_updated", flat=True) -            .order_by("last_updated").first() -        ) +        if self._static_build: +            last_update = None +        else: +            last_update = ( +                RepositoryMetadata.objects.values_list("last_updated", flat=True) +                .order_by("last_updated").first() +            )          # If we did not retrieve any results here, we should import them!          if last_update is None: @@ -104,7 +111,7 @@ class HomeView(View):              api_repositories = self._get_api_data()              # Create all the repodata records in the database. -            return RepositoryMetadata.objects.bulk_create( +            data = [                  RepositoryMetadata(                      repo_name=api_data["full_name"],                      description=api_data["description"], @@ -113,7 +120,12 @@ class HomeView(View):                      language=api_data["language"],                  )                  for api_data in api_repositories.values() -            ) +            ] + +            if settings.env("STATIC_BUILD"): +                return data +            else: +                return RepositoryMetadata.objects.bulk_create(data)          # If the data is stale, we should refresh it.          if (timezone.now() - last_update).seconds > self.repository_cache_ttl: diff --git a/pydis_site/apps/resources/urls.py b/pydis_site/apps/resources/urls.py index 19142081..10eda132 100644 --- a/pydis_site/apps/resources/urls.py +++ b/pydis_site/apps/resources/urls.py @@ -1,9 +1,25 @@ -from django.urls import path +import typing +from pathlib import Path + +from django_distill import distill_path  from pydis_site.apps.resources import views  app_name = "resources" + + +def get_all_resources() -> typing.Iterator[dict[str, str]]: +    """Yield a dict of all resource categories.""" +    for category in Path("pydis_site", "apps", "resources", "resources").iterdir(): +        yield {"category": category.name} + +  urlpatterns = [ -    path("", views.ResourcesView.as_view(), name="index"), -    path("<str:category>/", views.ResourcesListView.as_view(), name="resources") +    distill_path("", views.ResourcesView.as_view(), name="index"), +    distill_path( +        "<str:category>/", +        views.ResourcesListView.as_view(), +        name="resources", +        distill_func=get_all_resources +    ),  ] diff --git a/pydis_site/urls.py b/pydis_site/urls.py index 891dbdcc..51ef4214 100644 --- a/pydis_site/urls.py +++ b/pydis_site/urls.py @@ -1,8 +1,9 @@  from django.contrib import admin  from django.urls import include, path +from pydis_site import settings -urlpatterns = ( +NON_STATIC_PATTERNS = [      path('admin/', admin.site.urls),      # External API ingress (over the net) @@ -14,9 +15,15 @@ urlpatterns = (      # from wildcard matching all requests to `pages/...`.      path('', include('pydis_site.apps.redirect.urls')),      path('', include('django_prometheus.urls')), + +    path('staff/', include('pydis_site.apps.staff.urls', namespace='staff')), +] if not settings.env("STATIC_BUILD") else [] + + +urlpatterns = ( +    *NON_STATIC_PATTERNS,      path('pages/', include('pydis_site.apps.content.urls', namespace='content')),      path('resources/', include('pydis_site.apps.resources.urls')),      path('events/', include('pydis_site.apps.events.urls', namespace='events')), -    path('staff/', include('pydis_site.apps.staff.urls', namespace='staff')),      path('', include('pydis_site.apps.home.urls', namespace='home')),  ) | 
