Beispiel #1
0
def update_category_api(cid):
    # Content:  {'category': 'updated-category-name'}
    if valid_json(request.headers.get('content-type')):
        data = request.get_json()
        update_category = data.get('category')
    else:
        return jsonify(error="Unsupported content-type - expecting application/json."), 400

    if update_category:
        try:
            category = session.query(Category).filter_by(cid=cid).one()
        except NoResultFound as e:
            category = None

        if category:
            if g.user['uid'] != category.user_id:
                return jsonify(error='Not authorized'), 403

            category.name = update_category
            session.add(category)
            session.commit()

            return jsonify(category=category.serialize)
        # Category doesn't exist
        else:
            return jsonify(error="Category ID doesn't exist."), 422
    # Didn't receive valid data
    else:
        return jsonify(error="Couldn't find category name in JSON data."), 400
Beispiel #2
0
def create_category_api():
    # Consider allowing creation of one or multiple categories through a single POST
    # For now allow creation of one category at a time
    # Content:  {'category': 'new-category'}
    if valid_json(request.headers.get('content-type')):
        data = request.get_json()
        new_category = data.get('category')
    else:
        return jsonify(error="Unsupported content-type - expecting application/json."), 400

    if new_category:
        status, result = validate_category(new_category)
        # False and 'invalid' means category not in database - this is what we want
        if not status and result == 'invalid':
            user = g.user
            category = Category(name=new_category, user_id=user['uid'])
            session.add(category)
            session.commit()

            return jsonify(category=category.serialize), 201
        # Category already exists
        else:
            return jsonify(error="Category name already exists."), 409
    # Didn't receive valid data
    else:
        return jsonify(error="Couldn't find category name in JSON data."), 400
Beispiel #3
0
def create_item_api():
    # Allow creation of one or multiple items through a single POST
    # Content:
    # * Single: {'item': {'name': 'item-name', 'description': 'item-description',
    #                     'category_name': 'item-category'}}
    # * Multiple: {'items': [{'name': 'item-name', 'description': 'item-description',
    #                         'category_name': 'item-category'},
    #                        {<next-item...>}]}
    if valid_json(request.headers.get('content-type')):
        data = request.get_json()
    else:
        return jsonify(
            error="Unsupported content-type - expecting application/json."
        ), 400

    # Determine if single or multiple items
    new_item = data.get('item')
    new_items = data.get('items')

    # Single item?
    if new_item:
        name = new_item.get('name')
        description = new_item.get('description')
        category_name = new_item.get('category_name')

        status, error = validate_input(name, description, category_name)
        # True means item not in database - this is what we want
        if status:
            user = g.user
            category = session.query(Category).filter_by(
                name=category_name).one()
            filename = DEFAULT_PHOTO

            item = Item(name=name,
                        description=description,
                        picture=filename,
                        category_id=category.cid,
                        user_id=user['uid'])
            session.add(item)
            session.commit()

            return jsonify(item=item.serialize), 201
        # Problems
        else:
            return jsonify(error=error), 406
    # Multiple items?
    elif new_items:
        # Parse through each item, validate, and add if good
        # Need to keep track of successes and failures and report on at the end
        # This last part could be tricky...

        return jsonify(error='Not implemented...')
    # Didn't receive valid data
    else:
        return jsonify(error="Couldn't find item/items in JSON data."), 400
Beispiel #4
0
def update_item_api(iid):
    # Content:  {'item': {'name': 'new--name', 'description': 'new-description',
    #                     'category_name': 'new-category'}}
    # Note - cannot change picture through this view
    if valid_json(request.headers.get('content-type')):
        data = request.get_json()
        update_item = data.get('item')
    else:
        return jsonify(
            error="Unsupported content-type - expecting application/json."
        ), 400

    if update_item:
        try:
            item = session.query(Item).filter_by(iid=iid).one()
        except NoResultFound as e:
            item = None

        if item:
            if g.user['uid'] != item.user_id:
                return jsonify(error='Not authorized'), 403

            name = update_item.get('name')
            description = update_item.get('description')
            category_name = update_item.get('category_name')

            status, error = validate_input(name, description, category_name)
            if not status:
                # Check if only problem is non-unique name/title (OK since updating):
                valid_set = {'title_error', 'title_problem'}
                # Where overwriting existing item, make sure item.name (looked up from passed
                # iid) matches item_name or we'll get a database error!
                if not (error.get('title_problem') == 'nonunique'
                        and item.name == name and valid_set == set(error)):
                    return jsonify(error=error), 406

            category = session.query(Category).filter_by(
                name=category_name).one()

            # Update
            item.name = name
            item.description = description
            item.category_id = category.cid
            session.add(item)
            session.commit()

            return jsonify(item=item.serialize)
        # Category doesn't exist
        else:
            return jsonify(error="Category ID doesn't exist."), 422
    # Didn't receive valid data
    else:
        return jsonify(error="Couldn't find item in JSON data."), 400