-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathutil.py
104 lines (94 loc) · 3.59 KB
/
util.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
from django.utils.encoding import smart_str
try:
from django.utils.encoding import force_text as force_unicode
except ImportError:
from django.utils.encoding import force_unicode
try:
from django.utils.encoding import smart_text as smart_unicode
except ImportError:
try:
from django.utils.encoding import smart_unicode
except ImportError:
from django.forms.util import smart_unicode
from django.forms.forms import pretty_name
from django.db.models.fields import FieldDoesNotExist
from django.utils import formats
from mongoengine import fields
from mongodbforms.util import init_document_options
import collections
class RelationWrapper(object):
"""
Wraps a document referenced from a ReferenceField with an Interface similiar to
django's ForeignKeyField.rel
"""
def __init__(self, document):
self.to = init_document_options(document)
def is_django_user_model(user):
"""
Checks if a user model is compatible with Django's
recent changes. Django requires User models to have
an int pk, so we check here if it has (mongoengine hasn't)
"""
try:
if hasattr(user, 'pk'):
int(user.pk)
else:
int(user)
except (ValueError, TypeError):
return False
return True
def label_for_field(name, model, model_admin=None, return_attr=False):
attr = None
model._meta = init_document_options(model)
try:
field = model._meta.get_field_by_name(name)[0]
label = field.name.replace('_', ' ')
except FieldDoesNotExist:
if name == "__unicode__":
label = force_unicode(model._meta.verbose_name)
elif name == "__str__":
label = smart_str(model._meta.verbose_name)
else:
if isinstance(name, collections.Callable):
attr = name
elif model_admin is not None and hasattr(model_admin, name):
attr = getattr(model_admin, name)
elif hasattr(model, name):
attr = getattr(model, name)
else:
message = "Unable to lookup '%s' on %s" % (name, model._meta.object_name)
if model_admin:
message += " or %s" % (model_admin.__class__.__name__,)
raise AttributeError(message)
if hasattr(attr, "short_description"):
label = attr.short_description
elif isinstance(attr, collections.Callable):
if attr.__name__ == "<lambda>":
label = "--"
else:
label = pretty_name(attr.__name__)
else:
label = pretty_name(name)
if return_attr:
return (label, attr)
else:
return label
def display_for_field(value, field):
from django.contrib.admin.templatetags.admin_list import _boolean_icon
from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
if field.flatchoices:
return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE)
# NullBooleanField needs special-case null-handling, so it comes
# before the general null test.
elif isinstance(field, fields.BooleanField):
return _boolean_icon(value)
elif value is None:
return EMPTY_CHANGELIST_VALUE
elif isinstance(field, fields.DateTimeField):
return formats.localize(value)
elif isinstance(field, fields.DecimalField):
return formats.number_format(value, field.decimal_places)
elif isinstance(field, fields.FloatField):
return formats.number_format(value)
else:
return smart_unicode(value)