def homeMain():
    if request.method == 'POST':
        print(request.form['name'])
        data = request.form['name']
        item = session.query(Items).filter_by(name=data).one()
        if item.user_id == current_user.id:
            session.delete(item)
            failed = False
            try:
                session.commit()
            except Exception as e:
                session.rollback()
                session.flush()
                failed = True
                if not failed:
                    flash(f'Deleted Successfully', 'success')
                else:
                    flash(f'Not Deleted Successfully', 'danger')

                return redirect(url_for('homeMain'))
        else:
            flash(f'User is not Authenticated to Delete the Item', 'danger')

    return render_template('main.html',
                           categorie=session.query(Categories).all(),
                           items=session.query(Items)
                           .order_by(Items.id.desc()).all(),
                           current_user=current_user)
def add_category():
    if not current_user.is_authenticated:
        return redirect(url_for('homeMain'))
    name = ''
    if request.method == 'POST':
        for key in request.form:
            if key == 'ItemName':
                name = request.form[key]
                print(name)
        category = Categories(category_name=name, user_id=current_user.id)
        session.add(category)
        failed = False
        try:
            session.commit()
        except Exception as e:
            session.rollback()
            session.flush()
            failed = True

        if not failed:
            flash(f'Added Successfully', 'success')
        else:
            flash(f'Not Added Successfully', 'danger')
        return redirect(url_for('homeMain'))
    return render_template('add_category.html')
def getCategories(categories_name):
    if request.method == 'POST':
        print(request.form['name'])
        data = request.form['name']
        item = session.query(Items).filter_by(name=data).one()
        session.delete(item)
        failed = False
        try:
            session.commit()
        except Exception as e:
            session.rollback()
            session.flush()
            failed = True

        if not failed:
            flash(f'Deleted Successfully', 'success')
        else:
            flash(f'Not Deleted Successfully', 'danger')

        return redirect(url_for('getCategories'))

    cat_id = session.query(Categories). \
        filter(Categories.category_name == categories_name).first()
    item = session.query(Items). \
        filter(Items.cat_id == cat_id.category_id).all()
    print(cat_id.category_id)
    return render_template('view_items.html',
                           categorie=session.query(Categories).all(),
                           items=item,
                           current_user=current_user)
def editItem(itemName):
    if not current_user.is_authenticated:
        return redirect(url_for('homeMain'))
    name = ''
    desc = ''
    cat_id = ''
    print(itemName)
    item = session.query(Items).filter(Items.name == itemName).first()
    cat = session.query(Categories). \
        filter(Categories.category_id == item.cat_id).first()
    categories = session.query(Categories).all()
    if request.method == 'POST':
        for key in request.form:
            if key == 'ItemName':
                name = request.form[key]
                print(name)

            if key == 'ItemDesc':
                desc = request.form[key]
                print(desc)

            if key == 'ItemSelect':
                cat_id = request.form[key]
                print(cat_id)
        if item.user_id == current_user.id:
            print(name + ' ' + desc + ' ' + cat_id + "Data to be Updated")
            item = session.query(Items).filter_by(id=item.id).one()
            item.name = name
            item.description = desc
            item.cat_id = cat_id
            item.user_id = current_user.id
            session.add(item)
            failed = False
            try:
                session.commit()
            except Exception as e:
                session.rollback()
                session.flush()
                failed = True

            if not failed:
                flash(f'Updated Successfully', 'success')
            else:
                flash(f'Not Updated Successfully', 'danger')

            return redirect(url_for('homeMain'))
        else:
            flash(f'User is not Authenticated to Update this Item', 'danger')
    return render_template('edit_item.html',
                           item=item, cat=cat, categories=categories)
def create_user(login_session):
    """Crate a new user.
    Argument:
    login_session (dict): The login session.
    """

    new_user = User(
        name=login_session['username'],
        email=login_session['email'],
    )
    session.add(new_user)
    session.commit()
    user = session.query(User).filter_by(email=login_session['email']).one()
    return user.id
def user_registration():
    if current_user.is_authenticated:
        return redirect(url_for('homeMain'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')

        user = User(name=form.name.data, email=form.email.data,
                    password=hashed_password)
        session.add(user)
        session.commit()
        flash(f'Account Created for {form.name.data}!', 'success')
        return redirect(url_for('user_login'))
    return render_template('register.html', titile='Registration', form=form)
def addItem():
    if not current_user.is_authenticated:
        return redirect(url_for('homeMain'))
    name = ''
    desc = ''
    cat_id = ''
    categories = session.query(Categories).all()
    if request.method == 'POST':
        for key in request.form:
            if key == 'ItemName':
                name = request.form[key]
                print(name)

            if key == 'ItemDesc':
                desc = request.form[key]
                print(desc)

            if key == 'ItemSelect':
                cat_id = request.form[key]
                print(cat_id)

        item = Items(name=name,
                     description=desc,
                     user_id=current_user.id, cat_id=cat_id)
        session.add(item)
        try:
            session.commit()
        except Exception as e:
            session.rollback()
            session.flush()
            failed = True
            if not failed:
                flash(f'Added Successfully', 'success')
            else:
                flash(f'Not Added Successfully', 'danger')

        return redirect(url_for('homeMain'))

    return render_template('addItem.html', categories=categories)
Esempio n. 8
0
from catalog_app.models import User, Categories, Items
from catalog_app.models import session

category = Categories(category_id=1, category_name='Snowboarding', user_id=1)
session.add(category)
session.commit()

category = Categories(category_id=2, category_name='Skiying', user_id=1)
session.add(category)
session.commit()

category = Categories(category_id=3, category_name='Surfing', user_id=1)
session.add(category)
session.commit()

item = Items(id=1,
             description="These specialized boots will "
             "connect you to your board through "
             "the bindings. You can "
             "also rent these at"
             " the resort, but it is "
             "not recommended. "
             "Snowboard boots are "
             "designed to conform to your feet specifically,"
             " so owning your own pair will be far "
             "more comfortable. "
             "Snowboard boots come in regular shoe sizes, "
             "but sizing can vary "
             "among different companies."
             " Your boots should be snug, "
             "but not tight to the point of "