diff options
| author | 2021-11-05 10:09:35 +0530 | |
|---|---|---|
| committer | 2021-11-05 10:09:35 +0530 | |
| commit | 747a7b9cb465c85a064ac6e1a0444bcf8a81ec65 (patch) | |
| tree | 1bb529b764bed5c727a3956b7bb432a18ec82d43 /pydis_site/apps/home/views/home.py | |
| parent | Merge branch 'main' into otn_softdel (diff) | |
| parent | Merge pull request #617 from python-discord/mbaruh-patch-1 (diff) | |
Pull from upstream/main and resolve conflicts.
Diffstat (limited to 'pydis_site/apps/home/views/home.py')
| -rw-r--r-- | pydis_site/apps/home/views/home.py | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/pydis_site/apps/home/views/home.py b/pydis_site/apps/home/views/home.py index 0f26cef3..e28a3a00 100644 --- a/pydis_site/apps/home/views/home.py +++ b/pydis_site/apps/home/views/home.py @@ -8,8 +8,9 @@ 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 +from pydis_site.constants import GITHUB_TOKEN, TIMEOUT_PERIOD log = logging.getLogger(__name__) @@ -27,12 +28,15 @@ class HomeView(View): "python-discord/snekbox", "python-discord/sir-lancebot", "python-discord/metricity", - "python-discord/django-simple-bulma", + "python-discord/king-arthur", ] 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 @@ -51,9 +55,16 @@ class HomeView(View): If we're unable to get that info for any reason, return an empty dict. """ repo_dict = {} - - # Fetch the data from the GitHub API - api_data: List[dict] = requests.get(self.github_api, headers=self.headers).json() + try: + # Fetch the data from the GitHub API + api_data: List[dict] = requests.get( + self.github_api, + headers=self.headers, + timeout=TIMEOUT_PERIOD + ).json() + except requests.exceptions.Timeout: + log.error("Request to fetch GitHub repository metadata for timed out!") + return repo_dict # Process the API data into our dict for repo in api_data: @@ -84,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: @@ -97,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"], @@ -106,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: |