Skip to content

Commit

Permalink
menu reload when login, permission check actions
Browse files Browse the repository at this point in the history
  • Loading branch information
szuprefix committed Nov 22, 2019
1 parent adab292 commit 09d5d3a
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-django",
"version": "0.6.1",
"version": "0.6.2",
"description": "个人实验项目, 本框架的目标是借鉴并超越django admin的自动化思想, 实现UI前端的极简快速定制开发",
"main": "index.js",
"files": [
Expand Down
48 changes: 37 additions & 11 deletions src/components/layout/Actions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
<template v-for="a in dropdownActions">
<el-dropdown-item :command="a.do" v-if="!a.show || a.show()" :key="a.name" :title="a.title"
:icon="`fa fa-${a.icon}`">
{{a.label||a.title}}
{{a.label || a.title}}
</el-dropdown-item>
</template>
</el-dropdown-menu>
</el-dropdown>
</el-button-group>
</template>
<script>
import array_normalize from '../../utils/array_normalize'
export default{
props: {
items: Array,
Expand All @@ -32,26 +33,46 @@
return {}
}
},
permissionFunction: Function,
size: {type: String, default: "small"}
},
data () {
return {}
return {
_items: []
}
},
components: {},
created () {
this.normalizeItems()
},
methods: {
handleCommand (command) {
command(this.context)
}
},
normalizeItem(a)
{
if(a instanceof Array) {
return array_normalize(a, this.map, this.normalizeItem)
}
if (!a.show && this.permissionFunction && a.permission) {
a.show = () => this.permissionFunction(a.permission)
// console.log(a)
}
return a
},
normalizeItems() {
this._items = array_normalize(this.items, this.map, this.normalizeItem)
},
},
computed: {
_items (){
return this.items.map(a => {
if (typeof a === 'string') {
a = this.map[a]
}
return a
})
},
// _items (){
// return this.items.map(a => {
// if (typeof a === 'string') {
// a = this.map[a]
// }
// return a
// })
// },
showActions () {
return this._items.filter((a) => {
return !(a instanceof Array)
Expand All @@ -64,6 +85,11 @@
return !a.show || a.show()
})
}
},
watch: {
items () {
this.normalizeItems()
}
}
}
</script>
11 changes: 11 additions & 0 deletions src/components/layout/SideBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<script>
import {mapState} from 'vuex'
import menus from '@/configs/menus'
import {reload} from '@/configs/menus'
import {genMenusFromApps} from 'vue-django/src/configs/menus'
export default {
computed: mapState(['user']),
Expand All @@ -30,6 +32,15 @@
menus,
defaultOpeneds: ['0']
}
},
methods : {
reload () {
this.menu = Object.assign({}, reload(this.user.model_permissions))
console.log(this.menu)
}
},
created () {
this.$store.state.bus.$on('get-user-info', this.reload)
}
}
</script>
Expand Down
10 changes: 6 additions & 4 deletions src/components/model/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
title: '保存',
label: '',
do: this.onSubmit,
show: () => this.checkPermission('create') || this.checkPermission('update'),
type: 'primary'
},
'refresh': {
Expand All @@ -77,13 +78,14 @@
label: '',
do: this.toDelete,
type: 'danger',
show: () => this.mid && this.checkPermission('delete')
show: () => this.mid && this.checkPermission('destroy')
},
'saveAndAnother': {
icon: 'floppy-o',
label: '+',
title: '保存并新增另一个',
do: this.saveAndAnother
do: this.saveAndAnother,
show: () => this.checkPermission('create') || this.checkPermission('update')
}
}
}
Expand Down Expand Up @@ -126,7 +128,7 @@
}).catch(() => {
return {}
}).then(config => {
return config.items || this.model.config.formItems || Object.values(this.model.options.actions.POST).filter(a => a.read_only !== true)
return config.items || Object.values(this.model.options.actions.POST).filter(a => a.read_only !== true)
})
},
Expand Down Expand Up @@ -157,7 +159,7 @@
clear () {
this.model.clear()
this.mid = this.model.id = undefined
this.formValue = Object.assign({}, this.model.data)
this.formValue = Object.assign({}, this.model.data, this.defaults)
this.$nextTick(this.$refs.form.$refs.form.clearValidate)
},
onDelete(){
Expand Down
4 changes: 2 additions & 2 deletions src/components/model/MultiCreator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
return {}
}
},
pk: {type: String, default: 'code'}
pk: {type: String}
},
data () {
return {
Expand Down Expand Up @@ -170,7 +170,7 @@
},
postOne(){
let qi = this.queueIndex
let pkn = this.pk || this.viewConfig.pk
let pkn = this.pk || this.viewConfig.pk || 'code'
if (qi < this.records.length) {
let a = Object.assign({}, this.defaults, this.records[qi])
this.translateForeignKey(a)
Expand Down
57 changes: 23 additions & 34 deletions src/components/model/Table.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div>
<search v-model="search" :options="searchOptions" :model="model" :exclude="_baseQueries" ref="search"
<search v-model="search" :model="model" :exclude="_baseQueries" ref="search"
@change="onSearch"></search>
<batch-actions :items="batchActionItems" :count="selection.length" @done="refresh" :context="{selection}"
v-if="batchActionItems.length>0"></batch-actions>
Expand Down Expand Up @@ -78,7 +78,7 @@
icon: 'plus-square',
label: '添加',
do: this.addToParent,
show: () => this.checkPermission('create', this.parent)
show: () => this.checkPermission('create') && this.checkPermission('update', this.parent)
},
'edit': {
icon: 'pencil',
Expand All @@ -91,7 +91,7 @@
title: '删除',
do: this.toDeleteModel,
type: 'danger',
show: () => this.checkPermission('delete')
show: () => this.checkPermission('destroy')
},
'removeFromParent': {
icon: 'trash',
Expand Down Expand Up @@ -120,10 +120,8 @@
},
methods: {
init () {
this.model.loadOptions().then((data) => {
let search = this.model.options.actions.SEARCH
this.model.loadOptionsAndViewsConfig().then((data) => {
this.$refs.search.init()
// this.orderingFields = search.ordering_fields
return this.normalizeItems()
}).then(() => {
this.parentQueries = Object.assign({}, this.getParentQueries())
Expand Down Expand Up @@ -234,19 +232,13 @@
})
},
getConfig () {
return import(`@/views${this.model.getListUrl()}config.js`).then(m => {
return m.default.list
}).catch(() => {
return {}
}).then(config => {
let listItems = this.items || config.items || this.model.config.listItems || Object.values(this.model.fieldConfigs).filter(a => ['name', 'title'].includes(a.name))
if (!listItems || listItems.length === 0) {
listItems = ['__str__']
}
let batchActions = this.batchActions || config.batchActions || this.model.config.batchActions || []
return {listItems, batchActions}
})
let config = this.model.viewsConfig.list || {}
let listItems = this.items || config.items || Object.values(this.model.fieldConfigs).filter(a => ['name', 'title'].includes(a.name))
if (!listItems || listItems.length === 0) {
listItems = ['__str__']
}
let batchActions = this.batchActions || config.batchActions || []
return Promise.resolve({listItems, batchActions})
},
defaultWidget(f){
if (f.model) {
Expand Down Expand Up @@ -298,17 +290,14 @@
if (f && f.multiple !== true) {
r[f.name] = pid
} else {
f = fs.find(a => a.model === 'contenttypes.contenttype')
if (f) {
r[f.name] = this.parent.options.content_type_id
let idf = `${f.name.replace('_type', '_id')}` // todo: 要更严谨些
if (!this.model.fieldConfigs[idf]) {
idf = 'object_id'
}
if(!this.model.fieldConfigs[idf] ){
throw Error('genric foreign key id field not found.')
let popt = this.model.options
if (popt.generic_foreign_key) {
let {ct_field, fk_field} = popt.generic_foreign_key
r[ct_field] = this.parent.options.content_type_id
if (!this.model.fieldConfigs[fk_field]) {
throw Error(`genric foreign key id_field:${id_field} not found.`)
}
r[idf] = pid
r[fk_field] = pid
}
}
Expand Down Expand Up @@ -340,23 +329,23 @@
}
})
}
return mergeOptions({
let dopt = this.model.viewsConfig.list
dopt = dopt && dopt.options && dopt.options.remoteTable || {}
return mergeOptions(mergeOptions({
table: {
avairableActions: {...this.avairableActions, ...bactions},
excelFormat: this.excelFormat,
topActions: ['refresh', 'create', ['download'].concat(Object.keys(bactions))],
rowActions: ['edit', [this.parentMultipleRelationField ? 'removeFromParent' : 'delete']],
dblClickAction: 'edit',
permissionFunction: this.checkPermission,
elTable: {
onSelectionChange: this.onSelectionChange
},
title: this.model.config.verbose_name
}
}, this.options.remoteTable)
}, dopt), this.options.remoteTable)
},
// parentQueries () {
// return this.getParentQueries()
// },
_baseQueries () {
return Object.assign({}, this.parentQueries, this.baseQueries)
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/table/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<table-column :field="f" v-for="f in _items" :key="f.name"></table-column>
<el-table-column label="" align="right" v-if="rowActions || topActions">
<template slot="header" slot-scope="scope" v-if="topActions">
<actions :items="_topActions" :context="scope" :map="avairableActions"></actions>
<actions :items="_topActions" :context="scope" :permissionFunction="options.permissionFunction" :map="avairableActions"></actions>
</template>
<template slot-scope="scope" v-if="rowActions">
<actions :items="_rowActions" :context="scope" class="hover-show" trigger="hover"
Expand Down
10 changes: 8 additions & 2 deletions src/configs/menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
course:null
}
*/
export function genMenusFromApps(apps, menus) {
export function genMenusFromApps(apps, menus, modelPermissions) {
if (!menus) {
menus = Object.keys(apps)
} else if (typeof menus == 'object') {
Expand All @@ -29,13 +29,19 @@ export function genMenusFromApps(apps, menus) {
let menu = m
if (typeof m == 'string') {
let model = app.models[m]
menu = {name: model.verbose_name, icon: model.icon || 'file', url: `/${a}/${m}/`, hidden:model.hidden}
let mn = `${a}.${m}`
let hidden = model.hidden
if(hidden === undefined && modelPermissions) {
hidden = (mn in modelPermissions)? false:true
}
menu = {name: model.verbose_name, icon: model.icon || 'file', url: `/${a}/${m}/`, hidden}
}
return menu
})
return {name: app.verbose_name, items, icon:app.icon || 'file', hidden:app.hidden}
})
}

let menus = {
items: []
}
Expand Down

0 comments on commit 09d5d3a

Please sign in to comment.