Skip to content

Instantly share code, notes, and snippets.

@abhisuri97
Created November 27, 2017 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhisuri97/7b2bbe96e8e677b07f67ae1cbc2e96bf to your computer and use it in GitHub Desktop.
Save abhisuri97/7b2bbe96e8e677b07f67ae1cbc2e96bf to your computer and use it in GitHub Desktop.
<tbody>
{% for a in club.answers | sort(attribute='id') %}
<td>{{ a.answer }}</td>
<td>{{ a.rating }}</td>
<td>{{ a.question.content }}</td>
{% if current_user.is_admin() %}
<td onclick="window.location.href = '{{ url_for('question.delete_answer', answer_id=a.id) }}';">
<a>Delete Answer</a></td> {% endif %}
</tr>
{% endfor %}
</tbody>
```
Lastly, we actually write the logic for rendering the page within the `{% block content %} ... {% endblock %}` tags. We switch between the subpages to render by checking the `request.endpoint` to see if it is the deletion endpoint or if there is a form (in which case render the form). Otherwise, we just call the `club_info` macro.
And we're done with the club routes and views. Most of the other routes for category and questions follow a similar type of logic.
### Main Views and Forms
The main route is the public facing part of the application. The route behaviors are as follows
- `/` (GET): display all clubs, associated questions, and average ratings for each club per question in a table.
- `/submit-review/<int:club_id>` (GET, POST): Dynamically generate the form to submit a club review based on the questions in the `Question` db. Also accept data for the form and save as answers fo the club matching club_id.
The first route is very straightforward and matches the `/clubs` route we implemented earlier. The only difference is that the `questions` must also be passed in.
```py
@main.route('/')
def index():
clubs = Club.query.filter_by(is_confirmed=True).all()
questions = Question.query.all()
categories = ClubCategory.query.all()
all_c = []
for c in clubs:
club_obj = {
'id': c.id,
'description': c.description,
'name': c.name,
'categories': c.categories
}
for a in c.answers:
if a.question is not None:
if a.question.content not in club_obj:
club_obj[a.question.content] = []
club_obj[a.question.content].append(a.rating)
for q in club_obj:
if type(club_obj[q]) is list:
club_obj[q] = sum(club_obj[q]) / len(club_obj[q])
all_c.append(club_obj)
return render_template(
'main/index.html',
all_c=all_c,
questions=questions,
categories=categories)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment