Debian Codegraph @

src/organizations/fields.py

raw symbols SCIP
1 """
2 Most of this code extracted and borrowed from django-model-utils
3
4 Copyright (c) 2009-2015, Carl Meyer and contributors
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are
9 met:
10
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following
15 disclaimer in the documentation and/or other materials provided
16 with the distribution.
17 * Neither the name of the author nor the names of other
18 contributors may be used to endorse or promote products derived
19 from this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 """
33
34 from importlib import import_module
35
36 from django.conf import settings
37 from django.core.exceptions import ImproperlyConfigured
38 from django.db import models
39 from django.utils.timezone import now
40
41
42 class AutoCreatedField(models.DateTimeField):
43 """
44 A DateTimeField that automatically populates itself at
45 object creation.
46
47 By default, sets editable=False, default=datetime.now.
48
49 """
50
51 def __init__(self, *args, **kwargs):
52 kwargs.setdefault("editable", False)
53 kwargs.setdefault("default", now)
54 super().__init__(*args, **kwargs)
55
56
57 class AutoLastModifiedField(AutoCreatedField):
58 """
59 A DateTimeField that updates itself on each save() of the model.
60
61 By default, sets editable=False and default=datetime.now.
62
63 """
64
65 def pre_save(self, model_instance, add):
66 value = now()
67 setattr(model_instance, self.attname, value)
68 return value
69
70
71 ORGS_SLUGFIELD = getattr(
72 settings, "ORGS_SLUGFIELD", "django_extensions.db.fields.AutoSlugField"
73 )
74
75 try:
76 module, klass = ORGS_SLUGFIELD.rsplit(".", 1)
77 BaseSlugField = getattr(import_module(module), klass)
78 except (ImportError, ValueError, AttributeError):
79 raise ImproperlyConfigured(
80 "Your SlugField class, '{0}', is improperly defined. "
81 "See the documentation and install an auto slug field".format(ORGS_SLUGFIELD)
82 )
83
84
85 class SlugField(BaseSlugField):
86 """Class redefinition for migrations"""