def decline_agreement(user_info):
    supplier_code = user_info.get('supplier_code')
    email_address = user_info.get('email_address')

    supplier = suppliers.get_supplier_by_code(supplier_code)
    mandatory_supplier_checks(supplier)

    if email_address != supplier.data.get('email'):
        raise UnauthorisedError('Unauthorised to decline agreement')

    supplier_users = users.find(supplier_code=supplier_code).all()
    supplier.status = 'deleted'
    for user in supplier_users:
        user.active = False
        users.add_to_session(user)

    users.commit_changes()
    suppliers.save(supplier)

    send_decline_master_agreement_email(supplier.code)

    publish_tasks.supplier.delay(publish_tasks.compress_supplier(supplier),
                                 'declined_agreement',
                                 updated_by=email_address)

    audit_service.log_audit_event(
        audit_type=audit_types.declined_master_agreement,
        user=email_address,
        data={
            'supplierCode': supplier.code,
            'supplierData': supplier.data
        },
        db_object=supplier)
Exemple #2
0
def delete_supplier(code):
    supplier = Supplier.query.filter(Supplier.code == code).first_or_404()
    supplier.status = 'deleted'
    try:
        db.session.commit()
        publish_tasks.supplier.delay(publish_tasks.compress_supplier(supplier),
                                     'deleted')
    except IntegrityError as e:
        db.session.rollback()
        return jsonify(message="Database Error: {0}".format(e)), 400

    return jsonify(message="done"), 200
def set_a_declaration(code, framework_slug):
    framework = Framework.query.filter(
        Framework.slug == framework_slug
    ).first_or_404()

    supplier_framework = SupplierFramework.find_by_supplier_and_framework(
        code, framework_slug
    )
    supplier = None
    if supplier_framework is not None:
        status_code = 200 if supplier_framework.declaration else 201
    else:
        supplier = Supplier.query.filter(
            Supplier.code == code
        ).first_or_404()

        supplier_framework = SupplierFramework(
            supplier_code=supplier.code,
            framework_id=framework.id,
            declaration={}
        )
        status_code = 201

    request_data = get_json_from_request()
    updater_json = validate_and_return_updater_request()
    json_has_required_keys(request_data, ['declaration'])

    supplier_framework.declaration = request_data['declaration'] or {}
    db.session.add(supplier_framework)
    db.session.add(
        AuditEvent(
            audit_type=AuditTypes.answer_selection_questions,
            db_object=supplier_framework,
            user=updater_json['updated_by'],
            data={'update': request_data['declaration']})
    )

    try:
        db.session.commit()
        if supplier:
            publish_tasks.supplier.delay(
                publish_tasks.compress_supplier(supplier),
                'set_declaration',
                updated_by=updater_json['updated_by']
            )
    except IntegrityError as e:
        db.session.rollback()
        abort(400, "Database Error: {}".format(e))

    return jsonify(declaration=supplier_framework.declaration), status_code
def delete_supplier(code):
    supplier = Supplier.query.filter(
        Supplier.code == code
    ).first_or_404()
    supplier.status = 'deleted'
    try:
        db.session.commit()
        publish_tasks.supplier.delay(
            publish_tasks.compress_supplier(supplier),
            'deleted'
        )
    except IntegrityError as e:
        db.session.rollback()
        return jsonify(message="Database Error: {0}".format(e)), 400

    return jsonify(message="done"), 200
def update_supplier(data, user_info):
    supplier_code = user_info.get('supplier_code')
    email_address = user_info.get('email_address')

    supplier = suppliers.find(code=supplier_code).one_or_none()
    mandatory_supplier_checks(supplier)

    whitelist_fields = ['representative', 'email', 'phone']
    for wf in whitelist_fields:
        if wf not in data.get('data'):
            raise ValidationError('{} is not recognised'.format(wf))

    if 'email' in data.get('data'):
        email = data['data']['email']
        data['data']['email'] = email.encode('utf-8').lower()

    supplier.update_from_json(data.get('data'))

    messages = SupplierValidator(supplier).validate_representative(
        'representative')
    if len([m for m in messages if m.get('severity', '') == 'error']) > 0:
        raise ValidationError(',\n'.join([
            m.get('message') for m in messages
            if m.get('severity', '') == 'error'
        ]))

    suppliers.save(supplier)

    publish_tasks.supplier.delay(publish_tasks.compress_supplier(supplier),
                                 'updated',
                                 updated_by=email_address)

    audit_service.log_audit_event(audit_type=audit_types.update_supplier,
                                  user=email_address,
                                  data={
                                      'supplierCode': supplier.code,
                                      'supplierData': supplier.data
                                  },
                                  db_object=supplier)

    process_auth_rep_email(supplier, data, user_info)
Exemple #6
0
def set_a_declaration(code, framework_slug):
    framework = Framework.query.filter(
        Framework.slug == framework_slug).first_or_404()

    supplier_framework = SupplierFramework.find_by_supplier_and_framework(
        code, framework_slug)
    supplier = None
    if supplier_framework is not None:
        status_code = 200 if supplier_framework.declaration else 201
    else:
        supplier = Supplier.query.filter(Supplier.code == code).first_or_404()

        supplier_framework = SupplierFramework(supplier_code=supplier.code,
                                               framework_id=framework.id,
                                               declaration={})
        status_code = 201

    request_data = get_json_from_request()
    updater_json = validate_and_return_updater_request()
    json_has_required_keys(request_data, ['declaration'])

    supplier_framework.declaration = request_data['declaration'] or {}
    db.session.add(supplier_framework)
    db.session.add(
        AuditEvent(audit_type=AuditTypes.answer_selection_questions,
                   db_object=supplier_framework,
                   user=updater_json['updated_by'],
                   data={'update': request_data['declaration']}))

    try:
        db.session.commit()
        if supplier:
            publish_tasks.supplier.delay(
                publish_tasks.compress_supplier(supplier),
                'set_declaration',
                updated_by=updater_json['updated_by'])
    except IntegrityError as e:
        db.session.rollback()
        abort(400, "Database Error: {}".format(e))

    return jsonify(declaration=supplier_framework.declaration), status_code
def accept_agreement(user_info):
    supplier_code = user_info.get('supplier_code')
    email_address = user_info.get('email_address')
    user_id = user_info.get('user_id')

    supplier = suppliers.get_supplier_by_code(supplier_code)
    mandatory_supplier_checks(supplier)

    if email_address != supplier.data.get('email'):
        raise UnauthorisedError('Unauthorised to accept agreement')

    agreement = get_current_agreement()
    if agreement is None:
        raise NotFoundError('Current master agreement not found')

    already_signed = signed_agreement_service.first(
        agreement_id=agreement.id, supplier_code=supplier_code)
    if already_signed:
        raise ValidationError('Already signed agreement')

    signed_agreement = SignedAgreement(
        agreement_id=agreement.id,
        user_id=user_id,
        signed_at=pendulum.now('Australia/Canberra'),
        supplier_code=supplier_code)
    signed_agreement_service.save(signed_agreement)

    publish_tasks.supplier.delay(publish_tasks.compress_supplier(supplier),
                                 'accepted_agreement',
                                 updated_by=email_address)

    audit_service.log_audit_event(
        audit_type=audit_types.accepted_master_agreement,
        user=email_address,
        data={
            'supplierCode': supplier.code,
            'supplierData': supplier.data
        },
        db_object=supplier)