models.py 1.45 KB
Newer Older
luxcem's avatar
luxcem committed
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from imagekit.models import ImageSpecField
from pilkit.processors import ResizeToFill

from django.db import models
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext as _


class Profile(models.Model):
    """
    Extends `django.contrib.auth.models.User` with additional fields
    """

    #: OneToOneField to ``django.contrib.auth.models.User``
    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        related_name="profile"
    )

    #: User description
    description = models.CharField(max_length=200, null=True, blank=True)

    #: User avatar
    picture = models.ImageField(
        upload_to="uploads/users/%Y/%m/%d",
        null=True,
        blank=True
    )

    #: 50x50 thumbnail of user picture
    picture_t = ImageSpecField(
        source="picture",
        processors=[ResizeToFill(50, 50)],
        format="jpeg",
    )

    class Meta:
        verbose_name = _("User")
        app_label = "userprofile"

    def __str__(self):
        """Returns user full name"""
        return self.username

    def delete(self, *args, **kwargs):
        """Deletes ``django.contrib.auth.models.User`` and
        this profile"""
        self.user.delete()
        return super().delete(*args, **kwargs)

    @property
    def slug(self):
        """
        Returns slug version of user.username
        """
        return slugify(self.user.username)