diff options
| author | 2022-11-20 07:12:25 -0800 | |
|---|---|---|
| committer | 2022-11-20 07:12:25 -0800 | |
| commit | 6d8cafc45438f63918ccd300686e368354e1e4f0 (patch) | |
| tree | 39fd46a19381b0f358867c952e7a4b8ab43ab990 /pydis_site/apps/content/models | |
| parent | Rename file for consistency (diff) | |
| parent | Merge pull request #789 from python-discord/messages-in-past-n-days-endpoint (diff) | |
Merge branch 'main' into main
Diffstat (limited to 'pydis_site/apps/content/models')
| -rw-r--r-- | pydis_site/apps/content/models/__init__.py | 3 | ||||
| -rw-r--r-- | pydis_site/apps/content/models/tag.py | 80 | 
2 files changed, 83 insertions, 0 deletions
diff --git a/pydis_site/apps/content/models/__init__.py b/pydis_site/apps/content/models/__init__.py new file mode 100644 index 00000000..60007e27 --- /dev/null +++ b/pydis_site/apps/content/models/__init__.py @@ -0,0 +1,3 @@ +from .tag import Commit, Tag + +__all__ = ["Commit", "Tag"] diff --git a/pydis_site/apps/content/models/tag.py b/pydis_site/apps/content/models/tag.py new file mode 100644 index 00000000..1a20d775 --- /dev/null +++ b/pydis_site/apps/content/models/tag.py @@ -0,0 +1,80 @@ +import collections.abc +import json + +from django.db import models + + +class Commit(models.Model): +    """A git commit from the Python Discord Bot project.""" + +    URL_BASE = "https://github.com/python-discord/bot/commit/" + +    sha = models.CharField( +        help_text="The SHA hash of this commit.", +        primary_key=True, +        max_length=40, +    ) +    message = models.TextField(help_text="The commit message.") +    date = models.DateTimeField(help_text="The date and time the commit was created.") +    authors = models.TextField(help_text=( +        "The person(s) who created the commit. This is a serialized JSON object. " +        "Refer to the GitHub documentation on the commit endpoint " +        "(schema/commit.author & schema/commit.committer) for more info. " +        "https://docs.github.com/en/rest/commits/commits#get-a-commit" +    )) + +    @property +    def url(self) -> str: +        """The URL to the commit on GitHub.""" +        return self.URL_BASE + self.sha + +    def lines(self) -> collections.abc.Iterable[str]: +        """Return each line in the commit message.""" +        for line in self.message.split("\n"): +            yield line + +    def format_authors(self) -> collections.abc.Iterable[str]: +        """Return a nice representation of the author(s)' name and email.""" +        for author in json.loads(self.authors): +            yield f"{author['name']} <{author['email']}>" + + +class Tag(models.Model): +    """A tag from the python-discord bot repository.""" + +    URL_BASE = "https://github.com/python-discord/bot/tree/main/bot/resources/tags" + +    last_updated = models.DateTimeField( +        help_text="The date and time this data was last fetched.", +        auto_now=True, +    ) +    sha = models.CharField( +        help_text="The tag's hash, as calculated by GitHub.", +        max_length=40, +    ) +    last_commit = models.ForeignKey( +        Commit, +        help_text="The commit this file was last touched in.", +        null=True, +        on_delete=models.CASCADE, +    ) +    name = models.CharField( +        help_text="The tag's name.", +        primary_key=True, +        max_length=50, +    ) +    group = models.CharField( +        help_text="The group the tag belongs to.", +        null=True, +        max_length=50, +    ) +    body = models.TextField(help_text="The content of the tag.") + +    @property +    def url(self) -> str: +        """Get the URL of the tag on GitHub.""" +        url = Tag.URL_BASE +        if self.group: +            url += f"/{self.group}" +        url += f"/{self.name}.md" +        return url  |