def newCategory(): if 'username' not in login_session: return redirect(url_for('showLogin')) if request.method == 'POST': #check if category name is inputted if request.form['name']: # check if categry exists if not uniqueCategory(request.form['name']): flash('Category already exists. Please Enter a unique name','info') return redirect(url_for('showCatalog')) #default image filename = 'default.jpg' # check if the post request has the file part, set default image #handle uploaded image, give it a unique name and save it to file system if 'file' in request.files: file = request.files['file'] if allowed_file(request.files['file'].filename): filename = getFileName(file) else: #for cases when upload file is not an image flash('File extension is not supported. Only images are welcome :)', 'Info') return redirect(url_for('showCatalog')) # store only the file name in the database newCategory = Category(name=request.form['name'], status = 'A', image = filename, user_id = login_session['user_id']) session.add(newCategory) session.commit() flash('Category was created successfully', 'success') else: # category name field is empty flash('Please enter the category name', 'danger') return redirect(url_for('showCatalog')) else: # GET request return render_template('newcategory.html', title='Add new Catgeory')
DBSession = sessionmaker(bind=engine) session = DBSession() #Create users user1 = User(name='Fatima Alawami', email='*****@*****.**', picture='https://pbs.twimg.com/profile_images/2671170543/18debd694829ed78203a5a36dd364160_400x400.png') session.add(user1) session.commit() user2 = User(name='Fatima Alawami', email='*****@*****.**', picture='https://lh4.googleusercontent.com/-E0yXLnt4hw8/AAAAAAAAAAI/AAAAAAAAAAA/32d6AfpW4z8/photo.jpg') session.add(user2) session.commit() # Create categories first Soccer = Category (user_id = 2, name = 'Soccer', image = 'soccer.jpg', status = 'A') session.add(Soccer) session.commit() Basketball = Category (user_id = 2, name = 'Basketball', image = 'basketball.jpg', status = 'A') session.add(Basketball) session.commit() Baseball = Category (user_id = 2, name = 'Baseball', image = 'baseball.jpg', status = 'A') session.add(Baseball) session.commit() Frisbee = Category (user_id = 2, name = 'Frisbee', image = 'frisbee.jpg', status = 'A') session.add(Frisbee) session.commit()
# session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() user1 = User(name="Ahmed Assaf", email="*****@*****.**") session.add(user1) session.commit() user2 = User(name="Lujain Ghul", email="*****@*****.**") session.add(user2) session.commit() # category cat1 = Category(name="Soccer") session.add(cat1) session.commit() item_1 = Item(name="Shingaurd", desc=""" The rules of association football were codified in England by the Football Association in 1863 and the name association football was coined to distinguish the game from the other forms of football p """, category=cat1, user=user1) item_2 = Item(name="Players, equipment, and officials", desc="""
engine = create_engine('postgresql://*****:*****@localhost/catalog') # Bind the engine to the metadata of the Base class so that the # declaratives can be accessed through a DBSession instance Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() category1 = Category(title="Biographies") session.add(category1) session.commit() category2 = Category(title="Computers & Tech") session.add(category2) session.commit() category3 = Category(title="Cooking") session.add(category3) session.commit() category4 = Category(title="Romance") session.add(category4) session.commit()
# revert all of them back to the last commit by calling # session.rollback() session = DBSession() # User.__table__.drop() # Category.__table__.drop() # CategoryItem.__table__.drop() #Create dummy user User1 = User(name="Fatim Alawami", email="*****@*****.**", picture='https://pbs.twimg.com/profile_images/2671170543/18debd694829ed78203a5a36dd364160_400x400.png') session.add(User1) session.commit() # Soccer Category category1 = Category(user_id=1, name="Soccer", image="fifa.jpg",status='A') session.add(category1) session.commit() CategoryItem2 = CategoryItem(user_id=1, title="Soccer ball", description="adidas 2018 FIFA World Cup Russia Telstar Glider Soccer Ball", price=20, image='fifa.jpg', category=category1,status='A') session.add(CategoryItem2) session.commit() CategoryItem1 = CategoryItem(user_id=1, title="Goalkeeper Shirt", description="Storelli Youth BodyShield 3/4 Soccer Goalkeeper Shirt", price=30, image='goalkeepershirt.png', category=category1,status='A') session.add(CategoryItem1)