aboutsummaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2018-09-21 23:09:35 +0200
committerGravatar Johannes Christ <[email protected]>2018-09-21 23:09:35 +0200
commitadc8dc83dead1afcc9ff83ae7a0f57747a886a4b (patch)
treeba5335aaed73785d852c6218b942ca7397ffc7ca /api
parentAdd `__repr__` to all models. (diff)
Add `__str__` to all API models.
Diffstat (limited to 'api')
-rw-r--r--api/models.py15
-rw-r--r--api/tests/test_models.py28
2 files changed, 42 insertions, 1 deletions
diff --git a/api/models.py b/api/models.py
index c04984fa..6b681ebc 100644
--- a/api/models.py
+++ b/api/models.py
@@ -41,6 +41,9 @@ class DocumentationLink(ModelReprMixin, models.Model):
help_text="The URL at which the Sphinx inventory is available for this package."
)
+ def __str__(self):
+ return f"{self.package} - {self.base_url}"
+
class OffTopicChannelName(ModelReprMixin, models.Model):
name = models.CharField(
@@ -50,6 +53,9 @@ class OffTopicChannelName(ModelReprMixin, models.Model):
help_text="The actual channel name that will be used on our Discord server."
)
+ def __str__(self):
+ return self.name
+
class SnakeName(ModelReprMixin, models.Model):
"""A snake name used by the bot's snake cog."""
@@ -64,6 +70,9 @@ class SnakeName(ModelReprMixin, models.Model):
help_text="The scientific name for this snake, e.g. 'Python bivittatus'."
)
+ def __str__(self):
+ return f"{self.name} ({self.scientific})"
+
class Role(ModelReprMixin, models.Model):
"""A role on our Discord server."""
@@ -105,6 +114,9 @@ class Role(ModelReprMixin, models.Model):
help_text="The integer value of the permission bitset of this role from Discord."
)
+ def __str__(self):
+ return self.name
+
class Member(ModelReprMixin, models.Model):
"""A member of our Discord server."""
@@ -144,3 +156,6 @@ class Member(ModelReprMixin, models.Model):
Role,
help_text="Any roles this user has on our server."
)
+
+ def __str__(self):
+ return f"{self.name}#{self.discriminator}"
diff --git a/api/tests/test_models.py b/api/tests/test_models.py
index 7ed49a78..ff4bb226 100644
--- a/api/tests/test_models.py
+++ b/api/tests/test_models.py
@@ -1,6 +1,9 @@
from django.test import SimpleTestCase
-from ..models import ModelReprMixin
+from ..models import (
+ DocumentationLink, Member, ModelReprMixin,
+ OffTopicChannelName, Role, SnakeName
+)
class SimpleClass(ModelReprMixin):
@@ -15,3 +18,26 @@ class ReprMixinTests(SimpleTestCase):
def test_shows_attributes(self):
expected = "<SimpleClass(the_cake='is a lie')>"
self.assertEqual(repr(self.klass), expected)
+
+
+class StringDunderMethodTests(SimpleTestCase):
+ def setUp(self):
+ self.objects = (
+ DocumentationLink(
+ 'test', 'http://example.com', 'http://example.com'
+ ),
+ OffTopicChannelName(name='bob-the-builders-playground'),
+ SnakeName(name='python', scientific='3'),
+ Role(
+ id=5, name='test role',
+ colour=0x5, permissions=0
+ ),
+ Member(
+ id=5, name='bob',
+ discriminator=1, avatar_hash=None
+ )
+ )
+
+ def test_returns_string(self):
+ for instance in self.objects:
+ self.assertIsInstance(str(instance), str)