Exemplo n.º 1
0
def create_user(info):
    user = db_session.query(User).filter_by(email = info['email']).one_or_none()

    if user:
        return user.id

    new_user = User(
        email = info['email'],
        name = info['user_name']
    )

    db_session.add(new_user)
    db_session.commit()

    new_record = db_session.query(User).filter_by(email = info['email']).one()
    return new_record.id
Exemplo n.º 2
0
def addCatalog():
    if request.method == 'GET':
        return render_template('add.html')
    if request.method == 'POST':
        form = request.form
        catalogName = form['catalog'].lower()
        query = db_session.query(Catalog).filter_by(name=catalogName)\
            .one_or_none()

        if not query:
            catalog = Catalog(name=catalogName)
            db_session.add(catalog)
            db_session.commit()
            flash('Category Successfully Added!')
            return render_template('add.html')
        else:
            flash('Current Catalog has existed.', 'error')
            return render_template('add.html')
Exemplo n.º 3
0
def addItem():
    if request.method == 'GET':
        catalogs = db_session.query(Catalog).all()
        return render_template('add_item.html', catalogs=catalogs)
    if request.method == 'POST':
        form = request.form
        name = form['name']
        catalogName = form['catalog']
        description = form['description']
        catalogs = db_session.query(Catalog).all()
        query = db_session.query(Item).filter_by(name=name).one_or_none()

        if not query:
            new_item = Item(name=name,
                            catalog_name=catalogName,
                            description=description)
            db_session.add(new_item)
            db_session.commit()
            flash('Add Successfully.')
            return render_template('add_item.html', catalogs=catalogs)
        else:
            flash('Item Name has existed.')
            return render_template('add_item.html', catalogs=catalogs)
Exemplo n.º 4
0
def editCatalogItem(catalog, item):
    selected_item = db_session.query(Item).filter_by(name=item).one_or_none()
    hasChange = False

    if request.method == 'GET':
        all_catalogs = db_session.query(Catalog).all()
        return render_template('edit.html',
                               catalog=catalog,
                               all_catalogs=all_catalogs,
                               item=selected_item)

    # Update Item
    if request.method == 'POST':
        form = request.form
        form_description = form['description']
        if form['name'] and form['name'] != selected_item.name:
            selected_item.name = form['name']
            hasChange = True

        # check the post data if change
        if form_description and form_description != selected_item.description:
            selected_item.description = form_description
            hasChange = True

        if form['catalog'] and form['catalog'] != selected_item.catalog.name:
            selected_item.catalog_name = form['catalog']
            hasChange = True

        if hasChange:
            db_session.add(selected_item)
            db_session.commit()

        return redirect(
            url_for('catalogItem',
                    catalog=selected_item.catalog_name,
                    item=selected_item.name))
Exemplo n.º 5
0
def deleteItem(item):
    selected_item = db_session.query(Item).filter_by(name=item).one_or_none()
    db_session.delete(selected_item)
    db_session.commit()
    response = {'status': 0, 'msg': 'Delete Success'}
    return jsonify(response), 200