aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/models/mixins.py
blob: d32e6e72681a7d289f28a559bb25944d19a11641 (plain) (blame)
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
from abc import ABCMeta
from operator import itemgetter

from django.db import models


class ModelReprMixin:
    """Mixin providing a `__repr__()` to display model class name and initialisation parameters."""

    def __repr__(self):
        """Returns the current model class name and initialisation parameters."""
        attributes = ' '.join(
            f'{attribute}={value!r}'
            for attribute, value in sorted(
                self.__dict__.items(),
                key=itemgetter(0)
            )
            if not attribute.startswith('_')
        )
        return f'<{self.__class__.__name__}({attributes})>'


class ModelTimestampMixin(models.Model):
    """Mixin providing created_at and updated_at fields."""

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        """Metaconfig for the mixin."""

        abstract = True


class AbstractModelMeta(ABCMeta, type(models.Model)):
    """Metaclass for ABCModel class."""