2024-03-11 22:42:58 +00:00

99 lines
2.6 KiB
Python

# -*- encoding: utf-8 -*-
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from .managers import DiscordUserOAuth2Manager
class DiscordUser(models.Model):
"""
Represents a User authenticated with Discord OAuth2.
"""
# Discord Attributes
id = models.PositiveBigIntegerField(
primary_key=True,
help_text=_("the user's id")
)
username = models.CharField(
_("username"),
max_length=32,
unique=True,
help_text=_("the user's username, not unique across the platform")
)
global_name = models.CharField(
_("nickname"),
max_length=32,
help_text=_("the user's display name, if it is set. For bots, this is the application name")
)
avatar = models.CharField(
_("avatar"),
max_length=64,
help_text=_("the user's avatar hash")
)
public_flags = models.IntegerField(
_("public flags"),
help_text=_("the public flags on a user's account")
)
flags = models.IntegerField(
_("flags"),
help_text=_("the flags on a user's account")
)
locale = models.CharField(
_("locale"),
max_length=16,
help_text=_("the user's chosen language option")
)
mfa_enabled = models.BooleanField(
_("mfa enabled"),
help_text=_("whether the user has two factor enabled on their account")
)
last_login = models.DateTimeField(
_("last login"),
default=timezone.now,
help_text=_("datetime of the previous login")
)
# Access Token
access_token = models.CharField(
_("access token"),
max_length=100,
help_text=_("token for the application to make api calls on behalf of the user.")
)
# Custom Attributes
is_active = models.BooleanField(
_("active status"),
default=True,
help_text=_('Use as a "soft delete" rather than deleting the user.')
)
is_staff = models.BooleanField(
_("staff status"),
default=False,
help_text=_("Designates whether the user can log into this admin site.")
)
is_superuser = models.BooleanField(
_("superuser status"),
default=False,
help_text=_("Designates whether the user has unrestricted site control.")
)
# Object Manager
objects = DiscordUserOAuth2Manager()
def is_authenticated(self, request):
return True
def __str__(self):
return self.global_name or self.username
@property
def avatar_url(self):
return f"https://cdn.discordapp.com/avatars/{self.id}/{self.avatar}.webp?size=128"