Esempio n. 1
0
def edit_branditem(stockitem_id):
    """
    Edits a brand item.

    :param stockitem_id: WineBrand id
    :return: HTML page
    """
    session = current_app.config['db']
    maxyear = datetime.date.today().year
    item = session.query(WineBrand)\
        .filter_by(id=stockitem_id)\
        .one()

    if item.user_id != login_session['user_id']:
        flash("You cannot edit this item",
              'danger')
        return redirect(url_for(".show_branditem", stockitem_id=stockitem_id))

    if request.method == "POST":
        item.brand_name = request.form['itemname']
        item.winetype_id = request.form.get('winetypevalue', None)
        item.vintage = request.form.get('vintagevalue', None)
        item.date_edited = datetime.datetime.today()
        item.user_id = login_session.get('user_id', None)
        if request.files.get('filename', None):
            oldfilename = item.filename
            item.filename = brandphotos.save(
                request.files['filename'],
                name=make_safe_filename(request.files['filename'].filename))
            try:
                os.remove(brandphotos.path(oldfilename))
            except:
                # Unable to remove old file, leave it be.
                pass
        session.add(item)
        session.commit()
        session.flush()
        winetype = session.query(WineType.name)\
            .filter_by(id=item.winetype_id)\
            .one()
        flash("Successfully Added '%s (%s)'" % (winetype.name, item.brand_name),
              'success')
        return redirect(url_for('.show_branditem', stockitem_id=item.id))
    else:
        winetypes = session.query(WineType)\
            .order_by(asc(func.lower(WineType.name)))\
            .all()
        return render_template(template_prefix+'edit_form.html',
                               item=item,
                               winetypes=winetypes,
                               maxyear=maxyear)
Esempio n. 2
0
def new():
    """
    Creates a new brand for reviewing

    :return: HTML page
    """
    session = current_app.config['db']
    item = WineBrand(brand_name="", vintage=1980)
    maxyear = datetime.date.today().year
    if request.method == "POST":
        item.brand_name = request.form['itemname']
        item.winetype_id = request.form.get('winetypevalue', None)
        item.vintage = request.form.get('vintagevalue', None)
        item.date_created = datetime.datetime.today()
        item.user_id = login_session.get('user_id', None)
        if 'filename' in request.files:
            item.filename = brandphotos.save(
                request.files['filename'],
                name=make_safe_filename(request.files['filename'].filename))
        session.add(item)
        session.commit()
        session.flush()
        winetype = session.query(WineType.name)\
            .filter_by(id=item.winetype_id)\
            .one()
        flash("Successfully Added '%s (%s)'" % (winetype.name, item.brand_name),
              'success')
        return redirect(url_for('.show_branditem', stockitem_id=item.id))
    else:
        winetypes = session\
            .query(WineType)\
            .order_by(asc(func.lower(WineType.name)))\
            .all()
        return render_template(template_prefix+'new_form.html',
                               item=item,
                               winetypes=winetypes,
                               maxyear=maxyear)