def start_work(contract_id=-1):
    contract = ContractBase.query.get(contract_id)
    contract = contract if contract else ContractBase()
    form = NewContractForm(obj=contract)

    if form.validate_on_submit():
        if contract_id == -1:
            contract, _ = get_or_create(
                db.session, ContractBase, description=form.data.get('description'),
                department=form.data.get('department'), is_visible=False
            )
        else:
            contract = ContractBase.clone(contract)
            contract.description = form.data.get('description')
            contract.department = form.data.get('department')
            db.session.add(contract)
            db.session.commit()

        assigned = assign_a_contract(contract, form.data.get('flow'), form.data.get('assigned').id, clone=False)
        db.session.commit()

        if assigned:
            flash('Successfully assigned {} to {}!'.format(assigned.description, assigned.assigned.email), 'alert-success')
            return redirect(url_for('conductor.detail', contract_id=assigned.id))
        else:
            flash("That flow doesn't exist!", 'alert-danger')
    return render_template('conductor/new.html', form=form, contract_id=contract_id)
Exemple #2
0
def start_work(contract_id=-1):
    '''Start work on a contract

    When new work is started on a contract, a new contract is created
    (either as a clone of an existing contract, or as brand new work),
    assigned to the :py:class:`~purchasing.data.flows.Flow` and
    :py:class:`~purchasing.users.models.User` indicated in the
    :py:class:`~purchasing.conductor.forms.NewContractForm`

    :param contract_id: Primary key ID for a
        :py:class:`~purchasing.data.contracts.ContractBase`

    .. seealso::
        :py:meth:`~purchasing.data.contracts.ContractBase.clone` for
        more information on how new contracts get started, and
        :py:class:`~purchasing.conductor.forms.NewContractForm` for more
        on the form to start new work.

    :status 200: Render the new contract view
    :status 302: Create or start new work on a cloned contract
    '''
    contract = ContractBase.query.get(contract_id)

    if contract:
        first_stage = contract.get_first_stage()

        if first_stage and first_stage.stage_id != contract.current_stage_id:
            return redirect(
                url_for('conductor.detail', contract_id=contract.id))
        elif first_stage:
            contract.start = localize_datetime(
                contract.get_first_stage().entered)
    else:
        contract = ContractBase()
    form = NewContractForm(obj=contract)

    if form.validate_on_submit():
        if contract_id == -1:
            contract, _ = get_or_create(
                db.session,
                ContractBase,
                description=form.data.get('description'),
                department=form.data.get('department'),
                is_visible=False)
        elif not first_stage:
            contract = ContractBase.clone(contract)
            contract.description = form.data.get('description')
            contract.department = form.data.get('department')
            db.session.add(contract)
            db.session.commit()

        assigned = assign_a_contract(
            contract,
            form.data.get('flow'),
            form.data.get('assigned'),
            start_time=form.data.get('start').astimezone(
                pytz.UTC).replace(tzinfo=None),
            clone=False)
        db.session.commit()

        if assigned:
            flash(
                'Successfully assigned {} to {}!'.format(
                    assigned.description, assigned.assigned.email),
                'alert-success')
            return redirect(
                url_for('conductor.detail', contract_id=assigned.id))
        else:
            flash("That flow doesn't exist!", 'alert-danger')
    return render_template('conductor/new.html',
                           form=form,
                           contract_id=contract_id,
                           contract=contract)
def start_work(contract_id=-1):
    '''Start work on a contract

    When new work is started on a contract, a new contract is created
    (either as a clone of an existing contract, or as brand new work),
    assigned to the :py:class:`~purchasing.data.flows.Flow` and
    :py:class:`~purchasing.users.models.User` indicated in the
    :py:class:`~purchasing.conductor.forms.NewContractForm`

    :param contract_id: Primary key ID for a
        :py:class:`~purchasing.data.contracts.ContractBase`

    .. seealso::
        :py:meth:`~purchasing.data.contracts.ContractBase.clone` for
        more information on how new contracts get started, and
        :py:class:`~purchasing.conductor.forms.NewContractForm` for more
        on the form to start new work.

    :status 200: Render the new contract view
    :status 302: Create or start new work on a cloned contract
    '''
    contract = ContractBase.query.get(contract_id)

    if contract:
        first_stage = contract.get_first_stage()

        if first_stage and contract.completed_last_stage():
            pass
        elif first_stage and first_stage.stage_id != contract.current_stage_id:
            return redirect(url_for('conductor.detail', contract_id=contract.id))
        elif first_stage:
            contract.start = localize_datetime(contract.get_first_stage().entered)
    else:
        contract = ContractBase()
    form = NewContractForm(obj=contract)

    if form.validate_on_submit():
        if contract_id == -1:
            contract, _ = get_or_create(
                db.session, ContractBase, description=form.data.get('description'),
                department=form.data.get('department'), is_visible=False
            )
        elif not first_stage or contract.completed_last_stage():
            contract = ContractBase.clone(contract)
            contract.description = form.data.get('description')
            contract.department = form.data.get('department')
            db.session.add(contract)
            db.session.commit()

        assigned = assign_a_contract(
            contract, form.data.get('flow'), form.data.get('assigned'),
            start_time=form.data.get('start').astimezone(pytz.UTC).replace(tzinfo=None),
            clone=False
        )
        db.session.commit()

        if assigned:
            flash('Successfully assigned {} to {}!'.format(assigned.description, assigned.assigned.email), 'alert-success')
            return redirect(url_for('conductor.detail', contract_id=assigned.id))
        else:
            flash("That flow doesn't exist!", 'alert-danger')
    return render_template('conductor/new.html', form=form, contract_id=contract_id, contract=contract)