diff options
author | 2019-04-17 23:57:14 +0200 | |
---|---|---|
committer | 2019-04-17 23:57:14 +0200 | |
commit | cf1fc2870326c431d0c4dc3fe46f2ae41c2fd5aa (patch) | |
tree | f26b61c866912c7007260f2d946ebbb3e5454585 | |
parent | makemigrations pipeline created, RepoData model created, and a really naive i... (diff) |
The GitHub API stuff is done now, including data caching. Just a couple of minor bugfixes left before this is PR-ready.
-rw-r--r-- | pydis_site/apps/main/models.py | 3 | ||||
-rw-r--r-- | pydis_site/apps/main/views/home.py | 119 | ||||
-rw-r--r-- | pydis_site/settings.py | 10 | ||||
-rw-r--r-- | pydis_site/static/css/home/index.css | 11 | ||||
-rw-r--r-- | pydis_site/templates/base/navbar.html | 24 | ||||
-rw-r--r-- | pydis_site/templates/home/index.html | 105 |
6 files changed, 129 insertions, 143 deletions
diff --git a/pydis_site/apps/main/models.py b/pydis_site/apps/main/models.py deleted file mode 100644 index 0b4331b3..00000000 --- a/pydis_site/apps/main/models.py +++ /dev/null @@ -1,3 +0,0 @@ -# from django.db import models - -# Create your models here. diff --git a/pydis_site/apps/main/views/home.py b/pydis_site/apps/main/views/home.py index 883177bb..f3f9d726 100644 --- a/pydis_site/apps/main/views/home.py +++ b/pydis_site/apps/main/views/home.py @@ -3,67 +3,104 @@ import requests from django.shortcuts import render from django.utils import timezone from django.views import View +from django.conf import settings from pydis_site.apps.main.models import RepoData -GITHUB_API = "https://api.github.com/repos" -REPOS = [ - "python-discord/site", - "python-discord/bot", - "python-discord/snekbox", - "python-discord/seasonalbot", - "python-discord/django-simple-bulma", - "python-discord/django-crispy-bulma", -] - -# https://api.github.com/users/python-discord/repos gets all the data in one query. +GITHUB_API = "https://api.github.com/users/python-discord/repos" class Home(View): - def _get_repo_data(self, repo_name): - """This will get language, stars and forks for the requested GitHub repo.""" - # Try to get the data from the cache + def _get_api_data(self): + """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 + api_data = requests.get(GITHUB_API) + 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"] + + if full_name in settings.HOMEPAGE_REPOS: + repo_dict[full_name] = { + "full_name": repo["full_name"], + "description": repo["description"], + "language": repo["language"], + "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.""" + + # Try to get site data from the cache try: - repo_data = RepoData.objects.get(repo_name=repo_name) + repo_data = RepoData.objects.get(repo_name="python-discord/site") - # If the data is older than 2 minutes, we should refresh it + # If the data is older than 2 minutes, we should refresh it. THIS PROBABLY ALWAYS FAILS? if (timezone.now() - repo_data.last_updated).seconds > 120: - # Fetch the data from the GitHub API - api_data = requests.get(f"{GITHUB_API}/{repo_name}") - api_data = api_data.json() + diff = (timezone.now() - repo_data.last_updated).seconds + print(f"okay baby, it's old! the seconds difference comes to: {diff}") - # Update the current object, and save it. - 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"] - repo_data.save() - return repo_data + # Get new data from API + api_data_container = self._get_api_data() + repo_data_container = [] + + # 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.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( + repo_name=api_data["full_name"], + description=api_data["description"], + forks=api_data["forks_count"], + stargazers=api_data["stargazers_count"], + language=api_data["language"], + ) + repo_data.save() + repo_data_container.append(repo_data) + return repo_data_container # Otherwise, if the data is fresher than 2 minutes old, we should just return it. else: - return repo_data + return list(RepoData.objects.all()) - # If this is raised, the data isn't there at all, so we'll need to create it. + # If this is raised, the database has no repodata at all, we will create them all. except RepoData.DoesNotExist: - api_data = requests.get(f"{GITHUB_API}/{repo_name}") - api_data = api_data.json() - repo_data = RepoData( - description=api_data["description"], - forks=api_data["forks_count"], - stargazers=api_data["stargazers_count"], - language=api_data["language"], - ) - repo_data.save() - return repo_data + + # Get new data from API + api_data_container = self._get_api_data() + repo_data_container = [] + + # Create all the repodata records in the database. + for repo_name, api_data in api_data_container.items(): + repo_data = RepoData( + repo_name=api_data["full_name"], + description=api_data["description"], + forks=api_data["forks_count"], + stargazers=api_data["stargazers_count"], + language=api_data["language"], + ) + repo_data.save() + repo_data_container.append(repo_data) + + return repo_data_container def get(self, request): # Collect the repo data - repo_data = [] - for repo in REPOS: - repo_data.append(self._get_repo_data(repo)) + 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}) diff --git a/pydis_site/settings.py b/pydis_site/settings.py index 4a5b0523..eb21de10 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -258,3 +258,13 @@ BULMA_SETTINGS = { "footer-padding": "1rem 1.5rem 1rem", } } + +# Which of our GitHub repos should be displayed on the front page, and in which order? +HOMEPAGE_REPOS = [ + "python-discord/site", + "python-discord/bot", + "python-discord/snekbox", + "python-discord/seasonalbot", + "python-discord/django-simple-bulma", + "python-discord/django-crispy-bulma", +] diff --git a/pydis_site/static/css/home/index.css b/pydis_site/static/css/home/index.css index 2e201441..d1d1c01e 100644 --- a/pydis_site/static/css/home/index.css +++ b/pydis_site/static/css/home/index.css @@ -53,6 +53,17 @@ margin: 8px auto auto auto; } +div.card.github-card { + box-shadow: none; + border: #d1d5da 1px solid; + border-radius: 3px; +} + +div.repo-headline { + font-size: 1.25rem; + margin-bottom: 8px; +} + span.repo-language-dot { border-radius: 50%; height: 12px; diff --git a/pydis_site/templates/base/navbar.html b/pydis_site/templates/base/navbar.html index f9f576df..842d29d5 100644 --- a/pydis_site/templates/base/navbar.html +++ b/pydis_site/templates/base/navbar.html @@ -19,10 +19,10 @@ {# Content on the right side of the navbar #} <div class="navbar-menu is-paddingless" id="navbar_menu"> <div class="navbar-end"> - {# Wiki #} + {# Rules #} <a class="navbar-item" href="#"> <span class="icon is-size-4 is-medium"><i class="fas fa-book"></i></span> - <span> Wiki</span> + <span> Rules</span> </a> {# Reddit #} @@ -31,22 +31,22 @@ <span> Reddit</span> </a> - {# Patreon #} - <a class="navbar-item" href="#"> - <span class="icon is-size-4 is-medium"><i class="fab fa-patreon"></i></span> - <span> Patreon</span> - </a> - {# YouTube #} <a class="navbar-item" href="#"> <span class="icon is-size-4 is-medium"><i class="fab fa-youtube"></i></span> <span> YouTube</span> </a> + {# Patreon #} + <a class="navbar-item" href="#"> + <span class="icon is-size-4 is-medium"><i class="fab fa-patreon"></i></span> + <span> Patreon</span> + </a> + {# RedBubble #} <a class="navbar-item" href="#"> <span class="icon is-size-4 is-medium"><i class="fas fa-tshirt"></i></span> - <span> RedBubble</span> + <span> Merch</span> </a> {# More #} @@ -56,13 +56,13 @@ </a> <div class="navbar-dropdown"> <a class="navbar-item"> - About + Code Jams </a> <a class="navbar-item"> - Jobs + FAQ </a> <a class="navbar-item"> - Contact + Privacy </a> <hr class="navbar-divider"> <a class="navbar-item"> diff --git a/pydis_site/templates/home/index.html b/pydis_site/templates/home/index.html index 93361580..6bbf5444 100644 --- a/pydis_site/templates/home/index.html +++ b/pydis_site/templates/home/index.html @@ -52,95 +52,26 @@ <br> <div class="columns is-multiline"> - <!-- Site --> - <div class="column is-one-third"> - <div class="card has-equal-height"> - <div class="card-content"> - <i class="fab fa-github"></i> <a href="https://github.com/python-discord/site"> python-discord/site</a> - <br><br> - <em>Source code for our website</em> - <br><br> - </span><span class="repo-language-dot python"></span> Python - <span id="repo-footer-item"><i class="fas fa-star"></i> 566</span> - <span id="repo-footer-item"><i class="fas fa-code-branch"></i> 27</span> + <!-- Display projects from settings.HOMEPAGE_REPOS --> + {% for repo in repo_data %} + <div class="column is-one-third"> + <div class="card has-equal-height github-card"> + <div class="card-content"> + <div class="repo-headline"> + <i class="fab fa-github"></i> + <a href="https://github.com/{{ repo.repo_name }}"> <strong>{{ repo.repo_name }}</strong></a> + </div> + <div> + {{ repo.description }} + <br><br> + </span><span class="repo-language-dot {{ repo.language | lower }}"></span> {{ repo.language }} + <span id="repo-footer-item"><i class="fas fa-star"></i> {{ repo.stargazers }}</span> + <span id="repo-footer-item"><i class="fas fa-code-branch"></i> {{ repo.forks }}</span> + </div> + </div> </div> </div> - </div> - - <!-- Bot --> - <div class="column is-one-third"> - <div class="card has-equal-height"> - <div class="card-content"> - <i class="fab fa-github"></i> <a href="https://github.com/python-discord/bot"> python-discord/bot</a> - <br><br> - <em>Source code for our Discord bot</em> - <br><br> - </span><span class="repo-language-dot python"></span> Python - <span id="repo-footer-item"><i class="fas fa-star"></i> 566</span> - <span id="repo-footer-item"><i class="fas fa-code-branch"></i> 27</span> - </div> - </div> - </div> - - <!-- Snekbox --> - <div class="column is-one-third"> - <div class="card has-equal-height"> - <div class="card-content"> - <i class="fab fa-github"></i> <a href="https://github.com/python-discord/snekbox"> python-discord/snekbox</a> - <br><br> - <em>Easy, safe evaluation of arbitrary Python code</em> - <br><br> - </span><span class="repo-language-dot python"></span> Python - <span id="repo-footer-item"><i class="fas fa-star"></i> 566</span> - <span id="repo-footer-item"><i class="fas fa-code-branch"></i> 27</span> - </div> - </div> - </div> - - <!-- SeasonalBot --> - <div class="column is-one-third"> - <div class="card has-equal-height"> - <div class="card-content"> - <i class="fab fa-github"></i> <a href="https://github.com/python-discord/seasonalbot"> python-discord/seasonalbot</a> - <br><br> - <em>A Discord bot that changes with the seasons. Meant as a beginner-friendly learning project.</em> - <br><br> - </span><span class="repo-language-dot python"></span> Python - <span id="repo-footer-item"><i class="fas fa-star"></i> 566</span> - <span id="repo-footer-item"><i class="fas fa-code-branch"></i> 27</span> - </div> - </div> - </div> - - <!-- django-simple-bulma --> - <div class="column is-one-third"> - <div class="card has-equal-height"> - <div class="card-content"> - <i class="fab fa-github"></i> <a href="https://github.com/python-discord/django-simple-bulma"> python-discord/django-simple-bulma</a> - <br><br> - <em>Django application to add the Bulma CSS framework and its extensions</em> - <br><br> - </span><span class="repo-language-dot css"></span> CSS - <span id="repo-footer-item"><i class="fas fa-star"></i> 566</span> - <span id="repo-footer-item"><i class="fas fa-code-branch"></i> 27</span> - </div> - </div> - </div> - - <!-- django-crispy-bulma --> - <div class="column is-one-third"> - <div class="card has-equal-height"> - <div class="card-content"> - <i class="fab fa-github"></i> <a href="https://github.com/python-discord/django-crispy-bulma"> python-discord/django-crispy-bulma</a> - <br><br> - <em>Django application to add 'django-crispy-forms' layout objects for Bulma.io</em> - <br><br> - </span><span class="repo-language-dot html"></span> HTML - <span id="repo-footer-item"><i class="fas fa-star"></i> 566</span> - <span id="repo-footer-item"><i class="fas fa-code-branch"></i> 27</span> - </div> - </div> - </div> + {% endfor %} </div> </div> |