Skip to content

Commit f1d593a

Browse files
committed
2.0.4 Removed Lodash.js. Fixed Add New.
1 parent 90c3d0a commit f1d593a

11 files changed

Lines changed: 56 additions & 49 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.0.3
4+
Stable version: 2.0.4
55

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

frontend/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"license": "ISC",
1313
"devDependencies": {
1414
"@babel/preset-env": "^7.20.2",
15+
"@vue/compiler-sfc": "^3.2.47",
1516
"axios": "^1.3.4",
1617
"babel-loader": "^9.1.2",
1718
"clean-webpack-plugin": "^4.0.0",
@@ -20,7 +21,6 @@
2021
"file-loader": "^6.2.0",
2122
"html-webpack-plugin": "^5.5.0",
2223
"js-cookie": "^3.0.1",
23-
"lodash": "^4.17.21",
2424
"mini-css-extract-plugin": "^2.7.5",
2525
"sass": "^1.60.0",
2626
"sass-loader": "^13.2.2",
@@ -29,6 +29,7 @@
2929
"terser-webpack-plugin": "^5.3.7",
3030
"vue-loader": "^17.0.1",
3131
"vuedraggable": "^4.1.0",
32+
"webpack": "^5.77.0",
3233
"webpack-cli": "^5.0.1"
3334
}
3435
}

