diff options
| author | 2022-08-14 04:44:57 +0200 | |
|---|---|---|
| committer | 2022-08-14 05:36:12 +0200 | |
| commit | b4911d03faf8eecf5c4cced6f8036b0b2ef01d58 (patch) | |
| tree | cc32c183b6894829dea32506eaf77e25ffa7e73f | |
| parent | Improve Tag Cropping (diff) | |
Move Tag URL To Property And Add Group
The URLs can be simply constructed using the other tag properties, so
they were removed from the database in favor of a property.
A group field was also added to support tags within groups.
Signed-off-by: Hassan Abouelela <[email protected]>
Diffstat (limited to '')
| -rw-r--r-- | pydis_site/apps/content/migrations/0002_remove_tag_url_tag_group.py | 22 | ||||
| -rw-r--r-- | pydis_site/apps/content/models/tag.py | 17 | ||||
| -rw-r--r-- | pydis_site/apps/content/tests/test_utils.py | 6 | ||||
| -rw-r--r-- | pydis_site/apps/content/tests/test_views.py | 12 | 
4 files changed, 47 insertions, 10 deletions
| diff --git a/pydis_site/apps/content/migrations/0002_remove_tag_url_tag_group.py b/pydis_site/apps/content/migrations/0002_remove_tag_url_tag_group.py new file mode 100644 index 00000000..e59077f0 --- /dev/null +++ b/pydis_site/apps/content/migrations/0002_remove_tag_url_tag_group.py @@ -0,0 +1,22 @@ +# Generated by Django 4.0.6 on 2022-08-13 23:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + +    dependencies = [ +        ('content', '0001_initial'), +    ] + +    operations = [ +        migrations.RemoveField( +            model_name='tag', +            name='url', +        ), +        migrations.AddField( +            model_name='tag', +            name='group', +            field=models.CharField(help_text='The group the tag belongs to.', max_length=50, null=True), +        ), +    ] diff --git a/pydis_site/apps/content/models/tag.py b/pydis_site/apps/content/models/tag.py index 1437b96a..01264ff1 100644 --- a/pydis_site/apps/content/models/tag.py +++ b/pydis_site/apps/content/models/tag.py @@ -4,6 +4,8 @@ from django.db import models  class Tag(models.Model):      """A tag from the python-discord server.""" +    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, @@ -13,5 +15,18 @@ class Tag(models.Model):          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.") -    url = models.URLField(help_text="The URL to this tag on GitHub.") + +    @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 diff --git a/pydis_site/apps/content/tests/test_utils.py b/pydis_site/apps/content/tests/test_utils.py index 89ef81c4..a5d5dcb4 100644 --- a/pydis_site/apps/content/tests/test_utils.py +++ b/pydis_site/apps/content/tests/test_utils.py @@ -112,7 +112,7 @@ class TagUtilsTests(TestCase):      @mock.patch.object(utils, "fetch_tags")      def test_static_fetch(self, fetch_mock: mock.Mock):          """Test that the static fetch function is only called at most once during static builds.""" -        tags = [models.Tag(name="Name", body="body", url="url")] +        tags = [models.Tag(name="Name", body="body")]          fetch_mock.return_value = tags          result = utils.get_tags_static()          second_result = utils.get_tags_static() @@ -159,8 +159,8 @@ class TagUtilsTests(TestCase):          result = utils.fetch_tags()          self.assertEqual([ -            models.Tag(name="first_tag", body=bodies[0], url=f"{utils.TAG_URL_BASE}/first_tag.md"), -            models.Tag(name="second_tag", body=bodies[1], url=f"{utils.TAG_URL_BASE}/first_tag.md"), +            models.Tag(name="first_tag", body=bodies[0]), +            models.Tag(name="second_tag", body=bodies[1]),          ], sorted(result, key=lambda tag: tag.name))      def test_get_real_tag(self): diff --git a/pydis_site/apps/content/tests/test_views.py b/pydis_site/apps/content/tests/test_views.py index a5867260..c4d3474e 100644 --- a/pydis_site/apps/content/tests/test_views.py +++ b/pydis_site/apps/content/tests/test_views.py @@ -196,7 +196,7 @@ class TagViewTests(django.test.TestCase):      def test_valid_tag_returns_200(self):          """Test that a page is returned for a valid tag.""" -        Tag.objects.create(name="example", body="This is the tag body.", url="URL") +        Tag.objects.create(name="example", body="This is the tag body.")          response = self.client.get("/pages/tags/example/")          self.assertEqual(200, response.status_code)          self.assertIn("This is the tag body", response.content.decode("utf-8")) @@ -216,7 +216,7 @@ class TagViewTests(django.test.TestCase):          Tag content here.          """) -        tag = Tag.objects.create(name="example", body=body, url="URL") +        tag = Tag.objects.create(name="example", body=body)          response = self.client.get("/pages/tags/example/")          expected = {              "page_title": "example", @@ -238,7 +238,7 @@ class TagViewTests(django.test.TestCase):          **This text is in bold**          """) -        Tag.objects.create(name="example", body=body, url="URL") +        Tag.objects.create(name="example", body=body)          response = self.client.get("/pages/tags/example/")          content = response.content.decode("utf-8") @@ -257,7 +257,7 @@ class TagViewTests(django.test.TestCase):          Tag body.          """) -        Tag.objects.create(name="example", body=body, url="URL") +        Tag.objects.create(name="example", body=body)          response = self.client.get("/pages/tags/example/")          content = response.content.decode("utf-8") @@ -273,7 +273,7 @@ class TagViewTests(django.test.TestCase):          ---          """) -        Tag.objects.create(name="example", body=body, url="URL") +        Tag.objects.create(name="example", body=body)          response = self.client.get("/pages/tags/example/")          self.assertEqual(              "Embed title", @@ -285,7 +285,7 @@ class TagViewTests(django.test.TestCase):          """Test hyperlinking of tags works as intended."""          filler_before, filler_after = "empty filler text\n\n", "more\nfiller"          body = filler_before + "`!tags return`" + filler_after -        Tag.objects.create(name="example", body=body, url="URL") +        Tag.objects.create(name="example", body=body)          other_url = reverse("content:tag", kwargs={"name": "return"})          response = self.client.get("/pages/tags/example/") | 
