def wrapper(item_id, **kw): try: session.query(Item).filter_by(id=item_id).one() return function(item_id=item_id) except: flash('Item does not exist') return redirect(url_for('getCategories'))
def wrapper(category_id, **kw): try: session.query(Category).filter_by(id=category_id).one() return function(category_id=category_id, **kw) except: flash('Category does not exist') return redirect(url_for('getCategories'))
def newItem(): if request.method == 'GET': categories = session.query(Category).all() return render_template("additem.html", categories=categories) elif request.method == 'POST': categories = session.query(Category).all() name = request.form['name'] description = request.form['description'] category_id = request.form['category_id'] user_id = login_session['user_id'] # checks if the entered item name already exists # for the same category if name: try: existing_item = session.query(Item).filter_by( name=name, category_id=category_id).one().name if (existing_item): error_message = """Such an item already exists for this category!""" flash(error_message) return render_template( "additem.html", categories=categories, name=name, cat=category_id, description=description) except: print "Could not find an existing item" # checks if any of the entries is absent if (not description or not description.strip() or not name or not name.strip() or not category_id): error_message = """Please enter a name, a description and a category for your item!""" flash(error_message) return render_template( "additem.html", categories=categories, name=name, cat=category_id, description=description) # adds the new item to the database if everything is ok newItem = Item( name=name, description=description, category_id=category_id, user_id=user_id) session.add(newItem) session.commit() print category_id flash('Category item added successfully') return redirect(url_for('categoryItems', category_id=category_id))
def getCategories(): try: categories = session.query(Category).all() recently_added_items = session.query(Item).order_by(desc( Item.updated)).limit(10) return render_template('allcategories_public.html', categories=categories, recently_added_items=recently_added_items) except: return render_template('allcategories_public.html')
def wrapper(category_id, **kw): try: print category_id print login_session['user_id'] session.query(Category).filter_by( id=category_id, user_id=login_session['user_id']).one() return function(category_id=category_id, **kw) except: flash('Only creator can modify this category') return redirect(url_for('categoryItems', category_id=category_id))
def deleteItem(item_id): if request.method == 'GET': item = session.query(Item).filter_by(id=item_id).one() return render_template("deleteitem.html", item=item) elif request.method == 'POST': item = session.query(Item).filter_by(id=item_id).one() item_name = item.name category_id = item.category_id session.delete(item) session.commit() flash('%s Item deleted successfully' % item_name) return redirect(url_for('categoryItems', category_id=category_id))
def categoryItems(category_id): categories = session.query(Category).all() category = session.query(Category).filter_by(id=category_id).one() items = session.query(Item).filter_by( category_id=category_id).order_by( desc( Item.updated)) return render_template( 'categoryitems_public.html', categories=categories, category=category, items=items)
def userIdFromEmail(email): try: existing_user = session.query(User).filter_by(email=email).one() return existing_user.id except: return None
def wrapper(item_id): try: item = session.query(Item).filter_by( id=item_id, user_id=login_session['user_id']).one() return function(item_id=item_id) except: flash('Only creator can modify this item') return redirect(url_for('getCategories'))
def newUser(): new_user = User(name=login_session['username'], email=login_session['email'], picture=login_session['picture']) session.add(new_user) session.commit() new_user = session.query(User).filter_by( email=login_session['email']).one() return new_user.id
def editCategory(category_id): # shows the edit form if request.method == 'GET': category = session.query(Category).filter_by(id=category_id).one() return render_template("editcategory.html", name=category.name, description=category.description) elif request.method == 'POST': name = request.form['name'] description = request.form['description'] # checks if any other category has the new editted name if name: try: existing_category = session.query(Category).filter_by( name=name).filter_by(id != category_id).one().name if (existing_category): error_message = "A category for '%s' already exists!" % name flash(error_message) return render_template("editcategory.html", name=name, description=description) except: print "Could not find an existing category" # checks if any of the entries is absent if (not description or not description.strip() or not name or not name.strip()): error_message = "Please enter a name and a description!" flash(error_message) return render_template("editcategory.html", name=name, description=description) # updates the database if everything is fine category = session.query(Category).filter_by(id=category_id).one() category.name = request.form['name'] category.description = request.form['description'] session.add(category) session.commit() flash('Category edited successfully') return redirect(url_for('getCategories'))
def test_database(): Base.metadata.drop_all(engine) Base.metadata.create_all(engine) session.commit() notebook = Notebook('TESTE') session.add(notebook) session.commit() note1 = Note(notebook, 'DOC1', 'Test of Note number 1') note2 = Note(notebook, 'DOC2', 'Test of Note number 2') session.add(note1) session.add(note2) session.commit() notebooks = session.query(Notebook).all() assert len(notebooks) == 1 assert notebooks[0].name == "TESTE" notes = notebooks[0].notes assert len(notes) == 2 assert notes[0].title == "DOC1" assert notes[1].title == "DOC2"
def newCategory(): if request.method == 'GET': return render_template("addcategory.html") elif request.method == 'POST': name = request.form['name'] description = request.form['description'] user_id = login_session['user_id'] # checks if the entered category name already exists if name: try: existing_category = session.query(Category).filter_by( name=name).one().name if (existing_category): error_message = "A category for '%s' already exists!" % name flash(error_message) return render_template("addcategory.html", name=name, description=description) except: print "Could not find an existing category" # checks if any of the entries is absent if (not description or not description.strip() or not name or not name.strip()): error_message = "Please enter a name and a description!" flash(error_message) return render_template("addcategory.html", name=name, description=description) # adds the new category to the database if everything is ok newCategory = Category(name=name, description=description, user_id=user_id) session.add(newCategory) session.commit() flash('Category added successfully') return redirect(url_for('getCategories'))
def getUserID(email): try: user = session.query(User).filter_by(email=email).one() return user.id except: return None
def getItemJSON(item_id): item = session.query(Item).filter_by(id=item_id).one() return jsonify(Item=item.serialize())
def getItem(item_id): item = session.query(Item).filter_by(id=item_id).one() if 'username' in login_session: return render_template('item.html', item=item) else: return render_template('item_public.html', item=item)
def getCategoryJSON(category_id): category = session.query(Category).filter_by(id=category_id).one() return jsonify(Category=category.serialize())
def getCategory(category_id): category = session.query(Category).filter_by(id=category_id).one() if 'username' in login_session: return render_template('category.html', category=category) else: return render_template('category_public.html', category=category)
def getCategoriesJSON(): categories = session.query(Category).all() return jsonify( Categories=[category.serialize() for category in categories])
def getUserInfo(user_id): user = session.query(User).filter_by(id=user_id).one() return user
def categoryItemsJSON(category_id): category = session.query(Category).filter_by(id=category_id).one() items = session.query(Item).filter_by(category_id=category_id) return jsonify(Items=[item.serialize() for item in items])
def createUser(login_session): newUser = User(name=login_session["username"], email=login_session["email"], pic_url=login_session["picture"]) session.add(newUser) session.commit() user = session.query(User).filter_by(email=login_session["email"]).one() return user.id
def getExistingUser(user_id): existing_user = session.query(User).filter_by(id=user_id).one() return existing_user