Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

825 add toggle to add/remove current site to/from element sites #875

Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4e64e34
Update version
jochenklar Dec 12, 2023
e734196
tests: add example users to core test_views
MyPyDavid Dec 14, 2023
fb5304e
chore: add new CanToggleElementCurrentSite permission class and rules
MyPyDavid Dec 14, 2023
8ff469b
chore: add ElementToggleCurrentSiteViewSetMixin class
MyPyDavid Dec 14, 2023
64f1fec
feat: #825 add toggle button for element.sites endpoints components a…
MyPyDavid Dec 14, 2023
0c01099
chore: refactor ElementToggleCurrentSiteViewSetMixin into ViewSet act…
MyPyDavid Feb 12, 2024
59ff5fa
chore: refactor endpoint to toggle-site
MyPyDavid Feb 12, 2024
55b8912
js: refactor toggle site put call in MultiSiteApi and storeElement el…
MyPyDavid Feb 12, 2024
4b0db2e
js: fix show/hide for ToggleCurrentSiteLink
MyPyDavid Feb 12, 2024
e4d525c
js: include toggleCurrentSite action in element components
MyPyDavid Feb 12, 2024
bf61f0b
js: remove console.log
MyPyDavid Feb 12, 2024
b55935c
chore: rename rule to toggle_site, remove is_element_editor and use s…
MyPyDavid Feb 12, 2024
2860398
chore: refactor storeElement signature
MyPyDavid May 15, 2024
25ed018
chore: add action to methods in Api classes and remove MultiSiteApi
MyPyDavid May 15, 2024
00d2218
chore: update element components with signature for storeElements and…
MyPyDavid May 15, 2024
9d38aa5
chore: rename to hasCurrentSite
MyPyDavid May 15, 2024
3f8a311
chore: add element-button class to ReadOnlyIcon for margin-left
MyPyDavid May 15, 2024
5998074
chore: only return super in CanToggleElementCurrentSite.has_permission
MyPyDavid May 15, 2024
4474043
chore: remove unused logger from management/rules
MyPyDavid May 15, 2024
c56f53b
chore: update signature for storeElement in edit components
MyPyDavid May 15, 2024
8757d2c
chore: update signature for storeElement to use default parameters
MyPyDavid May 16, 2024
c8855b9
chore: update signature for storeElement in EditQuestionSet
MyPyDavid May 16, 2024
995a968
js: update ToggleCurrentSiteLink icon and move placement of icon in e…
MyPyDavid May 21, 2024
f0cab0f
js revert: remove elementAction arg from storeElementInit
MyPyDavid May 21, 2024
aa387f9
tests: refactor core multisite test helpers into constants and utils
MyPyDavid May 21, 2024
54c7bad
tests: add locked parameter to update ultisite toggle tests
MyPyDavid May 22, 2024
9235c01
js: remove locked parameter from ToggleCurrentSiteLink component and …
MyPyDavid May 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion rdmo/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.1.2.dev1"
__version__ = "2.2.0.dev1"
11 changes: 11 additions & 0 deletions rdmo/core/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,14 @@ def has_permission(self, request, view):
return True
else:
return False


class CanToggleElementCurrentSite(DjangoModelPermissions):

perms_map = {
'PUT': ['%(app_label)s.change_%(model_name)s_toggle_site'],
}

@log_result
def has_permission(self, request, view):
return super().has_permission(request, view)
29 changes: 28 additions & 1 deletion rdmo/core/tests/__init__.py
jochenklar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@
'bar-user': 404, 'bar-reviewer': 403, 'bar-editor': 204,
'user': 404, 'example-reviewer': 403, 'example-editor': 204,
'anonymous': 401, 'reviewer': 403, 'editor': 204,
}
},
'toggle-site': {
# foo-editor is not permitted to apply own site(foo.com) in test run(example.com)
'foo-user': 403, 'foo-reviewer': 403, 'foo-editor': 403,
# bar-editor is not permitted to apply own site(bar.com) in test run(example.com)
'bar-user': 403, 'bar-reviewer': 403, 'bar-editor': 403,
'user': 403, 'example-reviewer': 403, 'example-editor': 200,
'anonymous': 401, 'reviewer': 403, 'editor': 200,
},
}


