def add(): if request.method == 'GET': if "username" in session: user = get_logged_in_user() return render_template('add.html', user=user.name) else: desserts = Dessert.query.all() return render_template("menu.html", user=None, desserts=desserts, message="You must be logged in to add a dessert.") # Because we 'returned' for a 'GET', if we get to this next bit, we must # have received a POST # Get the incoming data from the request.form dictionary. # The values on the right, inside get(), correspond to the 'name' # values in the HTML form that was submitted. user = get_logged_in_user() dessert_name = request.form.get('name_field') dessert_price = request.form.get('price_field') dessert_cals = request.form.get('cals_field') # Now we are checking the input in create_dessert, we need to handle # the Exception that might happen here. # Wrap the thing we're trying to do in a 'try' block: try: dessert = create_dessert(dessert_name, dessert_price, dessert_cals, user.id) return render_template('add.html', user=user.name, dessert=dessert) except Exception as e: # Oh no, something went wrong! # We can access the error message via e.message: return render_template('add.html', error=e.message)
def add(): if request.method == 'GET': return render_template('add.html') # Because we 'returned' for a 'GET', if we get to this next bit, we must # have received a POST # Get the incoming data from the request.form dictionary. # The values on the right, inside get(), correspond to the 'name' # values in the HTML form that was submitted. dessert_name = request.form.get('name_field') dessert_price = request.form.get('price_field') dessert_cals = request.form.get('cals_field') dessert_website = request.form.get('website_field') dessert_status = request.form.get('status_field') dessert_introduce = request.form.get('introduce_field') dessert_time = request.form.get('time_field') dessert_create_time = str(datetime.now()) dessert_industry = request.form.get('industry_field') dessert_info = '{"website": %s, "status": %s}' % (dessert_website, dessert_status) dessert = create_dessert(dessert_name, dessert_price, dessert_info) return render_template('add.html', dessert=dessert)
def add(): if request.method == 'GET': return render_template('add.html') dessert_name = request.form.get('name_field') dessert_price = request.form.get('price_field') dessert_cals = request.form.get('cals_field') dessert = create_dessert(dessert_name, dessert_price, dessert_cals) return render_template('add.html', dessert=dessert)
def test_create_dessert_works(): test_name = "Test Dessert" test_price = 10 test_calories = 200 dessert = create_dessert(test_name, test_price, test_calories) assert dessert is not None # Delete this dessert now we are done db.session.delete(dessert) db.session.commit()
def test_create_dessert_missing_data(): # Test that if we pass 'None' in, we fail. dessert = None try: dessert = create_dessert(None, None, None) except Exception: # You could use e.message in here to check that the error message # is correct if you like. pass # Check dessert is still not created. assert dessert is None # Also try with empty strings try: dessert = create_dessert('', '', '') except Exception: pass # Check dessert is still not created. assert dessert is None
def add(): if request.method == 'GET': return render_template('add.html') dessert_name = request.form.get('name_field') dessert_price = request.form.get('price_field') dessert_cals = request.form.get('cals_field') dessert_sub = request.form.get('Subject_field') sex = request.form['options'] dessert = create_dessert(dessert_name, dessert_price, dessert_cals, dessert_sub, sex) return render_template('add.html', dessert=dessert)
def add(): if request.method == 'GET': return render_template('add.html') # Because we 'returned' for a 'GET', if we get to this next bit, we must # have received a POST # Get the incoming data from the request.form dictionary. # The values on the right, inside get(), correspond to the 'name' # values in the HTML form that was submitted. dessert_name = request.form.get('name_field') dessert_price = request.form.get('price_field') dessert_cals = request.form.get('cals_field') dessert = create_dessert(dessert_name, dessert_price, dessert_cals) return render_template('add.html', dessert=dessert)
def test_create_dessert_wrong_types(): # Test we can't create a dessert with the wrong type of input test_name = 4 test_price = "Cake" test_calories = "None" # Initialize the dessert variable so we can check it later. dessert = None # "Try" to create the dessert, and do nothing (pass) if we get an error. # After this next block of code, dessert should still be None. try: dessert = create_dessert(test_name, test_price, test_calories) except Exception: pass # Check dessert is still not created. assert dessert is None
def test_delete_dessert(): dessert = create_dessert('test', 0, 0) message = delete_dessert(dessert.id) assert message == 'Dessert test deleted'