Exemplo n.º 1
0
def delete_item_photos(photo_id):
    delete_photo(photo_id)
    flash('Photo have been deleted.')
    item = Item.query.filter_by(photo_id=photo_id).first()
    item.photo_id = None
    db.session.commit()
    return redirect(session['editing_item'])
Exemplo n.º 2
0
def edit_item(item_id):
    if 'editing_item' in session:
        del session['editing_item']
    item = Item.query.filter_by(id=item_id).first_or_404()
    categories = Category.query.all()
    form = EditItemForm()
    form.categories.choices = [(category.id, category.name)
                               for category in categories]
    session['editing_item'] = url_for('main.edit_item', item_id=item_id)
    if form.validate_on_submit():  # Updating
        item.title = form.title.data
        item.description = form.description.data
        item.price = form.price.data
        item.category_id = form.categories.data
        if item.photo_id:
            delete_photo(item.photo_id)
            item.photo_id = None
        if form.image.data:
            item.photo_id = upload_photo(form.image.data)
        db.session.commit()
        flash('Your changes have been saved.')
        return redirect(url_for('main.edit_item', item_id=item.id))
    elif request.method == 'GET':  # Rendering
        form.title.data = item.title
        form.description.data = item.description
        form.price.data = item.price
        form.categories.data = item.category_id
    return render_template('edit_item.html',
                           title='Edit Item',
                           form=form,
                           item=item)
Exemplo n.º 3
0
def delete_category_photos(photo_id):
    delete_photo(photo_id)
    flash('Photo have been deleted.')
    category = Category.query.filter_by(photo_id=photo_id).first()
    category.photo_id = None
    db.session.commit()
    return redirect(session['editing_category'])
Exemplo n.º 4
0
def delete_category(id):
    category = Category.query.get_or_404(id)
    if category.photo_id:
        delete_photo(category.photo_id)
    db.session.delete(category)
    db.session.commit()
    return make_response(204)
Exemplo n.º 5
0
def delete_item(id):
    item = Item.query.get_or_404(id)
    if item.photo_id:
        delete_photo(item.photo_id)
    db.session.delete(item)
    db.session.commit()
    return make_response(204)
Exemplo n.º 6
0
def delete_category(category_id):
    category = Category.query.filter_by(id=category_id).first_or_404()
    if category.photo_id:
        delete_photo(category.photo_id)
    db.session.delete(category)
    db.session.commit()
    flash('Category have been deleted.')
    return redirect(url_for('main.show_categories'))
Exemplo n.º 7
0
def delete_item(item_id):
    item = Item.query.filter_by(id=item_id).first_or_404()
    category_id = item.category_id
    if item.photo_id:
        delete_photo(item.photo_id)
    db.session.delete(item)
    db.session.commit()
    flash('Item have been deleted.')
    return redirect(url_for('main.edit_group', category_id=category_id))
Exemplo n.º 8
0
def edit_category(category_id):
    if 'editing_category' in session:
        del session['editing_category']
    category = Category.query.filter_by(id=category_id).first_or_404()
    form = EditCategoryForm()
    page = request.args.get('page', 1, type=int)
    items = category.get_items().paginate(page,
                                          current_app.config['ITEMS_PER_PAGE'],
                                          True)
    next_url = url_for('edit_category', category_id=category_id, page=items.next_num) \
        if items.has_next else None
    prev_url = url_for('edit_category', category_id=category_id, page=items.prev_num) \
        if items.has_prev else None
    session['editing_category'] = url_for('main.edit_category',
                                          category_id=category_id)
    if form.validate_on_submit():  # Updating
        category.name = form.name.data
        category.description = form.description.data
        if category.photo_id:
            delete_photo(category.photo_id)
            category.photo_id = None
        if form.image.data:
            category.photo_id = upload_photo(form.image.data)
        db.session.commit()
        flash('Your changes have been saved.')
        return redirect(url_for('main.edit_category', category_id=category.id))
    elif request.method == 'GET':  # Rendering
        form.name.data = category.name
        form.description.data = category.description
    return render_template('edit_category.html',
                           title='Edit Category',
                           form=form,
                           category=category,
                           items=items.items,
                           next_url=next_url,
                           prev_url=prev_url)