Expand Down Expand Up @@ -115,6 +123,25 @@
'example-reviewer': 404, 'example-editor': 404,
}
},
'toggle-site': {
'all-element': {
# foo-editor can not apply own site(foo.com) in test run(example.com)
'foo-reviewer': 403, 'foo-editor': 403,
# bar-editor can not apply own site(bar.com) in test run(example.com)
'bar-reviewer': 403, 'bar-editor': 403,
'example-reviewer': 403, 'example-editor': 200,
},
'foo-element': {
'foo-reviewer': 403, 'foo-editor': 403,
'bar-reviewer': 403, 'bar-editor': 403,
'example-reviewer': 403, 'example-editor': 200,
},
'bar-element': {
'foo-reviewer': 403, 'foo-editor': 403,
'bar-reviewer': 403, 'bar-editor': 403,
'example-reviewer': 403, 'example-editor': 200,
}
}
}


Expand Down
4 changes: 3 additions & 1 deletion rdmo/core/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
('reviewer', 'reviewer'),
('user', 'user'),
('api', 'api'),
('example-editor', 'example-editor'),
('example-reviewer', 'example-reviewer'),
)


Expand Down Expand Up @@ -57,7 +59,7 @@ def test_i18n_switcher(db, client):
def test_can_view_management(db, client, username, password):
client.login(username=username, password=password)
response = client.get(reverse('management'))
if username in ('editor', 'reviewer', 'api'):
if username in ('editor', 'reviewer', 'api', 'example-editor', 'example-reviewer'):
assert response.status_code == 200
else:
assert response.status_code == 403
22 changes: 11 additions & 11 deletions rdmo/management/assets/js/actions/elementActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,15 @@ export function fetchElementError(error) {

// store element

export function storeElement(elementType, element, back) {
export function storeElement(elementType, element, elementAction = null, back = false) {
return function(dispatch, getState) {
dispatch(storeElementInit(element))

dispatch(storeElementInit(element, elementAction))
MyPyDavid marked this conversation as resolved.
Show resolved Hide resolved

let action
switch (elementType) {
case 'catalogs':
action = () => QuestionsApi.storeCatalog(element)
action = () => QuestionsApi.storeCatalog(element, elementAction)
break

case 'sections':
Expand Down Expand Up @@ -385,11 +386,11 @@ export function storeElement(elementType, element, back) {
break

case 'tasks':
action = () => TasksApi.storeTask(element)
action = () => TasksApi.storeTask(element, elementAction)
break

case 'views':
action = () => ViewsApi.storeView(element)
action = () => ViewsApi.storeView(element, elementAction)
break
}

Expand All @@ -406,8 +407,8 @@ export function storeElement(elementType, element, back) {
}
}

export function storeElementInit(element) {
return {type: 'elements/storeElementInit', element}
export function storeElementInit(element, elementAction) {
return {type: 'elements/storeElementInit', element, elementAction}
}

export function storeElementSuccess(element) {
Expand Down Expand Up @@ -574,7 +575,6 @@ export function deleteElement(elementType, element) {
case 'catalogs':
action = () => QuestionsApi.deleteCatalog(element)
break

case 'sections':
action = () => QuestionsApi.deleteSection(element)
break
Expand Down Expand Up @@ -653,9 +653,9 @@ export function dropElement(dragElement, dropElement, mode) {
const element = {...getState().elements.element}
const { dragParent, dropParent } = moveElement(element, dragElement, dropElement, mode)

dispatch(storeElement(elementTypes[dragParent.model], dragParent))
if (!isNil(dropParent)) {
dispatch(storeElement(elementTypes[dropParent.model], dropParent))
dispatch(storeElement(elementTypes[dragParent.model], dragParent))
if (!isNil(dropParent)) {
dispatch(storeElement(elementTypes[dropParent.model], dropParent))
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions rdmo/management/assets/js/api/QuestionsApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ class QuestionsApi extends BaseApi {
return this.get(url)
}

static storeCatalog(catalog) {
static storeCatalog(catalog, action) {
jochenklar marked this conversation as resolved.
Show resolved Hide resolved
if (isNil(catalog.id)) {
return this.post('/api/v1/questions/catalogs/', catalog)
} else {
return this.put(`/api/v1/questions/catalogs/${catalog.id}/`, catalog)
const actionPath = isNil(action) ? '' : `${action}/`
return this.put(`/api/v1/questions/catalogs/${catalog.id}/${actionPath}`, catalog)
}
}

Expand Down
5 changes: 3 additions & 2 deletions rdmo/management/assets/js/api/TasksApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class TasksApi extends BaseApi {
return this.get(`/api/v1/tasks/tasks/${id}/`)
}

static storeTask(task) {
static storeTask(task, action) {
if (isNil(task.id)) {
return this.post('/api/v1/tasks/tasks/', task)
} else {
return this.put(`/api/v1/tasks/tasks/${task.id}/`, task)
const actionPath = isNil(action) ? '' : `${action}/`
return this.put(`/api/v1/tasks/tasks/${task.id}/${actionPath}`, task)
}
}

Expand Down
5 changes: 3 additions & 2 deletions rdmo/management/assets/js/api/ViewsApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class ViewsApi extends BaseApi {
return this.get(`/api/v1/views/views/${id}/`)
}

static storeView(view) {
static storeView(view, action) {
if (isNil(view.id)) {
return this.post('/api/v1/views/views/', view)
} else {
return this.put(`/api/v1/views/views/${view.id}/`, view)
const actionPath = isNil(action) ? '' : `${action}/`
return this.put(`/api/v1/views/views/${view.id}/${actionPath}`, view)
}
}

Expand Down
2 changes: 1 addition & 1 deletion rdmo/management/assets/js/components/common/Icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'

const ReadOnlyIcon = ({ title, show }) => {
return show && (
<i className="fa fa-ban" title={title}></i>
<i className="element-button fa fa-ban" title={title}></i>
)
}

Expand Down
22 changes: 21 additions & 1 deletion rdmo/management/assets/js/components/common/Links.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ LockedLink.propTypes = {
disabled: PropTypes.bool
}

const ToggleCurrentSiteLink = ({ hasCurrentSite, locked, onClick, show }) => {
const className = classNames({
'element-btn-link fa': true,
'fa-plus-square-o': !hasCurrentSite,
'fa-minus-square-o': hasCurrentSite,
})
const title = hasCurrentSite ? gettext('Remove your site'): gettext('Add your site')

return show && <LinkButton className={className} title={locked ? gettext('Locked') : title}
disabled={locked} onClick={onClick} />
}

ToggleCurrentSiteLink.propTypes = {
hasCurrentSite: PropTypes.bool.isRequired,
locked: PropTypes.bool.isRequired,
onClick: PropTypes.func.isRequired,
show: PropTypes.bool
}


const ShowElementsLink = ({ showElements, show, onClick }) => {
const className = classNames({
'element-btn-link fa': true,
Expand Down Expand Up @@ -242,5 +262,5 @@ ShowLink.propTypes = {
onClick: PropTypes.func.isRequired
}

export { EditLink, CopyLink, AddLink, AvailableLink, LockedLink, ShowElementsLink,
export { EditLink, CopyLink, AddLink, AvailableLink, ToggleCurrentSiteLink, LockedLink, ShowElementsLink,
NestedLink, ExportLink, ExtendLink, CodeLink, ErrorLink, WarningLink, ShowLink }
2 changes: 1 addition & 1 deletion rdmo/management/assets/js/components/edit/EditAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const EditAttribute = ({ config, attribute, elements, elementActions }) => {

const editAttribute = (attribute) => elementActions.fetchElement('attributes', attribute)
const updateAttribute = (key, value) => elementActions.updateElement(attribute, {[key]: value})
const storeAttribute = (back) => elementActions.storeElement('attributes', attribute, back)
const storeAttribute = (back) => elementActions.storeElement('attributes', attribute, elementAction, back)
const deleteAttribute = () => elementActions.deleteElement('attributes', attribute)

const [showDeleteModal, openDeleteModal, closeDeleteModal] = useDeleteModal()
Expand Down
2 changes: 1 addition & 1 deletion rdmo/management/assets/js/components/edit/EditCatalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const EditCatalog = ({ config, catalog, elements, elementActions }) => {
const { elementAction, sections } = elements

const updateCatalog = (key, value) => elementActions.updateElement(catalog, {[key]: value})
const storeCatalog = (back) => elementActions.storeElement('catalogs', catalog, back)
const storeCatalog = (back) => elementActions.storeElement('catalogs', catalog, elementAction, back)
const deleteCatalog = () => elementActions.deleteElement('catalogs', catalog)

const editSection = (value) => elementActions.fetchElement('sections', value.section)
Expand Down
2 changes: 1 addition & 1 deletion rdmo/management/assets/js/components/edit/EditCondition.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const EditCondition = ({ config, condition, elements, elementActions }) => {
const { elementAction, parent, attributes, options } = elements

const updateCondition = (key, value) => elementActions.updateElement(condition, {[key]: value})
const storeCondition = (back) => elementActions.storeElement('conditions', condition, back)
const storeCondition = (back) => elementActions.storeElement('conditions', condition, elementAction, back)
const deleteCondition = () => elementActions.deleteElement('conditions', condition)

const editAttribute = (attribute) => elementActions.fetchElement('attributes', attribute)
Expand Down
2 changes: 1 addition & 1 deletion rdmo/management/assets/js/components/edit/EditOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const EditOption = ({ config, option, elements, elementActions }) => {
const { elementAction, parent } = elements

const updateOption = (key, value) => elementActions.updateElement(option, {[key]: value})
const storeOption = (back) => elementActions.storeElement('options', option, back)
const storeOption = (back) => elementActions.storeElement('options', option, elementAction, back)
const deleteOption = () => elementActions.deleteElement('options', option)

const [showDeleteModal, openDeleteModal, closeDeleteModal] = useDeleteModal()
Expand Down
2 changes: 1 addition & 1 deletion rdmo/management/assets/js/components/edit/EditOptionSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const EditOptionSet = ({ config, optionset, elements, elementActions }) => {
const { elementAction, parent, conditions, options } = elements

const updateOptionSet = (key, value) => elementActions.updateElement(optionset, {[key]: value})
const storeOptionSet = (back) => elementActions.storeElement('optionsets', optionset, back)
const storeOptionSet = (back) => elementActions.storeElement('optionsets', optionset, elementAction, back)
const deleteOptionSet = () => elementActions.deleteElement('optionsets', optionset)

const editOption = (value) => elementActions.fetchElement('options', value.option)
Expand Down
2 changes: 1 addition & 1 deletion rdmo/management/assets/js/components/edit/EditPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const EditPage = ({ config, page, elements, elementActions }) => {
elementActions.updateElement(page, { [key]: value })
}
}
const storePage = (back) => elementActions.storeElement('pages', page, back)
const storePage = (back) => elementActions.storeElement('pages', page, elementAction, back)
const deletePage = () => elementActions.deleteElement('pages', page)

const editElement = (value) => {
Expand Down
2 changes: 1 addition & 1 deletion rdmo/management/assets/js/components/edit/EditQuestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const EditQuestion = ({ config, question, elements, elementActions}) => {
const { elementAction, parent, attributes, optionsets, options, conditions } = elements

const updateQuestion = (key, value) => elementActions.updateElement(question, {[key]: value})
const storeQuestion = (back) => elementActions.storeElement('questions', question, back)
const storeQuestion = (back) => elementActions.storeElement('questions', question, elementAction, back)
const deleteQuestion = () => elementActions.deleteElement('questions', question)

const editOptionSet = (optionset) => elementActions.fetchElement('optionsets', optionset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const EditQuestionSet = ({ config, questionset, elements, elementActions }) => {
elementActions.updateElement(questionset, { [key]: value })
}
}
const storeQuestionSet = (back) => elementActions.storeElement('questionsets', questionset, back)
const storeQuestionSet = (back) => elementActions.storeElement('questionsets', questionset, elementAction, back)
const deleteQuestionSet = () => elementActions.deleteElement('questionsets', questionset)

const editElement = (value) => {
Expand Down
2 changes: 1 addition & 1 deletion rdmo/management/assets/js/components/edit/EditSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const EditSection = ({ config, section, elements, elementActions }) => {
const { elementAction, parent, pages } = elements

const updateSection = (key, value) => elementActions.updateElement(section, {[key]: value})
const storeSection = (back) => elementActions.storeElement('sections', section, back)
const storeSection = (back) => elementActions.storeElement('sections', section, elementAction, back)
const deleteSection = () => elementActions.deleteElement('sections', section)

const editPage = (value) => elementActions.fetchElement('pages', value.page)
Expand Down
2 changes: 1 addition & 1 deletion rdmo/management/assets/js/components/edit/EditTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const EditTask = ({ config, task, elements, elementActions}) => {
const { elementAction, attributes, catalogs, conditions } = elements

const updateTask = (key, value) => elementActions.updateElement(task, {[key]: value})
const storeTask = (back) => elementActions.storeElement('tasks', task, back)
const storeTask = (back) => elementActions.storeElement('tasks', task, elementAction, back)
const deleteTask = () => elementActions.deleteElement('tasks', task)

const editCondition = (condition) => elementActions.fetchElement('conditions', condition)
Expand Down
2 changes: 1 addition & 1 deletion rdmo/management/assets/js/components/edit/EditView.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const EditView = ({ config, view, elements, elementActions }) => {
const { elementAction, catalogs } = elements

const updateView = (key, value) => elementActions.updateElement(view, {[key]: value})
const storeView = (back) => elementActions.storeElement('views', view, back)
const storeView = (back) => elementActions.storeElement('views', view, elementAction, back)
const deleteView = () => elementActions.deleteElement('views', view)

const [showDeleteModal, openDeleteModal, closeDeleteModal] = useDeleteModal()
Expand Down
8 changes: 7 additions & 1 deletion rdmo/management/assets/js/components/element/Catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { filterElement } from '../../utils/filter'
import { buildPath } from '../../utils/location'

import { ElementErrors } from '../common/Errors'
import { EditLink, CopyLink, AddLink, AvailableLink, LockedLink, NestedLink,
import { EditLink, CopyLink, AddLink, AvailableLink, ToggleCurrentSiteLink, LockedLink, NestedLink,
ExportLink, CodeLink } from '../common/Links'
import { ReadOnlyIcon } from '../common/Icons'

Expand All @@ -27,11 +27,17 @@ const Catalog = ({ config, catalog, elementActions, display='list',
const toggleAvailable = () => elementActions.storeElement('catalogs', {...catalog, available: !catalog.available })
const toggleLocked = () => elementActions.storeElement('catalogs', {...catalog, locked: !catalog.locked })

const toggleCurrentSite = () => elementActions.storeElement('catalogs', catalog, 'toggle-site')

const createSection = () => elementActions.createElement('sections', { catalog })

const elementNode = (
<div className="element">
<div className="pull-right">
<ToggleCurrentSiteLink hasCurrentSite={config.settings.multisite ? catalog.sites.includes(config.currentSite.id) : true}
locked={catalog.locked}
onClick={toggleCurrentSite}
show={config.settings.multisite}/>
<ReadOnlyIcon title={gettext('This catalog is read only')} show={catalog.read_only} />
<NestedLink title={gettext('View catalog nested')} href={nestedUrl} onClick={fetchNested} />
<EditLink title={gettext('Edit catalog')} href={editUrl} onClick={fetchEdit} />
Expand Down
7 changes: 6 additions & 1 deletion rdmo/management/assets/js/components/element/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { filterElement } from '../../utils/filter'
import { buildPath } from '../../utils/location'

import { ElementErrors } from '../common/Errors'
import { EditLink, CopyLink, AvailableLink, LockedLink, ExportLink, CodeLink } from '../common/Links'
import { EditLink, CopyLink, AvailableLink, LockedLink, ExportLink, CodeLink, ToggleCurrentSiteLink } from '../common/Links'
import { ReadOnlyIcon } from '../common/Icons'

const Task = ({ config, task, elementActions, filter=false, filterSites=false, filterEditors=false }) => {
Expand All @@ -21,13 +21,18 @@ const Task = ({ config, task, elementActions, filter=false, filterSites=false, f
const fetchCopy = () => elementActions.fetchElement('tasks', task.id, 'copy')
const toggleAvailable = () => elementActions.storeElement('tasks', {...task, available: !task.available })
const toggleLocked = () => elementActions.storeElement('tasks', {...task, locked: !task.locked })
const toggleCurrentSite = () => elementActions.storeElement('tasks', task, 'toggle-site')

const fetchCondition = (index) => elementActions.fetchElement('conditions', task.conditions[index])

return showElement && (
<li className="list-group-item">
<div className="element">
<div className="pull-right">
<ToggleCurrentSiteLink hasCurrentSite={config.settings.multisite ? task.sites.includes(config.currentSite.id) : true}
locked={task.locked}
onClick={toggleCurrentSite}
show={config.settings.multisite}/>
<ReadOnlyIcon title={gettext('This task is read only')} show={task.read_only} />
<EditLink title={gettext('Edit task')} href={editUrl} onClick={fetchEdit} />
<CopyLink title={gettext('Copy task')} href={copyUrl} onClick={fetchCopy} />
Expand Down