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 delete_category_item(item_id): """ Function to return a page to delete a category item. Args: item_id: ID of the category item to delete. """ user = get_user() item = db_session.query(CategoryItem)\ .filter_by(id=item_id)\ .first() category_id = '' if not item: if login_session.get('last_category_id', '') == '': return redirect(url_for('index')) else: category_id = login_session.get('last_category_id') else: category_id = item.category.id # Make sure the user is the creator of the item. if not user or user and user.id != 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 delete it.'.format(item.name)) if request.method == 'POST': db_session.delete(item) db_session.commit() flash("Item {} deleted.".format(item.name)) return redirect(url_for('category_info', category_id=category_id)) else: return render_template('delete_category_item.html', item=item)
def delete_category(category_id): """ Function to return a page to delete a category. Args: category_id: ID of the category to delete. """ user = get_user() category = db_session.query(Category)\ .filter_by(id=category_id).first() if not category: return redirect(url_for('index')) # Make sure the user is the creator of the category. if not user or user and user.id != category.user.id: return render_template('error.html', headline_text='Access Denied', error_text='Sorry, but you are not the creator of '\ 'the category "{}". As such, you are not authorized '\ 'to delete it.'.format(category.name)) if request.method == 'POST': # Get and delete all items associated with this category. items = db_session.query(CategoryItem)\ .filter_by(category_id=category.id)\ .all() for item in items: db_session.delete(item) # Delete the category itself and commit everything. db_session.delete(category) db_session.commit() flash("Category {} deleted.".format(category.name)) return redirect(url_for('index')) else: return render_template('delete_category.html', category=category)
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 "Goodbye." exit() print "Found the following users:" print for user in users: print user print confirmation = "%s admin privileges? (y/n): " % command.capitalize() decision = raw_input(confirmation).lower() if not decision.startswith("y"): print "Aborting" exit() for user in users: if command == "grant": user.admin = True user.activated = True else: user.admin = False db_session.commit() print "Admin privileges changed." print for user in users: print user print print "Goodbye" print