コード例 #1
0
def create_item(list_id, event_id):
    success, user = check_session()
    if not success:
        return user
    event_list = user.public_lists.filter_by(public_list_id = list_id).first()
    if event_list is None:
        return failure_response("list not found!")
    event_list = event_list.public_list
    event = event_list.events.filter_by(id = event_id).first()
    if event is None:
        return failure_response("event not found!")
    body = json.loads(request.data.decode())
    completed = body.get('completed')
    date = body.get('date')
    title = body.get('title')
    if completed is None or date is None or title is None:
        return failure_response("missing field(s)!")
    new_item = Item(completed=completed, date=date, title=title, event_id = event_id)
    event.items.append(new_item)
    db.session.add(new_item)
    db.session.commit()
    return success_response(new_item.serialize(), 201)
コード例 #2
0
def spend_budget(budget_id, category_name):
    budget = Budget.query.filter_by(id=budget_id).first()
    if budget is not None:
        category = Category.query.filter_by(budget_id=budget_id, name=category_name).first()
        if category is not None:
            budget_body = json.loads(request.data)
            spent = budget_body.get('spent', budget.spent)
            item = Item(
                name = budget_body.get('name'),
                total = spent,
                category_id = category.id,
                budget_id = budget.id
            )
            budget.spent += spent
            category.total += spent
            if budget.spent <= budget.total:
                category.items.append(item)
                db.session.add(item)
                db.session.commit()
                return json.dumps({'success': True, 'data': item.serialize()}), 200
            return json.dumps({'success': False, 'error': 'You are spending overbudget!'})
        return json.dumps({'success': False, 'error': 'Category not found!'}), 404
    return json.dumps({'success': False, 'error': 'Budget not found!'}), 404
コード例 #3
0
def create_item(name, price, stock):
    new_item = Item(name=name, price=price, stock=stock)
    db.session.add(new_item)
    db.session.commit()
    return new_item.serialize()