Beispiel #1
0
def delete_item(item_name):
    """Delete a specified item from the database.

    Args:
        item_name (str): Name of the item to be deleted.
    """
    if 'username' not in login_session:
        return redirect('/login')

    session = connect_to_database()

    try:
        item = session.query(Item).filter_by(name=item_name).one()
    except NoResultFound:
        flash("Error: The item '%s' does not exist." % item_name)
        session.close()
        return redirect(url_for('show_homepage'))

    if login_session['user_id'] != item.user_id:
        flash("You didn't add this Course, so you can't delete it. Sorry :-(")
        category = session.query(Category).filter_by(id=item.category_id).one()
        category_name = category.name
        item_name = item.name
        session.close()
        return redirect(
            url_for('show_item',
                    category_name=category_name,
                    item_name=item_name))

    if request.method == 'POST':
        if item.image_filename:
            delete_image(item.image_filename)
        session.delete(item)
        session.commit()
        category = session.query(Category).filter_by(id=item.category_id).one()

        flash("Course successfully deleted!")
        category_name = category.name
        session.close()
        return redirect(url_for('show_items', category_name=category_name))
    else:
        categories = session.query(Category).all()
        session.close()
        return render_template('delete_item.html',
                               categories=categories,
                               item=item)
Beispiel #2
0
def delete_item(item_name):
    """Delete a specified item from the database.

    Args:
        item_name (str): Name of the item to be deleted.
    """
    if 'username' not in login_session:
        return redirect('/login')

    session = connect_to_database()

    try:
        item = session.query(Item).filter_by(name=item_name).one()
    except NoResultFound:
        flash("Error: The item '%s' does not exist." % item_name)
        session.close()
        return redirect(url_for('show_homepage'))

    if login_session['user_id'] != item.user_id:
        flash("You didn't add this paper, so you can't delete it. Sorry :-(")
        category = session.query(Category).filter_by(id=item.category_id).one()
        category_name = category.name
        item_name = item.name
        session.close()
        return redirect(url_for('show_item',
                                category_name=category_name,
                                item_name=item_name))

    if request.method == 'POST':
        if item.image_filename:
            delete_image(item.image_filename)
        session.delete(item)
        session.commit()
        category = session.query(Category).filter_by(id=item.category_id).one()

        flash("Paper successfully deleted!")
        category_name = category.name
        session.close()
        return redirect(url_for('show_items', category_name=category_name))
    else:
        categories = session.query(Category).all()
        session.close()
        return render_template('delete_item.html',
                               categories=categories,
                               item=item)
Beispiel #3
0
def edit_item(item_name, category_name=None):
    """Edit the details of the specified item.

    Args:
        item_name (str): Name of item to be edited.
        category_name (str): Optionally, can also specify the category to
            which the item belongs to.
    """
    if 'username' not in login_session:
        flash("You need to login to edit any papers.")
        return redirect('/login')

    session = connect_to_database()

    try:
        item = session.query(Item).filter_by(name=item_name).one()
    except NoResultFound:
        flash("Error: The item '%s' does not exist." % item_name)
        return redirect(url_for('show_homepage'))

    if login_session['user_id'] != item.user_id:
        flash("You didn't add this paper, so you can't edit it. Sorry :-(")
        category = session.query(Category).filter_by(id=item.category_id).one()
        category_name = category.name
        item_name = item.name
        session.close()
        return redirect(url_for('show_item',
                                category_name=category_name,
                                item_name=item_name))

    if request.method == 'POST':
        if request.form['name'] != item.name:
            # Enforce rule that item names are unique
            qry = session.query(Item).filter(Item.name == request.form['name'])
            already_exists = (session.query(literal(True)).filter(qry.exists())
                              .scalar())
            if already_exists is True:
                original_category = (session.query(Category)
                                     .filter_by(id=item.category_id).one())
                flash("Error: There is already a paper with the name '%s'"
                      % request.form['name'])
                session.close()
                return redirect(url_for('show_items',
                                        category_name=original_category.name))
            item.name = request.form['name']

        form_category = (session.query(Category)
                         .filter_by(name=request.form['category']).one())
        if form_category != item.category:
            item.category = form_category

        item.description = request.form['description']
        item.quantity = request.form['quantity']

        # Process optional item image
        image_file = request.files['file']
        if image_file and allowed_file(image_file.filename):
            if item.image_filename:
                delete_image(item.image_filename)
            filename = secure_filename(image_file.filename)
            if os.path.isdir(app.config['UPLOAD_FOLDER']) is False:
                os.mkdir(app.config['UPLOAD_FOLDER'])
            image_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

            item.image_filename = filename
            item.image_url = None

        elif ('delete_image' in request.form and
              request.form['delete_image'] == 'delete'):
            if item.image_filename:
                delete_image(item.image_filename)
                item.image_filename = None

        if not image_file and request.form['image_url']:
            item.image_url = request.form['image_url']
            if item.image_filename:
                delete_image(item.image_filename)
                item.image_filename = None

        session.add(item)
        session.commit()

        flash("Paper successfully edited!")
        category_name = form_category.name
        item_name = item.name
        session.close()
        return redirect(url_for('show_item',
                                category_name=category_name,
                                item_name=item_name))
    else:
        categories = session.query(Category).all()
        session.close()
        return render_template('edit_item.html',
                               categories=categories,
                               item=item)
