def new_category_item(): """ Function to return a page to create a new category item. """ set_redirect_url() user = get_user() categories = get_all_objects_of_type(Category) category = None if not user: return redirect(url_for('login')) if request.method == 'POST': if request.form.get('name', '') == '' and request.form.get('category', '') != '': category = db_session.query(Category)\ .filter_by(id=request.form.get('category'))\ .first() return render_template('new_category_item.html', user=user, category=category, categories=categories, request=request) new_item = CategoryItem(name=request.form['name'], user_id=login_session['user_id'], description=request.form['description'], category_id=request.form['category']) db_session.add(new_item) db_session.commit() flash('New Item {} Successfully Created!'.format(new_item.name)) return redirect(url_for('index')) else: return render_template('new_category_item.html', user=user, category=category, categories=categories)
def create_user(): """ Function to create a new user with the info from login session. """ new_user = User(name=login_session['username'], email=login_session['email'], picture=login_session['picture']) db_session.add(new_user) db_session.commit() user = db_session.query(User)\ .filter_by(email=login_session['email'])\ .one() return user.id
def edit_category_item(item_id): """ Function to return a page to edit a category item. Args: item_id: ID value of the category item to edit. """ user = get_user() categories = get_all_objects_of_type(Category) edited_item = db_session.query(CategoryItem)\ .filter_by(id=item_id)\ .first() if not edited_item: return render_template('error.html', headline_text='Item Not Found', error_text='The specified item was not found.') # Make sure the user is the creator of the item. if not user or user and user.id != edited_item.user.id: return render_template('error.html', headline_text='Access Denied', error_text='Sorry, but you are not the creator of '\ 'the item "{}". As such, you are not authorized '\ 'to make edits to it.'.format(edited_item.name)) if request.method == 'POST': edited_item.name = request.form['name'] edited_item.description = request.form['description'] edited_item.category_id = request.form['category'] db_session.add(edited_item) db_session.commit() flash('Item Successfully Updated!') category = db_session.query(Category)\ .filter_by(id=edited_item.category_id)\ .first() return redirect(url_for('category_item_info', item_id=edited_item.id)) else: return render_template('edit_category_item.html', item=edited_item, user=user, categories=categories)
def new_category(): """ Function to create a new category. """ set_redirect_url() user = get_user() if not user: return redirect(url_for('login')) if request.method == 'POST': category = Category(name=request.form['name'], user_id=login_session['user_id']) db_session.add(category) db_session.commit() flash('New Category {} Successfully Created!'.format(category.name)) return redirect(url_for('index')) else: return render_template('new_category.html', user=user)
def new_category_item(): """ Function to return a page to create a new category item. """ set_redirect_url() user = get_user() categories = get_all_objects_of_type(Category) category = None if not user: return redirect(url_for('login')) if request.method == 'POST': if request.form.get( 'name', '') == '' and request.form.get('category', '') != '': category = db_session.query(Category)\ .filter_by(id=request.form.get('category'))\ .first() return render_template('new_category_item.html', user=user, category=category, categories=categories, request=request) new_item = CategoryItem(name=request.form['name'], user_id=login_session['user_id'], description=request.form['description'], category_id=request.form['category']) db_session.add(new_item) db_session.commit() flash('New Item {} Successfully Created!'.format(new_item.name)) return redirect(url_for('index')) else: return render_template('new_category_item.html', user=user, category=category, categories=categories)
print "Old database found. Deleting." os.remove(db_file_path) else: print "No old database found." # Create the database print "Creating and initialising database." init_db() #Create tags print "Creating tags." books = Tag(name='Books', user_id=1) db_session.add(books) novels = Tag(name='Novels', user_id=1) db_session.add(novels) nonfiction = Tag(name='Non-fiction', user_id=1) db_session.add(nonfiction) furniture = Tag(name='Furniture', user_id=1) db_session.add(furniture) cars = Tag(name='Cars', user_id=1) db_session.add(cars) vehicles = Tag(name='Vehicles', user_id=1) db_session.add(vehicles)