<commit_before>from flask import jsonify, abort, request, current_app
from sqlalchemy.exc import IntegrityError, DataError
from .. import main
from ... import db
from ...models import Supplier, ContactInformation, AuditEvent, Service, SupplierFramework, Framework
from ...validation import (
    validate_supplier_json_or_400,
    validate_contact_information_json_or_400,
    is_valid_string_or_400
)
from ...utils import pagination_links, drop_foreign_fields, get_json_from_request, \
    json_has_required_keys, json_has_matching_id, get_valid_page_or_1
from ...service_utils import validate_and_return_updater_request
from ...supplier_utils import validate_and_return_supplier_request
from dmutils.audit import AuditTypes

@main.route('/suppliers', methods=['GET'])
def list_suppliers():
    page = get_valid_page_or_1()

    prefix = request.args.get('prefix', '')

    framework = request.args.get('framework')

    duns_number = request.args.get('duns_number')

    if framework:
        is_valid_string_or_400(framework)

        suppliers = Supplier.query.join(
            Service.supplier, Service.framework
        ).filter(
            Framework.status == 'live',
            Framework.framework == framework,
            Service.status == 'published'
        ).order_by(Supplier.name, Supplier.supplier_id)
    else:
        suppliers = Supplier.query.order_by(Supplier.name, Supplier.supplier_id)

    if duns_number:
        is_valid_string_or_400(duns_number)
        suppliers = suppliers.filter(
            Supplier.duns_number == duns_number
        )

    if prefix:
        if prefix == 'other':
            suppliers = suppliers.filter(
                Supplier.name.op('~')('^[^A-Za-z]'))
        else:
            # case insensitive LIKE comparison for matching supplier names
            suppliers = suppliers.filter(
                Supplier.name.ilike(prefix + '%'))

    suppliers = suppliers.distinct(Supplier.name, Supplier.supplier_id)

    try:
        suppliers = suppliers.paginate(
            page=page,
            per_page=current_app.config['DM_API_SUPPLIERS_PAGE_SIZE'],
        )

        return jsonify(
            suppliers=[supplier.serialize() for supplier in suppliers.items],
            links=pagination_links(
                suppliers,
                '.list_suppliers',
                request.args
            ))
    except DataError:
        abort(400, 'invalid framework')

@main.route('/suppliers/<int:supplier_id>', methods=['GET'])
def get_supplier(supplier_id):
    supplier = Supplier.query.filter(
        Supplier.supplier_id == supplier_id
    ).first_or_404()

    service_counts = supplier.get_service_counts()

    return jsonify(suppliers=supplier.serialize({
        'service_counts': service_counts
    }))

@main.route('/suppliers/<int:supplier_id>', methods=['PUT'])
def import_supplier(supplier_id):
    supplier_data = validate_and_return_supplier_request(supplier_id)

    contact_informations_data = supplier_data['contactInformation']
    supplier_data = drop_foreign_fields(
        supplier_data,
        ['contactInformation']
    )

    supplier = Supplier.query.filter(
        Supplier.supplier_id == supplier_data['id']
    ).first()

    if supplier is None:
        supplier = Supplier(supplier_id=supplier_data['id'])

    # if a supplier was found, remove all contact information
    else:
        for contact in supplier.contact_information:
            db.session.delete(contact)

    supplier.update_from_json(supplier_data)

    for contact_information_data in contact_informations_data:
        contact_information = ContactInformation.from_json(contact_information_data)
        supplier.contact_information.append(contact_information)

        db.session.add(supplier)

    try:
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        abort(400, "Database Error: {0}".format(e))

    return jsonify(suppliers=supplier.serialize()), 201

@main.route('/suppliers', methods=['POST'])
def create_supplier():
    supplier_data = validate_and_return_supplier_request()

    contact_informations_data = supplier_data['contactInformation']
    supplier_data = drop_foreign_fields(
        supplier_data,
        ['contactInformation']
    )

    supplier = Supplier()
    supplier.update_from_json(supplier_data)

    for contact_information_data in contact_informations_data:
        contact_information = ContactInformation.from_json(contact_information_data)
        supplier.contact_information.append(contact_information)

    try:
        db.session.add(supplier)
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        return jsonify(message="Database Error: {0}".format(e)), 400

    return jsonify(suppliers=supplier.serialize()), 201

@main.route('/suppliers/<int:supplier_id>', methods=['POST'])
def update_supplier(supplier_id):
    request_data = get_json_from_request()

    supplier = Supplier.query.filter(
        Supplier.supplier_id == supplier_id
    ).first()

    if supplier is None:
        abort(404, "supplier_id '%d' not found" % supplier_id)

    json_has_required_keys(
        request_data,
        ['suppliers', 'updated_by']
    )

    supplier_data = supplier.serialize()
    supplier_data.update(request_data['suppliers'])
    supplier_data = drop_foreign_fields(
        supplier_data,
        ['links', 'contactInformation']
    )

    validate_supplier_json_or_400(supplier_data)
    json_has_matching_id(supplier_data, supplier_id)

    supplier.update_from_json(supplier_data)

    db.session.add(supplier)
    db.session.add(
        AuditEvent(
            audit_type=AuditTypes.supplier_update,
            db_object=supplier,
            user=request_data['updated_by'],
            data={'update': request_data['suppliers']})
    )

    try:
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        abort(400, "Database Error: {0}".format(e))

    return jsonify(suppliers=supplier.serialize())

@main.route(
    '/suppliers/<int:supplier_id>/contact-information/<int:contact_id>',
    methods=['POST'])
def update_contact_information(supplier_id, contact_id):
    request_data = get_json_from_request()

    contact = ContactInformation.query.filter(
        ContactInformation.id == contact_id,
        ContactInformation.supplier_id == supplier_id,
    ).first()

    if contact is None:
        abort(404, "contact_id '%d' not found" % contact_id)

    json_has_required_keys(request_data, [
        'contactInformation', 'updated_by'
    ])

    contact_data = contact.serialize()
    contact_data.update(request_data['contactInformation'])
    contact_data = drop_foreign_fields(
        contact_data,
        ['links']
    )

    validate_contact_information_json_or_400(contact_data)
    json_has_matching_id(contact_data, contact_id)

    contact.update_from_json(contact_data)

    db.session.add(contact)
    db.session.add(
        AuditEvent(
            audit_type=AuditTypes.contact_update,
            db_object=contact.supplier,
            user=request_data['updated_by'],
            data={'update': request_data['contactInformation']})
    )

    try:
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        abort(400, "Database Error: {0}".format(e))

    return jsonify(contactInformation=contact.serialize())

@main.route('/suppliers/<supplier_id>/frameworks/<framework_slug>/declaration', methods=['GET'])
def get_a_declaration(supplier_id, framework_slug):
    supplier_framework = SupplierFramework.find_by_supplier_and_framework(
        supplier_id, framework_slug
    )
    if supplier_framework is None:
        abort(404)

    return jsonify(declaration=supplier_framework.declaration)

@main.route('/suppliers/<supplier_id>/frameworks/<framework_slug>/declaration', methods=['PUT'])
def set_a_declaration(supplier_id, framework_slug):
    framework = Framework.query.filter(
        Framework.slug == framework_slug
    ).first_or_404()

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

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

    request_data = get_json_from_request()
    json_has_required_keys(request_data, ['declaration', 'updated_by'])

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

    try:
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        abort(400, "Database Error: {}".format(e))

    return jsonify(declaration=supplier_framework.declaration), status_code

@main.route('/suppliers/<supplier_id>/frameworks/interest', methods=['GET'])
def get_registered_frameworks(supplier_id):
    supplier_frameworks = SupplierFramework.query.filter(
        SupplierFramework.supplier_id == supplier_id
    ).all()
    slugs = []
    for framework in supplier_frameworks:
        framework = Framework.query.filter(
            Framework.id == framework.framework_id
        ).first()
        slugs.append(framework.slug)

    return jsonify(frameworks=slugs)

@main.route('/suppliers/<supplier_id>/frameworks/<framework_slug>', methods=['GET'])
def get_supplier_framework_info(supplier_id, framework_slug):
    supplier_framework = SupplierFramework.find_by_supplier_and_framework(
        supplier_id, framework_slug
    )
    if supplier_framework is None:
        abort(404)

    return jsonify(frameworkInterest=supplier_framework.serialize())

# TODO: deprecated - remove route once all frontend apps are using utils version 10.8.0 or higher
@main.route('/suppliers/<supplier_id>/frameworks/<framework_slug>/interest', methods=['POST'])
def register_interest_in_framework(supplier_id, framework_slug):
    current_app.logger.warning("Deprecated /suppliers/<supplier_id>/frameworks/<framework_slug>/interest route")
    updater_json = validate_and_return_updater_request()

    framework = Framework.query.filter(
        Framework.slug == framework_slug
    ).first_or_404()

    if framework.status != 'open':
        abort(400, "'{}' framework is not open".format(framework_slug))

    supplier = Supplier.query.filter(
        Supplier.supplier_id == supplier_id
    ).first()

    if supplier is None:
        abort(404, "supplier_id '{}' not found".format(supplier_id))

    interest_record = SupplierFramework.query.filter(
        SupplierFramework.supplier_id == supplier_id,
        SupplierFramework.framework_id == framework.id
    ).first()

    if interest_record:
        return jsonify(frameworkInterest=interest_record.serialize()), 200
    else:
        interest_record = SupplierFramework(
            supplier_id=supplier_id,
            framework_id=framework.id
        )
        audit_event = AuditEvent(
            audit_type=AuditTypes.register_framework_interest,
            user=updater_json.get('user'),
            data={'supplierId': supplier_id, 'frameworkSlug': framework_slug},
            db_object=supplier
        )

        try:
            db.session.add(interest_record)
            db.session.add(audit_event)
            db.session.commit()
        except IntegrityError as e:
            db.session.rollback()
            return jsonify(message="Database Error: {0}".format(e)), 400

        return jsonify(frameworkInterest=interest_record.serialize()), 201

