accrete 0.0.54__py3-none-any.whl → 0.0.55__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- accrete/contrib/ui/__init__.py +2 -1
- accrete/contrib/ui/context.py +50 -8
- accrete/contrib/ui/filter.py +5 -5
- accrete/contrib/ui/static/css/accrete.css +17 -6
- accrete/contrib/ui/static/css/accrete.css.map +1 -1
- accrete/contrib/ui/static/css/accrete.scss +25 -0
- accrete/contrib/ui/templates/ui/dashboard.html +7 -0
- accrete/contrib/ui/templates/ui/detail.html +0 -86
- accrete/contrib/ui/templates/ui/form.html +6 -15
- accrete/contrib/ui/templates/ui/layout.html +3 -1
- accrete/contrib/ui/templates/ui/partials/filter.html +2 -2
- accrete/contrib/ui/templates/ui/partials/form_errors.html +33 -20
- accrete/contrib/ui/templates/ui/partials/header.html +1 -1
- accrete/forms.py +1 -248
- accrete/managers.py +1 -2
- accrete/utils/__init__.py +2 -2
- accrete/utils/forms.py +79 -0
- accrete/utils/http.py +14 -3
- {accrete-0.0.54.dist-info → accrete-0.0.55.dist-info}/METADATA +1 -1
- {accrete-0.0.54.dist-info → accrete-0.0.55.dist-info}/RECORD +22 -21
- {accrete-0.0.54.dist-info → accrete-0.0.55.dist-info}/WHEEL +1 -1
- {accrete-0.0.54.dist-info → accrete-0.0.55.dist-info}/licenses/LICENSE +0 -0
@@ -456,6 +456,31 @@ select:focus {
|
|
456
456
|
|
457
457
|
}
|
458
458
|
|
459
|
+
/* Chrome, Safari, Edge, Opera */
|
460
|
+
input::-webkit-outer-spin-button,
|
461
|
+
input::-webkit-inner-spin-button {
|
462
|
+
-webkit-appearance: none;
|
463
|
+
margin: 0;
|
464
|
+
}
|
465
|
+
|
466
|
+
/* Firefox */
|
467
|
+
input[type=number] {
|
468
|
+
-moz-appearance: textfield;
|
469
|
+
}
|
470
|
+
|
471
|
+
/* Fix CKEditor List Display */
|
472
|
+
.ck-editor__editable_inline {
|
473
|
+
padding: 0 30px !important;
|
474
|
+
}
|
475
|
+
|
476
|
+
//ul {
|
477
|
+
// list-style-type: disc;
|
478
|
+
// padding: 10px 0 !important;
|
479
|
+
// padding-left: 20px !important;
|
480
|
+
//}
|
481
|
+
|
482
|
+
|
483
|
+
|
459
484
|
@media screen and (max-width: 1407px) {
|
460
485
|
#list-customization * .dropdown-menu {
|
461
486
|
width: auto;
|
@@ -21,79 +21,6 @@
|
|
21
21
|
{% endblock %}
|
22
22
|
|
23
23
|
{% block content %}
|
24
|
-
{# <div class="tile is-ancestor">#}
|
25
|
-
{# <div class="tile is-vertical is-8">#}
|
26
|
-
{# <div class="tile is-vertical">#}
|
27
|
-
{# <div class="tile is-parent is-vertical">#}
|
28
|
-
{# <article class="tile is-child notification">#}
|
29
|
-
{# <p class="title is-4 mb-1">{{ title }}</p>#}
|
30
|
-
{# {% block detail_content %}{% endblock %}#}
|
31
|
-
{# </article>#}
|
32
|
-
{# </div>#}
|
33
|
-
{# </div>#}
|
34
|
-
{# <div class="tile is-parent">#}
|
35
|
-
{# <article class="tile is-child notification is-danger">#}
|
36
|
-
{# <p class="title is-4">Budgets</p>#}
|
37
|
-
{# <p class="subtitle">Aligned with the right tile</p>#}
|
38
|
-
{# <div class="content">#}
|
39
|
-
{# </div>#}
|
40
|
-
{# </article>#}
|
41
|
-
{# </div>#}
|
42
|
-
{# </div>#}
|
43
|
-
{# <div class="tile is-vertical">#}
|
44
|
-
{# <div class="tile is-parent">#}
|
45
|
-
{# <article class="tile is-child notification">#}
|
46
|
-
{# <div class="content">#}
|
47
|
-
{# <p class="title is-4">Info</p>#}
|
48
|
-
{# <p class="subtitle">With even more content</p>#}
|
49
|
-
{# <div class="content">#}
|
50
|
-
{# <!-- Content -->#}
|
51
|
-
{# </div>#}
|
52
|
-
{# </div>#}
|
53
|
-
{# </article>#}
|
54
|
-
{# </div>#}
|
55
|
-
{# <div class="tile is-parent">#}
|
56
|
-
{# <article class="tile is-child notification">#}
|
57
|
-
{# <div class="content">#}
|
58
|
-
{# <p class="title is-4">Änderungsprotokoll</p>#}
|
59
|
-
{# <p>Änderung</p>#}
|
60
|
-
{# <p>Änderung</p>#}
|
61
|
-
{# <p>Änderung</p>#}
|
62
|
-
{# <p>Änderung</p>#}
|
63
|
-
{# <p>Änderung</p>#}
|
64
|
-
{# <p>Änderung</p>#}
|
65
|
-
{# <p>Änderung</p>#}
|
66
|
-
{# <p>Änderung</p>#}
|
67
|
-
{# <p>Änderung</p>#}
|
68
|
-
{# <p>Änderung</p>#}
|
69
|
-
{# <p>Änderung</p>#}
|
70
|
-
{# <p>Änderung</p>#}
|
71
|
-
{# <p>Änderung</p>#}
|
72
|
-
{# <p>Änderung</p>#}
|
73
|
-
{# <p>Änderung</p>#}
|
74
|
-
{# <p>Änderung</p>#}
|
75
|
-
{# <p>Änderung</p>#}
|
76
|
-
{# <p>Änderung</p>#}
|
77
|
-
{# <p>Änderung</p>#}
|
78
|
-
{# <div class="content">#}
|
79
|
-
{# {% for expense in future_expenses %}#}
|
80
|
-
{# <div class="list-column column is-12" style="min-height: 4rem">#}
|
81
|
-
{# <div class="box p-3 is-flex is-flex-direction-row is-justify-content-space-between is-flex-wrap-wrap" style="word-break: break-word; height: 100%; border: solid #ccc 1px; overflow-y: auto">#}
|
82
|
-
{# <a class="is-underlined is-flex-grow-5" href="{{ expense.get_absolute_url }}">{{ expense }}</a>#}
|
83
|
-
{# <div class="is-flex is-flex-wrap-nowrap is-justify-content-space-between is-flex-grow-1">#}
|
84
|
-
{# <span>{{ expense.amount }}</span>#}
|
85
|
-
{# <span>{{ expense.next_book_date }}</span>#}
|
86
|
-
{# </div>#}
|
87
|
-
{# </div>#}
|
88
|
-
{# </div>#}
|
89
|
-
{# {% endfor %}#}
|
90
|
-
{# </div>#}
|
91
|
-
{# </div>#}
|
92
|
-
{# </article>#}
|
93
|
-
{# </div>#}
|
94
|
-
{# </div>#}
|
95
|
-
{# </div>#}
|
96
|
-
|
97
24
|
<div class="columns is-desktop m-0">
|
98
25
|
<div class="column p-0 is-8-desktop">
|
99
26
|
<div class="box mt-2">
|
@@ -107,17 +34,4 @@
|
|
107
34
|
</nav>
|
108
35
|
</div>
|
109
36
|
</div>
|
110
|
-
|
111
|
-
|
112
|
-
{# <div class="columns is-desktop m-0">#}
|
113
|
-
{# <div class="column p-0 is-8-desktop">#}
|
114
|
-
{# {% block detail_content %}{% endblock %}#}
|
115
|
-
{# </div>#}
|
116
|
-
{# <div class="info-panel column is-4">#}
|
117
|
-
{# <nav class="panel">#}
|
118
|
-
{# {% block info_panel %}#}
|
119
|
-
{# {% endblock %}#}
|
120
|
-
{# </nav>#}
|
121
|
-
{# </div>#}
|
122
|
-
{# </div>#}
|
123
37
|
{% endblock %}
|
@@ -3,28 +3,19 @@
|
|
3
3
|
|
4
4
|
{% block content %}
|
5
5
|
<div class="columns is-desktop m-0">
|
6
|
-
<div class="column p-0 is-8-desktop">
|
6
|
+
<div id="form-content" class="column p-0 is-8-desktop">
|
7
7
|
<div class="box mt-2">
|
8
|
-
{%
|
8
|
+
{% if form.is_saved is False %}
|
9
|
+
{% include 'ui/partials/form_errors.html' with show_field_errors=True %}
|
10
|
+
{% endif %}
|
9
11
|
{% block form %}{% endblock %}
|
10
12
|
</div>
|
11
13
|
</div>
|
12
|
-
<div class="column is-4">
|
13
|
-
<nav
|
14
|
+
<div id="form-info" class="column is-4">
|
15
|
+
<nav>
|
14
16
|
{% block info_panel %}
|
15
17
|
{% endblock %}
|
16
18
|
</nav>
|
17
19
|
</div>
|
18
20
|
</div>
|
19
|
-
|
20
|
-
|
21
|
-
{# <div class="columns is-desktop m-0">#}
|
22
|
-
{# <div class="column p-0 is-8-desktop">#}
|
23
|
-
{# <div>#}
|
24
|
-
{# {% include 'ui/partials/form_errors.html' with show_field_errors=True %}#}
|
25
|
-
{# {% block form %}{% endblock %}#}
|
26
|
-
{# </div>#}
|
27
|
-
{# </div>#}
|
28
|
-
{# <div class="column is-4"></div>#}
|
29
|
-
{# </div>#}
|
30
21
|
{% endblock %}
|
@@ -18,6 +18,8 @@
|
|
18
18
|
{% block htmx %}
|
19
19
|
<script src="{% static "js/htmx.min.js" %}" defer type="text/javascript"></script>
|
20
20
|
{% endblock %}
|
21
|
+
<script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/sort@3.x.x/dist/cdn.min.js"></script>
|
22
|
+
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3.13.10/dist/cdn.min.js" defer type="text/javascript"></script>
|
21
23
|
{% block script %}
|
22
24
|
<script src="{% static "js/ui.js" %}" defer type="text/javascript"></script>
|
23
25
|
{% endblock %}
|
@@ -62,7 +64,7 @@
|
|
62
64
|
<div class="navbar-item has-dropdown is-hoverable" onclick="this.classList.toggle('is-active');">
|
63
65
|
<a class="navbar-link is-arrowless {% if request.member.name %}is-size-7 has-text-centered-desktop{% endif %}">
|
64
66
|
{% if request.member.name %}
|
65
|
-
{{ request.member }}
|
67
|
+
{{ request.member }}
|
66
68
|
{% else %}
|
67
69
|
{{ user }}
|
68
70
|
{% endif %}
|
@@ -56,8 +56,8 @@
|
|
56
56
|
</div>
|
57
57
|
|
58
58
|
<div id="list-customization" style="display: flex; flex-direction: column; flex-wrap: nowrap">
|
59
|
-
<div class="
|
60
|
-
<div class="field has-addons
|
59
|
+
<div class="mt-2">
|
60
|
+
<div class="field has-addons">
|
61
61
|
{% if field_selection %}
|
62
62
|
<div class="control is-expanded">
|
63
63
|
<button id="list-customization-fields-trigger"
|
@@ -1,23 +1,36 @@
|
|
1
|
-
|
2
|
-
<div class="
|
3
|
-
<div class="
|
4
|
-
<
|
5
|
-
|
6
|
-
{
|
7
|
-
|
8
|
-
|
9
|
-
{
|
10
|
-
{
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
</
|
17
|
-
|
1
|
+
<div class="columns py-0">
|
2
|
+
<div class="column is-12">
|
3
|
+
<div class="notification is-danger is-light">
|
4
|
+
<p>{{ form.non_field_errors }}</p>
|
5
|
+
{% for inline_form in form.inline_forms %}
|
6
|
+
<p>{{ inline_form.non_field_errors }}</p>
|
7
|
+
{% endfor %}
|
8
|
+
{% if form.save_error %}
|
9
|
+
<p>{{ form.save_error|safe }}</p>
|
10
|
+
<p>Error ID: {{ form.save_error_id }}</p>
|
11
|
+
{% endif %}
|
12
|
+
{% if show_field_errors %}
|
13
|
+
{% for f in form %}
|
14
|
+
{% if f.errors %}
|
15
|
+
<label>
|
16
|
+
<span class="is-underlined">{{ f.label_tag }}</span>
|
17
|
+
<span class="is-size-7">{{ f.errors }}</span>
|
18
|
+
</label>
|
19
|
+
{% endif %}
|
20
|
+
{% endfor %}
|
21
|
+
{% for formset in form.inline_forms %}
|
22
|
+
{% for inline_form in formset %}
|
23
|
+
{% for field in inline_form %}
|
24
|
+
{% if field.errors %}
|
25
|
+
<label>
|
26
|
+
<span class="is-underlined">{{ field.label_tag }}</span>
|
27
|
+
<span class="is-size-7">{{ field.errors }}</span>
|
28
|
+
</label>
|
29
|
+
{% endif %}
|
30
|
+
{% endfor %}
|
18
31
|
{% endfor %}
|
19
|
-
{%
|
20
|
-
|
32
|
+
{% endfor %}
|
33
|
+
{% endif %}
|
21
34
|
</div>
|
22
35
|
</div>
|
23
|
-
|
36
|
+
</div>
|
@@ -29,7 +29,7 @@
|
|
29
29
|
{% endif %}
|
30
30
|
</div>
|
31
31
|
|
32
|
-
<div class="is-flex is-hidden-fullhd is-justify-content-space-between mb-2
|
32
|
+
<div class="is-flex is-hidden-fullhd is-justify-content-space-between mb-2">
|
33
33
|
<div id="header-actions" class="is-flex py-1 px-1 ml-2" style="overflow-x: auto">
|
34
34
|
{% for action in actions %}
|
35
35
|
{% if action.actions %}
|
accrete/forms.py
CHANGED
@@ -1,250 +1,12 @@
|
|
1
1
|
import logging
|
2
|
-
from uuid import uuid4
|
3
|
-
from functools import partial
|
4
|
-
|
5
2
|
from django import forms
|
6
3
|
from django.contrib.auth import get_user_model
|
7
|
-
from django.db import transaction
|
8
4
|
from accrete.tenant import get_tenant
|
9
5
|
|
10
6
|
_logger = logging.getLogger(__name__)
|
11
7
|
User = get_user_model()
|
12
8
|
|
13
9
|
|
14
|
-
class One2ManyModelForm(forms.ModelForm):
|
15
|
-
"""
|
16
|
-
Template Access:
|
17
|
-
In Templates/Forms the generated fields can be accessed via
|
18
|
-
form.get_>>related_name<< e.g. form.get_items returns
|
19
|
-
a list of dictionaries containing the fields.
|
20
|
-
|
21
|
-
Cleaning:
|
22
|
-
For every One2Many field, clean_>>related_name<< is called.
|
23
|
-
To clean all entries together, self.cleaned_data[>>related_name<<] is
|
24
|
-
available in clean() containing a list of all entries
|
25
|
-
|
26
|
-
Initial data can be supplied by creating a form method called
|
27
|
-
get_initial_>>related_name<<_>>field_name<< which takes the
|
28
|
-
related instance as a parameter
|
29
|
-
or by setting an attribute on the model which can be a callable
|
30
|
-
|
31
|
-
|
32
|
-
One2Many field configuration:
|
33
|
-
o2m_fields = {
|
34
|
-
# related_name
|
35
|
-
'items': {
|
36
|
-
# field that holds the key to self.model
|
37
|
-
'fk_field': 'pricelist',
|
38
|
-
# Order by the given fields value, can be prefixed by '-' to
|
39
|
-
# control ascending/descending order
|
40
|
-
'order': '-pk'
|
41
|
-
# fields of the related model that are needed in the form
|
42
|
-
'fields': {
|
43
|
-
# pk field must be present for updating existing rows.
|
44
|
-
# Otherwise, all related objects will be deleted and recreated
|
45
|
-
# upon saving.
|
46
|
-
'pk': {
|
47
|
-
'field_class': partial(
|
48
|
-
forms.IntegerField,
|
49
|
-
required=False,
|
50
|
-
widget=forms.HiddenInput
|
51
|
-
),
|
52
|
-
# if only data is sent with fields that have
|
53
|
-
# "count_as_empty" set to true, the whole fieldset is
|
54
|
-
# ignored and won't be added to self.fields
|
55
|
-
'count_as_empty': True
|
56
|
-
},
|
57
|
-
'product': {
|
58
|
-
'field_class': partial(
|
59
|
-
forms.ModelChoiceField,
|
60
|
-
Product.objects.all(),
|
61
|
-
label=_('Product')
|
62
|
-
)
|
63
|
-
},
|
64
|
-
'price': {
|
65
|
-
'field_class': partial(
|
66
|
-
forms.FloatField,
|
67
|
-
label=_('Price')
|
68
|
-
)
|
69
|
-
}
|
70
|
-
}
|
71
|
-
}
|
72
|
-
}
|
73
|
-
"""
|
74
|
-
o2m_fields = {}
|
75
|
-
|
76
|
-
def __init__(self, *args, **kwargs):
|
77
|
-
super().__init__(*args, **kwargs)
|
78
|
-
self.o2m_keys = None
|
79
|
-
if self.instance and self.instance.pk and not kwargs.get('data'):
|
80
|
-
self.add_instance_o2m_objects()
|
81
|
-
else:
|
82
|
-
self.add_o2m_fields(**kwargs)
|
83
|
-
self.add_o2m_getter()
|
84
|
-
|
85
|
-
def add_instance_o2m_objects(self):
|
86
|
-
self.o2m_keys = set()
|
87
|
-
for field in self.o2m_fields.keys():
|
88
|
-
related_objects = getattr(self.instance, field)
|
89
|
-
for o in related_objects.all():
|
90
|
-
field_key = f'{field}_{str(uuid4())[:8]}'
|
91
|
-
for k, v in self.o2m_fields[field]['fields'].items():
|
92
|
-
func_name = f'get_initial_{field}_{k}'
|
93
|
-
field_name = f'{field_key}-{k}'
|
94
|
-
self.fields[field_name] = v['field_class']()
|
95
|
-
if hasattr(self, func_name):
|
96
|
-
initial = getattr(self, func_name)(o)
|
97
|
-
else:
|
98
|
-
initial = getattr(o, k) if hasattr(o, k) else False
|
99
|
-
if callable(self.fields[field_name].initial):
|
100
|
-
initial = initial()
|
101
|
-
self.fields[field_name].initial = initial
|
102
|
-
self.o2m_keys.add(field_key)
|
103
|
-
|
104
|
-
def add_o2m_fields(self, **kwargs):
|
105
|
-
self.o2m_keys = set()
|
106
|
-
data = kwargs.get('data', {})
|
107
|
-
for field, config in self.o2m_fields.items():
|
108
|
-
fields = config['fields']
|
109
|
-
field_keys = set(
|
110
|
-
k.split('-')[0] for k in data.keys() if k.startswith(field)
|
111
|
-
)
|
112
|
-
if not field_keys:
|
113
|
-
field_keys = set(
|
114
|
-
f'{k}_1' for k in self.o2m_fields.keys()
|
115
|
-
)
|
116
|
-
for field_key in field_keys:
|
117
|
-
to_add = any([
|
118
|
-
data.get(f'{field_key}-{name}')
|
119
|
-
for name in list(fields.keys())
|
120
|
-
if not fields.get(f'{name}', {}).get('count_as_empty')
|
121
|
-
])
|
122
|
-
if to_add:
|
123
|
-
for k, v in fields.items():
|
124
|
-
fname = f'{field_key}-{k}'
|
125
|
-
self.fields[fname] = v['field_class']()
|
126
|
-
self.fields[fname].initial = kwargs.get(fname)
|
127
|
-
self.o2m_keys.add(field_key)
|
128
|
-
|
129
|
-
def add_o2m_getter(self):
|
130
|
-
for k in self.o2m_fields.keys():
|
131
|
-
func_name = f'get_{k}'
|
132
|
-
setattr(self, func_name, partial(self.get_o2m_fields, k))
|
133
|
-
|
134
|
-
def get_o2m_fields(self, prefix):
|
135
|
-
fields = []
|
136
|
-
for k in self.o2m_keys:
|
137
|
-
if k.startswith(prefix):
|
138
|
-
fnames = list(
|
139
|
-
self.o2m_fields.get(prefix, {}).get('fields', [])
|
140
|
-
)
|
141
|
-
fields.append({
|
142
|
-
fname: self[f'{k}-{fname}']
|
143
|
-
for fname in fnames
|
144
|
-
})
|
145
|
-
|
146
|
-
order = self.o2m_fields.get(prefix, {}).get('order')
|
147
|
-
reverse = False
|
148
|
-
if order and order.startswith('-'):
|
149
|
-
order = order[1:]
|
150
|
-
reverse = True
|
151
|
-
if order and fields:
|
152
|
-
fields = sorted(
|
153
|
-
fields, key=lambda x: x[order].value(), reverse=reverse
|
154
|
-
)
|
155
|
-
return fields
|
156
|
-
|
157
|
-
def _clean_fields(self):
|
158
|
-
super()._clean_fields()
|
159
|
-
self._clean_o2m_fields()
|
160
|
-
|
161
|
-
def _clean_o2m_fields(self):
|
162
|
-
for related_name, related_field_config in self.o2m_fields.items():
|
163
|
-
self.cleaned_data[related_name] = []
|
164
|
-
fields = related_field_config['fields'].keys()
|
165
|
-
entries = filter(
|
166
|
-
lambda x: x.startswith(related_name), self.o2m_keys
|
167
|
-
)
|
168
|
-
for entry in entries:
|
169
|
-
value = {
|
170
|
-
fname: self.cleaned_data.get(f'{entry}-{fname}')
|
171
|
-
for fname in fields
|
172
|
-
}
|
173
|
-
try:
|
174
|
-
self.cleaned_data[related_name].append(
|
175
|
-
getattr(self, f'clean_{related_name}')(value)
|
176
|
-
)
|
177
|
-
except AttributeError:
|
178
|
-
self.cleaned_data[related_name].append(value)
|
179
|
-
except forms.ValidationError as error:
|
180
|
-
self.add_error(entry, error)
|
181
|
-
|
182
|
-
def save_o2m(self):
|
183
|
-
for field in self.o2m_fields.keys():
|
184
|
-
fk_field = self.o2m_fields[field].get('fk_field')
|
185
|
-
if not fk_field:
|
186
|
-
raise KeyError(
|
187
|
-
'One2Many Form definition is missing the '
|
188
|
-
f'"fk_field" attribute on field {field}'
|
189
|
-
)
|
190
|
-
|
191
|
-
related_objects = getattr(self.instance, field)
|
192
|
-
if not self.cleaned_data.get(field, []):
|
193
|
-
related_objects.all().delete()
|
194
|
-
|
195
|
-
related_object_fields = [
|
196
|
-
field.name for field
|
197
|
-
in related_objects.model._meta.get_fields()
|
198
|
-
]
|
199
|
-
update_pks, to_update, to_create = [], [], []
|
200
|
-
|
201
|
-
for item in self.cleaned_data.get(field, []):
|
202
|
-
item.update({fk_field: self.instance})
|
203
|
-
if pk := item.get('pk'):
|
204
|
-
update_pks.append(pk)
|
205
|
-
to_update.append(item)
|
206
|
-
else:
|
207
|
-
to_create.append(item)
|
208
|
-
|
209
|
-
qs = related_objects.filter(pk__in=update_pks).order_by('pk')
|
210
|
-
related_objects.exclude(pk__in=update_pks).delete()
|
211
|
-
to_update = sorted(to_update, key=lambda x: x['pk'])
|
212
|
-
if qs.count() != len(to_update):
|
213
|
-
raise ValueError(
|
214
|
-
'Mismatching length of found objects and update data'
|
215
|
-
)
|
216
|
-
updated = []
|
217
|
-
for obj, update in zip(qs, to_update):
|
218
|
-
for k, v in update.items():
|
219
|
-
if k not in related_object_fields:
|
220
|
-
continue
|
221
|
-
setattr(obj, k, v)
|
222
|
-
updated.append(obj)
|
223
|
-
objects_to_create = []
|
224
|
-
for c in to_create:
|
225
|
-
values = {}
|
226
|
-
for k, v in c.items():
|
227
|
-
if k in related_object_fields:
|
228
|
-
values.update({k: v})
|
229
|
-
objects_to_create.append(related_objects.model(**values))
|
230
|
-
if updated:
|
231
|
-
field_list = [
|
232
|
-
f for f in to_update[0].keys()
|
233
|
-
if f in related_object_fields
|
234
|
-
]
|
235
|
-
related_objects.bulk_update(
|
236
|
-
updated, fields=field_list
|
237
|
-
)
|
238
|
-
if to_create:
|
239
|
-
related_objects.bulk_create(objects_to_create)
|
240
|
-
|
241
|
-
def save(self, commit=True):
|
242
|
-
super().save(commit=commit)
|
243
|
-
if commit:
|
244
|
-
self.save_o2m()
|
245
|
-
return self.instance
|
246
|
-
|
247
|
-
|
248
10
|
class TenantForm(forms.Form):
|
249
11
|
|
250
12
|
def __init__(self, *args, **kwargs):
|
@@ -259,7 +21,7 @@ class TenantForm(forms.Form):
|
|
259
21
|
field.queryset = field.queryset.filter(tenant=self.tenant)
|
260
22
|
|
261
23
|
|
262
|
-
class TenantModelForm(
|
24
|
+
class TenantModelForm(forms.ModelForm):
|
263
25
|
|
264
26
|
def __init__(self, *args, **kwargs):
|
265
27
|
self.tenant = get_tenant()
|
@@ -272,13 +34,6 @@ class TenantModelForm(One2ManyModelForm):
|
|
272
34
|
for field in fields_to_filter:
|
273
35
|
field.queryset = field.queryset.filter(tenant=self.tenant)
|
274
36
|
|
275
|
-
def save_o2m(self):
|
276
|
-
o2m_fields = self.o2m_fields.keys()
|
277
|
-
for field in o2m_fields:
|
278
|
-
for item in self.cleaned_data.get(field, []):
|
279
|
-
item.update({'tenant': self.tenant})
|
280
|
-
return super().save_o2m()
|
281
|
-
|
282
37
|
def save(self, commit=True):
|
283
38
|
super().save(commit=False)
|
284
39
|
if not self.instance.pk or not self.instance.tenant:
|
@@ -287,6 +42,4 @@ class TenantModelForm(One2ManyModelForm):
|
|
287
42
|
if commit:
|
288
43
|
self.instance.save()
|
289
44
|
self.save_m2m()
|
290
|
-
self.save_o2m()
|
291
|
-
|
292
45
|
return self.instance
|
accrete/managers.py
CHANGED
@@ -7,8 +7,7 @@ class TenantManager(models.Manager, AnnotationManagerMixin):
|
|
7
7
|
|
8
8
|
def get_queryset(self):
|
9
9
|
queryset = super().get_queryset()
|
10
|
-
tenant
|
11
|
-
if tenant:
|
10
|
+
if tenant := get_tenant():
|
12
11
|
queryset = queryset.filter(tenant=tenant)
|
13
12
|
return queryset.annotate(**self.get_annotations())
|
14
13
|
|
accrete/utils/__init__.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
from . import dates
|
2
|
-
from .forms import save_form
|
3
|
-
from .http import filter_from_querystring
|
2
|
+
from .forms import save_form, save_forms, inline_vals_from_post, extend_formset
|
3
|
+
from .http import filter_from_querystring, cast_param
|
4
4
|
from .models import get_related_model
|
accrete/utils/forms.py
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
+
import re
|
1
2
|
import logging
|
2
3
|
from uuid import uuid4
|
4
|
+
from typing import Type
|
3
5
|
from django.db import transaction
|
6
|
+
from django.forms import BaseFormSet
|
4
7
|
|
5
8
|
_logger = logging.getLogger(__name__)
|
6
9
|
|
@@ -22,3 +25,79 @@ def save_form(form, reraise=False):
|
|
22
25
|
if reraise:
|
23
26
|
raise e
|
24
27
|
return form
|
28
|
+
|
29
|
+
|
30
|
+
def save_forms(form, inline_formsets: list = None, reraise: bool = False):
|
31
|
+
|
32
|
+
def handle_error(error):
|
33
|
+
form.save_error = repr(error)
|
34
|
+
error_id = str(uuid4())[:8]
|
35
|
+
_logger.exception(f'{error_id}: {error}')
|
36
|
+
form.save_error_id = error_id
|
37
|
+
|
38
|
+
form.is_saved = False
|
39
|
+
form.save_error = None
|
40
|
+
form.save_error_id = None
|
41
|
+
form.inline_forms = inline_formsets
|
42
|
+
|
43
|
+
try:
|
44
|
+
form.is_valid()
|
45
|
+
inlines_valid = all([
|
46
|
+
inline_formset.is_valid() for inline_formset in inline_formsets
|
47
|
+
])
|
48
|
+
except Exception as e:
|
49
|
+
handle_error(e)
|
50
|
+
if reraise:
|
51
|
+
raise e
|
52
|
+
return form
|
53
|
+
|
54
|
+
if not form.is_valid() or not inlines_valid:
|
55
|
+
return form
|
56
|
+
|
57
|
+
try:
|
58
|
+
with transaction.atomic():
|
59
|
+
form.save()
|
60
|
+
for inline_formset in inline_formsets:
|
61
|
+
inline_formset.save()
|
62
|
+
except Exception as e:
|
63
|
+
handle_error(e)
|
64
|
+
if reraise:
|
65
|
+
raise e
|
66
|
+
return form
|
67
|
+
|
68
|
+
form.is_saved = True
|
69
|
+
return form
|
70
|
+
|
71
|
+
|
72
|
+
def inline_vals_from_post(post: dict, prefix: str) -> list[dict]:
|
73
|
+
post_keys = set(re.findall(f'{prefix}-[0-9]+', ', '.join(post.keys())))
|
74
|
+
initial_data = {
|
75
|
+
post_key: {}
|
76
|
+
for post_key in post_keys if not post.get(f'{post_key}-DELETE')
|
77
|
+
}
|
78
|
+
for key, val in post.items():
|
79
|
+
post_key = '-'.join(key.split('-')[:-1])
|
80
|
+
if post_key not in initial_data:
|
81
|
+
continue
|
82
|
+
field_name = key.split('-')[-1]
|
83
|
+
initial_data[post_key].update({field_name: val})
|
84
|
+
return [val for val in initial_data.values()]
|
85
|
+
|
86
|
+
|
87
|
+
def extend_formset(formset_class, post: dict, data: list[dict]|dict, **formset_kwargs) -> Type[BaseFormSet]:
|
88
|
+
formset = formset_class(post, **formset_kwargs)
|
89
|
+
if not formset.is_valid():
|
90
|
+
return formset
|
91
|
+
form_data = post.copy()
|
92
|
+
if isinstance(data, dict):
|
93
|
+
data = [data]
|
94
|
+
prefix = formset_kwargs.get('prefix', 'form')
|
95
|
+
total = int(form_data[f'{prefix}-TOTAL_FORMS']) - 1
|
96
|
+
for item in data:
|
97
|
+
total += 1
|
98
|
+
form_data.update({f'{prefix}-{total}-{key}': value for key, value in item.items()})
|
99
|
+
form_data[f'{prefix}-TOTAL_FORMS'] = total + 1
|
100
|
+
formset = formset_class(form_data, **formset_kwargs)
|
101
|
+
for form in formset:
|
102
|
+
form._errors = {}
|
103
|
+
return formset
|