Skip to content

Commit

Permalink
adds a preliminary tag-feature to recipe searches
Browse files Browse the repository at this point in the history
  • Loading branch information
trp07 committed Mar 5, 2019
1 parent f9399f5 commit 7f67161
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
26 changes: 23 additions & 3 deletions pyrecipe/app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
* register -- user registration
* user -- user profile page
* recipe -- view a specific recipe
* tag -- view recipes with selected tag(s)
"""

from collections.abc import MutableSequence
from typing import List

import flask
from flask import render_template, flash, redirect, url_for, request
from flask_login import current_user, login_user, logout_user, login_required
Expand All @@ -28,7 +32,8 @@ def index(method=["GET"]):
Login is required.
"""
recipes = list(Recipe.objects().filter())
return render_template("index.html", title="Home Page", recipes=recipes)
tags = Recipe.get_tags()
return render_template("index.html", title="Home Page", recipes=recipes, tags=tags)


@app.route("/login", methods=["GET", "POST"])
Expand Down Expand Up @@ -101,7 +106,7 @@ def user(username:str):
return render_template('user.html', user=user, recipes=recipes)


@app.route("/user/recipe/<recipe_id>")
@app.route("/recipe/<recipe_id>")
@login_required
def recipe(recipe_id:str):
"""
Expand All @@ -114,4 +119,19 @@ def recipe(recipe_id:str):
recipe = Recipe.objects().filter(id=recipe_id).first()
if not recipe:
flask.abort(404)
return render_template('recipe.html', recipe=recipe)
return render_template("recipe.html", recipe=recipe)

@app.route("/tag/<tags>")
@login_required
def tag(tags: List[str]):
"""
Routing required to view recipes with a given tag.
:param recipe_id: (str) the DB id of the recipe.
:returns: recipe or 404 if recipe is not found.
"""
if not isinstance(tags, MutableSequence):
tags = [tags]
recipes = Recipe.find_recipes_by_tag(tags)
return render_template("tag.html", recipes=recipes)
1 change: 1 addition & 0 deletions pyrecipe/app/templates/_list_tags.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="{{ url_for('tag', tags=tag) }}">{{ tag }}</a>
9 changes: 8 additions & 1 deletion pyrecipe/app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@

<h1>Hello, {{ current_user.username }}!</h1>

<hr>
{% for tag in tags %}
{% include "_list_tags.html" %}
{% endfor %}


<hr>
{% for recipe in recipes %}
{% include "_list_recipes.html" %}
{% include "_list_recipes.html" %}
{% endfor %}

{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion pyrecipe/app/templates/recipe.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% block content %}

<h1>{{ recipe.name }}</h1>
<h1>{{ recipe.name.title() }}</h1>
<h2>Prep Time: {{ recipe.prep_time }} | Cook Time: {{ recipe.cook_time }} </h2>
<h2>Servings: {{ recipe.servings }}</h2>

Expand Down
9 changes: 9 additions & 0 deletions pyrecipe/app/templates/tag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends "base.html" %}

{% block content %}

{% for recipe in recipes %}
{% include "_list_recipes.html" %}
{% endfor %}

{% endblock %}

0 comments on commit 7f67161

Please sign in to comment.