예제 #1
0
def get_attributes():
    try:
        attributes = Attribute.query.\
            order_by(desc(Attribute.id_attribute)). \
            all()
        return jsonify([Attribute.json() for Attribute in attributes])
    except Exception:
        traceback.print_exc()
        return jsonify(error='Invalid JSON.'), 400
예제 #2
0
def save_attributes():
    try:
        payload = request.get_json()
    except Exception:
        return jsonify(error='Invalid JSON.')

    validation = attributes_validate_required(payload)
    if validation['errors']:
        return jsonify(error={'name': 'invalid_model',
                              'errors': validation['errors']}), 400
    attribute = Attribute(**payload)
    try:
        db.session.add(attribute)
        db.session.commit()
        return jsonify(attribute.json())
    except (IntegrityError, Exception) as e:
        traceback.print_exc()
        db.session.rollback()
예제 #3
0
def patch_attributes():
    try:
        payload = request.get_json()
    except Exception:
        return jsonify(error='Invalid JSON.')

    validation = attributes_validate_required(payload)
    if validation['errors']:
        return jsonify(error={'name': 'invalid_model',
                              'errors': validation['errors']}), 400
    attribute = Attribute(**payload)
    try:
        id = int(payload['id_attribute'])
        del payload['id_attribute']
        db.session.query(Attribute).filter(Attribute.id_attribute == id).update(payload)
        db.session.commit()
        return jsonify(attribute.json())
    except (IntegrityError, Exception) as e:
        traceback.print_exc()
        db.session.rollback()