accrete 0.0.53__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 +59 -8
- accrete/contrib/ui/filter.py +5 -5
- accrete/contrib/ui/static/css/accrete.css +20 -4
- accrete/contrib/ui/static/css/accrete.css.map +1 -1
- accrete/contrib/ui/static/css/accrete.scss +34 -4
- accrete/contrib/ui/static/js/ui.js +26 -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 +11 -24
- accrete/contrib/ui/templates/ui/list.html +2 -0
- accrete/contrib/ui/templates/ui/partials/filter.html +32 -23
- accrete/contrib/ui/templates/ui/partials/form_errors.html +33 -20
- accrete/contrib/ui/templates/ui/table.html +2 -0
- 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.53.dist-info → accrete-0.0.55.dist-info}/METADATA +1 -1
- {accrete-0.0.53.dist-info → accrete-0.0.55.dist-info}/RECORD +24 -23
- {accrete-0.0.53.dist-info → accrete-0.0.55.dist-info}/WHEEL +1 -1
- accrete/contrib/ui/static/js/list.js +0 -146
- {accrete-0.0.53.dist-info → accrete-0.0.55.dist-info}/licenses/LICENSE +0 -0
@@ -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,7 +18,11 @@
|
|
18
18
|
{% block htmx %}
|
19
19
|
<script src="{% static "js/htmx.min.js" %}" defer type="text/javascript"></script>
|
20
20
|
{% endblock %}
|
21
|
-
|
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>
|
23
|
+
{% block script %}
|
24
|
+
<script src="{% static "js/ui.js" %}" defer type="text/javascript"></script>
|
25
|
+
{% endblock %}
|
22
26
|
{% block title_tag %}<title>{% block title %}{{ title }}{% endblock %}</title>{% endblock %}
|
23
27
|
{% endblock %}
|
24
28
|
</head>
|
@@ -60,7 +64,7 @@
|
|
60
64
|
<div class="navbar-item has-dropdown is-hoverable" onclick="this.classList.toggle('is-active');">
|
61
65
|
<a class="navbar-link is-arrowless {% if request.member.name %}is-size-7 has-text-centered-desktop{% endif %}">
|
62
66
|
{% if request.member.name %}
|
63
|
-
{{ request.member }}
|
67
|
+
{{ request.member }}
|
64
68
|
{% else %}
|
65
69
|
{{ user }}
|
66
70
|
{% endif %}
|
@@ -75,7 +79,7 @@
|
|
75
79
|
</nav>
|
76
80
|
<div id="tenant-quick-switch" class="navbar-brand-dropdown is-invisible box py-3 px-0 is-flex is-flex-direction-column" style="min-width: 200px">
|
77
81
|
{% for tenant in request.user.all_tenants %}
|
78
|
-
<a class="navbar-brand-dropdown-item px-4 py-1 mb-1" href="?tenant_id={{ tenant.id }}">
|
82
|
+
<a class="navbar-brand-dropdown-item px-4 py-1 mb-1" href="{% block tenant_switch_url %}/{% endblock %}?tenant_id={{ tenant.id }}">
|
79
83
|
<span class=""
|
80
84
|
onclick="document.getElementById('tenant-quick-switch').classList.toggle('is-invisible')"
|
81
85
|
>
|
@@ -103,7 +107,7 @@
|
|
103
107
|
<div id="panel-actions">
|
104
108
|
{% for action in actions %}
|
105
109
|
{% if action.actions %}
|
106
|
-
<div class="dropdown" style="width: 100%">
|
110
|
+
<div class="dropdown" style="width: 100%" onclick="setDropDown(this)">
|
107
111
|
<div class="dropdown-trigger" style="width: 100%">
|
108
112
|
<div class="side-panel-action" style="width: 292px">
|
109
113
|
<button class="button side-panel-action-content" aria-haspopup="true" style="width: 100%">
|
@@ -196,7 +200,9 @@
|
|
196
200
|
<div class="is-flex-grow-1" style="overflow-x: auto">
|
197
201
|
<div class="is-flex is-flex-direction-column" style="height: 100%; overflow: hidden">
|
198
202
|
<div>
|
199
|
-
{%
|
203
|
+
{% block header %}
|
204
|
+
{% include 'ui/partials/header.html' %}
|
205
|
+
{% endblock %}
|
200
206
|
</div>
|
201
207
|
<div id="content" class="px-3 pb-4" style="overflow: auto; flex-grow: 1">
|
202
208
|
{% block messages %}{% endblock %}
|
@@ -245,23 +251,4 @@
|
|
245
251
|
|
246
252
|
<div id="form-modal" class="modal"></div>
|
247
253
|
{% endblock %}
|
248
|
-
<script>
|
249
|
-
document.querySelectorAll('.dropdown').forEach(dropdown => {
|
250
|
-
dropdown.addEventListener('click', setDropDown);
|
251
|
-
})
|
252
|
-
|
253
|
-
function setDropDown(event) {
|
254
|
-
console.log(event.target, this)
|
255
|
-
const dropdownMenu = this.querySelector('.dropdown-menu');
|
256
|
-
document.querySelectorAll('.dropdown').forEach(dropdown => {
|
257
|
-
if (dropdown !== this) {
|
258
|
-
dropdown.classList.remove('is-active');
|
259
|
-
}
|
260
|
-
else if (dropdownMenu.contains(event.target)) {}
|
261
|
-
else if (dropdown === this) {
|
262
|
-
dropdown.classList.toggle('is-active');
|
263
|
-
}
|
264
|
-
})
|
265
|
-
}
|
266
|
-
</script>
|
267
254
|
</body>
|
@@ -7,7 +7,7 @@
|
|
7
7
|
<script src="{% static "js/filter.js" %}" defer type="text/javascript"></script>
|
8
8
|
|
9
9
|
<div id="query-apply" hx-get="" hx-trigger="click" hx-replace-url="true"
|
10
|
-
hx-select-oob="#content,#list-pagination,#query-apply">
|
10
|
+
hx-select-oob="#content,#list-pagination,#query-apply,#panel-actions">
|
11
11
|
</div>
|
12
12
|
|
13
13
|
<div id="query-tags" class="is-flex-direction-column is-flex-grow-1 is-multiline mb-1"
|
@@ -31,7 +31,7 @@
|
|
31
31
|
</div>
|
32
32
|
</div>
|
33
33
|
|
34
|
-
<div id="query-input-fieldset" class="field has-addons pt-1 mb-0">
|
34
|
+
<div id="query-input-fieldset" class="field has-addons pt-1 mb-0 mt-1">
|
35
35
|
<p id="query-input-control" class="control is-expanded">
|
36
36
|
<input id="query-input"
|
37
37
|
type="text"
|
@@ -55,20 +55,39 @@
|
|
55
55
|
{{ filter.to_html.params }}
|
56
56
|
</div>
|
57
57
|
|
58
|
-
<div id="list-customization" style="display: flex; flex-direction:
|
59
|
-
|
60
|
-
<div
|
61
|
-
|
62
|
-
<
|
63
|
-
|
58
|
+
<div id="list-customization" style="display: flex; flex-direction: column; flex-wrap: nowrap">
|
59
|
+
<div class="mt-2">
|
60
|
+
<div class="field has-addons">
|
61
|
+
{% if field_selection %}
|
62
|
+
<div class="control is-expanded">
|
63
|
+
<button id="list-customization-fields-trigger"
|
64
|
+
class="button dropdown-button side-panel-action-content is-fullwidth"
|
65
|
+
onclick="setDropDown(this)" data-dropdown-id="list-customization-fields"
|
66
|
+
>
|
67
|
+
<span>{% translate 'Fields' %}</span>
|
68
|
+
<span class="icon is-small">
|
69
|
+
<i class="fa fa-angle-down"></i>
|
70
|
+
</span>
|
71
|
+
</button>
|
72
|
+
</div>
|
73
|
+
{% endif %}
|
74
|
+
<div class="control is-expanded">
|
75
|
+
<button id="list-customization-order-trigger"
|
76
|
+
class="button dropdown-button side-panel-action-content is-fullwidth"
|
77
|
+
onclick="setDropDown(this)" data-dropdown-id="list-customization-order"
|
64
78
|
>
|
65
|
-
<span>{% translate '
|
79
|
+
<span>{% translate 'Order' %}</span>
|
66
80
|
<span class="icon is-small">
|
67
|
-
<i class="
|
81
|
+
<i class="fa fa-angle-down"></i>
|
68
82
|
</span>
|
69
83
|
</button>
|
70
84
|
</div>
|
71
|
-
|
85
|
+
</div>
|
86
|
+
</div>
|
87
|
+
|
88
|
+
{% if field_selection %}
|
89
|
+
<div id="list-customization-fields" class="dropdown is-flex-grow-1 pt-0" role="menu" data-dropdown-trigger-id="list-customization-fields-trigger">
|
90
|
+
<div class="dropdown-menu pt-0" style="width: 100%">
|
72
91
|
<div class="dropdown-content">
|
73
92
|
<div class="dropdown-item">
|
74
93
|
<div class="field has-addons mr-1">
|
@@ -95,18 +114,8 @@
|
|
95
114
|
</div>
|
96
115
|
{% endif %}
|
97
116
|
|
98
|
-
<div id="order
|
99
|
-
<div class="dropdown-
|
100
|
-
<button class="button side-panel-action-content" aria-haspopup="true"
|
101
|
-
style="{% if field_selection %} border-top-left-radius: 0; border-bottom-left-radius: 0; {% endif %}">
|
102
|
-
<span>{% translate 'Order' %}</span>
|
103
|
-
<span class="icon is-small">
|
104
|
-
<i class="fas fa-angle-down" aria-hidden="true"></i>
|
105
|
-
</span>
|
106
|
-
</button>
|
107
|
-
</div>
|
108
|
-
<div id="order-paths" class="dropdown-menu pt-0" role="menu" style="{% if field_selection %} left: -146px;{% endif %}"
|
109
|
-
>
|
117
|
+
<div id="list-customization-order" class="dropdown is-flex-grow-1 pt-0" role="menu" data-dropdown-trigger-id="list-customization-order-trigger">
|
118
|
+
<div class="dropdown-menu pt-0" style="width: 100%">
|
110
119
|
<div class="dropdown-content">
|
111
120
|
<div class="dropdown-item">
|
112
121
|
<div class="field has-addons mr-1">
|
@@ -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>
|
@@ -3,6 +3,8 @@
|
|
3
3
|
{% load i18n %}
|
4
4
|
{% load accrete_ui %}
|
5
5
|
|
6
|
+
{% block tenant_switch_url %}{% endblock %}
|
7
|
+
|
6
8
|
{% block content %}
|
7
9
|
<table id="content-table" class="table is-fullwidth is-hoverable has-box" hx-indicator=".htmx-indicator">
|
8
10
|
<thead style="position: sticky; top: 0; z-index: 10">
|
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
|
|