def editItem(category_name, item_name): editedItem = session.query(Items).filter_by(name=item_name).one() categories = session.query(Category).all() # See if the logged in user is the owner of item creator = getUserInfo(editedItem.user_id) user = getUserInfo(login_session['user_id']) # If logged in user != item owner redirect them if creator.id != login_session['user_id']: flash ("You cannot edit this item. This item belongs to %s" % creator.name) return redirect(url_for('showCatalog')) # POST methods if request.method == 'POST': if request.form['name']: editedItem.name = request.form['name'] if request.form['description']: editedItem.description = request.form['description'] if request.form['picture']: editedItem.picture = request.form['picture'] if request.form['category']: category = session.query(Category).filter_by(name=request.form['category']).one() editedItem.category = category time = datetime.datetime.now() editedItem.date = time session.add(editedItem) session.commit() flash('Category Item Successfully Edited!') return redirect(url_for('showCategory', category_name=editedItem.category.name)) else: return render_template('edititem.html', item=editedItem, categories=categories)
def gdisconnect(): # Only disconnect a connected user. access_token = login_session.get('access_token') if access_token is None: response = make_response( json.dumps('Current user not connected.'), 401) response.headers['Content-Type'] = 'application/json' return response url = 'https://accounts.google.com/o/oauth2/revoke?token=%s' % access_token h = httplib2.Http() result = h.request(url, 'GET')[0] if result['status'] == '200': # Reset the user's sesson. del login_session['access_token'] del login_session['gplus_id'] del login_session['username'] del login_session['email'] del login_session['picture'] # response = make_response(json.dumps('Successfully disconnected.'), 200) # response.headers['Content-Type'] = 'application/json' response = redirect(url_for('showCatalog')) flash("You are now logged out.") return response else: # For whatever reason, the given token was invalid. response = make_response( json.dumps('Failed to revoke token for given user.', 400)) response.headers['Content-Type'] = 'application/json' return response
def dated_url_for(endpoint, **values): if endpoint == 'static': filename = values.get('filename', None) if filename: file_path = os.path.join(app.root_path, endpoint, filename) values['q'] = int(os.stat(file_path).st_mtime) return url_for(endpoint, **values)
def deleteCategory(category_name): categoryToDelete = session.query(Category).filter_by(name=category_name).one() # See if the logged in user is the owner of item creator = getUserInfo(categoryToDelete.user_id) user = getUserInfo(login_session['user_id']) # If logged in user != item owner redirect them if creator.id != login_session['user_id']: flash ("You cannot delete this Category. This Category belongs to %s" % creator.name) return redirect(url_for('showCatalog')) if request.method =='POST': session.delete(categoryToDelete) session.commit() flash('Category Successfully Deleted! '+categoryToDelete.name) return redirect(url_for('showCatalog')) else: return render_template('deletecategory.html', category=categoryToDelete)
def addCategory(): if request.method == 'POST': newCategory = Category( name=request.form['name'], user_id=login_session['user_id']) print (newCategory) session.add(newCategory) session.commit() flash('Category Successfully Added!') return redirect(url_for('showCatalog')) else: return render_template('addcategory.html')
def editCategory(category_name): editedCategory = session.query(Category).filter_by(name=category_name).one() category = session.query(Category).filter_by(name=category_name).one() # See if the logged in user is the owner of item creator = getUserInfo(editedCategory.user_id) user = getUserInfo(login_session['user_id']) # If logged in user != item owner redirect them if creator.id != login_session['user_id']: flash ("You cannot edit this Category. This Category belongs to %s" % creator.name) return redirect(url_for('showCatalog')) # POST methods if request.method == 'POST': if request.form['name']: editedCategory.name = request.form['name'] session.add(editedCategory) session.commit() flash('Category Item Successfully Edited!') return redirect(url_for('showCatalog')) else: return render_template('editcategory.html', categories=editedCategory, category = category)
def addItem(): categories = session.query(Category).all() if request.method == 'POST': newItem = Items( name=request.form['name'], description=request.form['description'], picture=request.form['picture'], category=session.query(Category).filter_by(name=request.form['category']).one(), date=datetime.datetime.now(), user_id=login_session['user_id']) session.add(newItem) session.commit() flash('Item Successfully Added!') return redirect(url_for('showCatalog')) else: return render_template('additem.html', categories=categories)
def index(): if session['logged_in'] == true: return render_template('index.html') else: return redirect(url_for('login'))