Пример #1
0
def common_task_a():
    ''' to add a common task '''
    form = TaskForm(common=True)

    if form.validate_on_submit():
        task = data.Task(form.name.data, form.hidden.data)

        if data.Task.query.filter_by(name=form.name.data).first() is not None:
            flash('Error: name is used by another one, choose another name',
                  'danger')
            return redirect(url_for('manage_app.common_task_a'))

        offices_validation = [
            form[f'check{o.id}'].data for o in data.Office.query.all()
        ]
        if len(offices_validation) > 0 and not any(offices_validation):
            flash('Error: one office must be selected at least', 'danger')
            return redirect(url_for('manage_app.common_task_a'))

        db.session.add(task)
        db.session.commit()

        for office in data.Office.query.all():
            if form['check%i' % office.id].data and office not in task.offices:
                task.offices.append(office)

        for office in task.offices:
            initial_ticket = data.Serial.query\
                                 .filter_by(office_id=office.id, number=100)\
                                 .first()

            if not initial_ticket:
                db.session.add(
                    data.Serial(office_id=office.id, task_id=task.id, p=True))

        db.session.commit()
        flash('Notice: a common task has been added.', 'info')
        return redirect(url_for('manage_app.all_offices'))
    return render_template('task_add.html',
                           form=form,
                           offices=data.Office.query,
                           serial=data.Serial.all_clean(),
                           tasks=data.Task.query,
                           operators=data.Operators.query,
                           navbar='#snb1',
                           common=True,
                           page_title='Add a common task',
                           hash='#da6')
Пример #2
0
def task_a(office):
    ''' to add a task '''
    form = TaskForm()

    if is_operator() and not is_office_operator(office.id):
        flash('Error: operators are not allowed to access the page ', 'danger')
        return redirect(url_for('core.root'))

    if form.validate_on_submit():
        if data.Task.query.filter_by(name=form.name.data).first() is not None:
            flash('Error: name is used by another one, choose another name',
                  'danger')
            return redirect(url_for('manage_app.task_a', o_id=office.id))

        task = data.Task(form.name.data, form.hidden.data)
        db.session.add(task)
        db.session.commit()

        if office.id not in ids(task.offices):
            task.offices.append(office)
            db.session.commit()

        initial_ticket = data.Serial.query.filter_by(task_id=task.id,
                                                     office_id=office.id,
                                                     number=100)\
                                          .first()

        if not initial_ticket:
            db.session.add(
                data.Serial(office_id=task.offices[0].id,
                            task_id=task.id,
                            p=True))
            db.session.commit()

        flash('Notice: New task been added.', 'info')
        return redirect(url_for('manage_app.offices', o_id=office.id))
    return render_template('task_add.html',
                           form=form,
                           offices=data.Office.query,
                           serial=data.Serial.all_clean(),
                           tasks=data.Task.query,
                           operators=data.Operators.query,
                           navbar='#snb1',
                           common=False,
                           dropdown='#dropdown-lvl' + str(office.id),
                           hash='#t3' + str(office.id),
                           page_title='Add new task')
Пример #3
0
def task(task, ofc_id):
    ''' view specific task. '''
    if is_operator() and not is_common_task_operator(task.id):
        flash('Error: operators are not allowed to access the page ', 'danger')
        return redirect(url_for('core.root'))

    task = data.Task.get(task.id)  # NOTE: session's lost
    form = TaskForm(common=task.common)
    page = request.args.get('page', 1, type=int)
    tickets = data.Serial.all_task_tickets(ofc_id, task.id)
    last_ticket_pulled = tickets.filter_by(p=True).first()
    pagination = tickets.paginate(page, per_page=10, error_out=False)

    if form.validate_on_submit():
        if data.Task.query.filter_by(name=form.name.data).count() > 1:
            flash('Error: name is used by another one, choose another name',
                  'danger')
            return redirect(
                url_for('manage_app.task', o_id=task.id, ofc_id=ofc_id))

        task = data.Task.get(task.id)
        task.name = form.name.data
        task.hidden = form.hidden.data

        if task.common:
            checked_offices = [
                o for o in data.Office.query.all() if form[f'check{o.id}'].data
            ]
            removed_offices = [
                o for o in task.offices if o.id not in ids(checked_offices)
            ]
            to_add_offices = [
                o for o in checked_offices if o.id not in ids(task.offices)
            ]

            if not checked_offices:
                flash('Error: one office must be selected at least', 'danger')
                return redirect(url_for('manage_app.common_task_a'))

            for office in removed_offices:
                task.migrate_tickets(office, checked_offices[0])
                task.offices.remove(office)

            for office in to_add_offices:
                task.offices.append(office)

        db.session.commit()
        flash('Notice: task has been updated .', 'info')
        return redirect(url_for('manage_app.task', o_id=task.id,
                                ofc_id=ofc_id))

    if not form.errors:
        form.name.data = task.name
        form.hidden.data = task.hidden

        for office in task.offices:
            form[f'check{office.id}'].data = True

    if not ofc_id:
        # NOTE: sidebar collapse failsafe, just incase the office id wasn't passed
        ofc_id = task.offices[0].id

    return render_template(
        'tasks.html',
        form=form,
        page_title='Task : ' + task.name,
        tasksp=pagination.items,
        pagination=pagination,
        serial=tickets,
        o_id=task.id,
        ofc_id=ofc_id,
        common=task.common,
        len=len,
        offices=data.Office.query,
        tasks=data.Task.query,
        users=data.User.query,
        operators=data.Operators.query,
        task=task,
        navbar='#snb1',
        dropdown='#dropdown-lvl%i' % ofc_id,  # dropdown a list of offices
        hash='#tt%i%i' % (ofc_id, task.id),
        last_ticket_pulled=last_ticket_pulled,
        edit_task=len(task.offices) == 1 or not is_operator(),
        office=data.Office.get(ofc_id))