@main.route('/suppliers/<supplier_id>/frameworks/<framework_slug>', methods=['POST'])
def update_supplier_framework_details(supplier_id, framework_slug):
    updater_json = validate_and_return_updater_request()

    framework = Framework.query.filter(
        Framework.slug == framework_slug
    ).first_or_404()

    supplier = Supplier.query.filter(
        Supplier.supplier_id == supplier_id
    ).first()

    if supplier is None:
        abort(404, "supplier_id '{}' not found".format(supplier_id))

    json_payload = get_json_from_request()
    json_has_required_keys(json_payload, ["update"])
    update_json = json_payload["update"]

    interest_record = SupplierFramework.query.filter(
        SupplierFramework.supplier_id == supplier_id,
        SupplierFramework.framework_id == framework.id
    ).first()

    if interest_record:
        # Updating an existing supplier_frameworks entry
        audit_event = AuditEvent(
            audit_type=AuditTypes.supplier_update,
            user=updater_json.get('updated_by'),
            data={'supplierId': supplier.supplier_id, 'frameworkSlug': framework_slug, 'update': update_json},
            db_object=supplier
        )
        status_code = 200

    else:
        # Registering interest for the first time
        if framework.status != 'open':
            abort(400, "'{}' framework is not open".format(framework_slug))
        interest_record = SupplierFramework(
            supplier_id=supplier_id,
            framework_id=framework.id
        )
        audit_event = AuditEvent(
            audit_type=AuditTypes.register_framework_interest,
            user=updater_json.get('updated_by'),
            data={'supplierId': supplier.supplier_id, 'frameworkSlug': framework_slug},
            db_object=supplier
        )
        status_code = 201

    if 'onFramework' in update_json:
        interest_record.on_framework = update_json['onFramework']
        if interest_record.on_framework is True and interest_record.agreement_returned is None:
            interest_record.agreement_returned = False
    if 'agreementReturned' in update_json:
        interest_record.agreement_returned = update_json['agreementReturned']
    try:
        db.session.add(interest_record)
        db.session.add(audit_event)
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        return jsonify(message="Database Error: {0}".format(e)), 400

    return jsonify(frameworkInterest=interest_record.serialize()), status_code
<commit_msg>Deprecate GET declaration route<commit_after>from flask import jsonify, abort, request, current_app
from sqlalchemy.exc import IntegrityError, DataError
from .. import main
from ... import db
from ...models import Supplier, ContactInformation, AuditEvent, Service, SupplierFramework, Framework
from ...validation import (
    validate_supplier_json_or_400,
    validate_contact_information_json_or_400,
    is_valid_string_or_400
)
from ...utils import pagination_links, drop_foreign_fields, get_json_from_request, \
    json_has_required_keys, json_has_matching_id, get_valid_page_or_1
from ...service_utils import validate_and_return_updater_request
from ...supplier_utils import validate_and_return_supplier_request
from dmutils.audit import AuditTypes

@main.route('/suppliers', methods=['GET'])
def list_suppliers():
    page = get_valid_page_or_1()

    prefix = request.args.get('prefix', '')

    framework = request.args.get('framework')

    duns_number = request.args.get('duns_number')

    if framework:
        is_valid_string_or_400(framework)

        suppliers = Supplier.query.join(
            Service.supplier, Service.framework
        ).filter(
            Framework.status == 'live',
            Framework.framework == framework,
            Service.status == 'published'
        ).order_by(Supplier.name, Supplier.supplier_id)
    else:
        suppliers = Supplier.query.order_by(Supplier.name, Supplier.supplier_id)

    if duns_number:
        is_valid_string_or_400(duns_number)
        suppliers = suppliers.filter(
            Supplier.duns_number == duns_number
        )

    if prefix:
        if prefix == 'other':
            suppliers = suppliers.filter(
                Supplier.name.op('~')('^[^A-Za-z]'))
        else:
            # case insensitive LIKE comparison for matching supplier names
            suppliers = suppliers.filter(
                Supplier.name.ilike(prefix + '%'))

    suppliers = suppliers.distinct(Supplier.name, Supplier.supplier_id)

    try:
        suppliers = suppliers.paginate(
            page=page,
            per_page=current_app.config['DM_API_SUPPLIERS_PAGE_SIZE'],
        )

        return jsonify(
            suppliers=[supplier.serialize() for supplier in suppliers.items],
            links=pagination_links(
                suppliers,
                '.list_suppliers',
                request.args
            ))
    except DataError:
        abort(400, 'invalid framework')

@main.route('/suppliers/<int:supplier_id>', methods=['GET'])
def get_supplier(supplier_id):
    supplier = Supplier.query.filter(
        Supplier.supplier_id == supplier_id
    ).first_or_404()

    service_counts = supplier.get_service_counts()

    return jsonify(suppliers=supplier.serialize({
        'service_counts': service_counts
    }))

@main.route('/suppliers/<int:supplier_id>', methods=['PUT'])
def import_supplier(supplier_id):
    supplier_data = validate_and_return_supplier_request(supplier_id)

    contact_informations_data = supplier_data['contactInformation']
    supplier_data = drop_foreign_fields(
        supplier_data,
        ['contactInformation']
    )

    supplier = Supplier.query.filter(
        Supplier.supplier_id == supplier_data['id']
    ).first()

    if supplier is None:
        supplier = Supplier(supplier_id=supplier_data['id'])

    # if a supplier was found, remove all contact information
    else:
        for contact in supplier.contact_information:
            db.session.delete(contact)

    supplier.update_from_json(supplier_data)

    for contact_information_data in contact_informations_data:
        contact_information = ContactInformation.from_json(contact_information_data)
        supplier.contact_information.append(contact_information)

        db.session.add(supplier)

    try:
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        abort(400, "Database Error: {0}".format(e))

    return jsonify(suppliers=supplier.serialize()), 201

@main.route('/suppliers', methods=['POST'])
def create_supplier():
    supplier_data = validate_and_return_supplier_request()

    contact_informations_data = supplier_data['contactInformation']
    supplier_data = drop_foreign_fields(
        supplier_data,
        ['contactInformation']
    )

    supplier = Supplier()
    supplier.update_from_json(supplier_data)

    for contact_information_data in contact_informations_data:
        contact_information = ContactInformation.from_json(contact_information_data)
        supplier.contact_information.append(contact_information)

    try:
        db.session.add(supplier)
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        return jsonify(message="Database Error: {0}".format(e)), 400

    return jsonify(suppliers=supplier.serialize()), 201

@main.route('/suppliers/<int:supplier_id>', methods=['POST'])
def update_supplier(supplier_id):
    request_data = get_json_from_request()

    supplier = Supplier.query.filter(
        Supplier.supplier_id == supplier_id
    ).first()

    if supplier is None:
        abort(404, "supplier_id '%d' not found" % supplier_id)

    json_has_required_keys(
        request_data,
        ['suppliers', 'updated_by']
    )

    supplier_data = supplier.serialize()
    supplier_data.update(request_data['suppliers'])
    supplier_data = drop_foreign_fields(
        supplier_data,
        ['links', 'contactInformation']
    )

    validate_supplier_json_or_400(supplier_data)
    json_has_matching_id(supplier_data, supplier_id)

    supplier.update_from_json(supplier_data)

    db.session.add(supplier)
    db.session.add(
        AuditEvent(
            audit_type=AuditTypes.supplier_update,
            db_object=supplier,
            user=request_data['updated_by'],
            data={'update': request_data['suppliers']})
    )

    try:
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        abort(400, "Database Error: {0}".format(e))

    return jsonify(suppliers=supplier.serialize())

@main.route(
    '/suppliers/<int:supplier_id>/contact-information/<int:contact_id>',
    methods=['POST'])
def update_contact_information(supplier_id, contact_id):
    request_data = get_json_from_request()

    contact = ContactInformation.query.filter(
        ContactInformation.id == contact_id,
        ContactInformation.supplier_id == supplier_id,
    ).first()

    if contact is None:
        abort(404, "contact_id '%d' not found" % contact_id)

    json_has_required_keys(request_data, [
        'contactInformation', 'updated_by'
    ])

    contact_data = contact.serialize()
    contact_data.update(request_data['contactInformation'])
    contact_data = drop_foreign_fields(
        contact_data,
        ['links']
    )

    validate_contact_information_json_or_400(contact_data)
    json_has_matching_id(contact_data, contact_id)

    contact.update_from_json(contact_data)

    db.session.add(contact)
    db.session.add(
        AuditEvent(
            audit_type=AuditTypes.contact_update,
            db_object=contact.supplier,
            user=request_data['updated_by'],
            data={'update': request_data['contactInformation']})
    )

    try:
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        abort(400, "Database Error: {0}".format(e))

    return jsonify(contactInformation=contact.serialize())

# TODO: deprecated - remove route once all frontend apps are using utils version 10.8.0 or higher
@main.route('/suppliers/<supplier_id>/frameworks/<framework_slug>/declaration', methods=['GET'])
def get_a_declaration(supplier_id, framework_slug):
    current_app.logger.warning("Deprecated /suppliers/<supplier_id>/frameworks/<framework_slug>/declaration route")
    supplier_framework = SupplierFramework.find_by_supplier_and_framework(
        supplier_id, framework_slug
    )
    if supplier_framework is None:
        abort(404)

    return jsonify(declaration=supplier_framework.declaration)

