Skip to content

Commit 59ec87b

Browse files
committed
method copy for StreamObject
1 parent 2e2db63 commit 59ec87b

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,18 @@ def save(self, *args, **kwargs):
435435
```
436436
...and use this field in your html
437437

438+
### Create a copy
439+
StreamObject have a method 'copy', which create a copies of all the instances that used in this field.
440+
For example, if you have object 'page' with the field 'stream', and you need a copy of this object. You can do this:
441+
442+
```python
443+
page.pk = None
444+
page.stream = page.stream.copy()
445+
page.save()
446+
```
447+
> Note: If you will not use `page.stream.copy()` instances will be the same as in the original object
448+
449+
438450
## Settings
439451
```python
440452
# settings.py

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="django-streamfield",
8-
version="1.4.3",
8+
version="1.4.4",
99
author="Yury Lapshinov",
1010
author_email="y.raagin@gmail.com",
1111
description="StreamField for native Django Admin or with Grappelli",

streamfield/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
name = "streamfield"
2-
VERSION = '1.4.3'
2+
VERSION = '1.4.4'

streamfield/base.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from uuid import uuid4
23
from django.utils.functional import cached_property
34
from django.utils.html import format_html_join
45
from django.template import loader
@@ -123,10 +124,17 @@ def from_json(self):
123124
else:
124125
return self.value
125126

127+
def copy(self):
128+
return StreamObject(
129+
value=json.dumps(self._iterate_over_models(_copy)),
130+
model_list=self.model_list
131+
)
132+
126133
@cached_property
127134
def to_json(self):
128135
return self.value
129136

137+
130138
def _get_block_tmpl(model_class, model_str):
131139
if hasattr(model_class, 'block_template'):
132140
return model_class.block_template
@@ -168,6 +176,39 @@ def _get_data_list(model_class, model_str, content, ctx):
168176
'template': _get_block_tmpl(model_class, model_str)
169177
}
170178

179+
def _copy(model_class, model_str, content, ctx):
180+
as_list = ctx['as_list']
181+
if as_list:
182+
id = []
183+
for obj in content:
184+
obj.pk = None
185+
obj.save()
186+
id.append(obj.pk)
187+
else:
188+
content.pk = None
189+
content.save()
190+
id = content.pk
191+
192+
# check if have subblocks
193+
changed = False
194+
for f in content._meta.fields:
195+
# isinstance(f, StreamField)
196+
if hasattr(f, 'model_list'):
197+
changed = True
198+
value = getattr(content, f.name)
199+
# value is StreamObject
200+
new_value = value.copy()
201+
setattr(content, f.name, new_value)
202+
if changed:
203+
content.save()
204+
205+
return dict(
206+
unique_id=uuid4().hex[:6],
207+
model_name=model_str,
208+
id=id,
209+
options=ctx['options']
210+
)
211+
171212
def get_streamblocks_models():
172213
streamblock_models = []
173214

0 commit comments

Comments
 (0)