/
models.py
172 lines (141 loc) · 5.11 KB
/
models.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
try:
from django.contrib.contenttypes.fields import GenericForeignKey
except ImportError:
# This fallback import is the version that was deprecated in
# Django 1.7 and is removed in 1.9:
from django.contrib.contenttypes.generic import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.db import models
from django.utils.translation import ugettext_lazy as _
from model_utils.fields import AutoCreatedField, AutoLastModifiedField
from autoslug import AutoSlugField
from datetime import datetime
__author__ = 'guglielmo'
def get_slug_source(instance):
"""For use in AutoSlugField's populate_from"""
return instance.slug_source
class GenericRelatable(models.Model):
"""
An abstract class that provides the possibility of generic relations
"""
content_type = models.ForeignKey(ContentType, blank=True, null=True, db_index=True)
object_id = models.PositiveIntegerField(null=True, db_index=True)
content_object = GenericForeignKey('content_type', 'object_id')
class Meta:
abstract = True
def validate_partial_date(value):
"""
Validate a partial date, it can be partial, but it must yet be a valid date.
Accepted formats are: YYYY-MM-DD, YYYY-MM, YYYY.
2013-22 must rais a ValidationError, as 2013-13-12, or 2013-11-55.
"""
try:
datetime.strptime(value, '%Y-%m-%d')
except ValueError:
try:
datetime.strptime(value, '%Y-%m')
except ValueError:
try:
datetime.strptime(value, '%Y')
except ValueError:
raise ValidationError(
u'date seems not to be correct %s' % value)
class Dateframeable(models.Model):
"""
An abstract base class model that provides a start and an end dates to
the class.
Uncomplete dates can be used. The validation pattern is: "^[0-9]{4}(-[
0-9]{2}){0,2}$"
"""
partial_date_validator = RegexValidator(regex="^[0-9]{4}(-[0-9]{2}){0,2}$",
message="Date has wrong format")
start_date = models.CharField(
_("start date"), max_length=10, blank=True, null=True,
validators=[partial_date_validator, validate_partial_date],
help_text=_("The date when the validity of the item starts"),
)
end_date = models.CharField(
_("end date"), max_length=10, blank=True, null=True,
validators=[partial_date_validator, validate_partial_date],
help_text=_("The date when the validity of the item ends")
)
end_reason = models.CharField(
_("end reason"),
max_length=255,
null=True, blank=True,
help_text=_(
"The reason why the entity isn't valid any longer (eg: merge)"
)
)
@property
def is_active_now(self):
"""Return the current status of the item, whether active or not
:return: boolean
"""
return self.is_active()
def is_active(self, moment=datetime.strftime(datetime.now(), '%Y-%m-%d')):
"""Return the status of the item at the given moment
:param moment: date in '%Y-%m-%d' format
:return: boolean
"""
return (self.end_date is None or self.end_date >= moment)
def close(
self,
moment=datetime.strftime(datetime.now(), '%Y-%m-%d'),
reason=None
):
"""closes the validity of the entity, specifying a reason
:param moment: the moment the validity ends, in %Y-%m-%d format
:param reason: the reason whi the validity ends
:return:
"""
self.end_date = moment
if reason:
self.end_reason = reason
self.save()
class Meta:
abstract = True
class Timestampable(models.Model):
"""
An abstract base class model that provides self-updating
``created`` and ``modified`` fields.
"""
created_at = AutoCreatedField(_('creation time'))
updated_at = AutoLastModifiedField(_('last modification time'))
class Meta:
abstract = True
class Permalinkable(models.Model):
"""
An abstract base class model that provides a unique slug,
and the methods necessary to handle the permalink
"""
from django.utils.text import slugify
slug = AutoSlugField(
populate_from=get_slug_source,
max_length=255,
unique=True,
slugify=slugify
)
class Meta:
abstract = True
def get_url_kwargs(self, **kwargs):
kwargs.update(getattr(self, 'url_kwargs', {}))
return kwargs
@models.permalink
def get_absolute_url(self):
url_kwargs = self.get_url_kwargs(slug=self.slug)
return (self.url_name, (), url_kwargs)
class PrioritizedModel(models.Model):
"""
An abstract base class that provides an optional priority field,
to impose a custom sorting order.
"""
priority = models.IntegerField(
_('Priority'),
null=True, blank=True, default=0,
help_text=_("Sort order in case ambiguities arise")
)
class Meta:
abstract = True