Code

Ticket #3377: __init__.py

File __init__.py, 2.1 KB (added by Brut[all] <brut.alll@…>, 7 years ago)
Line 
1from django.conf import settings
2from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
3from django.core import validators
4from django.db import backend, connection
5from django.db.models.loading import get_apps, get_app, get_models, get_model, register_models
6from django.db.models.query import Q
7from django.db.models.manager import Manager
8from django.db.models.base import Model, AdminOptions
9from django.db.models.fields import *
10from django.db.models.fields.related import ForeignKey, OneToOneField, ManyToManyField, ManyToOneRel, ManyToManyRel, OneToOneRel, TABULAR, STACKED
11from django.db.models.fields.generic import GenericRelation, GenericRel, GenericForeignKey
12from django.db.models import signals
13from django.utils.functional import curry
14from django.utils.text import capfirst
15
16# Admin stages.
17ADD, CHANGE, BOTH = 1, 2, 3
18
19# Decorator. Takes a function that returns a tuple in this format:
20#     (viewname, viewargs, viewkwargs)
21# Returns a function that calls urlresolvers.reverse() on that data, to return
22# the URL for those parameters.
23def permalink(func):
24    from django.core.urlresolvers import reverse
25    def inner(*args, **kwargs):
26        bits = func(*args, **kwargs)
27        viewname = bits[0]
28        return reverse(bits[0], None, *bits[1:3])
29    return inner
30
31class LazyDate(object):
32    """
33    Use in limit_choices_to to compare the field to dates calculated at run time
34    instead of when the model is loaded.  For example::
35
36        ... limit_choices_to = {'date__gt' : models.LazyDate(days=-3)} ...
37
38    which will limit the choices to dates greater than three days ago.
39    """
40    def __init__(self, **kwargs):
41        self.delta = datetime.timedelta(**kwargs)
42
43    def __str__(self):
44        return str(self.__get_value__())
45
46    def __repr__(self):
47        return "<LazyDate: %s>" % self.delta
48
49    def __get_value__(self):
50        return (datetime.datetime.now() + self.delta).date()
51
52    def __getattr__(self, attr):
53        if attr == 'delta':
54            raise AttributeError
55        return getattr(self.__get_value__(), attr)