From fc11a23f6561407883aa2f6107f14f9089ac85af Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 6 Oct 2020 20:37:34 +0300 Subject: Implement fetching contributors and last modification date from GitHub --- pydis_site/apps/content/utils.py | 38 ++++++++++++++++++++++++++++++- pydis_site/apps/content/views/article.py | 3 ++- pydis_site/templates/content/article.html | 18 +++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) (limited to 'pydis_site') diff --git a/pydis_site/apps/content/utils.py b/pydis_site/apps/content/utils.py index e164c39f..0c2a027d 100644 --- a/pydis_site/apps/content/utils.py +++ b/pydis_site/apps/content/utils.py @@ -1,12 +1,17 @@ import os from pathlib import Path -from typing import Dict, Optional, Union +from typing import Dict, List, Optional, Union +import requests import yaml +from dateutil import parser from django.conf import settings from django.http import Http404 from markdown2 import markdown +COMMITS_URL = "https://api.github.com/repos/{owner}/{name}/commits?path={path}&sha={branch}" +BASE_ARTICLES_LOCATION = "pydis_site/apps/content/resources/content/" + def _get_base_path() -> Path: """Have extra function for base path getting for testability.""" @@ -79,3 +84,34 @@ def get_article(article: str, category: Optional[str]) -> Dict[str, Union[str, D ) return {"article": str(html), "metadata": html.metadata} + + +def get_github_information( + article: str, + category: Optional[str] = None +) -> Dict[str, Union[List[str], str]]: + """Get article last modified date and contributors from GitHub.""" + result = requests.get( + COMMITS_URL.format( + owner=settings.SITE_REPOSITORY_OWNER, + name=settings.SITE_REPOSITORY_NAME, + branch=settings.SITE_REPOSITORY_BRANCH, + path=f"{BASE_ARTICLES_LOCATION}{f'{category}/' if category else ''}{article}.md" + ) + ) + + if result.status_code == 200: + data = result.json() + return { + "last_modified": parser.isoparse( + data[0]["commit"]["committer"]["date"] + ).strftime("%dth %B %Y"), + "contributors": { + c["commit"]["committer"]["name"]: c["committer"]["html_url"] for c in data + } + } + else: + return { + "last_modified": "N/A", + "contributors": [] + } diff --git a/pydis_site/apps/content/views/article.py b/pydis_site/apps/content/views/article.py index 02e8103f..42b92cee 100644 --- a/pydis_site/apps/content/views/article.py +++ b/pydis_site/apps/content/views/article.py @@ -5,7 +5,7 @@ from django.http import HttpResponse from django.shortcuts import render from django.views import View -from pydis_site.apps.content.utils import get_article, get_category +from pydis_site.apps.content.utils import get_article, get_category, get_github_information class ArticleView(View): @@ -38,5 +38,6 @@ class ArticleView(View): article_result["metadata"].get("relevant_link_values", "").split(",") ) if link != "" and value != "" }, + "github_data": get_github_information(article, category), } ) diff --git a/pydis_site/templates/content/article.html b/pydis_site/templates/content/article.html index 5e090050..151d6cf5 100644 --- a/pydis_site/templates/content/article.html +++ b/pydis_site/templates/content/article.html @@ -33,11 +33,29 @@

{{ article.metadata.title }}

+

Last modified: {{ github_data.last_modified }}

{{ article.article|safe }}
+
+
+

Contributors

+
+
+ {% if github_data.contributors|length %} +
+ {% for user, profile_url in github_data.contributors.items %} + {{ user }} + {% endfor %} +
+ {% else %} +

N/A

+ {% endif %} +
+
+ {% if relevant_links|length > 0 %}
-- cgit v1.2.3