Merge branch 'master' into code-format-black
[mygpo.git] / mygpo / core / models.py
blob746361ddeda978d0225edf8e9ed425b6cadc98fb
1 """ This module contains abstract models that are used in multiple apps """
4 from django.db import models, connection
7 class UUIDModel(models.Model):
8 """ Models that have an UUID as primary key """
10 id = models.UUIDField(primary_key=True)
12 class Meta:
13 abstract = True
15 def get_id(self):
16 """ String representation of the ID """
17 return self.id
20 class TwitterModel(models.Model):
21 """ A model that has a twitter handle """
23 twitter = models.CharField(max_length=15, null=True, blank=True)
25 class Meta:
26 abstract = True
29 class GenericManager(models.Manager):
30 """ Generic manager methods """
32 def count_fast(self):
33 """ Fast approximate count of all model instances
35 PostgreSQL is slow when counting records without an index. This is a
36 workaround which only gives approximate results. see:
37 http://wiki.postgresql.org/wiki/Slow_Counting """
38 cursor = connection.cursor()
39 cursor.execute(
40 "select reltuples from pg_class where relname='%s';"
41 % self.model._meta.db_table
43 row = cursor.fetchone()
44 return int(row[0])
47 class UpdateInfoModel(models.Model):
48 """ Model that keeps track of when it was created and updated """
50 created = models.DateTimeField(auto_now_add=True)
51 modified = models.DateTimeField(auto_now=True)
53 class Meta:
54 abstract = True
57 class DeleteableModel(models.Model):
58 """ A model that can be marked as deleted """
60 # indicates that the object has been deleted
61 deleted = models.BooleanField(default=False)
63 class Meta:
64 abstract = True
67 class OrderedModel(models.Model):
68 """ A model that can be ordered
70 The implementing Model must make sure that 'order' is sufficiently unique
71 """
73 order = models.PositiveSmallIntegerField()
75 class Meta:
76 abstract = True
77 ordering = ['order']
80 class OptionallyOrderedModel(models.Model):
81 """ A model that can be ordered, w/ unknown order of individual objects
83 The implementing Model must make sure that 'order' is sufficiently unique
84 """
86 order = models.BigIntegerField(null=True, default=None)
88 class Meta:
89 abstract = True
90 ordering = ['order']