admin: do not allow to remove a status in use (#1241)

Instead, present the user with the possibility to reassign formdata to another
status, or to remove them.
This commit is contained in:
Frédéric Péters 2012-08-13 13:43:13 +02:00
parent 335c26f8c5
commit 9e9308cfd9
1 changed files with 78 additions and 1 deletions

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import time
from quixote import redirect, get_publisher
from quixote.directory import Directory
@ -23,6 +25,8 @@ from qommon.form import *
from qommon.admin.menu import html_top, command_icon, error_page
from wcs.workflows import *
from wcs.formdef import FormDef
from wcs.formdata import Evolution
from wcs.admin.forms import ET, indent
@ -137,7 +141,8 @@ class WorkflowItemsDir(Directory):
return redirect('..')
class WorkflowStatusPage(Directory):
_q_exports = ['', 'delete', 'newitem', ('items', 'items_dir'), 'update_order', 'edit']
_q_exports = ['', 'delete', 'newitem', ('items', 'items_dir'),
'update_order', 'edit', 'reassign']
def __init__(self, workflow, status_id):
self.workflow = workflow
@ -253,6 +258,13 @@ class WorkflowStatusPage(Directory):
'<h2>%s %s</h2>' % (_('Deleting Status:'), self.status.name)
form.render()
else:
# Before removing the status, scan formdata to know if it's in use.
for formdef in FormDef.select():
if formdef.workflow_id != self.workflow.id:
continue
if formdef.data_class().get_with_indexed_value(
str('status'), 'wf-%s' % self.status.id):
return redirect('reassign')
del self.workflow.possible_status[ self.workflow.possible_status.index(self.status) ]
self.workflow.store()
return redirect('../../')
@ -281,6 +293,71 @@ class WorkflowStatusPage(Directory):
'<h2>%s</h2>' % _('Edit Workflow Status')
form.render()
def reassign [html] (self):
options = [(None, 'Do nothing'), ('remove', _('Remove'))]
for status in self.workflow.possible_status:
if status.id == self.status.id:
continue
options.append(('reassign-%s' % status.id, _('Change to "%s"') % status.name))
form = Form(enctype='multipart/form-data')
form.add(SingleSelectWidget, 'action', title=_('Pick an Action'),
options=options)
form.add_submit('submit', _('Submit'))
form.add_submit('cancel', _('Cancel'))
if form.get_submit() == 'cancel':
return redirect('.')
if form.is_submitted() and not form.get_widget('action').parse():
return redirect('.')
if not form.is_submitted() or form.has_errors():
get_response().breadcrumb.append(('reassign', _('Reassign')))
html_top('workflows', title = _('Reassign'))
'<h2>%s</h2>' % _('Reassigning ')
'<p>'
_('''There are forms set to this status, they need to be changed before
this status can be deleted.''')
'</p>'
'<ul>'
for formdef in FormDef.select():
if formdef.workflow_id != self.workflow.id:
continue
items = formdef.data_class().get_with_indexed_value(
str('status'), 'wf-%s' % self.status.id)
'<li>%s: %s</li>' % (formdef.name, _('%s items') % len(items))
'</ul>'
form.render()
else:
self.submit_reassign(form)
return redirect('.')
def submit_reassign(self, form):
action = form.get_widget('action').parse()
if action.startswith(str('reassign-')):
new_status = 'wf-%s' % str(action)[9:]
for formdef in FormDef.select():
if formdef.workflow_id != self.workflow.id:
continue
items = formdef.data_class().get_with_indexed_value(
str('status'), 'wf-%s' % self.status.id)
for item in items:
if action == 'remove':
item.remove_self()
else:
item.status = new_status
evo = Evolution()
evo.time = time.localtime()
evo.status = new_status
evo.comment = _('Administrator reassigned status')
if not item.evolution:
item.evolution = []
item.evolution.append(evo)
item.store()
class WorkflowStatusDirectory(Directory):
_q_exports = ['']