def recipe(recipe_seo): recipe = Recipe.by_seo(recipe_seo) if not recipe: abort(404) scale_to = float(request.args.get('servings', recipe.serves)) scale = 1 if scale_to and recipe.serves: scale = scale_to / recipe.serves return render_response( 'recipe.html', dict( title=recipe.title, notes=recipe.notes, prephours=recipe.preptime // 60, prepmins=recipe.preptime % 60, cookhours=recipe.cooktime // 60, cookmins=recipe.cooktime % 60, source=recipe.source, sourceurl=recipe.sourceurl, serves=recipe.serves, seo=recipe.seo, image=recipe.image or "/static/images/recipes/noimage.png", ingredients=ingredient_groups(recipe.ingredients, scale=scale), method=ingredient_groups(recipe.method), vegetarian=recipe.vegetarian, editable=current_user.is_active, id=recipe.id, scale=scale, ))
def test_renders_text(app, context): """A txt extension results in no doctype and a text/plain mimetype""" with app.test_request_context(): rendered = render_response("test.txt", context) assert rendered.mimetype == "text/plain" assert rendered.data == b"Hi Rudolf\n"
def index(): vegetarian = request.args.get('vegetarian', 'yes') veg = vegetarian.lower() in h.true_strings starters = Recipe.by_type('starter', vegetarian=veg) mains = Recipe.by_type('main', vegetarian=veg) desserts = Recipe.by_type('dessert', vegetarian=veg) snacks = Recipe.by_type('snack', vegetarian=veg) drinks = Recipe.by_type('drink', vegetarian=veg) breads = Recipe.by_type('bread', vegetarian=veg) mealsets = ( ('Starters', starters), ('Mains', mains), ('Desserts', desserts), ('Breads', breads), ('Snacks', snacks), ('Drinks', drinks), ) authenticated = False # TODO - authentication # if identity.has_permission('recipe'): # authenticated = True return render_response( 'recipes.html', dict( mealsets=mealsets, authenticated=authenticated, vegetarian=veg, ))
def test_renders_css(app, context): """A css extension results in no doctype and a text/css mimetype""" with app.test_request_context(): rendered = render_response("test.css", context) assert rendered.mimetype == "text/css" assert rendered.data == b'h1:after { content: " Rudolf"; }\n'
def edit(): r = Recipe.get(request.values['id']) form = RecipeForm(obj=r) if form.validate_on_submit(): r.set(title=form.title.data, notes=form.notes.data, preptime=int(form.preptime.data or 0), cooktime=int(form.cooktime.data or 0), source=form.source.data, sourceurl=form.sourceurl.data, serves=int(form.serves.data or 0), ingredients=form.ingredients.data, method=form.method.data, type=form.type.data, vegetarian=int(form.vegetarian.data)) if form.image.data: form.image.data.save(r.image_disk) flash('Saved!') return redirect(url_for('.recipe', recipe_seo=r.seo)) return render_response('edit_recipe.html', dict( id=r.id, form=form, image=r.image, ))
def new(): form = RecipeForm() if form.validate_on_submit(): r = Recipe(title=form.title.data, seo=Recipe.make_seo(form.title.data), notes=form.notes.data, preptime=int(form.preptime.data or 0), cooktime=int(form.cooktime.data or 0), source=form.source.data, sourceurl=form.sourceurl.data, serves=int(form.serves.data or 0), ingredients=form.ingredients.data, method=form.method.data, type=form.type.data, vegetarian=int(form.vegetarian.data)) if form.image.data: form.image.data.save(r.image_disk) flash('Created!') return redirect(url_for('.recipe', recipe_seo=r.seo)) return render_response('edit_recipe.html', dict( id=None, form=form, image=None, ))
def test_renders_strings(app, context): """Strings can be rendered as templates directly""" with app.test_request_context(): rendered = render_response(string="The name is $name", context=context, method="text") assert rendered.data == b"The name is Rudolf"
def index(): # log.debug("Happy TurboGears Controller Responding For Duty") dw_response = homepage_feed( 'https://iguana.dreamwidth.org/data/rss')['data'][:3] pleroma_response = homepage_feed( 'https://social.nevira.net/users/iguana/feed.atom')['data'][:10] return render_response( 'welcome.html', dict(statuses=pleroma_response, entries=dw_response))
def test_renders_js(app, context): """ A js extension results in no doctype and a application/javascript mimetype """ with app.test_request_context(): rendered = render_response("test.js", context) assert rendered.mimetype == "application/javascript" assert rendered.data == b'alert("Rudolf");\n'
def test_renders_xml(app, context): """An xml extension results in no doctype and a application/xml mimetype""" with app.test_request_context(): rendered = render_response("test.xml", context) assert rendered.mimetype == "application/xml" assert rendered.data == b"<name>Rudolf</name>" rendered = render("test.xml", **context) assert rendered.mimetype == "application/xml" assert rendered.data == b"<name>Rudolf</name>"
def test_renders_html(app, context): """A html extension results in an HTML doctype and mimetype""" with app.test_request_context(): rendered = render_response('test.html', context) expected_data = (b'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" ' b'"http://www.w3.org/TR/html4/strict.dtd">\n' b'<body>Hi Rudolf</body>') assert rendered.mimetype == 'text/html' assert rendered.data == expected_data
def login(): error = None if request.method == 'POST': if request.form['username'] != app.config['USERNAME']: error = 'Invalid username' elif request.form['password'] != app.config['PASSWORD']: error = 'Invalid password' else: session['logged_in'] = True flash('You were logged in') return redirect(url_for('show_entries')) return render_response('login.html', dict(error=error))
def test_renders_html(app, context): """A html extension results in an HTML doctype and mimetype""" with app.test_request_context(): rendered = render_response("test.html", context) # Remove leading indentation and encode since `render_response` returns bytes expected_data = cleandoc( """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <body>Hi Rudolf</body> """).encode("UTF-8") assert rendered.mimetype == "text/html" assert rendered.data == expected_data
def error(e): tb = traceback.format_exc() code = 500 title = "Internal server error" if isinstance(e, HTTPException): code = e.code title = e.description if code == 500: log.error(tb) return render_response( 'error.html', dict(title=title, status=code), ), code
def test_renders_svg(app, context): """An svg extension results in an SVG doctype and mimetype""" with app.test_request_context(): rendered = render_response("test.svg", context) # Remove leading indentation and encode since `render_response` returns bytes expected_data = cleandoc(""" <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg viewBox="0 0 1000 300"> <text x="250" y="150" font-size="55">Hi Rudolf</text> </svg> """).encode("UTF-8") assert rendered.mimetype == "image/svg+xml" assert rendered.data == expected_data
def test_renders_svg(app, context): """An svg extension results in an SVG doctype and mimetype""" with app.test_request_context(): rendered = render_response('test.svg', context) expected_data = ( b'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ' b'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n' b'<svg viewBox="0 0 1000 300">\n' b'<text x="250" y="150" font-size="55">Hi Rudolf</text>\n' b'</svg>') assert rendered.mimetype == 'image/svg+xml' assert rendered.data == expected_data
def test_updates_context(app): """Render calls update the template context with context processors""" with app.test_request_context(): @app.context_processor def inject_rudolf(): return dict(rudolf='The red-nosed reindeer') rendered = render_response('context.html') expected_data = (b'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" ' b'"http://www.w3.org/TR/html4/strict.dtd">\n' b'<pre>rudolf = The red-nosed reindeer</pre>') assert rendered.mimetype == 'text/html' assert rendered.data == expected_data
def login(): if current_user.is_authenticated: return redirect(url_for('.index')) form = LoginForm() if form.validate_on_submit(): # Login and validate the user. user = User.by_username(form.username.data) if user is None or not user.check_password(form.password.data): flash('Invalid username or password') else: flash('Logged in successfully.') login_user(user, remember=form.remember_me.data) next_url = request.args.get('next') if not is_safe_url(next_url): abort(400) return redirect(next_url or url_for('.index')) return render_response('login.html', dict(form=form))
def test_updates_context(app): """Render calls update the template context with context processors""" with app.test_request_context(): @app.context_processor def inject_rudolf(): return dict(rudolf="The red-nosed reindeer") rendered = render_response("context.html") # Remove leading indentation and encode since `render_response` returns bytes expected_data = cleandoc( """ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <pre>rudolf = The red-nosed reindeer</pre> """ ).encode("UTF-8") assert rendered.mimetype == "text/html" assert rendered.data == expected_data
def test_fails_without_template_or_string(app, context): """A template or string must be provided to render""" with app.test_request_context(): with pytest.raises(RuntimeError): render_response(context=context, method='text')
def show_entries(): cur = g.db.execute('select title, text from entries order by id desc') entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()] return render_response('show_entries.html', dict(entries=entries))
def about(): age = (datetime.date.today() - datetime.date(1984, 12, 28)).days / 365.24 # (CLOSE ENOUGH SHUT UP) return render_response('about.html', dict(age=int(age)))
def L(): return render_response('L.html', dict())