@main.route('/suppliers/<supplier_id>/frameworks/<framework_slug>/declaration', methods=['PUT'])
def set_a_declaration(supplier_id, framework_slug):
    framework = Framework.query.filter(
        Framework.slug == framework_slug
    ).first_or_404()

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

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

    request_data = get_json_from_request()
    json_has_required_keys(request_data, ['declaration', 'updated_by'])

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

    try:
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        abort(400, "Database Error: {}".format(e))

    return jsonify(declaration=supplier_framework.declaration), status_code

@main.route('/suppliers/<supplier_id>/frameworks/interest', methods=['GET'])
def get_registered_frameworks(supplier_id):
    supplier_frameworks = SupplierFramework.query.filter(
        SupplierFramework.supplier_id == supplier_id
    ).all()
    slugs = []
    for framework in supplier_frameworks:
        framework = Framework.query.filter(
            Framework.id == framework.framework_id
        ).first()
        slugs.append(framework.slug)

    return jsonify(frameworks=slugs)

@main.route('/suppliers/<supplier_id>/frameworks/<framework_slug>', methods=['GET'])
def get_supplier_framework_info(supplier_id, framework_slug):
    supplier_framework = SupplierFramework.find_by_supplier_and_framework(
        supplier_id, framework_slug
    )
    if supplier_framework is None:
        abort(404)

    return jsonify(frameworkInterest=supplier_framework.serialize())

# TODO: deprecated - remove route once all frontend apps are using utils version 10.8.0 or higher
@main.route('/suppliers/<supplier_id>/frameworks/<framework_slug>/interest', methods=['POST'])
def register_interest_in_framework(supplier_id, framework_slug):
    current_app.logger.warning("Deprecated /suppliers/<supplier_id>/frameworks/<framework_slug>/interest route")
    updater_json = validate_and_return_updater_request()

    framework = Framework.query.filter(
        Framework.slug == framework_slug
    ).first_or_404()

    if framework.status != 'open':
        abort(400, "'{}' framework is not open".format(framework_slug))

    supplier = Supplier.query.filter(
        Supplier.supplier_id == supplier_id
    ).first()

    if supplier is None:
        abort(404, "supplier_id '{}' not found".format(supplier_id))

    interest_record = SupplierFramework.query.filter(
        SupplierFramework.supplier_id == supplier_id,
        SupplierFramework.framework_id == framework.id
    ).first()

    if interest_record:
        return jsonify(frameworkInterest=interest_record.serialize()), 200
    else:
        interest_record = SupplierFramework(
            supplier_id=supplier_id,
            framework_id=framework.id
        )
        audit_event = AuditEvent(
            audit_type=AuditTypes.register_framework_interest,
            user=updater_json.get('user'),
            data={'supplierId': supplier_id, 'frameworkSlug': framework_slug},
            db_object=supplier
        )

        try:
            db.session.add(interest_record)
            db.session.add(audit_event)
            db.session.commit()
        except IntegrityError as e:
            db.session.rollback()
            return jsonify(message="Database Error: {0}".format(e)), 400

        return jsonify(frameworkInterest=interest_record.serialize()), 201

@main.route('/suppliers/<supplier_id>/frameworks/<framework_slug>', methods=['POST'])
def update_supplier_framework_details(supplier_id, framework_slug):
    updater_json = validate_and_return_updater_request()

    framework = Framework.query.filter(
        Framework.slug == framework_slug
    ).first_or_404()

    supplier = Supplier.query.filter(
        Supplier.supplier_id == supplier_id
    ).first()

    if supplier is None:
        abort(404, "supplier_id '{}' not found".format(supplier_id))

    json_payload = get_json_from_request()
    json_has_required_keys(json_payload, ["update"])
    update_json = json_payload["update"]

    interest_record = SupplierFramework.query.filter(
        SupplierFramework.supplier_id == supplier_id,
        SupplierFramework.framework_id == framework.id
    ).first()

    if interest_record:
        # Updating an existing supplier_frameworks entry
        audit_event = AuditEvent(
            audit_type=AuditTypes.supplier_update,
            user=updater_json.get('updated_by'),
            data={'supplierId': supplier.supplier_id, 'frameworkSlug': framework_slug, 'update': update_json},
            db_object=supplier
        )
        status_code = 200

    else:
        # Registering interest for the first time
        if framework.status != 'open':
            abort(400, "'{}' framework is not open".format(framework_slug))
        interest_record = SupplierFramework(
            supplier_id=supplier_id,
            framework_id=framework.id
        )
        audit_event = AuditEvent(
            audit_type=AuditTypes.register_framework_interest,
            user=updater_json.get('updated_by'),
            data={'supplierId': supplier.supplier_id, 'frameworkSlug': framework_slug},
            db_object=supplier
        )
        status_code = 201

    if 'onFramework' in update_json:
        interest_record.on_framework = update_json['onFramework']
        if interest_record.on_framework is True and interest_record.agreement_returned is None:
            interest_record.agreement_returned = False
    if 'agreementReturned' in update_json:
        interest_record.agreement_returned = update_json['agreementReturned']
    try:
        db.session.add(interest_record)
        db.session.add(audit_event)
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        return jsonify(message="Database Error: {0}".format(e)), 400

    return jsonify(frameworkInterest=interest_record.serialize()), status_code
<|endoftext|>


<commit_before>#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Helpers functions for updating different kinds of models in datastore.
"""

__authors__ = [
  '"Todd Larsen" <tlarsen@google.com>',
  '"Sverre Rabbelier" <sverer@rabbelier.nl>',
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
  ]

from google.appengine.ext import db

from soc.logic import out_of_band

class Logic:
  """Base logic for entity classes.

  The BaseLogic class functions specific to Entity classes by relying
  on the the child-classes to implement _model, _name and _key_name
  """

  def _updateField(self, model, name, value):
    """Hook called when a field is updated.

    Base classes should override if any special actions need to be
    taken when a field is updated. The field is not updated if the
    method does not return a True value.
    """

    return True

  def getFromKeyName(self, key_name):
    """"Returns User entity for key_name or None if not found.
-
-    Args:
-      key_name: key name of entity
    """

    return self._model.get_by_key_name(key_name)

  def getFromFields(self, **kwargs):
    """Returns the entity for a given link name, or None if not found.

    Args:
      **kwargs: the fields of the entity that uniquely identifies it
    """

    key_name = self.getKeyNameForFields(**kwargs)

    if key_name:
      entity = self._model.get_by_key_name(key_name)
    else:
      entity = None

    return entity

  def getIfFields(self, **kwargs):
    """Returns entity for supplied link name if one exists.

    Args:
      **kwargs: the fields of the entity that uniquely identifies it

    Returns:
      * None if a field is false.
      * Entity for supplied fields

    Raises:
      out_of_band.ErrorResponse if link name is not false, but no entity
      with the supplied link name exists in the Datastore
    """

    if not all(kwargs.values()):
      # exit without error, to let view know that link_name was not supplied
      return None

    entity = self.getFromFields(**kwargs)

    if entity:
      # an entity exist for this link_name, so return that entity
      return entity

    fields = []

    for key, value in kwargs.iteritems():
      fields.extend('"%s" is "%s" ' % (key, value))

    # else: fields were supplied, but there is no Entity that has it
    raise out_of_band.ErrorResponse(
        'There is no %s with %s.' % (self._name, ''.join(fields)), status=404)

  def getKeyNameForFields(self, **kwargs):
    """Return a Datastore key_name for a Entity from the specified fields.

    Args:
      **kwargs: the fields of the entity that uniquely identifies it
    """

    if not all(kwargs.values()):
      return None

    return self._keyName(**kwargs)

  def extractKeyFields(self, fields):
    """Extracts all the fields from that are in the mode's key_fields property

    Args:
      fields: A dict from which the fields should be extracted
    """

    key_fields = {}

    for key, value in fields.iteritems():
      if key in self._model.key_fields:
        key_fields[key] = value

    return key_fields

  def getForLimitAndOffset(self, limit, offset=0):
    """Returns entities for given offset and limit or None if not found.

    Args:
      limit: max amount of entities to return
      offset: optional offset in entities list which defines first entity to
        return; default is zero (first entity)
    """

    query = self._model.all()
    return query.fetch(limit, offset)

  def updateModelProperties(self, model, **model_properties):
    """Update existing model entity using supplied model properties.

    Args:
      model: a model entity
      **model_properties: keyword arguments that correspond to model entity
        properties and their values

    Returns:
      the original model entity with any supplied properties changed
    """

    def update():
      return self._unsafeUpdateModelProperties(model, **model_properties)

    return db.run_in_transaction(update)

  def _unsafeUpdateModelProperties(self, model, **model_properties):
    """(see updateModelProperties)

    Like updateModelProperties(), but not run within a transaction.
    """

    properties = model.properties()

    for prop in properties.values():
      name = prop.name

      if not name in self._skip_properties and name in model_properties:
        value = model_properties[prop.name]

        if self._updateField(model, name, value):
          prop.__set__(model, value)

    model.put()
    return model

  def updateOrCreateFromKeyName(self, properties, key_name):
    """Update existing entity, or create new one with supplied properties.

    Args:
      properties: dict with entity properties and their values
      key_name: the key_name of the entity that uniquely identifies it

    Returns:
      the entity corresponding to the key_name, with any supplied
      properties changed, or a new entity now associated with the
      supplied key_name and properties.
    """

    entity = self.getFromKeyName(key_name)

    if not entity:
      # entity did not exist, so create one in a transaction
      entity = self._model.get_or_insert(key_name, **properties)

    # there is no way to be sure if get_or_insert() returned a new entity or
    # got an existing one due to a race, so update with properties anyway,
    # in a transaction
    return self.updateModelProperties(entity, **properties)

  def updateOrCreateFromFields(self, properties, **kwargs):
    """Like updateOrCreateFromKeyName, but resolves **kwargs to a key_name first.
    """

    # attempt to retrieve the existing entity
    key_name  = self.getKeyNameForFields(**kwargs)

    return self.updateOrCreateFromKeyName(properties, key_name)

  def isDeletable(self, entity):
    """Returns whether the specified entity can be deleted.

    Args:
      entity: an existing entity in datastore
    """

    return True

  def delete(self, entity):
    """Delete existing entity from datastore.

    Args:
      entity: an existing entity in datastore
    """

    entity.delete()
<commit_msg>Improve the error message for getIfFields<commit_after>#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Helpers functions for updating different kinds of models in datastore.
"""

