Example #1
0
def saiki_templates_edit():
    """Docstring."""
    if only_check():
        if request.method == 'POST':
            template_form = TemplateForm()
            template_form.validate_on_submit()
            print(template_form)
            if template_form.validate() is False:
                flash('Please check that all the fields are valid!.',
                      'critical')
                return check_and_render('saiki_templates_edit.html',
                                        form=template_form)
            else:
                update_template(template_form)
                flash('updated Config for Topic : ' +
                      template_form.template_name.data)
                return redirect(url_for('saiki_templates'))
        elif request.method == 'GET':
            template = request.args.get('template')
            if template != '' and template is not None:
                template_data = get_saiki_template_single(template)
            else:
                template = ''
                template_data = '{}'
            template_form = TemplateForm(template_name=template,
                                         template_data=template_data)
            return check_and_render('saiki_templates_edit.html',
                                    form=template_form,
                                    template_data=template_data)
    else:
        return check_and_render('index.html')
def edit_notebook(nb_id):
    nb = NotebookTemplate.query.get_or_404(nb_id)
    form = TemplateForm(obj=nb)
    task_list = Task.query.order_by(Task.short).all()
    form.tasks.choices = [(task.id, "[{}] {}".format(task.short, task.name))
                          for task in task_list]
    selected_tasks = [task.id for task in nb.tasks]
    if request.method == 'GET':
        form.tasks.data = [task.id for task in nb.tasks]
        for _ in form.order_options:
            form.order_options.pop_entry()
        for order_option in nb.options:
            option_field = OrderOptionForm()
            option_field.order_type = 'random' if order_option.random else 'fixed'
            form.order_options.append_entry(option_field)
    for (idx, subform) in enumerate(form.order_options):
        subform.tasks_random.choices = [(task.id, task.name)
                                        for task in task_list]
        subform.tasks_fixed.choices = [(task.id, task.name)
                                       for task in task_list]
        if request.method == 'GET':
            subform.tasks_random.data = [
                task.id for task in nb.options[idx].tasks
            ] if nb.options[idx].random else None
            subform.tasks_fixed.data = nb.options[idx].tasks[
                0].id if not nb.options[idx].random else None
    if form.validate_on_submit():
        if form.name.data != nb.name and NotebookTemplate.query.filter_by(
                name=form.name.data).first():
            flash('A notebook with this name already exists.', 'danger')
        else:
            nb.name = form.name.data
            selected_tasks = form.tasks.data
            nb.tasks = Task.query.filter(Task.id.in_(selected_tasks)).all()
            to_remove = len(nb.options) - len(form.order_options.entries)
            if to_remove > 0:
                nb.options = nb.options[:-to_remove]
            for (idx, subform) in enumerate(form.order_options.entries):
                is_random = subform.order_type.data == 'random'
                option_tasks = subform.tasks_random.data if is_random else [
                    subform.tasks_fixed.data
                ]
                if idx >= len(nb.options):
                    # new option
                    option = OrderOption(idx, is_random)
                    nb.options.append(option)
                else:
                    option = nb.options[idx]
                    option.random = is_random
                option.tasks = Task.query.filter(
                    Task.id.in_(option_tasks)).all()
            db.session.commit()
            flash('Saved changes', 'success')
    return render_template('notebook.html',
                           form=form,
                           notebook=nb,
                           selected_tasks=selected_tasks,
                           action=url_for('nbg.edit_notebook', nb_id=nb_id))
def create_notebook():
    form = TemplateForm()
    task_list = Task.query.order_by(Task.short).all()
    form.tasks.choices = [(task.id, "[{}] {}".format(task.short, task.name))
                          for task in task_list]
    for subform in form.order_options:
        subform.tasks_random.choices = [(task.id, task.name)
                                        for task in task_list]
        subform.tasks_fixed.choices = [(task.id, task.name)
                                       for task in task_list]
    if form.validate_on_submit():
        if NotebookTemplate.query.filter_by(name=form.name.data).first():
            flash('A notebook template with this name already exists',
                  'danger')
        else:
            notebook = NotebookTemplate(form.name.data)
            for task_id in form.tasks.data:
                notebook.tasks.append(Task.query.get(task_id))
                db.session.add(notebook)
            db.session.commit()
            for idx, option in enumerate(form.order_options.entries):
                order_option = OrderOption(idx,
                                           option.order_type.data == 'random')
                order_option.notebook = notebook.id
                if order_option.random:
                    for task_id in option.tasks_random.data:
                        order_option.tasks.append(Task.query.get(task_id))
                else:
                    order_option.tasks.append(
                        Task.query.get(option.tasks_fixed.data))
                db.session.add(order_option)
            db.session.commit()
            flash('Template created', 'success')
            return redirect(url_for('nbg.list_notebooks'))
    if not form.tasks.data:
        form.tasks.data = [task_list[0].id]
    selected_tasks = [task_id for task_id in form.tasks.data]
    return render_template('notebook.html',
                           form=form,
                           selected_tasks=selected_tasks,
                           action=url_for('nbg.create_notebook'))