示例#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
示例#2
0
def catalog_item_api(item):
    query = db_session.query(Item).filter_by(name=item).one_or_none()
    result = [{
        'name': query.name,
        'description': query.description,
        'catalog': query.catalog.name
    }]
    return jsonify(result)
示例#3
0
def index():
    catalogs = db_session.query(Catalog).all()
    items = db_session.query(Item).all()
    result = {}
    for catalog in catalogs:
        for item in items:
            if item.catalog_name == catalog.name:
                catalog_name = normalize('NFKD', catalog.name)\
                    .encode('ascii', 'ignore')
                item_name = normalize('NFKD', item.name)\
                    .encode('ascii', 'ignore')
                item_description = normalize('NFKD', item.description)\
                    .encode('ascii', 'ignore')
                if not result.get(catalog_name):
                    result[catalog_name] = {}
                result[catalog_name][item_name] = item_description
    return render_template('index.html', result=result)
示例#4
0
def catalog_api(catalog):
    query = db_session.query(Item).filter_by(catalog_name=catalog).all()
    result = []
    for i in query:
        result.append({
            'name': i.name,
            'description': i.description,
            'catalog': i.catalog.name
        })
    return jsonify(result)
示例#5
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)
示例#6
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))
示例#7
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')
示例#8
0
def get_user_id(email):
    try:
        user = db_session.query(User).filter_by(id=email).one()
        return user
    except:
        return None
示例#9
0
def catalogs_api():
    result = db_session.query(Catalog).all()
    return jsonify([i.serialize for i in result])
示例#10
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
示例#11
0
def catalogItem(catalog, item):
    result = db_session.query(Item).filter_by(name=item).one_or_none()
    return render_template('detail.html',
                           catalog=catalog,
                           item=item,
                           description=result.description)