frontend/src/components/AbstractBlock.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
return '/streamfield/abstract-block/' + this.model_name_lower + '/';
2020
},
2121
getContent() {
22-
let block = _.find(this.$root.stream, ['unique_id', this.block.unique_id]);
22+
const block = this.$root.stream.find((o) => {return o.unique_id = this.block.unique_id})
2323
window.ax.get(this.render_url(this.block)).then((response) => {
2424
this.$root.blocks[this.block.model_name] = response.data;
2525
});

frontend/src/components/App.vue

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
11
<script type="text/javascript">
2-
import {isEmpty, isArray} from 'lodash'
3-
import draggable from 'vuedraggable'
2+
import draggable from 'vuedraggable'
3+
import {isArray, isEmpty} from '@/utils.js'
44
import StreamBlock from '@/components/StreamBlock.vue'
55
import AbstractBlock from '@/components/AbstractBlock.vue'
66
7-
let
8-
text_area,
9-
delete_blocks_from_db,
10-
popup_size;
7+
// const _ = require('lodash');
118
12-
function id_to_windowname(text) {
9+
let
10+
text_area,
11+
delete_blocks_from_db,
12+
popup_size;
13+
14+
function id_to_windowname(text) {
1315
text = text.replace(/\./g, '__dot__');
1416
text = text.replace(/\-/g, '__dash__');
1517
return text;
1618
}
1719
18-
export default {
19-
props: ['app_node'],
20-
components: {
20+
export default {
21+
props: ['app_node'],
22+
components: {
2123
draggable,
2224
StreamBlock,
2325
AbstractBlock
2426
},
2527
data () {
2628
return {
27-
blocks: {}, // save content of all instances
28-
show_help: false,
29-
show_add_block: false,
30-
will_removed: [], // blocks that will be removed from db
31-
stream: [], // [{model_name: ..., id: ...}, ...]
32-
model_info: {}, // {'model_name': model.__doc__},
29+
blocks: {}, // save content of all instances
30+
show_help: false,
31+
show_add_block: false,
32+
will_removed: [], // blocks that will be removed from db
33+
stream: [], // [{model_name: ..., id: ...}, ...]
34+
model_info: {}, // {'model_name': model.__doc__},
3335
base_admin_url: "",
34-
stream_texts: window.stream_texts
36+
stream_texts: window.stream_texts
3537
}
3638
},
3739
beforeMount: function() {
38-
text_area = this.app_node.querySelector('textarea')
39-
delete_blocks_from_db = Boolean(text_area.hasAttribute('delete_blocks_from_db'))
40-
popup_size = text_area.dataset.popup_size ? JSON.parse(text_area.dataset.popup_size) : [1000, 500]
40+
text_area = this.app_node.querySelector('textarea')
41+
delete_blocks_from_db = Boolean(text_area.hasAttribute('delete_blocks_from_db'))
42+
popup_size = text_area.dataset.popup_size ? JSON.parse(text_area.dataset.popup_size) : [1000, 500]
4143
4244
this.base_admin_url = text_area.getAttribute('base_admin_url')
43-
this.stream = JSON.parse(text_area.innerHTML)
44-
this.model_info = JSON.parse(text_area.getAttribute('model_list_info'))
45+
this.stream = JSON.parse(text_area.innerHTML)
46+
this.model_info = JSON.parse(text_area.getAttribute('model_list_info'))
4547
4648
if (this.stream.length && !this.stream[0].hasOwnProperty('collapsed')) {
4749
this.setAllCollapsed(false)
@@ -89,7 +91,7 @@
8991
isEmpty: isEmpty,
9092
isArray: isArray,
9193
setAllCollapsed: function(value) {
92-
_.forEach(this.stream, (v, k) => {
94+
this.stream.forEach((v, k) => {
9395
v.collapsed = value
9496
})
9597
},
@@ -102,7 +104,7 @@
102104
103105
},
104106
getBlockIndex: function(block_unique_id) {
105-
const block = _.find(this.stream, ['unique_id', block_unique_id])
107+
const block = this.stream.find(function(o) {return o.unique_id = block_unique_id})
106108
const index = this.stream.indexOf(block)
107109
return [index, block]
108110
},
@@ -124,7 +126,6 @@
124126
}
125127
}
126128
}
127-
128129
},
129130
deleteInstance: function(block_unique_id, instance_id) {
130131
let [block_index, block] = this.getBlockIndex(block_unique_id)
@@ -151,7 +152,7 @@
151152
let options = {};
152153
let new_block;
153154
154-
_.forEach(this.model_info[model_name].options, (option, key) => {
155+
Object.entries(this.model_info[model_name].options).forEach(([key, option],index) => {
155156
options[key] = option.default;
156157
});
157158
new_block = {
@@ -181,19 +182,19 @@
181182
},
182183
watch: {
183184
stream: {
184-
handler(nv) {
185-
text_area.innerHTML = JSON.stringify(nv.map(function(i){
186-
// return only fields that in initial data
187-
return {
185+
handler(nv) {
186+
text_area.innerHTML = JSON.stringify(nv.map(function(i){
187+
// return only fields that in initial data
188+
return {
188189
unique_id: i.unique_id,
189190
model_name: i.model_name,
190191
id: i.id,
191192
options: i.options,
192193
collapsed: i.collapsed
193194
};
194-
}));
195-
},
196-
deep: true
195+
}));
196+
},
197+
deep: true
197198
}
198199
}
199200
}
@@ -212,10 +213,10 @@
212213

213214
<div class="streamfield-models">
214215
<draggable v-model="stream" group="stream" handle=".block-move" item-key="unique_id">
215-
<template #item="{element: block}">
216-
<StreamBlock v-if="!isAbstract(block)" :block="block" :ref="block.unique_id"/>
216+
<template #item="{element: block}">
217+
<StreamBlock v-if="!isAbstract(block)" :block="block" :ref="block.unique_id"/>
217218
<AbstractBlock v-else :block="block" />
218-
</template>
219+
</template>
219220
</draggable>
220221
<div class="stream-insert-new-block">
221222
<div class="add-new-block-button" @click="show_add_block=!show_add_block" v-text="stream_texts['AddNewBlock']"></div>

frontend/src/components/BlockOptions.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script>
2-
import {isEmpty} from 'lodash'
32
export default {
43
props: ['block']
54
}

frontend/src/components/StreamBlock.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
components: { draggable, BlockOptions, BlockHeader, AddBlockHere },
1414
beforeMount: function() {
15-
if (_.isArray(this.block.id)) {
15+
if (this.$root.isArray(this.block.id)) {
1616
for (var j = this.block.id.length - 1; j >= 0; j--) {
1717
this.updateBlock(this.block.id[j].toString());
1818
}
@@ -37,7 +37,7 @@
3737
'&app_id=' + this.$root.app_node.id;
3838
},
3939
getBlockTitle: function(block, item_id) {
40-
if (!_.isNumber(item_id) && _.isNumber(item_id[0])) {
40+
if (!Number.isInteger(item_id) && Number.isInteger(item_id[0])) {
4141
item_id = item_id[0]
4242
}
4343
let block_data = this.$root.blocks[this.instance_unique_id(block, item_id)]
@@ -62,12 +62,12 @@
6262
6363
// if added new instance – add new instance id to block list
6464
let instance_id_int = parseInt(instance_id)
65-
if (_.isArray(this.block.id) && this.block.id.indexOf(instance_id_int) == -1) {
65+
if (this.$root.isArray(this.block.id) && this.block.id.indexOf(instance_id_int) == -1) {
6666
this.block.id.push(instance_id_int);
6767
}
6868
6969
// if added new instance to block without list
70-
if ( !_.isArray(this.block.id) && this.block.id == -1 ) {
70+
if ( !this.$root.isArray(this.block.id) && this.block.id == -1 ) {
7171
this.block.id = instance_id_int;
7272
}
7373
@@ -143,7 +143,7 @@
143143
<template v-else>
144144
<a class="stream-btn"
145145
:id="'add_id_' + block.unique_id"
146-
title="$root.stream_texts['AddContent']"
146+
:title="$root.stream_texts['AddContent']"
147147
:href="get_add_model_link(block)"
148148
@click.prevent="$root.openPopup"
149149
v-text="'+' + $root.stream_texts['AddContent']"

frontend/src/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const isArray = Array.isArray;
2+
const isEmpty = obj => [Object, Array].includes((obj || {}).constructor) && !Object.entries((obj || {})).length;
3+
4+
export {isArray, isEmpty}

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="2.0.3",
8+
version="2.0.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 = "2.0.3"
2+
VERSION = "2.0.4"

streamfield/fields.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ class Media:
5757
# form field
5858
class StreamFormField(forms.JSONField):
5959
def prepare_value(self, value):
60-
return super().prepare_value(value.value)
60+
if isinstance(value, StreamObject):
61+
value = value.value
62+
return super().prepare_value(value)
6163

6264
# main field
6365
class StreamField(Field):

0 commit comments

Comments
 (0)