File: models.py

package info (click to toggle)
python-django-braces 1.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 484 kB
  • sloc: python: 2,680; makefile: 138; sh: 6
file content (40 lines) | stat: -rw-r--r-- 1,301 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from django.contrib.auth.models import Permission, User
from django.db import models


class Article(models.Model):
    """
    A small but useful model for testing most features
    """
    author = models.ForeignKey(
        "auth.User", null=True, blank=True, on_delete=models.CASCADE
    )
    title = models.CharField(max_length=30)
    body = models.TextField()
    slug = models.SlugField(blank=True)


class CanonicalArticle(models.Model):
    """
    Model specifically for testing the canonical slug mixins
    """
    author = models.ForeignKey(
        "auth.User", null=True, blank=True, on_delete=models.CASCADE
    )
    title = models.CharField(max_length=30)
    body = models.TextField()
    slug = models.SlugField(blank=True)

    def get_canonical_slug(self):
        """Required by mixin to use the model as the source of truth"""
        if self.author:
            return f"{self.author.username}-{self.slug}"
        return f"unauthored-{self.slug}"


class UserObjectPermissions(models.Model):
    """Django model used to test and assign object level permissions"""
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    permission = models.ForeignKey(Permission, on_delete=models.CASCADE)
    article_object = models.ForeignKey(Article, on_delete=models.CASCADE)