1 | from django.conf import settings
|
---|
2 | from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
|
---|
3 | from django.core import validators
|
---|
4 | from django.db import backend, connection
|
---|
5 | from django.db.models.loading import get_apps, get_app, get_models, get_model, register_models
|
---|
6 | from django.db.models.query import Q
|
---|
7 | from django.db.models.manager import Manager
|
---|
8 | from django.db.models.base import Model, AdminOptions
|
---|
9 | from django.db.models.fields import *
|
---|
10 | from django.db.models.fields.related import ForeignKey, OneToOneField, ManyToManyField, ManyToOneRel, ManyToManyRel, OneToOneRel, TABULAR, STACKED
|
---|
11 | from django.db.models.fields.generic import GenericRelation, GenericRel, GenericForeignKey
|
---|
12 | from django.db.models import signals
|
---|
13 | from django.utils.functional import curry
|
---|
14 | from django.utils.text import capfirst
|
---|
15 |
|
---|
16 | # Admin stages.
|
---|
17 | ADD, 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.
|
---|
23 | def 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 |
|
---|
31 | class 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)
|
---|