def delete_project(request): if not request.user.is_admin: raise HTTPForbidden() project_id = int(request.POST.get('project_id')) session = DBSession() project = session.query(Project).filter_by(id=project_id).one() name, title = project.name, project.title session.delete(project) msg = _(u'Project "${name}" ("${title}") has been deleted.') % { 'name': name, 'title': title} request.session.flash(msg, 'success') url = request.route_url('projects') return HTTPSeeOther(location=url)
def configure_statuses(request): project_name = request.matchdict['project_name'] session = DBSession() try: project = session.query(Project).filter_by(name=project_name).one() except NoResultFound: raise HTTPNotFound() if not has_permission(request, PERM_MANAGE_PROJECT, project): raise HTTPForbidden() posted_statuses = map(int, request.POST.getall('statuses')) # The UI should not allow to remove default statuses, but let's # enforce it here. for default_status in DEFAULT_STATUSES: if default_status['id'] not in posted_statuses: msg = _('You cannot remove this status.') request.session.flash(msg, 'error') return configure_statuses_form(request) # The UI should not allow to remove statuses that are being used, # but let's enforce it here. # FIXME: to do current_statuses = {} for status in project.statuses: current_statuses[status.id] = status statuses = zip(posted_statuses, request.POST.getall('labels')) # Change existing statuses and add new ones new_id = session.execute( 'SELECT MAX(id) FROM statuses ' 'WHERE project_id=%d' % project.id).fetchone()[0] for position, (status_id, label) in enumerate(statuses, 1): if not status_id: new_id += 1 status = Status(id=new_id, project_id=project.id, label=label, position=position) session.add(status) else: status = current_statuses[status_id] if label != status.label: status.label = label if position != status.position: status.position = position # Remove statuses for status in project.statuses: if status.id not in posted_statuses: session.delete(status) msg = _('Your changes have been saved.') request.session.flash(msg, 'success') url = request.route_url('project_configure_statuses', project_name=project.name) return HTTPSeeOther(location=url)