-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameters.py
159 lines (150 loc) · 8.47 KB
/
parameters.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from dataclasses import dataclass
from functools import lru_cache, cache
from itertools import product
from django_resources import DjangoResources
@dataclass(frozen=True)
class DjangoMessagesParameters:
django_resources: DjangoResources
@lru_cache
def _load_resources(self):
auth = self.django_resources.django_pofile('contrib/auth')
sessions = self.django_resources.django_pofile('contrib/sessions')
sites = self.django_resources.django_pofile('contrib/sites')
redirects = self.django_resources.django_pofile('contrib/redirects')
flatpages = self.django_resources.django_pofile('contrib/flatpages')
admin = self.django_resources.django_pofile('contrib/admin')
contenttypes = self.django_resources.django_pofile('contrib/contenttypes')
return admin, auth, contenttypes, flatpages, redirects, sessions, sites
@lru_cache
def model_verbose_name(self):
admin, auth, contenttypes, flatpages, redirects, sessions, sites = self._load_resources()
return [
auth.find('user'),
auth.find('group'),
auth.find('permission'),
sessions.find('session'),
sites.find('site'),
redirects.find('redirect'),
flatpages.find('flat page'),
admin.find('log entry'),
contenttypes.find('content type'),
]
@lru_cache
def model_verbose_name_plural(self):
admin, auth, contenttypes, flatpages, redirects, sessions, sites = self._load_resources()
return [
auth.find('users'),
auth.find('groups'),
auth.find('permissions'),
sessions.find('sessions'),
sites.find('sites'),
redirects.find('redirects'),
flatpages.find('flat pages'),
admin.find('log entries'),
contenttypes.find('content types'),
]
@cache
def fields(self):
admin, auth, contenttypes, flatpages, redirects, sessions, sites = self._load_resources()
return [auth.find('email address')]
def model_ngettext(self, counts: list[int] = [1, 5, 22]):
result = []
for count in counts:
models = self.model_verbose_name() if count == 1 else self.model_verbose_name_plural()
for model in models:
result.append((count, model))
return result
def parameters_mapping(self):
return {
'Select %s': [{'': value} for value in self.model_verbose_name()],
'Select %s to change': [{'': value} for value in self.model_verbose_name()],
'Select %s to view': [{'': value} for value in self.model_verbose_name()],
'Add %(name)s': [{'name': value} for value in self.model_verbose_name()],
'Delete selected %(verbose_name_plural)s': [
{'verbose_name_plural': value} for value in self.model_verbose_name_plural()
],
'Successfully deleted %(count)d %(items)s.': [
{'count': count, 'items': items} for count, items in self.model_ngettext()
],
'Cannot delete %(name)s': [{'name': name} for name in set(map(lambda x: x[1], self.model_ngettext()))],
'Add another %(verbose_name)s': [{'verbose_name': value} for value in self.model_verbose_name()],
'Added {name} “{object}”.': [{'name': name, 'object': 'maciek'} for name in self.model_verbose_name()],
'Changed {fields} for {name} “{object}”.': [
{'fields': 'username', 'name': name, 'object': 'maciek'} for name in self.model_verbose_name()
],
'Deleted {name} “{object}”.': [{'name': name, 'object': 'maciek'} for name in self.model_verbose_name()],
'The {name} “{obj}” was added successfully.': [
{'name': name, 'obj': 'maciek'} for name in self.model_verbose_name()
],
'The {name} “{obj}” was added successfully. You may add another {name} below.': [
{'name': name, 'obj': 'maciek'} for name in self.model_verbose_name()
],
'The {name} “{obj}” was changed successfully. You may edit it again below.': [
{'name': name, 'obj': 'maciek'} for name in self.model_verbose_name()
],
'The {name} “{obj}” was added successfully. You may edit it again below.': [
{'name': name, 'obj': 'maciek'} for name in self.model_verbose_name()
],
'The {name} “{obj}” was changed successfully. You may add another {name} below.': [
{'name': name, 'obj': 'maciek'} for name in self.model_verbose_name()
],
'The {name} “{obj}” was changed successfully.': [
{'name': name, 'obj': 'maciek'} for name in self.model_verbose_name()
],
'The %(name)s “%(obj)s” was deleted successfully.': [
{'name': name, 'obj': 'maciek'} for name in self.model_verbose_name()
],
'%(name)s with ID “%(key)s” doesn’t exist. Perhaps it was deleted?': [
{'name': name, 'key': 'maciek'} for name in self.model_verbose_name()
],
'Add %s': [{'': name} for name in self.model_verbose_name()],
'Change %s': [{'': name} for name in self.model_verbose_name()],
'View %s': [{'': name} for name in self.model_verbose_name()],
'%(count)s %(name)s was changed successfully.': [
{'count': number, 'name': name} for number, name in self.model_ngettext()
],
'%(class_name)s %(instance)s': [
{'class_name': name, 'instance': 'maciek'} for name in self.model_verbose_name()
],
'Deleting %(class_name)s %(instance)s would require deleting the following protected '
'related objects: %(related_objects)s': [
{'class_name': name, 'instance': 'maciek', 'related_objects': 'group admins'}
for name in self.model_verbose_name()
],
'Select all %(total_count)s %(module_name)s': [
{'total_count': 2, 'module_name': name_plural} for name_plural in self.model_verbose_name_plural()
]
+ [{'total_count': 5, 'module_name': name_plural} for name_plural in self.model_verbose_name_plural()],
"Deleting the %(object_name)s '%(escaped_object)s' would result in deleting "
"related objects, but your account doesn't have permission to delete the "
"following types of objects:": [
{'object_name': name, 'escaped_object': 'maciek'} for name in self.model_verbose_name()
],
"Deleting the %(object_name)s '%(escaped_object)s' would require deleting the following protected"
" related objects:": [
{'object_name': name, 'escaped_object': 'maciek'} for name in self.model_verbose_name()
],
"Are you sure you want to delete the %(object_name)s \"%(escaped_object)s\"? "
"All of the following related items will be deleted:": [
{'object_name': name, 'escaped_object': 'maciek'} for name in self.model_verbose_name()
],
"Deleting the selected %(objects_name)s would result in deleting related "
"objects, but your account doesn't have permission to delete the following "
"types of objects:": [
{'objects_name': name, 'count': count} for count, name in self.model_ngettext([1, 2])
],
"Deleting the selected %(objects_name)s would require deleting the following protected related objects:": [
{'objects_name': name, 'count': count} for count, name in self.model_ngettext([1, 2])
],
"Are you sure you want to delete the selected %(objects_name)s? All of the "
"following objects and their related items will be deleted:": [
{'objects_name': name, 'count': count} for count, name in self.model_ngettext([1, 2])
],
"Change selected %(model)s": [{'model': model} for model in self.model_verbose_name()],
"%(count)d %(name)s": [
{"count": number, "name": name, "name_plural": name} for number, name in self.model_ngettext()
],
"%(model_name)s with this %(field_labels)s already exists.": [
{"model_name": model, "field_labels": fields} for model, fields in product(self.model_verbose_name(), self.fields())
],
}