__authors__ = [
  '"Todd Larsen" <tlarsen@google.com>',
  '"Sverre Rabbelier" <sverer@rabbelier.nl>',
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
  ]

from google.appengine.ext import db

from soc.logic import out_of_band

class Logic:
  """Base logic for entity classes.

  The BaseLogic class functions specific to Entity classes by relying
  on the the child-classes to implement _model, _name and _key_name
  """

  def _updateField(self, model, name, value):
    """Hook called when a field is updated.

    Base classes should override if any special actions need to be
    taken when a field is updated. The field is not updated if the
    method does not return a True value.
    """

    return True

  def getFromKeyName(self, key_name):
    """"Returns User entity for key_name or None if not found.
-
-    Args:
-      key_name: key name of entity
    """

    return self._model.get_by_key_name(key_name)

  def getFromFields(self, **kwargs):
    """Returns the entity for a given link name, or None if not found.

    Args:
      **kwargs: the fields of the entity that uniquely identifies it
    """

    key_name = self.getKeyNameForFields(**kwargs)

    if key_name:
      entity = self._model.get_by_key_name(key_name)
    else:
      entity = None

    return entity

  def getIfFields(self, **kwargs):
    """Returns entity for supplied link name if one exists.

    Args:
      **kwargs: the fields of the entity that uniquely identifies it

    Returns:
      * None if a field is false.
      * Entity for supplied fields

    Raises:
      out_of_band.ErrorResponse if link name is not false, but no entity
      with the supplied link name exists in the Datastore
    """

    if not all(kwargs.values()):
      # exit without error, to let view know that link_name was not supplied
      return None

    entity = self.getFromFields(**kwargs)

    if entity:
      # an entity exist for this link_name, so return that entity
      return entity

    fields = []

    msg = 'There is no %s where ' % self._name

    for index, pair in enumerate(kwargs.iteritems()):
      if index != 0:
        msg += ' and '

      msg += '"%s" is "%s"' % pair

    msg += '.'

    # else: fields were supplied, but there is no Entity that has it
    raise out_of_band.ErrorResponse(msg, status=404)

  def getKeyNameForFields(self, **kwargs):
    """Return a Datastore key_name for a Entity from the specified fields.

    Args:
      **kwargs: the fields of the entity that uniquely identifies it
    """

    if not all(kwargs.values()):
      return None

    return self._keyName(**kwargs)

  def extractKeyFields(self, fields):
    """Extracts all the fields from that are in the mode's key_fields property

    Args:
      fields: A dict from which the fields should be extracted
    """

    key_fields = {}

    for key, value in fields.iteritems():
      if key in self._model.key_fields:
        key_fields[key] = value

    return key_fields

  def getForLimitAndOffset(self, limit, offset=0):
    """Returns entities for given offset and limit or None if not found.

    Args:
      limit: max amount of entities to return
      offset: optional offset in entities list which defines first entity to
        return; default is zero (first entity)
    """

    query = self._model.all()
    return query.fetch(limit, offset)

  def updateModelProperties(self, model, **model_properties):
    """Update existing model entity using supplied model properties.

    Args:
      model: a model entity
      **model_properties: keyword arguments that correspond to model entity
        properties and their values

    Returns:
      the original model entity with any supplied properties changed
    """

    def update():
      return self._unsafeUpdateModelProperties(model, **model_properties)

    return db.run_in_transaction(update)

  def _unsafeUpdateModelProperties(self, model, **model_properties):
    """(see updateModelProperties)

    Like updateModelProperties(), but not run within a transaction.
    """

    properties = model.properties()

    for prop in properties.values():
      name = prop.name

      if not name in self._skip_properties and name in model_properties:
        value = model_properties[prop.name]

        if self._updateField(model, name, value):
          prop.__set__(model, value)

    model.put()
    return model

  def updateOrCreateFromKeyName(self, properties, key_name):
    """Update existing entity, or create new one with supplied properties.

    Args:
      properties: dict with entity properties and their values
      key_name: the key_name of the entity that uniquely identifies it

    Returns:
      the entity corresponding to the key_name, with any supplied
      properties changed, or a new entity now associated with the
      supplied key_name and properties.
    """

    entity = self.getFromKeyName(key_name)

    if not entity:
      # entity did not exist, so create one in a transaction
      entity = self._model.get_or_insert(key_name, **properties)

    # there is no way to be sure if get_or_insert() returned a new entity or
    # got an existing one due to a race, so update with properties anyway,
    # in a transaction
    return self.updateModelProperties(entity, **properties)

  def updateOrCreateFromFields(self, properties, **kwargs):
    """Like updateOrCreateFromKeyName, but resolves **kwargs to a key_name first.
    """

    # attempt to retrieve the existing entity
    key_name  = self.getKeyNameForFields(**kwargs)

    return self.updateOrCreateFromKeyName(properties, key_name)

  def isDeletable(self, entity):
    """Returns whether the specified entity can be deleted.

    Args:
      entity: an existing entity in datastore
    """

    return True

  def delete(self, entity):
    """Delete existing entity from datastore.

    Args:
      entity: an existing entity in datastore
    """

    entity.delete()
<|endoftext|>


<commit_before>from datetime import date
from django.utils import unittest
from django.test import Client

from milkman.dairy import milkman

from lakaxita.lost_found.models import Item, Notification
from lakaxita.lost_found.forms import NotificationForm

class ItemTestCase(unittest.TestCase):
    def setUp(self):
        self.jacket = milkman.deliver(Item)
        self.trousers = milkman.deliver(Item)

    def test_recently_lost_first(self):
        self.jacket.lost = date(1, 1, 1)
        self.jacket.save()

        self.trousers.lost = date(1, 1, 2)
        self.trousers.save()

        self.assertEqual(list(Item.objects.all()), [self.trousers, self.jacket])

    def test_has_been_returned(self):
        self.jacket.found = None
        self.assertEqual(self.jacket.has_been_returned, False)

        self.jacket.found = date(1, 1, 1)
        self.assertEqual(self.jacket.has_been_returned, True)

    def test_manager(self):
        self.jacket.found = None
        self.jacket.save()

        self.trousers.found = date(1, 1, 1)
        self.trousers.save()

        self.assertEqual(list(Item.objects.not_returned()), [self.jacket])
        self.assertEqual(list(Item.objects.returned()), [self.trousers])

    def tearDown(self):
        self.jacket.delete()
        self.trousers.delete()

class NotificationTestCase(unittest.TestCase):
    def setUp(self):
        self.hi = milkman.deliver(Notification)
        self.hello = milkman.deliver(Notification)

    def test_last_ones_first(self):
        self.hi.date = date(1, 1, 1)
        self.hi.save()

        self.hello.date = date(1, 1, 2)
        self.hello.save()

        self.assertEqual(
                list(Notification.objects.all()),
                [self.hello, self.hi]
                )

    def tearDown(self):
        self.hi.delete()
        self.hello.delete()

class ItemListTestCase(unittest.TestCase):
    def setUp(self):
        self.url = '/lost_found/'
        self.client = Client()
        self.jacket = milkman.deliver(Item, lost=date(1, 1, 1))
        self.trousers = milkman.deliver(Item, lost=date(1, 1, 2))
        self.response = self.client.get(self.url)

    def test_status(self):
        self.assertEqual(self.response.status_code, 200)

    def test_template(self):
        self.assertEqual(self.response.template[0].name,
                'lost_found/item_list.yammy')

    def test_context(self):
        self.assertEqual(self.response.context['item_list'],
                [self.trousers, self.jacket])

    def tearDown(self):
        self.jacket.delete()
        self.trousers.delete()

class ItemDetailTestCase(unittest.TestCase):
    def setUp(self):
        self.jacket = milkman.deliver(Item, name='jacket')
        self.url = '/lost_found/jacket/'
        self.client = Client()
        self.response = self.client.get(self.url)

    def test_status(self):
        self.assertEqual(self.response.status_code, 200)

    def test_template(self):
        self.assertEqual(self.response.template[0].name,
                'lost_found/item_detail.yammy')

    def test_context(self):
        self.assertEqual(self.response.context['item'], self.jacket)
        self.assertIsInstance(self.response.context['form'], NotificationForm)

    def tearDown(self):
        self.jacket.delete()

