1
|
|
|
""" |
2
|
|
|
Django compatibility features |
3
|
|
|
""" |
4
|
|
|
from django.db import transaction, models |
5
|
|
|
|
6
|
|
|
__all__ = ( |
7
|
|
|
'transaction_atomic', |
8
|
|
|
'add_preserved_filters', |
9
|
|
|
'with_metaclass', |
10
|
|
|
'HideChoicesCharField', |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
# New transaction support in Django 1.6 |
14
|
|
|
try: |
15
|
|
|
transaction_atomic = transaction.atomic |
16
|
|
|
except AttributeError: |
17
|
|
|
transaction_atomic = transaction.commit_on_success |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
# Preserving admin form filters when adding parameters to the URL |
21
|
|
|
try: |
22
|
|
|
# Django 1.6 supports this, and django-parler also applies this fix. |
23
|
|
|
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters |
24
|
|
|
except ImportError: |
25
|
|
|
# Django <1.6 does not preserve filters |
26
|
|
|
def add_preserved_filters(context, form_url): |
27
|
|
|
return form_url |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def with_metaclass(meta, *bases): |
31
|
|
|
# Function from python-future and jinja2. License: BSD. |
32
|
|
|
# Allow consistent behaviours across all django versions |
33
|
|
|
# Also avoids a temporary intermediate class |
34
|
|
|
class metaclass(meta): |
35
|
|
|
__call__ = type.__call__ |
36
|
|
|
__init__ = type.__init__ |
37
|
|
|
|
38
|
|
|
def __new__(cls, name, this_bases, d): |
39
|
|
|
if this_bases is None: |
40
|
|
|
return type.__new__(cls, name, (), d) |
41
|
|
|
return meta(name, bases, d) |
42
|
|
|
return metaclass('temporary_class', None, {}) |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
class HideChoicesCharField(models.CharField): |
46
|
|
|
# For Django 1.7, hide the 'choices' for a field. |
47
|
|
|
|
48
|
|
|
def deconstruct(self): |
49
|
|
|
name, path, args, kwargs = models.CharField.deconstruct(self) |
50
|
|
|
|
51
|
|
|
# Hide the fact this model was used. |
52
|
|
|
if path == __name__ + '.HideChoicesCharField': |
53
|
|
|
path = 'django.db.models.CharField' |
54
|
|
|
try: |
55
|
|
|
del kwargs['choices'] |
56
|
|
|
except KeyError: |
57
|
|
|
pass |
58
|
|
|
|
59
|
|
|
return name, path, args, kwargs |
60
|
|
|
|
61
|
|
|
def south_field_triple(self): |
62
|
|
|
from south.modelsinspector import introspector |
63
|
|
|
args, kwargs = introspector(self) |
64
|
|
|
return ('django.db.models.fields.CharField', args, kwargs) |
65
|
|
|
|
66
|
|
|
try: |
67
|
|
|
from south.modelsinspector import add_introspection_rules |
68
|
|
|
except ImportError: |
69
|
|
|
pass |
70
|
|
|
else: |
71
|
|
|
add_introspection_rules([], ["^" + __name__.replace(".", "\.") + "\.HideChoicesCharField"]) |
72
|
|
|
|