diff options
author | 2019-04-18 23:38:31 +0200 | |
---|---|---|
committer | 2019-04-18 23:38:31 +0200 | |
commit | 52d178d7e469fad802bf4dec06453d1764a08787 (patch) | |
tree | a4ee0d736c8252763699e6a40a18b0312d6017f1 /pydis_site | |
parent | Fixing all linting problems introduced by this PR. There's still a TODO left ... (diff) |
Addressing all of volcyys review comments, and adding linting and full test coverage.
Diffstat (limited to 'pydis_site')
-rw-r--r-- | pydis_site/apps/main/migrations/0001_initial.py | 2 | ||||
-rw-r--r-- | pydis_site/apps/main/models/__init__.py | 4 | ||||
-rw-r--r-- | pydis_site/apps/main/models/repo_data.py | 2 | ||||
-rw-r--r-- | pydis_site/apps/main/templatetags/__init__.py | 3 | ||||
-rw-r--r-- | pydis_site/apps/main/tests.py | 16 | ||||
-rw-r--r-- | pydis_site/apps/main/tests/__init__.py | 0 | ||||
-rw-r--r-- | pydis_site/apps/main/tests/test_repodata_helpers.py | 29 | ||||
-rw-r--r-- | pydis_site/apps/main/tests/test_templatetags.py | 8 | ||||
-rw-r--r-- | pydis_site/apps/main/tests/test_views.py | 9 | ||||
-rw-r--r-- | pydis_site/apps/main/urls.py | 6 | ||||
-rw-r--r-- | pydis_site/apps/main/views/__init__.py | 4 | ||||
-rw-r--r-- | pydis_site/apps/main/views/home.py | 35 |
12 files changed, 73 insertions, 45 deletions
diff --git a/pydis_site/apps/main/migrations/0001_initial.py b/pydis_site/apps/main/migrations/0001_initial.py index 20a43d39..a2bf9f3e 100644 --- a/pydis_site/apps/main/migrations/0001_initial.py +++ b/pydis_site/apps/main/migrations/0001_initial.py @@ -13,7 +13,7 @@ class Migration(migrations.Migration): operations = [ migrations.CreateModel( - name='RepoData', + name='RepositoryMetadata', fields=[ ('last_updated', models.DateTimeField(default=django.utils.timezone.now, help_text='The date and time this data was last fetched.')), ('repo_name', models.CharField(help_text='The full name of the repo, e.g. python-discord/site', max_length=40, primary_key=True, serialize=False)), diff --git a/pydis_site/apps/main/models/__init__.py b/pydis_site/apps/main/models/__init__.py index 7a2cbb0b..f327795a 100644 --- a/pydis_site/apps/main/models/__init__.py +++ b/pydis_site/apps/main/models/__init__.py @@ -1,3 +1,3 @@ -from .repo_data import RepoData +from .repo_data import RepositoryMetadata -__all__ = ["RepoData"] +__all__ = ["RepositoryMetadata"] diff --git a/pydis_site/apps/main/models/repo_data.py b/pydis_site/apps/main/models/repo_data.py index 40540410..c975c904 100644 --- a/pydis_site/apps/main/models/repo_data.py +++ b/pydis_site/apps/main/models/repo_data.py @@ -2,7 +2,7 @@ from django.db import models from django.utils import timezone -class RepoData(models.Model): +class RepositoryMetadata(models.Model): """Information about one of our repos fetched from the GitHub API.""" last_updated = models.DateTimeField( diff --git a/pydis_site/apps/main/templatetags/__init__.py b/pydis_site/apps/main/templatetags/__init__.py index e69de29b..70aca169 100644 --- a/pydis_site/apps/main/templatetags/__init__.py +++ b/pydis_site/apps/main/templatetags/__init__.py @@ -0,0 +1,3 @@ +from .extra_filters import starts_with + +__all__ = ["starts_with"] diff --git a/pydis_site/apps/main/tests.py b/pydis_site/apps/main/tests.py deleted file mode 100644 index 733ddaa3..00000000 --- a/pydis_site/apps/main/tests.py +++ /dev/null @@ -1,16 +0,0 @@ -from django.test import TestCase -from django_hosts.resolvers import reverse - -from pydis_site.apps.home.templatetags import extra_filters - - -class TestIndexReturns200(TestCase): - def test_index_returns_200(self): - url = reverse('home.index') - resp = self.client.get(url) - self.assertEqual(resp.status_code, 200) - - -class TestExtraFilterTemplateTags(TestCase): - def test_starts_with(self): - self.assertTrue(extra_filters.starts_with('foo', 'f')) diff --git a/pydis_site/apps/main/tests/__init__.py b/pydis_site/apps/main/tests/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/pydis_site/apps/main/tests/__init__.py diff --git a/pydis_site/apps/main/tests/test_repodata_helpers.py b/pydis_site/apps/main/tests/test_repodata_helpers.py new file mode 100644 index 00000000..66aaa3d6 --- /dev/null +++ b/pydis_site/apps/main/tests/test_repodata_helpers.py @@ -0,0 +1,29 @@ +from django.conf import settings +from django.test import TestCase + +from pydis_site.apps.main.models import RepositoryMetadata +from pydis_site.apps.main.views import HomeView + + +class TestRepositoryMetadataHelpers(TestCase): + + def test_returns_metadata(self): + """Test if the _get_repo_data helper actually returns what it should.""" + + home_view = HomeView() + metadata = home_view._get_repo_data() + + self.assertIsInstance(metadata[0], RepositoryMetadata) + self.assertEquals(len(metadata), len(settings.HOMEPAGE_REPOS)) + + def test_returns_api_data(self): + """Tests if the _get_api_data helper returns what it should.""" + + home_view = HomeView() + api_data = home_view._get_api_data() + repo = settings.HOMEPAGE_REPOS[0] + + self.assertIsInstance(api_data, dict) + self.assertEquals(len(api_data), len(settings.HOMEPAGE_REPOS)) + self.assertIn(repo, api_data.keys()) + self.assertIn("stargazers_count", api_data[repo]) diff --git a/pydis_site/apps/main/tests/test_templatetags.py b/pydis_site/apps/main/tests/test_templatetags.py new file mode 100644 index 00000000..44a5c491 --- /dev/null +++ b/pydis_site/apps/main/tests/test_templatetags.py @@ -0,0 +1,8 @@ +from django.test import TestCase + +from pydis_site.apps.main.templatetags import starts_with + + +class TestTemplateTags(TestCase): + def test_starts_with(self): + self.assertTrue(starts_with('foo', 'f')) diff --git a/pydis_site/apps/main/tests/test_views.py b/pydis_site/apps/main/tests/test_views.py new file mode 100644 index 00000000..73678b0a --- /dev/null +++ b/pydis_site/apps/main/tests/test_views.py @@ -0,0 +1,9 @@ +from django.test import TestCase +from django_hosts.resolvers import reverse + + +class TestIndexReturns200(TestCase): + def test_index_returns_200(self): + url = reverse('home') + resp = self.client.get(url) + self.assertEqual(resp.status_code, 200) diff --git a/pydis_site/apps/main/urls.py b/pydis_site/apps/main/urls.py index 11142040..edef9873 100644 --- a/pydis_site/apps/main/urls.py +++ b/pydis_site/apps/main/urls.py @@ -1,11 +1,11 @@ from django.contrib import admin from django.urls import path -from .views import Home +from .views import HomeView -app_name = 'home' +app_name = 'main' urlpatterns = [ - path('', Home.as_view(), name='home.index'), + path('', HomeView.as_view(), name='home'), path('admin/', admin.site.urls) ] diff --git a/pydis_site/apps/main/views/__init__.py b/pydis_site/apps/main/views/__init__.py index ab52894b..971d73a3 100644 --- a/pydis_site/apps/main/views/__init__.py +++ b/pydis_site/apps/main/views/__init__.py @@ -1,3 +1,3 @@ -from .home import Home +from .home import HomeView -__all__ = ["Home"] +__all__ = ["HomeView"] diff --git a/pydis_site/apps/main/views/home.py b/pydis_site/apps/main/views/home.py index e06b3783..d9255ed2 100644 --- a/pydis_site/apps/main/views/home.py +++ b/pydis_site/apps/main/views/home.py @@ -4,15 +4,17 @@ from django.shortcuts import render from django.utils import timezone from django.views import View -from pydis_site.apps.main.models import RepoData +from pydis_site.apps.main.models import RepositoryMetadata GITHUB_API = "https://api.github.com/users/python-discord/repos" -class Home(View): +class HomeView(View): - def _get_api_data(self): + @staticmethod + def _get_api_data(): """Call the GitHub API and get information about our repos.""" + repo_dict = {repo_name: {} for repo_name in settings.HOMEPAGE_REPOS} # Fetch the data from the GitHub API @@ -20,7 +22,6 @@ class Home(View): api_data = api_data.json() # Process the API data into our dict - print(f"repo_dict = {repo_dict}") for repo in api_data: full_name = repo["full_name"] @@ -32,22 +33,18 @@ class Home(View): "forks_count": repo["forks_count"], "stargazers_count": repo["stargazers_count"], } - print(f"repo_dict after processing = {repo_dict}") return repo_dict def _get_repo_data(self): - """Build a list of RepoData objects that we can use to populate the front page.""" + """Build a list of RepositoryMetadata objects that we can use to populate the front page.""" # Try to get site data from the cache try: - repo_data = RepoData.objects.get(repo_name="python-discord/site") + repo_data = RepositoryMetadata.objects.get(repo_name="python-discord/site") - # If the data is older than 2 minutes, we should refresh it. THIS PROBABLY ALWAYS FAILS? + # If the data is older than 2 minutes, we should refresh it. if (timezone.now() - repo_data.last_updated).seconds > 120: - diff = (timezone.now() - repo_data.last_updated).seconds - print(f"okay baby, it's old! the seconds difference comes to: {diff}") - # Get new data from API api_data_container = self._get_api_data() repo_data_container = [] @@ -55,13 +52,13 @@ class Home(View): # Update or create all RepoData objects in settings.HOMEPAGE_REPOS for repo_name, api_data in api_data_container.items(): try: - repo_data = RepoData.objects.get(repo_name=repo_name) + repo_data = RepositoryMetadata.objects.get(repo_name=repo_name) repo_data.description = api_data["description"] repo_data.language = api_data["language"] repo_data.forks = api_data["forks_count"] repo_data.stargazers = api_data["stargazers_count"] - except RepoData.DoesNotExist: - repo_data = RepoData( + except RepositoryMetadata.DoesNotExist: + repo_data = RepositoryMetadata( repo_name=api_data["full_name"], description=api_data["description"], forks=api_data["forks_count"], @@ -74,10 +71,10 @@ class Home(View): # Otherwise, if the data is fresher than 2 minutes old, we should just return it. else: - return list(RepoData.objects.all()) + return RepositoryMetadata.objects.all() # If this is raised, the database has no repodata at all, we will create them all. - except RepoData.DoesNotExist: + except RepositoryMetadata.DoesNotExist: # Get new data from API api_data_container = self._get_api_data() @@ -85,7 +82,7 @@ class Home(View): # Create all the repodata records in the database. for api_data in api_data_container.values(): - repo_data = RepoData( + repo_data = RepositoryMetadata( repo_name=api_data["full_name"], description=api_data["description"], forks=api_data["forks_count"], @@ -98,9 +95,7 @@ class Home(View): return repo_data_container def get(self, request): + """Collect repo data and render the homepage view""" - # Collect the repo data repo_data = self._get_repo_data() - - # Call the GitHub API and ask it for some data return render(request, "home/index.html", {"repo_data": repo_data}) |