class CreateNotificationTestCase(unittest.TestCase):
    def setUp(self):
        self.jacket = milkman.deliver(Item, name='jacket')
        self.url = '/lost_found/jacket/notify/'
        self.client = Client()
        self.response = self.client.get(self.url)

    def test_status(self):
        self.assertEqual(self.response.status_code, 200)

    def test_template(self):
        self.assertEqual(self.response.template.name,
                'lost_found/notify.yammy')

    def test_context(self):
        self.assertEqual(self.response.context['item'], self.jacket)
        self.assertIsInstance(self.response.context['form'], NotificationForm)

    def test_correct_post(self):
        data = {
                'title': 'mine',
                'reply_to': 'test@mail.com',
                'text': 'how are you?',
                'date': '0001-01-01',
                }
        post_response = self.client.post(self.url, data=data)
        self.assertEqual(post_response.status_code, 200)
        self.assertEqual(post_response.content, '')
        self.assertEqual(Notification.objects.filter(
            title='mine', item=self.jacket).count(), 1)

    def test_incorrect_post(self):
        data = {
                'title': 'mine',
                'reply_to': 'NO CORRECT EMAIL',
                'text': 'how are you?',
                'date': '0001-01-01',
                }
        post_response = self.client.post(self.url, data=data)
        self.assertEqual(post_response.status_code, 200)
        self.assertNotEqual(post_response.content, '')

    def tearDown(self):
        self.jacket.delete()
<commit_msg>test cases for lost & found admin<commit_after>from datetime import date
from django.utils import unittest
from django.test import Client
from django.http import HttpRequest, QueryDict
from django.contrib.admin import site

from milkman.dairy import milkman

from lakaxita.lost_found.models import Item, Notification
from lakaxita.lost_found.forms import NotificationForm
from lakaxita.lost_found.admin import (
                                        NotificationInline,
                                        ReturnedItemFilter,
                                        ItemAdmin,
                                        )

class ItemTestCase(unittest.TestCase):
    def setUp(self):
        self.jacket = milkman.deliver(Item)
        self.trousers = milkman.deliver(Item)

    def test_recently_lost_first(self):
        self.jacket.lost = date(1, 1, 1)
        self.jacket.save()

        self.trousers.lost = date(1, 1, 2)
        self.trousers.save()

        self.assertEqual(list(Item.objects.all()), [self.trousers, self.jacket])

    def test_has_been_returned(self):
        self.jacket.found = None
        self.assertEqual(self.jacket.has_been_returned, False)

        self.jacket.found = date(1, 1, 1)
        self.assertEqual(self.jacket.has_been_returned, True)

    def test_manager(self):
        self.jacket.found = None
        self.jacket.save()

        self.trousers.found = date(1, 1, 1)
        self.trousers.save()

        self.assertEqual(list(Item.objects.not_returned()), [self.jacket])
        self.assertEqual(list(Item.objects.returned()), [self.trousers])

    def tearDown(self):
        self.jacket.delete()
        self.trousers.delete()

class NotificationTestCase(unittest.TestCase):
    def setUp(self):
        self.hi = milkman.deliver(Notification)
        self.hello = milkman.deliver(Notification)

    def test_last_ones_first(self):
        self.hi.date = date(1, 1, 1)
        self.hi.save()

        self.hello.date = date(1, 1, 2)
        self.hello.save()

        self.assertEqual(
                list(Notification.objects.all()),
                [self.hello, self.hi]
                )

    def tearDown(self):
        self.hi.delete()
        self.hello.delete()

class ItemListTestCase(unittest.TestCase):
    def setUp(self):
        self.url = '/lost_found/'
        self.client = Client()
        self.jacket = milkman.deliver(Item, lost=date(1, 1, 1))
        self.trousers = milkman.deliver(Item, lost=date(1, 1, 2))
        self.response = self.client.get(self.url)

    def test_status(self):
        self.assertEqual(self.response.status_code, 200)

    def test_template(self):
        self.assertEqual(self.response.template[0].name,
                'lost_found/item_list.yammy')

    def test_context(self):
        self.assertEqual(self.response.context['item_list'],
                [self.trousers, self.jacket])

    def tearDown(self):
        self.jacket.delete()
        self.trousers.delete()

class ItemDetailTestCase(unittest.TestCase):
    def setUp(self):
        self.jacket = milkman.deliver(Item, name='jacket')
        self.url = '/lost_found/jacket/'
        self.client = Client()
        self.response = self.client.get(self.url)

    def test_status(self):
        self.assertEqual(self.response.status_code, 200)

    def test_template(self):
        self.assertEqual(self.response.template[0].name,
                'lost_found/item_detail.yammy')

    def test_context(self):
        self.assertEqual(self.response.context['item'], self.jacket)
        self.assertIsInstance(self.response.context['form'], NotificationForm)

    def tearDown(self):
        self.jacket.delete()

class CreateNotificationTestCase(unittest.TestCase):
    def setUp(self):
        self.jacket = milkman.deliver(Item, name='jacket')
        self.url = '/lost_found/jacket/notify/'
        self.client = Client()
        self.response = self.client.get(self.url)

    def test_status(self):
        self.assertEqual(self.response.status_code, 200)

    def test_template(self):
        self.assertEqual(self.response.template.name,
                'lost_found/notify.yammy')

    def test_context(self):
        self.assertEqual(self.response.context['item'], self.jacket)
        self.assertIsInstance(self.response.context['form'], NotificationForm)

    def test_correct_post(self):
        data = {
                'title': 'mine',
                'reply_to': 'test@mail.com',
                'text': 'how are you?',
                'date': '0001-01-01',
                }
        post_response = self.client.post(self.url, data=data)
        self.assertEqual(post_response.status_code, 200)
        self.assertEqual(post_response.content, '')
        self.assertEqual(Notification.objects.filter(
            title='mine', item=self.jacket).count(), 1)

    def test_incorrect_post(self):
        data = {
                'title': 'mine',
                'reply_to': 'NO CORRECT EMAIL',
                'text': 'how are you?',
                'date': '0001-01-01',
                }
        post_response = self.client.post(self.url, data=data)
        self.assertEqual(post_response.status_code, 200)
        self.assertNotEqual(post_response.content, '')

    def tearDown(self):
        self.jacket.delete()

class AdminTestCase(unittest.TestCase):
    def setUp(self):
        self.jacket = milkman.deliver(Item)
        self.trousers = milkman.deliver(Item)
        self.item_admin = ItemAdmin(Item, site)
        self.notification_inline = NotificationInline(self.item_admin, site)

    def test_no_inline_add_permission(self):
        self.assertFalse(self.notification_inline.has_add_permission(
            HttpRequest()
            ))

    def test_returned_item_filter(self):
        self.jacket.found = date(1, 1, 1)
        self.jacket.save()

        self.trousers.found = None
        self.trousers.save()

        request = HttpRequest()

        returned_filter = ReturnedItemFilter(
                request,
                params={'returned': 'returned'},
                model=Item,
                model_admin=ItemAdmin,
                )

        self.assertEqual(
                list(returned_filter.queryset(request, Item.objects.all())),
                [self.jacket],
                )

    def test_not_returned_item_filter(self):
        self.jacket.found = date(1, 1, 1)
        self.jacket.save()

        self.trousers.found = None
        self.trousers.save()

        request = HttpRequest()

        returned_filter = ReturnedItemFilter(
                request,
                params={'returned': 'not_returned'},
                model=Item,
                model_admin=ItemAdmin,
                )

        self.assertEqual(
                list(returned_filter.queryset(request, Item.objects.all())),
                [self.trousers],
                )

    def test_has_been_returned(self):
        self.trousers.found = date(1, 1, 1)
        self.trousers.save()

        self.assertTrue(self.item_admin.has_been_returned(self.trousers))

        self.trousers.found = None
        self.trousers.save()

        self.assertFalse(self.item_admin.has_been_returned(self.trousers))

    def test_mark_returned(self):
        self.item_admin.mark_returned(HttpRequest(), Item.objects.all())
        jacket, trousers = Item.objects.all()
        self.assertTrue(jacket.has_been_returned)
        self.assertTrue(trousers.has_been_returned)

    def test_mark_not_returned(self):
        self.item_admin.mark_not_returned(HttpRequest(), Item.objects.all())
        jacket, trousers = Item.objects.all()
        self.assertFalse(jacket.has_been_returned)
        self.assertFalse(trousers.has_been_returned)

    def tearDown(self):
        self.jacket.delete()
        self.trousers.delete()
<|endoftext|>


<commit_before># coding: utf-8
# flake8: noqa

"""
ROOT contrib functionality.
"""

__all__ = ["ROOTFormatter", "ROOTNumpyFormatter", "UprootFormatter"]

# provisioning imports
from law.contrib.root.formatter import ROOTFormatter, ROOTNumpyFormatter, UprootFormatter
<commit_msg>Expose GuardedTFile in root contrib package.<commit_after># coding: utf-8
# flake8: noqa

"""
ROOT contrib functionality.
"""

__all__ = ["ROOTFormatter", "ROOTNumpyFormatter", "UprootFormatter", "GuardedTFile"]

# provisioning imports
from law.contrib.root.formatter import ROOTFormatter, ROOTNumpyFormatter, UprootFormatter
<|endoftext|>


