diff options
author | 2020-10-06 20:37:34 +0300 | |
---|---|---|
committer | 2020-10-06 20:37:34 +0300 | |
commit | fc11a23f6561407883aa2f6107f14f9089ac85af (patch) | |
tree | 18cdd02bc1a8cfae0ba785049d5eb85012772bb8 /pydis_site/apps/content | |
parent | Add dateutil dependency because datetime's fromisoformat don't work (diff) |
Implement fetching contributors and last modification date from GitHub
Diffstat (limited to 'pydis_site/apps/content')
-rw-r--r-- | pydis_site/apps/content/utils.py | 38 | ||||
-rw-r--r-- | pydis_site/apps/content/views/article.py | 3 |
2 files changed, 39 insertions, 2 deletions
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), } ) |