示例#1
0
def newMensSubCategory():

    if request.method == 'POST':
        newSubCategory = subCategories(
            category_id=2, name=request.form['name'], description=request.form['description'])

        # Get filename and save w. filesys
        file = request.files['file']

        if file and allowed_file(file.filename):
            fileExt = str(file.filename).split('.')[1]
            filename = str(session.query(subCategories).order_by(
                subCategories.id.desc()).first().id+1) + '.' + fileExt

            file.save(os.path.join('static/img/category/', filename))

        # Add and commit
        session.add(newSubCategory)
        flash('New SubCategory %s Successfully Created' % newSubCategory.name)
        session.commit()
        return redirect('/men/')
    else:
        return render_template('newSubCategory.html', login=loginState)
示例#2
0
DBSession = sessionmaker(bind=engine)
session = DBSession()

data = json.loads(
    open('db_init.json', 'r').read())



try:
	for i in data['categories']:
	    c = Categories(name=i)
	    session.add(c)

	for i in data['subCategories']:
	    sc = subCategories(
	        category_id=i['category_id'], name=i['name'], description=i['description'])
	    session.add(sc)

	for i in data['items']:
	    item = Items(name=i['name'], description=i['description'], category=i[
	                 'category'], subCategory=i['subCategory'], price=i['price'])
	    session.add(item)
	
	session.commit()

except:
	session.rollback()
	raise RuntimeError('Database write failed.')


print '\nDatabase build successful!'