diff options
| author | 2019-08-23 15:00:15 +0200 | |
|---|---|---|
| committer | 2019-08-23 15:00:15 +0200 | |
| commit | f6c90633bc415aa1a1e625fb078212e9e015ba37 (patch) | |
| tree | b211be1ec8e4b7ef38b824973716241e5bc6316b /pydis_site/apps/api/models/bot | |
| parent | Properly handle `actor` via `PrimaryKeyRelatedField`. (diff) | |
| parent | Making the comparison operators for Role act like those for d.py Role objects (diff) | |
Merge pull request #236 from python-discord/django-roles-api-add-position
Add position field to Role model
Diffstat (limited to 'pydis_site/apps/api/models/bot')
| -rw-r--r-- | pydis_site/apps/api/models/bot/role.py | 22 | ||||
| -rw-r--r-- | pydis_site/apps/api/models/bot/user.py | 12 |
2 files changed, 32 insertions, 2 deletions
diff --git a/pydis_site/apps/api/models/bot/role.py b/pydis_site/apps/api/models/bot/role.py index 34e74009..58bbf8b4 100644 --- a/pydis_site/apps/api/models/bot/role.py +++ b/pydis_site/apps/api/models/bot/role.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models @@ -5,7 +7,12 @@ from pydis_site.apps.api.models.utils import ModelReprMixin class Role(ModelReprMixin, models.Model): - """A role on our Discord server.""" + """ + A role on our Discord server. + + The comparison operators <, <=, >, >=, ==, != act the same as they do with Role-objects of the + discord.py library, see https://discordpy.readthedocs.io/en/latest/api.html#discord.Role + """ id = models.BigIntegerField( primary_key=True, @@ -43,7 +50,18 @@ class Role(ModelReprMixin, models.Model): ), help_text="The integer value of the permission bitset of this role from Discord." ) + position = models.IntegerField( + help_text="The position of the role in the role hierarchy of the Discord Guild." + ) - def __str__(self): + def __str__(self) -> str: """Returns the name of the current role, for display purposes.""" return self.name + + def __lt__(self, other: Role) -> bool: + """Compares the roles based on their position in the role hierarchy of the guild.""" + return self.position < other.position + + def __le__(self, other: Role) -> bool: + """Compares the roles based on their position in the role hierarchy of the guild.""" + return self.position <= other.position diff --git a/pydis_site/apps/api/models/bot/user.py b/pydis_site/apps/api/models/bot/user.py index d4deb630..00c24d3d 100644 --- a/pydis_site/apps/api/models/bot/user.py +++ b/pydis_site/apps/api/models/bot/user.py @@ -51,3 +51,15 @@ class User(ModelReprMixin, models.Model): def __str__(self): """Returns the name and discriminator for the current user, for display purposes.""" return f"{self.name}#{self.discriminator}" + + @property + def top_role(self) -> Role: + """ + Attribute that returns the user's top role. + + This will fall back to the Developers role if the user does not have any roles. + """ + roles = self.roles.all() + if not roles: + return Role.objects.get(name="Developers") + return max(self.roles.all()) |