<commit_before>"""
Django settings for testproject project.

Generated by 'django-admin startproject' using Django 1.10.6.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*lx!g2o%m)b613n$709334=+ulwi^&6e8=o6h3upwn4&3c$o^p'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'crispy_forms',
    'lbattachment',
    'lbadminlte',
    'lbutils',
    'compressor',
    'djangobower',
    'el_pagination',

    'lbworkflow',
    'lbworkflow.tests.leave',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'lbworkflow.tests.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'testproject.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
    }
}

# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'

LBWF_APPS = {
    'leave': 'lbworkflow.tests.leave',
}

STATIC_ROOT = os.path.join(BASE_DIR, 'collectedstatic')

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

CRISPY_TEMPLATE_PACK = 'bootstrap3'

# bower
STATICFILES_FINDERS += (('djangobower.finders.BowerFinder'),)
BOWER_COMPONENTS_ROOT = BASE_DIR

BOWER_INSTALLED_APPS = (
    'admin-lte#2.3.11',
    'font-awesome#4.7.0',
    'ionicons#2.0.1',

    'modernizr',
    # POLYFILLS: javascript fallback solutions for older browsers.
    # CSS3 selectors for IE 6-8.
    'selectivizr',
    # min/max width media queries for IE 6-8.
    'respond',
    # CSS3 styles for IE 6-8.
    'pie',
    # HTML5 tag support for IE 6-8.
    'html5shiv',

    'masonry#4.1.1',
)

# django-compressor
STATICFILES_FINDERS += (('compressor.finders.CompressorFinder'),)
COMPRESS_PRECOMPILERS = (
    ('text/coffeescript', 'coffee --compile --stdio'),
    ('text/less', 'lessc {infile} {outfile}'),
    ('text/x-sass', 'sass {infile} {outfile}'),
    ('text/x-scss', 'sass --scss {infile} {outfile}'),
)

PROJECT_TITLE = 'LB-Workflow'
<commit_msg>add MIDDLEWARE_CLASSES = MIDDLEWARE for django < 1.10<commit_after>"""
Django settings for testproject project.

Generated by 'django-admin startproject' using Django 1.10.6.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*lx!g2o%m)b613n$709334=+ulwi^&6e8=o6h3upwn4&3c$o^p'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'crispy_forms',
    'lbattachment',
    'lbadminlte',
    'lbutils',
    'compressor',
    'djangobower',
    'el_pagination',

    'lbworkflow',
    'lbworkflow.tests.leave',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

MIDDLEWARE_CLASSES = MIDDLEWARE

ROOT_URLCONF = 'lbworkflow.tests.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'testproject.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
    }
}

# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'

LBWF_APPS = {
    'leave': 'lbworkflow.tests.leave',
}

STATIC_ROOT = os.path.join(BASE_DIR, 'collectedstatic')

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

CRISPY_TEMPLATE_PACK = 'bootstrap3'

# bower
STATICFILES_FINDERS += (('djangobower.finders.BowerFinder'),)
BOWER_COMPONENTS_ROOT = BASE_DIR

BOWER_INSTALLED_APPS = (
    'admin-lte#2.3.11',
    'font-awesome#4.7.0',
    'ionicons#2.0.1',

    'modernizr',
    # POLYFILLS: javascript fallback solutions for older browsers.
    # CSS3 selectors for IE 6-8.
    'selectivizr',
    # min/max width media queries for IE 6-8.
    'respond',
    # CSS3 styles for IE 6-8.
    'pie',
    # HTML5 tag support for IE 6-8.
    'html5shiv',

    'masonry#4.1.1',
)

# django-compressor
STATICFILES_FINDERS += (('compressor.finders.CompressorFinder'),)
COMPRESS_PRECOMPILERS = (
    ('text/coffeescript', 'coffee --compile --stdio'),
    ('text/less', 'lessc {infile} {outfile}'),
    ('text/x-sass', 'sass {infile} {outfile}'),
    ('text/x-scss', 'sass --scss {infile} {outfile}'),
)

PROJECT_TITLE = 'LB-Workflow'
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# django-ldapdb
# Copyright (c) 2009-2011, Bolloré telecom
# Copyright (c) 2013, Jeremy Lainé
# All rights reserved.
#
# See AUTHORS file for a full list of contributors.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     1. Redistributions of source code must retain the above copyright notice,
#        this list of conditions and the following disclaimer.
#
#     2. Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

import ldap
import django

from django.db.backends import (BaseDatabaseFeatures, BaseDatabaseOperations,
                                BaseDatabaseWrapper)
from django.db.backends.creation import BaseDatabaseCreation

class DatabaseCreation(BaseDatabaseCreation):
    def create_test_db(self, verbosity=1, autoclobber=False):
        """
        Creates a test database, prompting the user for confirmation if the
        database already exists. Returns the name of the test database created.
        """
        pass

    def destroy_test_db(self, old_database_name, verbosity=1):
        """
        Destroy a test database, prompting the user for confirmation if the
        database already exists. Returns the name of the test database created.
        """
        pass

class DatabaseCursor(object):
    def __init__(self, ldap_connection):
        self.connection = ldap_connection

class DatabaseFeatures(BaseDatabaseFeatures):
    def __init__(self, connection):
        self.connection = connection
        self.supports_transactions = False

class DatabaseOperations(BaseDatabaseOperations):
    compiler_module = "ldapdb.backends.ldap.compiler"

    def quote_name(self, name):
        return name

class DatabaseWrapper(BaseDatabaseWrapper):
    vendor = 'ldap'

    def __init__(self, *args, **kwargs):
        super(DatabaseWrapper, self).__init__(*args, **kwargs)

        self.charset = "utf-8"
        self.creation = DatabaseCreation(self)
        self.features = DatabaseFeatures(self)
        if django.VERSION > (1, 4):
            self.ops = DatabaseOperations(self)
        else:
            self.ops = DatabaseOperations()
        self.settings_dict['SUPPORTS_TRANSACTIONS'] = False

    def close(self):
        if hasattr(self, 'validate_thread_sharing'):
            # django >= 1.4
            self.validate_thread_sharing()
        if self.connection is not None:
            self.connection.unbind_s()
            self.connection = None

    def ensure_connection(self):
        if self.connection is None:
            self.connection = ldap.initialize(self.settings_dict['NAME'])

            options = self.settings_dict.get('CONNECTION_OPTIONS', {})
            for opt, value in options.items():
                self.connection.set_option(opt, value)

            if self.settings_dict.get('TLS', False):
                self.connection.start_tls_s()

            self.connection.simple_bind_s(
                self.settings_dict['USER'],
                self.settings_dict['PASSWORD'])

    def _commit(self):
        pass

    def _cursor(self):
        self.ensure_connection()
        return DatabaseCursor(self.connection)

    def _rollback(self):
        pass

    def add_s(self, dn, modlist):
        cursor = self._cursor()
        return cursor.connection.add_s(dn.encode(self.charset), modlist)

    def delete_s(self, dn):
        cursor = self._cursor()
        return cursor.connection.delete_s(dn.encode(self.charset))

    def modify_s(self, dn, modlist):
        cursor = self._cursor()
        return cursor.connection.modify_s(dn.encode(self.charset), modlist)

    def rename_s(self, dn, newrdn):
        cursor = self._cursor()
        return cursor.connection.rename_s(dn.encode(self.charset),
                                          newrdn.encode(self.charset))

    def search_s(self, base, scope, filterstr='(objectClass=*)',
                 attrlist=None):
        cursor = self._cursor()
        results = cursor.connection.search_s(base, scope,
                                             filterstr.encode(self.charset),
                                             attrlist)
        output = []
        for dn, attrs in results:
            # skip referrals
            if dn is not None:
                output.append((dn.decode(self.charset), attrs))
        return output
<commit_msg>ignore create_test_db / destroy_test_db kwargs<commit_after># -*- coding: utf-8 -*-
#
# django-ldapdb
# Copyright (c) 2009-2011, Bolloré telecom
# Copyright (c) 2013, Jeremy Lainé
# All rights reserved.
#
# See AUTHORS file for a full list of contributors.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     1. Redistributions of source code must retain the above copyright notice,
#        this list of conditions and the following disclaimer.
#
#     2. Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

import ldap
import django

from django.db.backends import (BaseDatabaseFeatures, BaseDatabaseOperations,
                                BaseDatabaseWrapper)
from django.db.backends.creation import BaseDatabaseCreation

class DatabaseCreation(BaseDatabaseCreation):
    def create_test_db(self, **kwargs):
        """
        Creates a test database, prompting the user for confirmation if the
        database already exists. Returns the name of the test database created.
        """
        pass

    def destroy_test_db(self, **kwargs):
        """
        Destroy a test database, prompting the user for confirmation if the
        database already exists. Returns the name of the test database created.
        """
        pass

class DatabaseCursor(object):
    def __init__(self, ldap_connection):
        self.connection = ldap_connection

class DatabaseFeatures(BaseDatabaseFeatures):
    def __init__(self, connection):
        self.connection = connection
        self.supports_transactions = False

class DatabaseOperations(BaseDatabaseOperations):
    compiler_module = "ldapdb.backends.ldap.compiler"

    def quote_name(self, name):
        return name

class DatabaseWrapper(BaseDatabaseWrapper):
    vendor = 'ldap'

    def __init__(self, *args, **kwargs):
        super(DatabaseWrapper, self).__init__(*args, **kwargs)

        self.charset = "utf-8"
        self.creation = DatabaseCreation(self)
        self.features = DatabaseFeatures(self)
        if django.VERSION > (1, 4):
            self.ops = DatabaseOperations(self)
        else:
            self.ops = DatabaseOperations()
        self.settings_dict['SUPPORTS_TRANSACTIONS'] = False

    def close(self):
        if hasattr(self, 'validate_thread_sharing'):
            # django >= 1.4
            self.validate_thread_sharing()
        if self.connection is not None:
            self.connection.unbind_s()
            self.connection = None

    def ensure_connection(self):
        if self.connection is None:
            self.connection = ldap.initialize(self.settings_dict['NAME'])

            options = self.settings_dict.get('CONNECTION_OPTIONS', {})
            for opt, value in options.items():
                self.connection.set_option(opt, value)

            if self.settings_dict.get('TLS', False):
                self.connection.start_tls_s()

            self.connection.simple_bind_s(
                self.settings_dict['USER'],
                self.settings_dict['PASSWORD'])

    def _commit(self):
        pass

    def _cursor(self):
        self.ensure_connection()
        return DatabaseCursor(self.connection)

    def _rollback(self):
        pass

    def add_s(self, dn, modlist):
        cursor = self._cursor()
        return cursor.connection.add_s(dn.encode(self.charset), modlist)

    def delete_s(self, dn):
        cursor = self._cursor()
        return cursor.connection.delete_s(dn.encode(self.charset))

    def modify_s(self, dn, modlist):
        cursor = self._cursor()
        return cursor.connection.modify_s(dn.encode(self.charset), modlist)

    def rename_s(self, dn, newrdn):
        cursor = self._cursor()
        return cursor.connection.rename_s(dn.encode(self.charset),
                                          newrdn.encode(self.charset))

    def search_s(self, base, scope, filterstr='(objectClass=*)',
                 attrlist=None):
        cursor = self._cursor()
        results = cursor.connection.search_s(base, scope,
                                             filterstr.encode(self.charset),
                                             attrlist)
        output = []
        for dn, attrs in results:
            # skip referrals
            if dn is not None:
                output.append((dn.decode(self.charset), attrs))
        return output
