Skip to content

Commit b49a06d

Browse files
committed
fix copy method for abstract object
1 parent f1d593a commit b49a06d

2 files changed

Lines changed: 35 additions & 35 deletions

File tree

frontend/src/streamfield_widget.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@ import Cookies from 'js-cookie'
44
import { createApp } from 'vue'
55
import App from '@/components/App.vue'
66

7-
(function(w, $){
7+
(function(){
88
function onReady() {
99
let streamfield_app = document.querySelectorAll('.streamfield_app');
10-
w.ax = axios.create({
10+
window.ax = axios.create({
1111
headers: {"X-CSRFToken": Cookies.get('csrftoken')}
1212
});
13-
w.streamapps = {};
13+
window.streamapps = {};
1414
for (let i = 0; i < streamfield_app.length; i++) {
1515
let app_node =streamfield_app[i];
1616
let app = createApp(App, {app_node}).mount(app_node.querySelector('.mount-node'));
17-
w.streamapps[streamfield_app[i].id] = app;
17+
window.streamapps[streamfield_app[i].id] = app;
1818
}
1919
};
20-
21-
w.addEventListener('DOMContentLoaded', function(event) {
20+
window.addEventListener('DOMContentLoaded', function(event) {
2221
onReady();
2322
});
24-
25-
})(window, django.jQuery);
23+
})();

streamfield/base.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class NumberInText(models.Model):
3636
3737
as_list = True
3838
39-
4039
# data in db
4140
value = [
4241
{
@@ -185,38 +184,41 @@ def _get_data_list(model_class, model_str, content, ctx):
185184
'template': _get_block_tmpl(model_class, model_str)
186185
}
187186

187+
def _check_subblocks(obj):
188+
# check if have subblocks
189+
changed = False
190+
for f in obj._meta.fields:
191+
# isinstance(f, StreamField)
192+
if hasattr(f, 'model_list'):
193+
changed = True
194+
value = getattr(obj, f.name)
195+
# value is StreamObject
196+
setattr(obj, f.name, value.copy())
197+
if changed:
198+
obj.save()
199+
188200
def _copy(model_class, model_str, content, ctx):
189201
as_list = ctx['as_list']
190-
if as_list:
191-
id = []
192-
for obj in content:
193-
obj.pk = None
194-
obj.save()
195-
id.append(obj.pk)
196-
else:
197-
content.pk = None
198-
content.save()
199-
id = content.pk
200-
201-
# check if have subblocks
202-
changed = False
203-
for f in content._meta.fields:
204-
# isinstance(f, StreamField)
205-
if hasattr(f, 'model_list'):
206-
changed = True
207-
value = getattr(content, f.name)
208-
# value is StreamObject
209-
new_value = value.copy()
210-
setattr(content, f.name, new_value)
211-
if changed:
212-
content.save()
213-
214-
return dict(
202+
resp = dict(
215203
unique_id=uuid4().hex[:6],
216204
model_name=model_str,
217-
id=id,
218205
options=ctx['options']
219206
)
207+
if not model_class._meta.abstract:
208+
if as_list:
209+
id = []
210+
for obj in content:
211+
obj.pk = None
212+
obj.save()
213+
id.append(obj.pk)
214+
_check_subblocks(obj)
215+
else:
216+
content.pk = None
217+
content.save()
218+
id = content.pk
219+
_check_subblocks(content)
220+
resp['id'] = id
221+
return resp
220222

221223
def get_streamblocks_models():
222224
streamblock_models = []

0 commit comments

Comments
 (0)