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 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 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 exists_category(category_id):
    category = session.query(Categories). \
        filter_by(category_id=category_id).first()
    if category is not None:
        return True
    else:
        return False
def get_user_id(email):
    """Get user ID by email.
    Argument:
        email (str) : the email of the user.
    """

    try:
        user = session.query(User).filter_by(email=email).one()
        return user.id
    except BaseException:
        return None
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 catalog_item_json(category_id, item_id):
    """Return JSON of a particular item in the catalog."""

    if exists_category(category_id) and exists_item(item_id):
        item = session.query(Items) \
            .filter_by(id=item_id, cat_id=category_id).first()
        if item is not None:
            return jsonify(item=item.serialize)
        else:
            return jsonify(
                error='item {} does not belong to category {}.'.format
                (item_id,
                 category_id))
    else:
        return jsonify(error='The item or the category does not exist.')
def user_login():
    if current_user.is_authenticated:
        return redirect(url_for('homeMain'))
    form = LoginForm()
    if form.validate_on_submit():
        user = session.query(User).filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password,
                                               form.password.data):
            login_user(user, remember=form.remember.data)
            return redirect(url_for('homeMain'))
        else:
            flash(f'Login Not Success!', 'danger')

    state = ''.join(random.choice(string.ascii_uppercase + string.digits)
                    for x in range(32))
    login_session['state'] = state
    return render_template('login.html', title='Login',
                           form=form, STATE=state, client_id=GOOGLE_CLIENT_ID)
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. 10
0
 def validate_email(self, email):
     user = session.query(User).filter_by(email=email.data).first()
     if user:
         raise ValidationError('The Email is Already Registered.'
                               ' Please Login with your Email.')
def exists_item(item_id):
    item = session.query(Items).filter_by(id=item_id).first()
    if item is not None:
        return True
    else:
        return False
def categories_json():
    categories = session.query(Categories).all()
    return jsonify(categories=[i.serialize for i in categories])