1
|
|
|
|
2
|
|
|
import re |
3
|
|
|
import string |
4
|
|
|
import imp |
5
|
|
|
|
6
|
|
|
try: |
7
|
|
|
# Django versions >= 1.9 |
8
|
|
|
from django.utils.module_loading import import_module |
9
|
|
|
except ImportError: |
10
|
|
|
# Django versions < 1.9 |
11
|
|
|
from django.utils.importlib import import_module |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
__all__ = ['plural', 'mixedToUnder', 'capword', 'lowerword', 'underToMixed'] |
15
|
|
|
|
16
|
|
|
# http://code.activestate.com/recipes/82102-smart-pluralisation-english/ |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def plural(text): |
20
|
|
|
""" |
21
|
|
|
>>> plural('activity') |
22
|
|
|
'activities' |
23
|
|
|
""" |
24
|
|
|
aberrant = { |
25
|
|
|
'knife': 'knives', |
26
|
|
|
'self': 'selves', |
27
|
|
|
'elf': 'elves', |
28
|
|
|
'life': 'lives', |
29
|
|
|
'hoof': 'hooves', |
30
|
|
|
'leaf': 'leaves', |
31
|
|
|
'echo': 'echoes', |
32
|
|
|
'embargo': 'embargoes', |
33
|
|
|
'hero': 'heroes', |
34
|
|
|
'potato': 'potatoes', |
35
|
|
|
'tomato': 'tomatoes', |
36
|
|
|
'torpedo': 'torpedoes', |
37
|
|
|
'veto': 'vetoes', |
38
|
|
|
'child': 'children', |
39
|
|
|
'woman': 'women', |
40
|
|
|
'man': 'men', |
41
|
|
|
'person': 'people', |
42
|
|
|
'goose': 'geese', |
43
|
|
|
'mouse': 'mice', |
44
|
|
|
'barracks': 'barracks', |
45
|
|
|
'deer': 'deer', |
46
|
|
|
'nucleus': 'nuclei', |
47
|
|
|
'syllabus': 'syllabi', |
48
|
|
|
'focus': 'foci', |
49
|
|
|
'fungus': 'fungi', |
50
|
|
|
'cactus': 'cacti', |
51
|
|
|
'phenomenon': 'phenomena', |
52
|
|
|
'index': 'indices', |
53
|
|
|
'appendix': 'appendices', |
54
|
|
|
'criterion': 'criteria', |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if text in aberrant: |
60
|
|
|
result = '%s' % aberrant[text] |
61
|
|
|
else: |
62
|
|
|
postfix = 's' |
63
|
|
|
if len(text) > 2: |
64
|
|
|
vowels = 'aeiou' |
65
|
|
|
if text[-2:] in ('ch', 'sh'): |
66
|
|
|
postfix = 'es' |
67
|
|
|
elif text[-1:] == 'y': |
68
|
|
|
if (text[-2:-1] in vowels) or (text[0] in string.ascii_uppercase): |
69
|
|
|
postfix = 's' |
70
|
|
|
else: |
71
|
|
|
postfix = 'ies' |
72
|
|
|
text = text[:-1] |
73
|
|
|
elif text[-2:] == 'is': |
74
|
|
|
postfix = 'es' |
75
|
|
|
text = text[:-2] |
76
|
|
|
elif text[-1:] in ('s', 'z', 'x'): |
77
|
|
|
postfix = 'es' |
78
|
|
|
|
79
|
|
|
result = '%s%s' % (text, postfix) |
80
|
|
|
return result |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
# Text utilities |
84
|
|
|
# |
85
|
|
|
_mixedToUnderRE = re.compile(r'[A-Z]+') |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
def mixedToUnder(s): # pragma: no cover |
89
|
|
|
""" |
90
|
|
|
Sample: |
91
|
|
|
>>> mixedToUnder("FooBarBaz") |
92
|
|
|
'foo_bar_baz' |
93
|
|
|
|
94
|
|
|
Special case for ID: |
95
|
|
|
>>> mixedToUnder("FooBarID") |
96
|
|
|
'foo_bar_id' |
97
|
|
|
""" |
98
|
|
|
if s.endswith('ID'): |
99
|
|
|
return mixedToUnder(s[:-2] + "_id") |
100
|
|
|
trans = _mixedToUnderRE.sub(mixedToUnderSub, s) |
101
|
|
|
if trans.startswith('_'): |
102
|
|
|
trans = trans[1:] |
103
|
|
|
return trans |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
def mixedToUnderSub(match): |
107
|
|
|
m = match.group(0).lower() |
108
|
|
|
if len(m) > 1: |
109
|
|
|
return '_%s_%s' % (m[:-1], m[-1]) |
110
|
|
|
else: |
111
|
|
|
return '_%s' % m |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
def capword(s): |
115
|
|
|
""" |
116
|
|
|
>>> capword('foo') |
117
|
|
|
'Foo' |
118
|
|
|
""" |
119
|
|
|
return s[0].upper() + s[1:] |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
def lowerword(s): # pragma: no cover |
123
|
|
|
""" |
124
|
|
|
>>> lowerword('Hello') |
125
|
|
|
'hello' |
126
|
|
|
""" |
127
|
|
|
return s[0].lower() + s[1:] |
128
|
|
|
|
129
|
|
|
_underToMixedRE = re.compile('_.') |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
def underToMixed(name): |
133
|
|
|
""" |
134
|
|
|
>>> underToMixed('some_large_model_name_perhaps') |
135
|
|
|
'someLargeModelNamePerhaps' |
136
|
|
|
|
137
|
|
|
>>> underToMixed('exception_for_id') |
138
|
|
|
'exceptionForID' |
139
|
|
|
""" |
140
|
|
|
if name.endswith('_id'): |
141
|
|
|
return underToMixed(name[:-3] + "ID") |
142
|
|
|
return _underToMixedRE.sub(lambda m: m.group(0)[1].upper(), |
143
|
|
|
name) |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
def model_class_form(name): |
147
|
|
|
""" |
148
|
|
|
>>> model_class_form('foo_bar_baz') |
149
|
|
|
'FooBarBaz' |
150
|
|
|
""" |
151
|
|
|
return capword(underToMixed(name)) |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
def underToAllCaps(value): # pragma: no cover |
155
|
|
|
""" |
156
|
|
|
>>> underToAllCaps('foo_bar_baz') |
157
|
|
|
'Foo Bar Baz' |
158
|
|
|
""" |
159
|
|
|
return ' '.join(map(lambda x: x.title(), value.split('_'))) |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
def import_crud(app): |
163
|
|
|
''' |
164
|
|
|
Import moderator module and register all models it contains with moderation |
165
|
|
|
''' |
166
|
|
|
|
167
|
|
|
try: |
168
|
|
|
app_path = import_module(app).__path__ |
169
|
|
|
except (AttributeError, ImportError): |
170
|
|
|
return None |
171
|
|
|
|
172
|
|
|
try: |
173
|
|
|
imp.find_module('crud', app_path) |
174
|
|
|
except ImportError: |
175
|
|
|
return None |
176
|
|
|
|
177
|
|
|
module = import_module("%s.crud" % app) |
178
|
|
|
|
179
|
|
|
return module |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
def auto_discover(): |
183
|
|
|
''' |
184
|
|
|
Auto register all apps that have module moderator with moderation |
185
|
|
|
''' |
186
|
|
|
from django.conf import settings |
187
|
|
|
|
188
|
|
|
for app in settings.INSTALLED_APPS: |
189
|
|
|
import_crud(app) |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
def custom_postfix_url(crud, model): |
193
|
|
|
postfix = getattr(crud, 'custom_postfix_url', None) |
194
|
|
|
|
195
|
|
|
if not postfix: |
196
|
|
|
postfix = plural(model) |
197
|
|
|
return postfix |
198
|
|
|
|