[UserSettings] refactor, add tests
[mygpo.git] / mygpo / core / models.py
blobb246ab994cb25da057c05ff3d0dacad97d0d4873
1 """ This module contains abstract models that are used in multiple apps """
3 from __future__ import absolute_import, unicode_literals
5 import json
7 from uuidfield import UUIDField
9 from django.db import models, connection
12 class UUIDModel(models.Model):
13 """ Models that have an UUID as primary key """
15 id = UUIDField(primary_key=True)
17 class Meta:
18 abstract = True
20 def get_id(self):
21 """ String representation of the ID """
22 return self.id.hex
25 class TwitterModel(models.Model):
26 """ A model that has a twitter handle """
28 twitter = models.CharField(max_length=15, null=True, blank=False)
30 class Meta:
31 abstract = True
34 class GenericManager(models.Manager):
35 """ Generic manager methods """
37 def count_fast(self):
38 """ Fast approximate count of all model instances
40 PostgreSQL is slow when counting records without an index. This is a
41 workaround which only gives approximate results. see:
42 http://wiki.postgresql.org/wiki/Slow_Counting """
43 cursor = connection.cursor()
44 cursor.execute("select reltuples from pg_class where relname='%s';" %
45 self.model._meta.db_table)
46 row = cursor.fetchone()
47 return int(row[0])
50 class UpdateInfoModel(models.Model):
51 """ Model that keeps track of when it was created and updated """
52 created = models.DateTimeField(auto_now_add=True)
53 modified = models.DateTimeField(auto_now=True)
55 class Meta:
56 abstract = True
59 class DeleteableModel(models.Model):
60 """ A model that can be marked as deleted """
62 # indicates that the object has been deleted
63 deleted = models.BooleanField(default=False)
65 class Meta:
66 abstract = True
69 class OrderedModel(models.Model):
70 """ A model that can be ordered
72 The implementing Model must make sure that 'order' is sufficiently unique
73 """
75 order = models.PositiveSmallIntegerField()
77 class Meta:
78 abstract = True
79 ordering = ['order']