Skip to content

Commit 4dadc20

Browse files
committed
add to stream object method
1 parent dcdaa7b commit 4dadc20

7 files changed

Lines changed: 39 additions & 20 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Django StreamField
22

33
This is a simple realisation of StreamField's idea of Wagtail CMS for plain Django admin or with Grappelli skin.
4-
Stable version: 2.1.0
4+
Stable version: 2.1.1
55
Django <= 4.2.1
66

77
[Major changes (1.4.5 > 2)](changes2.0.md)

frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "streamfield_widget.js",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"description": "",
55
"private": true,
66
"scripts": {

setup.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="django-streamfield",
8-
version="2.1.0",
8+
version="2.1.1",
99
author="Yury Lapshinov",
1010
author_email="y.raagin@gmail.com",
1111
description="StreamField for native Django Admin or with Grappelli",
@@ -18,22 +18,17 @@
1818
python_requires=">=3.7",
1919
classifiers=[
2020
'Development Status :: 5 - Production/Stable',
21-
'Programming Language :: Python :: 3',
22-
'Framework :: Django :: 3.1',
23-
'Framework :: Django :: 3.2',
24-
'Framework :: Django :: 4.1',
2521
'License :: OSI Approved :: BSD License',
2622
'Operating System :: OS Independent',
2723
'Programming Language :: Python',
2824
'Programming Language :: Python :: 3',
29-
'Programming Language :: Python :: 3.7',
30-
'Programming Language :: Python :: 3.8',
3125
'Programming Language :: Python :: 3.9',
3226
'Programming Language :: Python :: 3.10',
3327
"Programming Language :: Python :: 3.11",
3428
"Framework :: Django",
3529
"Framework :: Django :: 3.2",
3630
"Framework :: Django :: 4.0",
3731
"Framework :: Django :: 4.1",
32+
"Framework :: Django :: 4.2",
3833
],
3934
)

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 = "2.1.0"
2+
VERSION = "2.1.1"

streamfield/base.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ def copy(self):
137137
def to_json(self):
138138
return json.dumps(self.value)
139139

140+
def add(self, block):
141+
model_class = block.__class__
142+
options = _get_default_options(model_class)
143+
self.value.append({
144+
"id": block.id,
145+
"options": options,
146+
"unique_id": uuid4().hex[:6],
147+
"model_name": model_class.__name__
148+
})
149+
140150
@cached_property
141151
def help_text(self):
142152
return ADMIN_HELP_TEXT
@@ -197,6 +207,14 @@ def _check_subblocks(obj):
197207
if changed:
198208
obj.save()
199209

210+
def _get_default_options(model_class):
211+
options = model_class.options if hasattr(model_class, "options") else BLOCK_OPTIONS
212+
if hasattr(model_class, "extra_options"):
213+
options = deepcopy(options)
214+
options.update(model_class.extra_options)
215+
options = { k: v['default'] for k, v in options.items() if bool(v.get('default')) }
216+
return options
217+
200218
def _copy(model_class, model_str, content, ctx):
201219
as_list = ctx['as_list']
202220
resp = dict(
@@ -249,11 +267,7 @@ def migrate_stream_options(stream_obj):
249267
stream_dict = stream_obj.from_json()
250268
for b in stream_dict:
251269
model_class = get_model_by_string(b['model_name'])
252-
options = model_class.options if hasattr(model_class, "options") else BLOCK_OPTIONS
253-
if hasattr(model_class, "extra_options"):
254-
options = deepcopy(options)
255-
options.update(model_class.extra_options)
256-
options = { k: v['default'] for k, v in options.items() if bool(v.get('default')) }
270+
options = _get_default_options(model_class)
257271
options.update(b['options'])
258272
b['options'] = options
259273
return StreamObject(

test_project/requirements.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

test_project/test_project/settings.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@
2929
ALLOWED_HOSTS = []
3030

3131

32+
STREAMFIELD_BLOCK_OPTIONS = {
33+
'margin_bottom': {
34+
'label': 'Bottom margin',
35+
'default': '1',
36+
'type': 'select',
37+
'options': [
38+
{'value': '', 'name': 'No'},
39+
{'value': '1', 'name': 'x1'},
40+
{'value': '2', 'name': 'x2'},
41+
{'value': 'auto', 'name': 'Auto'},
42+
]
43+
}
44+
}
45+
46+
3247
# Application definition
3348

3449
INSTALLED_APPS = [

0 commit comments

Comments
 (0)