diff options
| author | 2019-01-01 21:15:29 +0100 | |
|---|---|---|
| committer | 2019-01-01 21:15:29 +0100 | |
| commit | a6e81b6757b43c68ee749006086035c3e8033f15 (patch) | |
| tree | b9fbfe6a0daac1884c83ad60476cc34a7b5d7971 /api/models.py | |
| parent | apply stash (diff) | |
| parent | Merge pull request #156 from python-discord/django-beautify (diff) | |
Merge branch 'django' into django+add-role-viewset
Diffstat (limited to '')
| -rw-r--r-- | api/models.py | 171 | 
1 files changed, 119 insertions, 52 deletions
diff --git a/api/models.py b/api/models.py index 9990e266..21b5975a 100644 --- a/api/models.py +++ b/api/models.py @@ -60,6 +60,50 @@ class OffTopicChannelName(ModelReprMixin, models.Model):          return self.name +class Role(ModelReprMixin, models.Model): +    """A role on our Discord server.""" + +    id = models.BigIntegerField(  # noqa +        primary_key=True, +        validators=( +            MinValueValidator( +                limit_value=0, +                message="Role IDs cannot be negative." +            ), +        ), +        help_text="The role ID, taken from Discord." +    ) +    name = models.CharField( +        max_length=100, +        help_text="The role name, taken from Discord." +    ) +    colour = models.IntegerField( +        validators=( +            MinValueValidator( +                limit_value=0, +                message="Colour hex cannot be negative." +            ), +        ), +        help_text="The integer value of the colour of this role from Discord." +    ) +    permissions = models.IntegerField( +        validators=( +            MinValueValidator( +                limit_value=0, +                message="Role permissions cannot be negative." +            ), +            MaxValueValidator( +                limit_value=2 << 32, +                message="Role permission bitset exceeds value of having all permissions" +            ) +        ), +        help_text="The integer value of the permission bitset of this role from Discord." +    ) + +    def __str__(self): +        return self.name + +  class SnakeFact(ModelReprMixin, models.Model):      """A snake fact used by the bot's snake cog.""" @@ -92,11 +136,13 @@ class SnakeName(ModelReprMixin, models.Model):      name = models.CharField(          primary_key=True,          max_length=100, -        help_text="The regular name for this snake, e.g. 'Python'." +        help_text="The regular name for this snake, e.g. 'Python'.", +        validators=[RegexValidator(regex=r'^([^0-9])+$')]      )      scientific = models.CharField(          max_length=150, -        help_text="The scientific name for this snake, e.g. 'Python bivittatus'." +        help_text="The scientific name for this snake, e.g. 'Python bivittatus'.", +        validators=[RegexValidator(regex=r'^([^0-9])+$')]      )      def __str__(self): @@ -109,7 +155,8 @@ class SpecialSnake(ModelReprMixin, models.Model):      name = models.CharField(          max_length=140,          primary_key=True, -        help_text='A special snake name.' +        help_text='A special snake name.', +        validators=[RegexValidator(regex=r'^([^0-9])+$')]      )      info = models.TextField(          help_text='Info about a special snake.' @@ -123,52 +170,28 @@ class SpecialSnake(ModelReprMixin, models.Model):          return self.name -class Role(ModelReprMixin, models.Model): -    """A role on our Discord server.""" +class Tag(ModelReprMixin, models.Model): +    """A tag providing (hopefully) useful information.""" -    id = models.BigIntegerField(  # noqa -        primary_key=True, -        validators=( -            MinValueValidator( -                limit_value=0, -                message="Role IDs cannot be negative." -            ), -        ), -        help_text="The role ID, taken from Discord." -    ) -    name = models.CharField( +    title = models.CharField(          max_length=100, -        help_text="The role name, taken from Discord." -    ) -    colour = models.IntegerField( -        validators=( -            MinValueValidator( -                limit_value=0, -                message="Colour hex cannot be negative." -            ), +        help_text=( +            "The title of this tag, shown in searches and providing " +            "a quick overview over what this embed contains."          ), -        help_text="The integer value of the colour of this role from Discord." +        primary_key=True      ) -    permissions = models.IntegerField( -        validators=( -            MinValueValidator( -                limit_value=0, -                message="Role permissions cannot be negative." -            ), -            MaxValueValidator( -                limit_value=2 << 32, -                message="Role permission bitset exceeds value of having all permissions" -            ) -        ), -        help_text="The integer value of the permission bitset of this role from Discord." +    embed = pgfields.JSONField( +        help_text="The actual embed shown by this tag.", +        validators=(validate_tag_embed,)      )      def __str__(self): -        return self.name +        return self.title -class Member(ModelReprMixin, models.Model): -    """A member of our Discord server.""" +class User(ModelReprMixin, models.Model): +    """A Discord user."""      id = models.BigIntegerField(  # noqa          primary_key=True, @@ -205,26 +228,70 @@ class Member(ModelReprMixin, models.Model):          Role,          help_text="Any roles this user has on our server."      ) +    in_guild = models.BooleanField( +        default=True, +        help_text="Whether this user is in our server." +    )      def __str__(self):          return f"{self.name}#{self.discriminator}" -class Tag(ModelReprMixin, models.Model): -    """A tag providing (hopefully) useful information.""" +class Infraction(ModelReprMixin, models.Model): +    """An infraction for a Discord user.""" -    title = models.CharField( -        max_length=100, +    TYPE_CHOICES = ( +        ("warning", "Warning"), +        ("mute", "Mute"), +        ("ban", "Ban"), +        ("kick", "Kick"), +        ("superstar", "Superstar") +    ) +    inserted_at = models.DateTimeField( +        auto_now_add=True, +        help_text="The date and time of the creation of this infraction." +    ) +    expires_at = models.DateTimeField( +        null=True,          help_text=( -            "The title of this tag, shown in searches and providing " -            "a quick overview over what this embed contains." -        ), -        primary_key=True +            "The date and time of the expiration of this infraction. " +            "Null if the infraction is permanent or it can't expire." +        )      ) -    embed = pgfields.JSONField( -        help_text="The actual embed shown by this tag.", -        validators=(validate_tag_embed,) +    active = models.BooleanField( +        default=True, +        help_text="Whether the infraction is still active." +    ) +    user = models.ForeignKey( +        User, +        on_delete=models.CASCADE, +        related_name='infractions_received', +        help_text="The user to which the infraction was applied." +    ) +    actor = models.ForeignKey( +        User, +        on_delete=models.CASCADE, +        related_name='infractions_given', +        help_text="The user which applied the infraction." +    ) +    type = models.CharField( +        max_length=9, +        choices=TYPE_CHOICES, +        help_text="The type of the infraction." +    ) +    reason = models.TextField( +        null=True, +        help_text="The reason for the infraction." +    ) +    hidden = models.BooleanField( +        default=False, +        help_text="Whether the infraction is a shadow infraction."      )      def __str__(self): -        return self.title +        s = f"#{self.id}: {self.type} on {self.user_id}" +        if self.expires_at: +            s += f" until {self.expires_at}" +        if self.hidden: +            s += " (hidden)" +        return s  |