def project_edit(project_id=None): from models.account import Account from models.project import Project, Label project = None if not project_id: project = Project() project.status = Project.STATUS_ACTIVE if not app.access('project', action='create'): abort(403) else: project_id = urllib.unquote_plus(project_id) project = Project.query.filter_by(id=project_id).first() if not project: abort(404) elif not app.access('project', action='update', project=project): abort(403) validationErrors = [] if request.method == 'POST' and request.form.get('csrf_token', None): project.alias = request.form.get('project_alias', project.alias).strip() project.title = request.form.get('project_title', project.title).strip() project.info = request.form.get('project_info', project.info).strip() project.status = int(request.form.get('project_status', project.status).strip()) validationErrors.extend(project.validate()) if not validationErrors: project.save() if not Label.query.filter_by(project_id=project.id, title=Label.LABEL_DEFAULT).first(): label = Label() label.project_id = project.id label.title = Label.LABEL_DEFAULT label.save() flash(g._t('project submit success')) return redirect(url_for('project_view', project_id=urllib.quote_plus(str(project.id)))) if project_id: title = g._t('edit project') else: title = g._t('add project') if project_id: breadcrumbs = ( (g._t('projects'), url_for('project_index')), (project.__str__(), url_for('project_view', project_id=urllib.quote_plus(str(project_id)))), (title, "#") ) else: breadcrumbs = ( (g._t('projects'), url_for('project_index')), (title, "#") ) return render_template('project/edit.html', project_id=project_id, project=project, errors=validationErrors, title=title, breadcrumbs=breadcrumbs)
def project_labels(project_id): from models.project import Project, Label project_id = urllib.unquote_plus(project_id) project = Project.query.filter_by(id=project_id).first() if not project: abort(404) elif not app.access('project', action='update', project=project): abort(403) title = g._t('project labels') breadcrumbs = ( (g._t('projects'), url_for('project_index')), (project.__str__(), url_for('project_view', project_id=urllib.quote_plus(str(project_id)))), (title, "#") ) if (request.form.get('method') == 'PUT' or request.method == 'PUT') and request.form.get('csrf_token'): submittedLabel = Label.query.filter_by(project=project, id=request.form.get('label_id')).first() if submittedLabel: submittedLabel.title = request.form.get('label_title', '') if not submittedLabel.validate(): submittedLabel.save() flash( g._t('label update success')) return redirect(url_for('project_labels', project_id=urllib.quote_plus(str(project_id)))) else: flash( g._t('label not found'), 'error') elif (request.form.get('method') == 'DELETE' or request.method == 'DELETE') and request.form.get('csrf_token'): submittedLabel = Label.query.filter_by(project=project, id=request.form.get('label_id')).first() if submittedLabel: submittedLabel.delete() # project may not have zero labels, default one will be set if not Label.query.filter_by(project_id=project.id, title=Label.LABEL_DEFAULT).first(): label = Label() label.project_id = project.id label.title = Label.LABEL_DEFAULT label.save() flash( g._t('label delete success')) return redirect(url_for('project_labels', project_id=urllib.quote_plus(str(project_id)))) else: flash( g._t('label not found'), 'error') elif (request.form.get('method') == 'POST' or request.method == 'POST') and request.form.get('csrf_token'): submittedLabel = Label() submittedLabel.project_id = project_id submittedLabel.title = request.form.get('label_title', '') if not submittedLabel.validate(): submittedLabel.save() flash( g._t('label create success')) return redirect(url_for('project_labels', project_id=urllib.quote_plus(str(project_id)))) else: submittedLabel = Label() return render_template('project/labels.html', project_id=project_id, project=project, submittedLabel=submittedLabel, title=title, breadcrumbs=breadcrumbs)