<|endoftext|>


<commit_before>import sys, os

from hbase.ttypes import *
from hbase import Hbase

from thrift import Thrift
from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol

class HBaseConnection:
    def __init__(self, hostname, port):
       self.hostname = hostname
       self.port = port
       self.transport = None
       self.protocol = None
       self.client = None

    def connect(self):
        if self.client == None or self.transport == None or not self.transport.isOpen():
            # Make socket
            self.transport = TSocket.TSocket(self.hostname, self.port)
            # Buffering is critical. Raw sockets are very slow
            self.transport = TTransport.TBufferedTransport(self.transport)
            # Wrap in a protocol
            self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
            # Create a client to use the protocol encoder
            self.client = Hbase.Client(self.protocol)
            print "Opening hbase connection on %s %s" % (self.hostname, self.port)
            self.transport.open()
            print "Opened!"

    def disconnect(self):
        if self.transport:
            self.transport.close()

    def validate_column_descriptors(self, table_name, column_descriptors):
        hbase_families = self.client.getColumnDescriptors(table_name)
        for col_desc in column_descriptors:
            family, column = col_desc.split(":")
            if not family in hbase_families:
                raise Exception("Invalid column descriptor \"%s\" for hbase table \"%s\"" % (col_desc, table_name))

    def validate_table_name(self, table_name):
        if not table_name in self.client.getTableNames():
            raise Exception("hbase table '%s' not found." % (table_name))

    def put(self, table, puts):
        batches = []
        for put in puts:
            mutations = []
            columns = put.columns
            for family in columns:
                for qualifier, value in columns[family].iteritems():
                    column = "%s:%s" % (family, qualifier)
                    mutations.append(Mutation(column=column, value=value))
            if len(mutations) > 0:
                batches.append(BatchMutation(row=put.row, mutations=mutations))
        if len(batches) > 0:
            try:
                self.client.mutateRows(table, batches)
            except IOError, e:
                raise Exception(e.message)

    def delete(self, table, deletes):
        batches = []
        for delete in deletes:
            mutations = []
            columns = delete.columns
            for family in columns:
                for qualifier in columns[family]:
                    column = "%s:%s" % (family, qualifier)
                    mutations.append(Mutation(column=column, isDelete=True))
            if len(mutations) > 0:
                batches.append(BatchMutation(row=put.row, mutations=mutations))
        if len(batches) > 0:
            try:
                self.client.mutateRows(table, batches)
            except Exception, e:
                raise Exception(e.message)

class Put(object):
    def __init__(self, row):
        self.row = row
        self.columns = {}
    def add(self, familyQualifier, value):
        family, qualifier = familyQualifier.split(':')
        self.add(family, qualifier, value)
    def add(self, family, qualifier, value):
        value = unicode(value, 'utf-8')
        f = self.columns.get(family, {})
        f[qualifier] = value.encode( "utf-8" )
        self.columns[family] = f

class Delete(object):
    def __init__(self, row):
        self.row = row
        self.columns = {}
    def add(self, familyQualifier):
        family, qualifier = familyQualifier.split(':')
        self.add(family, qualifier)
    def add(self, family, qualifier):
        value = unicode(value, 'utf-8')
        f = self.columns.get(family, [])
        f.append(qualifier)
        self.columns[family] = f
    <commit_msg>logging<commit_after>import sys, os

from hbase.ttypes import *
from hbase import Hbase

from thrift import Thrift
from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol

class HBaseConnection:
    def __init__(self, hostname, port):
       self.hostname = hostname
       self.port = port
       self.transport = None
       self.protocol = None
       self.client = None

    def connect(self):
        if self.client == None or self.transport == None or not self.transport.isOpen():
            # Make socket
            self.transport = TSocket.TSocket(self.hostname, self.port)
            # Buffering is critical. Raw sockets are very slow
            self.transport = TTransport.TBufferedTransport(self.transport)
            # Wrap in a protocol
            self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
            # Create a client to use the protocol encoder
            self.client = Hbase.Client(self.protocol)
            print "Opening hbase connection on %s %s" % (self.hostname, self.port)
            self.transport.open()

    def disconnect(self):
        print "Closing hbase connection"
        if self.transport:
            self.transport.close()

    def validate_column_descriptors(self, table_name, column_descriptors):
        hbase_families = self.client.getColumnDescriptors(table_name)
        for col_desc in column_descriptors:
            family, column = col_desc.split(":")
            if not family in hbase_families:
                raise Exception("Invalid column descriptor \"%s\" for hbase table \"%s\"" % (col_desc, table_name))

    def validate_table_name(self, table_name):
        if not table_name in self.client.getTableNames():
            raise Exception("hbase table '%s' not found." % (table_name))

    def put(self, table, puts):
        batches = []
        for put in puts:
            mutations = []
            columns = put.columns
            for family in columns:
                for qualifier, value in columns[family].iteritems():
                    column = "%s:%s" % (family, qualifier)
                    mutations.append(Mutation(column=column, value=value))
            if len(mutations) > 0:
                batches.append(BatchMutation(row=put.row, mutations=mutations))
        if len(batches) > 0:
            try:
                self.client.mutateRows(table, batches)
            except IOError, e:
                raise Exception(e.message)

    def delete(self, table, deletes):
        batches = []
        for delete in deletes:
            mutations = []
            columns = delete.columns
            for family in columns:
                for qualifier in columns[family]:
                    column = "%s:%s" % (family, qualifier)
                    mutations.append(Mutation(column=column, isDelete=True))
            if len(mutations) > 0:
                batches.append(BatchMutation(row=put.row, mutations=mutations))
        if len(batches) > 0:
            try:
                self.client.mutateRows(table, batches)
            except Exception, e:
                raise Exception(e.message)

class Put(object):
    def __init__(self, row):
        self.row = row
        self.columns = {}
    def add(self, familyQualifier, value):
        family, qualifier = familyQualifier.split(':')
        self.add(family, qualifier, value)
    def add(self, family, qualifier, value):
        value = unicode(value, 'utf-8')
        f = self.columns.get(family, {})
        f[qualifier] = value.encode( "utf-8" )
        self.columns[family] = f

class Delete(object):
    def __init__(self, row):
        self.row = row
        self.columns = {}
    def add(self, familyQualifier):
        family, qualifier = familyQualifier.split(':')
        self.add(family, qualifier)
    def add(self, family, qualifier):
        value = unicode(value, 'utf-8')
        f = self.columns.get(family, [])
        f.append(qualifier)
        self.columns[family] = f
    <|endoftext|>


<commit_before>from pippi import dsp, tune
from hcj import fx, keys
import ctl

tlength = dsp.stf(60 * 3)

out = ''
elapsed = 0
count = 0

dloop = dsp.read('samples/jess/loop2.wav').data
kicksoft = dsp.read('samples/jess/kickshuffle.wav').data
kickhard = dsp.read('samples/jess/kickcym.wav').data
snare = dsp.read('samples/jess/snare.wav').data
flam = dsp.read('samples/jess/snareflam.wav').data
smash = dsp.read('samples/jess/smash.wav').data
skitter = dsp.read('samples/jess/skitter.wav').data

def makeSwells(cname, numswells, length, key='e', octave=1):
    swells = []

    for _ in range(numswells):
        slength = dsp.randint((length / numswells) * 0.75, length / numswells)
        swell = keys.pulsars(cname, slength, drift=dsp.rand(0.001, 0.01), speed=dsp.rand(0.1, 0.5), amp=dsp.rand(0.2, 0.5), key=key, octave=octave)
        swell = dsp.env(swell, 'hann')
        swell = dsp.taper(swell, 30)

        swells += [ swell ]

    swells = [ dsp.fill(swell, length / numswells, silence=True) for swell in swells ]

    return ''.join(swells)

def makeSnare(length, i):
    return dsp.fill(snare, length, silence=True)

def makeKick(length, i):
    k = dsp.randchoose([ kickhard, kicksoft ])
    return dsp.fill(k, length, silence=True)

