def __init__(self): '''Creates entities to populate the mock database. ''' self.mock_users = (User('A', '*****@*****.**'), User('B', '*****@*****.**'), User('C', '*****@*****.**')) self.pantries = [ Pantry('Pantry_A', 1), Pantry('Pantry_B', 2), Pantry('Pantry_C', 3), Pantry('Pantry_D', 1) ] self.categories = [ Category('vegetables', 1), Category('starches', 1), Category('desserts', 1), Category('veggies', 2), Category('snacks', 2), Category('meat', 2), Category('fruit', 3), # 7 Category('meat', 3), Category('drinks', 3) ] self.items = [ Item('apple', 'shiny and red', 5, 1, 1), # 0 Item('broccoli', 'small tree', 10, 5, 1), # 1 Item('chips', 'crispy', 4, 5, 5), # 2 Item('steak', 'high in protein', 1, 20, 8), Item('seltzer', 'fizzy', 15, 1, 3), Item('cake', 'moist', 1, 15, 3), Item('potato', 'high in carbs', 50, 20, 2), Item('apple', 'shiny and red', 5, 1, 7) ]
def addItem(): """This method adds an item to the database. RETURNS on POST the add page on GET the main page """ if 'username' in login_session: categories = session.query(Category).all() # If the request is POST, add the item into the db if request.method == 'POST': new_category = session.query(Category).filter_by( name=request.form['new_category']).one() newItem = Item(name=request.form['new_name'], description=request.form['new_description'], category_id=new_category.id, user_id=login_session['user_id']) session.add(newItem) newLatestItem = LatestItem(item_name=request.form['new_name'], category_name=new_category.name) session.add(newLatestItem) session.commit() return redirect(url_for('mainPage')) else: return render_template('addItem.html', p_categories=categories, p_session=login_session) else: return redirect(url_for('mainPage'))
def add_item(): logged_in = True if 'username' not in login_session: return redirect(url_for('showLogin')) else: email = login_session['email'] try: user = session.query(User).filter_by(email=email).one() except exc.SQLAlchemyError: return redirect(url_for('showLogin')) if request.method == 'GET': result_set = session.query(Category).all() categories = [] for category in result_set: categories.append(category) return render_template('addItem.html', categories=categories, logged_in=logged_in) else: title = request.values.get('title') description = request.values.get('description') category_name = request.values.get('category') # get category object of new item by category name category = session.query(Category).filter_by(name=category_name).one() item = Item(name=title, description=description, category=category, owner=user) session.add(item) session.commit() return redirect(url_for('index'))
def newIteminCategory(category_id): if 'username' not in login_session: return redirect('/login') if request.method == 'POST': newItem = Item(name=request.form['name'], description=request.form['description'], category_id=category_id) session.add(newItem) session.commit() return redirect(url_for('showItems', category_id=category_id)) else: category = session.query(Category).filter_by(id=category_id).one() return render_template('newIteminCategory.html', category=category)
def newItem(): if 'username' not in login_session: return redirect('/login') if request.method == 'POST': newItem = Item(name=request.form['name'], description=request.form['description'], category_id=request.form['category']) session.add(newItem) session.commit() return redirect(url_for('showCategories')) else: categories = session.query(Category).all() return render_template('newItem.html', categories=categories)
def createItem(category_name): """This route allows a user to create a new item for a given category.""" if 'username' not in session: return redirect('/catalog/login') categories = db_session.query(Category).order_by(asc(Category.name)) category = db_session.query(Category).filter_by(name=category_name).one() form = createItemForm(category=category.id) form.category.choices = [(c.id, c.name) for c in categories] if request.method == 'POST' and form.validate_on_submit(): newItem = Item(name=request.form['name'], description=request.form['description'], category_id=request.form['category'], user_id=session['user_id']) db_session.add(newItem) db_session.commit() # now add image_url storing the file under the item's ID image_url = MISSING_IMAGE filename = secure_filename(form.photo.data.filename) if filename: image_dir = 'static/uploads/%s/' % newItem.id os.mkdir(image_dir) form.photo.data.save(image_dir + filename) image_url = image_dir + filename newItem.image_url = image_url db_session.add(newItem) db_session.commit() category = db_session.query(Category).filter_by( id=request.form['category']).one() flash(u"“%s” has been successfully created." % newItem.name) return redirect(url_for('showCategory', category_name=category.name)) return render_template('createItem.html', form=form, category_name=category_name)
def addItem(): # Check if the user is logged-in if 'username' not in login_session: flash("please login") return redirect(url_for("showHomePage")) # if the http method is a post if request.method == 'POST': # reads the all the values from the from title = request.form['title'] description = request.form['description'] category = request.form['category'] # Check if the category exits in the database if categoryCheck(category): flash("Invalid category") return redirect(url_for('addItem')) cat_id = session.query(Category).filter_by( name=request.form['category']).one().id # Check if the title or description are emtpy if title == "" or description == "": flash("Please enter your title and/or description") return redirect(url_for('addItem')) item = session.query(Item).filter_by(title=title).one_or_none() # Check if the title already exits in the database if item is not None: flash("{} already exists," " please choose another title".format(title)) return redirect(url_for('addItem')) # makes the item and saves it to the database newitem = Item(title=title, description=description, cat_id=cat_id, user_id=login_session['user_id']) session.add(newitem) session.commit() flash("{} has been successfully added".format(title)) return redirect(url_for('showHomePage')) # returns the form page to the use categorys = session.query(Category).all() return render_template("addItem.html", categorys=categorys)
def newItem(category_id): """Adds a new item in a category and redirects to main page""" if 'username' not in login_session: return redirect('/login') user_id = login_session['user_id'] if request.method == 'POST': newItem = Item(name=request.form['name'], description=request.form['description'], category_id=category_id, user_id=user_id) session.add(newItem) session.commit() flash('%s Successfully Created' % (newItem.name)) return redirect(url_for('showItem', category_id=category_id)) else: return render_template('newItem.html', category_id=category_id) return render_template('newItem.html', category_id=category_id)
def new_item(category_name): db_session = DBSession() if request.method == 'POST': category = db_session.query(Category).filter_by( name=category_name).one() newItem = Item( name=request.form['iname'], category=category, category_id=category.id, description=request.form['idesc'], user_id=session['gplus_id']) if item and item.user_id != session['user_id']: flash("Error") state = session['state'] categories = db_session.query(Category) return render_template('home.html', categories=categories, STATE=state) else: db_session.add(newItem) db_session.commit() flash("new menu item created") return (catalog_desc(category.name)) else: return render_template('new_item.html', category_name=category_name)
session.add(Electronics) session.commit() BedBath = Category(name="Bed & Bath") session.add(BedBath) session.commit() Sports = Category(name="Sports") session.add(Sports) session.commit() item = Item( name="Flat Screen TV", description= "Make your friday nights a little less lonely by watching your favourite characters come to life", category_id=4) session.add(item) session.commit() item = Item( name="Speakers", description="Music so loud your neighbours will definitely hate you", category_id=4) session.add(item) session.commit() item = Item( name="Tennis Shoes", description=
session.add(snowBoarding) rock_climbing = Category(name='Rock Climbing') session.add(rock_climbing) foosball = Category(name='Foosball') session.add(foosball) skating = Category(name='Skating') session.add(skating) hockey = Category(name='Hockey') session.add(hockey) soccerItem1 = Item(name='SoccerBall', category=soccer, description=" one size fits all", user_id='*****@*****.**') session.add(soccerItem1) soccerItem2 = Item(name='SoccerNet', category=soccer, description="sturdy and grade A quality", user_id='*****@*****.**') session.add(soccerItem2) snowBoardingItem1 = Item(name='Goggles', category=snowBoarding, description="Must be worn while snowboarding", user_id='*****@*****.**') session.add(snowBoardingItem1) snowBoardingItem2 = Item(name='Snowboard', category=snowBoarding, description="Best for any terrain and conditions",
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from catalog_database_setup import Base, Category, Item from random import randint url = 'postgresql://{}:{}@{}:{}/{}' url = url.format('catalog', 'password', 'localhost', '5432', 'catalog_db') engine = create_engine(url, client_encoding='utf8') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() category = Category(name="Apps & Games") session.add(category) item = Item(name="Tetris", description="Description of Tetris", category=category) session.add(item) item = Item(name="Minecraft", description="Description of Minecraft", category=category) session.add(item) item = Item(name="Hungry Shark Evolution", description="Description of Hungry Shark Evolution", category=category) session.add(item) item = Item(name="Portal", description="Description of Portal", category=category) session.add(item) item = Item(name="Dota 2",
user2 = User(name="Sonny Tilander", email="*****@*****.**") session.add(user1) session.commit() user3 = User(name="Hugh Altretch", email="*****@*****.**") session.add(user1) session.commit() # Category 'Soccer' category1 = Category(name="Soccer") session.add(category1) session.commit() item1 = Item(name="Pair of Shin Guards", description=("Description: Black pair of shin guards. " "Made of foam rubber. Light and sturdy."), category=category1, user=user1) session.add(item1) session.commit() item2 = Item(name="Soccer Jersey of Leeds", description=("Description: White jersey with blue logo on. " "Made of cotton. Machine-fitted."), category=category1, user=user2) session.add(item2) session.commit() item3 = Item( name="Soccer Shoes",