Beispiel #4
0
def edit_item(item_name, category_name=None):
    """Edit the details of the specified item.

    Args:
        item_name (str): Name of item to be edited.
        category_name (str): Optionally, can also specify the category to
            which the item belongs to.
    """
    if 'username' not in login_session:
        flash("You need to login to edit any Courses.")
        return redirect('/login')

    session = connect_to_database()

    try:
        item = session.query(Item).filter_by(name=item_name).one()
    except NoResultFound:
        flash("Error: The item '%s' does not exist." % item_name)
        return redirect(url_for('show_homepage'))

    if login_session['user_id'] != item.user_id:
        flash("You didn't add this Course, so you can't edit it. Sorry :-(")
        category = session.query(Category).filter_by(id=item.category_id).one()
        category_name = category.name
        item_name = item.name
        session.close()
        return redirect(
            url_for('show_item',
                    category_name=category_name,
                    item_name=item_name))

    if request.method == 'POST':
        if request.form['name'] != item.name:
            # Enforce rule that item names are unique
            qry = session.query(Item).filter(Item.name == request.form['name'])
            already_exists = (session.query(literal(True)).filter(
                qry.exists()).scalar())
            if already_exists is True:
                original_category = (session.query(Category).filter_by(
                    id=item.category_id).one())
                flash("Error: There is already an Course with the name '%s'" %
                      request.form['name'])
                session.close()
                return redirect(
                    url_for('show_items',
                            category_name=original_category.name))
            item.name = request.form['name']

        form_category = (session.query(Category).filter_by(
            name=request.form['category']).one())
        if form_category != item.category:
            item.category = form_category

        item.description = request.form['description']
        item.duration = request.form['duration']

        # Process optional item image
        image_file = request.files['file']
        if image_file and allowed_file(image_file.filename):
            if item.image_filename:
                delete_image(item.image_filename)
            filename = secure_filename(image_file.filename)
            if os.path.isdir(app.config['UPLOAD_FOLDER']) is False:
                os.mkdir(app.config['UPLOAD_FOLDER'])
            image_file.save(os.path.join(app.config['UPLOAD_FOLDER'],
                                         filename))

            item.image_filename = filename
            item.image_url = None

        elif ('delete_image' in request.form
              and request.form['delete_image'] == 'delete'):
            if item.image_filename:
                delete_image(item.image_filename)
                item.image_filename = None

        if not image_file and request.form['image_url']:
            item.image_url = request.form['image_url']
            if item.image_filename:
                delete_image(item.image_filename)
                item.image_filename = None

        session.add(item)
        session.commit()

        flash("Course successfully edited!")
        category_name = form_category.name
        item_name = item.name
        session.close()
        return redirect(
            url_for('show_item',
                    category_name=category_name,
                    item_name=item_name))
    else:
        categories = session.query(Category).all()
        session.close()
        return render_template('edit_item.html',
                               categories=categories,
                               item=item)