while elapsed < tlength:
    print 'Rendering section %s...' % count

    bar = ''

    bar += dsp.mix([ dloop, kickhard ])
    bar += dsp.mix([ dloop, kicksoft ])
    bar += dsp.mix([ dloop, kicksoft ])

    beat = dsp.flen(bar) / 16
    bar = dsp.split(bar, beat)
    bar = dsp.randshuffle(bar)
    for i, b in enumerate(bar):
        if dsp.rand() > 0.5:
            bar[i] = dsp.pad('', 0, dsp.flen(b))

    bar = ''.join(bar)

    snarep = '..x.'
    pattern = ctl.parseBeat(snarep)
    snares = ctl.makeBeat(pattern, [ beat for _ in range(16) ], makeSnare)

    kickp = 'x.......'
    pattern = ctl.parseBeat(kickp)
    kicks = ctl.makeBeat(pattern, [ beat for _ in range(16) ], makeKick)

    sm = dsp.fill(smash, dsp.flen(bar))
    sm = dsp.mix([ dsp.alias(sm), fx.penv(sm) ])
    sm = dsp.amp(sm, dsp.rand(0.5, 1))
    sk = dsp.fill(skitter, dsp.flen(bar))
    sk = fx.penv(sk)

    if count % 4 == 0:
        bar = dsp.mix([ bar, dsp.pad(flam, dsp.flen(bar) - dsp.flen(flam), 0) ])

    progression = 'i v vi'.split(' ')
    cname = progression[ count % len(progression) ]
    swells = dsp.mix([ makeSwells(cname, nswell, dsp.flen(bar)) for nswell in [4,5] ])

    bar = dsp.mix([ bar, sm, sk, snares, kicks, swells ])

    out += bar

    elapsed += dsp.flen(bar)
    count += 1

dsp.write(out, 'study.ix')
<commit_msg>Update IX changes / pulses<commit_after>from pippi import dsp, tune
from hcj import fx, keys
import ctl

tlength = dsp.stf(60 * 3)

out = ''
elapsed = 0
count = 0

dloop = dsp.read('samples/jess/loop2.wav').data
kicksoft = dsp.read('samples/jess/kickshuffle.wav').data
kickhard = dsp.read('samples/jess/kickcym.wav').data
snare = dsp.read('samples/jess/snare.wav').data
flam = dsp.read('samples/jess/snareflam.wav').data
smash = dsp.read('samples/jess/smash.wav').data
skitter = dsp.read('samples/jess/skitter.wav').data

def makeSwells(cname, numswells, length, key='e', octave=1):
    swells = []

    for _ in range(numswells):
        slength = dsp.randint((length / numswells) * 0.85, length / numswells)
        swell = keys.pulsars(cname, slength, drift=dsp.rand(0.001, 0.01), speed=dsp.rand(0.1, 0.5), amp=dsp.rand(0.2, 0.5), key=key, octave=octave)
        swell = dsp.env(swell, 'hann')
        swell = dsp.taper(swell, 30)

        swells += [ swell ]

    swells = [ dsp.fill(swell, length / numswells, silence=True) for swell in swells ]

    return ''.join(swells)

def makeSnare(length, i):
    return dsp.fill(snare, length, silence=True)

def makeKick(length, i):
    k = dsp.randchoose([ kickhard, kicksoft ])
    return dsp.fill(k, length, silence=True)

while elapsed < tlength:
    print 'Rendering section %s...' % count

    bar = ''

    bar += dsp.mix([ dloop, kickhard ])
    bar += dsp.mix([ dloop, kicksoft ])
    bar += dsp.mix([ dloop, kicksoft ])

    beat = dsp.flen(bar) / 16
    bar = dsp.split(bar, beat)
    bar = dsp.randshuffle(bar)
    for i, b in enumerate(bar):
        if dsp.rand() > 0.5:
            bar[i] = dsp.pad('', 0, dsp.flen(b))

    bar = ''.join(bar)

    snarep = '..x.'
    pattern = ctl.parseBeat(snarep)
    snares = ctl.makeBeat(pattern, [ beat for _ in range(16) ], makeSnare)

    kickp = 'x.......'
    pattern = ctl.parseBeat(kickp)
    kicks = ctl.makeBeat(pattern, [ beat for _ in range(16) ], makeKick)

    sm = dsp.fill(smash, dsp.flen(bar))
    sm = dsp.mix([ dsp.alias(sm), fx.penv(sm) ])
    sm = dsp.amp(sm, dsp.rand(0.5, 1))
    sk = dsp.fill(skitter, dsp.flen(bar))
    sk = fx.penv(sk)

    if count % 4 == 0:
        bar = dsp.mix([ bar, dsp.pad(flam, dsp.flen(bar) - dsp.flen(flam), 0) ])

    # Look ma, harmonic motion!
    progression = 'i i v IV IV6'.split(' ')
    cname = progression[ count % len(progression) ]
    swells = dsp.mix([ makeSwells(cname, nswell, dsp.flen(bar)) for nswell in [8,4] ])

    bar = dsp.mix([ bar, sm, sk, snares, kicks, swells ])

    out += bar

    elapsed += dsp.flen(bar)
    count += 1

dsp.write(out, 'study.ix')
<|endoftext|>


<commit_before>import numpy as np
import sys, os
import nrrd

scale=1

if (len(sys.argv) < 2):
    print('Error: missing arguments!')
    print('e.g. python swc2nrrd.py template.nrrd neuron.swc Image.nrrd [width] [scale]')
else:
    Itemp = str(sys.argv[1])
    Iswc = str(sys.argv[2])
    Iout = str(sys.argv[3])
    bounded = True

    w = 0
    if (len(sys.argv) > 4):
      w=np.int32(sys.argv[4])

    if (len(sys.argv) > 5):
      scale=np.int32(sys.argv[5])
      bounded = False

    print('Loading %s...'% (Itemp))
    tempData1, tempHeader1 = nrrd.read(Itemp)

    print('Loading %s...'% (Iswc))
    with open(Iswc) as fI:
        swcIn = fI.readlines()

    lineDict = {}
    for thisLine in swcIn:
        if thisLine[0]!='#':
            splitLine = thisLine.split(" ")
            lineDict[int(splitLine[0])] = {'position':np.array([splitLine[2],splitLine[3],splitLine[4]],dtype=np.float),
                                      'radius':splitLine[5],
                                      'parent':int(splitLine[6])}
    extent=[1000,1000,1000]

    if bounded:
        for i in range(3):
            extent[i] = np.max([(np.max([x['position'][i] for x in lineDict.values()])/scale).astype(np.int),np.shape(tempData1)[i]])
    else:
        for i in range(3):
            extent[i] = (np.max([x['position'][i] for x in lineDict.values()])/scale).astype(np.int)

    print(extent)

    outputImg = np.zeros(extent,dtype=np.uint8)

    for thisDict in lineDict.values():
        p = np.round(np.divide(np.divide(thisDict['position'],[tempHeader1['space directions'][0][0],tempHeader1['space directions'][1][1],tempHeader1['space directions'][2][2]]),scale)).astype(np.int)
        outputImg[p[0]-w:p[0]+w+1,p[1]-w:p[1]+w+1,p[2]-w:p[2]+w+1]=np.uint8(255)

    nrrd.write(Iout, np.uint8(outputImg), header=tempHeader1)
    print('saved to ' + Iout)
<commit_msg>incorporating radius using spheres<commit_after>import numpy as np
import sys, os
import nrrd

def sphere(shape, radius, position):
    # assume shape and position are both a 3-tuple of int or float
    # the units are pixels / voxels (px for short)
    # radius is a int or float in px
    semisizes = (radius,) * 3

    # genereate the grid for the support points
    # centered at the position indicated by position
    grid = [slice(-x0, dim - x0) for x0, dim in zip(position, shape)]
    position = np.ogrid[grid]
    # calculate the distance of all points from `position` center
    # scaled by the radius
    arr = np.zeros(shape, dtype=float)
    for x_i, semisize in zip(position, semisizes):
        arr += (np.abs(x_i / semisize) ** 2)
    # the inner part of the sphere will have distance below 1
    return arr <= 1.0

scale=1

if (len(sys.argv) < 2):
    print('Error: missing arguments!')
    print('e.g. python swc2nrrd.py template.nrrd neuron.swc Image.nrrd [width] [scale]')
else:
    Itemp = str(sys.argv[1])
    Iswc = str(sys.argv[2])
    Iout = str(sys.argv[3])
    bounded = True

    w = 0
    if (len(sys.argv) > 4):
      w=np.int32(sys.argv[4])

    if (len(sys.argv) > 5):
      scale=np.int32(sys.argv[5])
      bounded = False

    print('Loading %s...'% (Itemp))
    tempData1, tempHeader1 = nrrd.read(Itemp)

    print('Loading %s...'% (Iswc))
    with open(Iswc) as fI:
        swcIn = fI.readlines()

    lineDict = {}
    for thisLine in swcIn:
        if thisLine[0]!='#':
            splitLine = thisLine.split(" ")
            lineDict[int(splitLine[0])] = {'position':np.array([splitLine[2],splitLine[3],splitLine[4]],dtype=np.float),
                                      'radius':splitLine[5],
                                      'parent':int(splitLine[6])}
    extent=[1000,1000,1000]

    if bounded:
        for i in range(3):
            extent[i] = np.max([(np.max([x['position'][i] for x in lineDict.values()])/scale).astype(np.int),np.shape(tempData1)[i]])
    else:
        for i in range(3):
            extent[i] = (np.max([x['position'][i] for x in lineDict.values()])/scale).astype(np.int)

    print(extent)

    outputImg = np.zeros(extent,dtype=np.uint8)

    r=0

    for thisDict in lineDict.values():
        p = np.round(np.divide(np.divide(thisDict['position'],[tempHeader1['space directions'][0][0],tempHeader1['space directions'][1][1],tempHeader1['space directions'][2][2]]),scale)).astype(np.int)

        if w==0 AND thisDict['radius']>0:
            r=thisDict['radius']
        else:
            r=w
        point = np.multiply(sphere(extent, r, p),np.uint8(255)).astype(np.uint8)
        outputImg = np.maximum(outputImg, point).astype(np.uint8)

    nrrd.write(Iout, np.uint8(outputImg), header=tempHeader1)
    print('saved to ' + Iout)
<|endoftext|>