aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps
diff options
context:
space:
mode:
authorGravatar Leon Sandøy <[email protected]>2020-12-13 17:43:45 +0100
committerGravatar Leon Sandøy <[email protected]>2020-12-13 17:43:45 +0100
commit13e09af7d50194359c2fa5575a6e6107a9f8bc9a (patch)
tree31372cd932b285b31539d7f8dc7b567aa5b81650 /pydis_site/apps
parentRemove pointless try/except. (diff)
Clean up stale metadata when HomeView starts.
We don't want to keep lots of old metadata for repos we no longer need to track on the frontpage. Previously, whenever we changed the frontpage line-up, we'd be left with useless, stale data in the database. This fixes that, and adds tests.
Diffstat (limited to 'pydis_site/apps')
-rw-r--r--pydis_site/apps/home/tests/test_repodata_helpers.py34
-rw-r--r--pydis_site/apps/home/views/home.py6
2 files changed, 40 insertions, 0 deletions
diff --git a/pydis_site/apps/home/tests/test_repodata_helpers.py b/pydis_site/apps/home/tests/test_repodata_helpers.py
index 34bbdcde..5634bc9b 100644
--- a/pydis_site/apps/home/tests/test_repodata_helpers.py
+++ b/pydis_site/apps/home/tests/test_repodata_helpers.py
@@ -124,3 +124,37 @@ class TestRepositoryMetadataHelpers(TestCase):
metadata = self.home_view._get_repo_data()
self.assertEquals(len(metadata), 0)
+
+ def test_cleans_up_stale_metadata(self):
+ """Tests that we clean up stale metadata when we start the HomeView."""
+ repo_data = RepositoryMetadata(
+ repo_name="python-discord/INVALID",
+ description="testrepo",
+ forks=42,
+ stargazers=42,
+ language="English",
+ last_updated=timezone.now() - timedelta(seconds=HomeView.repository_cache_ttl + 1),
+ )
+ repo_data.save()
+ self.home_view.__init__()
+ cached_repos = RepositoryMetadata.objects.all()
+ cached_names = [repo.repo_name for repo in cached_repos]
+
+ self.assertNotIn("python-discord/INVALID", cached_names)
+
+ def test_dont_clean_up_unstale_metadata(self):
+ """Tests that we don't clean up good metadata when we start the HomeView."""
+ repo_data = RepositoryMetadata(
+ repo_name="python-discord/site",
+ description="testrepo",
+ forks=42,
+ stargazers=42,
+ language="English",
+ last_updated=timezone.now() - timedelta(seconds=HomeView.repository_cache_ttl + 1),
+ )
+ repo_data.save()
+ self.home_view.__init__()
+ cached_repos = RepositoryMetadata.objects.all()
+ cached_names = [repo.repo_name for repo in cached_repos]
+
+ self.assertIn("python-discord/site", cached_names)
diff --git a/pydis_site/apps/home/views/home.py b/pydis_site/apps/home/views/home.py
index 97253a0c..5c535b65 100644
--- a/pydis_site/apps/home/views/home.py
+++ b/pydis_site/apps/home/views/home.py
@@ -29,6 +29,12 @@ class HomeView(View):
"python-discord/django-simple-bulma",
]
+ def __init__(self):
+ """Clean up stale RepositoryMetadata."""
+ for cached_repo in RepositoryMetadata.objects.all():
+ if cached_repo.repo_name not in self.repos:
+ cached_repo.delete()
+
def _get_api_data(self) -> Dict[str, Dict[str, str]]:
"""
Call the GitHub API and get information about our repos.