def add_item(category_path):
    category = Category.get_one(category_path)
    fields = field_list()

    if request.method == 'GET':
        return render_template('add.html', fields=fields, category=category)
    else:
        error = check_request_fields(fields)

        if error:
            return render_template('add.html',
                                   fields=fields,
                                   category=category,
                                   error=error)
        else:
            obj = {}
            for field in fields:
                obj[field['name']] = field['value']

            error = Item.add(g.current_user, category, Item(**obj))

            if error:
                return render_template('add.html',
                                       fields=fields,
                                       category=category,
                                       error=error)
            else:
                return redirect(
                    url_for('show_category', category_path=category.path))
Esempio n. 2
0
def add_items():
    email = '*****@*****.**'
    username = '******'
    user = User.create(email=email, username=username, picture='')

    for category_path in items:
        category = Category.get_one(category_path)

        for item in items[category_path]:
            Item.add(user, category, Item(**item))
def item_JSON(category_path, item_label):
    try:
        category = Category.get_one(category_path)
        item = Item.get_one(category, item_label)

        return jsonify(item.serialized)
    except NotFound:
        return json_not_found()
def items_of_category_JSON(category_path):
    try:
        category = Category.get_one(category_path)
        items = Item.get_all(category)

        return jsonify(itemList=[item.serialized for item in items])
    except NotFound:
        return json_not_found()
def edit_item(category_path, item_label):
    category = Category.get_one(category_path)
    item = Item.get_one(category, item_label)
    fields = field_list()

    if request.method == 'GET':
        title = item.title
        author = item.author
        source = item.source
        image = item.image
        text = item.text

        extend_fields_with_value(fields=fields,
                                 title=title,
                                 author=author,
                                 source=source,
                                 image=image,
                                 text=text)

        return render_template('add.html', fields=fields, category=category)
    else:
        error = check_request_fields(fields)

        if error:
            return render_template('add.html',
                                   fields=fields,
                                   category=category,
                                   error=error)
        else:
            obj = {}
            for field in fields:
                obj[field['name']] = field['value']

            error = item.edit(g.current_user, obj)

            if error:
                return render_template('add.html',
                                       fields=fields,
                                       category=category,
                                       error=error)
            else:
                return redirect(
                    url_for('show_category', category_path=category.path))
def delete_item(category_path, item_label):
    category = Category.get_one(category_path)
    item = Item.get_one(category, item_label)
    item.delete(g.current_user)

    return json_result('deleted successfully', 200)
def show_article(category_path, item_label):
    category = Category.get_one(category_path)
    item = Item.get_one(category, item_label)

    return render_template('article.html', category=category, item=item)
def show_category(category_path):
    category = Category.get_one(category_path)
    items = Item.get_all(category)

    return render_template('items.html', category=category, items=items)