Skip to content

Commit 0998549

Browse files
committed
Update default example app views and look
1 parent 610ca0a commit 0998549

File tree

5 files changed

+127
-67
lines changed

5 files changed

+127
-67
lines changed

example_app/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
db.init_app(app)
3939

4040

41-
app.add_url_rule("/", view_func=views.index)
41+
app.add_url_rule("/", view_func=views.index, methods=["GET", "POST"])
4242
app.add_url_rule("/pagination", view_func=views.pagination, methods=["GET", "POST"])
4343

4444
if __name__ == "__main__":

example_app/templates/index.html

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
{% extends "layout.html" %}
22
{% block body %}
3-
{% for todo in todos %}
4-
<div>
5-
<h2>{{ todo.title }}</h2>
6-
{{ todo.text|safe }}
7-
</div>
8-
{% else %}
9-
<em>Unbelievable. No todos here so far <a href="/add">Add one</a></em>
10-
{% endfor %}
11-
<br/>
12-
<a href="{{ url_for('pagination') }}">See pagination</a>
3+
<p>This is demo application that shows Flask-Mongoengine features and integrations.</p>
4+
<p>Feel free to open Flask debug toolbar after any action to check executed database requests.</p>
5+
<p>If you use this application for first time, it is good idea to generate some fake data.</p>
6+
<p>
7+
Data will be generated inside docker container database.
8+
Two databases will be used to show Flask Debug Panel integration capabilities.
9+
</p>
10+
<p>You can also delete all data, to test forms only.</p>
11+
{% if message %}
12+
<p><mark>{{ message }}</mark></p>
13+
{% endif %}
14+
<form action="{{ url_for("index") }}" method="post">
15+
<input type="submit" name="button" value="Generate data"/>
16+
<input type="submit" name="button" value="Delete data"/>
17+
</form>
1318
{% endblock %}

example_app/templates/layout.html

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
<!doctype html>
2-
<html>
2+
<html lang="en">
33
<head>
4-
<title>Flask MongoEngine</title>
5-
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
4+
<title>Flask MongoEngine</title>
5+
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
66
</head>
77
<body>
8-
<div class=page>
9-
<h1>Toolbar example</h1>
8+
<nav>
9+
<ul>
10+
<li><a href="{{ url_for("index") }}">Home</a></li>
11+
<li><a href="{{ url_for("pagination") }}">Pagination</a></li>
12+
</ul>
13+
</nav>
14+
<div>
1015
<div>
11-
{% block body %}{% endblock %}
16+
{% block body %}{% endblock %}
1217
</div>
1318
</div>
1419
</body>

example_app/templates/pagination.html

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,48 @@
22

33
{# Macro for creating navigation links #}
44
{% macro render_navigation(pagination, endpoint) %}
5-
<div class=pagination>
6-
{% for page in pagination.iter_pages() %}
7-
{% if page %}
8-
{% if page != pagination.page %}
9-
<a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a>
10-
{% else %}
11-
<strong>{{ page }}</strong>
12-
{% endif %}
13-
{% else %}
14-
<span class=ellipsis></span>
15-
{% endif %}
16-
{% endfor %}
17-
</div>
5+
<div>
6+
{% for page in pagination.iter_pages() %}
7+
{% if page %}
8+
{% if page != pagination.page %}
9+
<a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a>
10+
{% else %}
11+
<strong>{{ page }}</strong>
12+
{% endif %}
13+
{% else %}
14+
<span>nothing to show</span>
15+
{% endif %}
16+
{% endfor %}
17+
</div>
1818
{% endmacro %}
1919

2020
{% block body %}
2121

22-
<div class="todos">
23-
<ul>
24-
{% for todo in todos_page.items %}
25-
<li>{{ todo.title }}</li>
26-
{% endfor %}
27-
</ul>
28-
</div>
29-
30-
<div class="navigation">
31-
{{ render_navigation(todos_page, 'pagination') }}
32-
</div>
22+
<div>
23+
<table>
24+
<thead>
25+
<tr>
26+
<th>Title</th>
27+
<th>Text</th>
28+
<th>Done</th>
29+
<th>Publication date</th>
30+
</tr>
31+
</thead>
32+
<tbody>
33+
{% for todo in todos_page.items %}
34+
<tr>
35+
<td>{{ todo.title }}</td>
36+
<td>{{ todo.text }}</td>
37+
<td>{{ todo.done }}</td>
38+
<td>{{ todo.pub_date }}</td>
39+
</tr>
40+
{% endfor %}
41+
</tbody>
42+
</table>
43+
</div>
44+
<div>
45+
{{ render_navigation(todos_page, "pagination") }}
46+
</div>
3347

3448

3549
{% endblock %}

example_app/views.py

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,73 @@
1-
import flask
1+
"""Demo views for example application."""
2+
from faker import Faker
3+
from flask import render_template, request
24
from mongoengine.context_managers import switch_db
35

6+
from example_app import models
47

5-
def index():
6-
from models import Todo
7-
8-
with switch_db(Todo, "default") as Todo:
9-
# As a list to test debug toolbar
10-
Todo.objects().delete() # Removes
11-
Todo(title="Simple todo A", text="12345678910").save() # Insert
12-
Todo(title="Simple todo B", text="12345678910").save() # Insert
13-
Todo(title="Simple todo C", text="12345678910").save() # Insert
8+
9+
def generate_data():
10+
"""Generates fake data for all supported models and views."""
11+
fake = Faker(locale=["en_US"])
12+
13+
with switch_db(models.Todo, "default"):
14+
models.Todo.objects().delete() # Removes
15+
models.Todo(
16+
title="Simple todo A", text=fake.sentence(), done=False
17+
).save() # Insert
18+
models.Todo(
19+
title="Simple todo B", text=fake.sentence(), done=True
20+
).save() # Insert
21+
models.Todo(
22+
title="Simple todo C", text=fake.sentence(), done=True
23+
).save() # Insert
1424
# Bulk insert
15-
bulk = (Todo(title="Bulk 1"), Todo(title="Bulk 2"))
16-
Todo.objects().insert(bulk)
17-
Todo.objects(title__contains="B").order_by("-title").update(
25+
bulk = (
26+
models.Todo(title="Bulk 1", text=fake.sentence(), done=False),
27+
models.Todo(title="Bulk 2", text=fake.sentence(), done=True),
28+
)
29+
models.Todo.objects().insert(bulk)
30+
models.Todo.objects(title__contains="B").order_by("-title").update(
1831
set__text="Hello world"
1932
) # Update
20-
Todo.objects(title__contains="C").order_by("-title").delete() # Removes
21-
todos = list(Todo.objects.order_by("-title", "done")[2:10])
22-
todos = Todo.objects.all().order_by("-title")
23-
return flask.render_template("index.html", todos=todos)
33+
models.Todo.objects(title__contains="C").order_by("-title").delete() # Removes
2434

35+
with switch_db(models.Todo, "secondary"):
36+
models.Todo.objects().delete()
37+
for _ in range(10):
38+
models.Todo(
39+
title=fake.text(max_nb_chars=59),
40+
text=fake.sentence(),
41+
done=fake.pybool(),
42+
pub_date=fake.date(),
43+
).save()
2544

26-
def pagination():
27-
from models import Todo
2845

29-
with switch_db(Todo, "secondary") as Todo:
30-
Todo.objects().delete()
31-
for i in range(10):
32-
Todo(title=f"Simple todo {i}", text="12345678910").save()
46+
def delete_data():
47+
"""Clear database."""
48+
with switch_db(models.Todo, "default"):
49+
models.Todo.objects().delete()
50+
with switch_db(models.Todo, "secondary"):
51+
models.Todo.objects().delete()
52+
3353

34-
page_num = int(flask.request.args.get("page") or 1)
35-
todos_page = Todo.objects.paginate(page=page_num, per_page=3)
54+
def index():
55+
"""Return page with welcome words and instructions."""
56+
message = None
57+
if request.method == "POST":
58+
if request.form["button"] == "Generate data":
59+
generate_data()
60+
message = "Fake data generated"
61+
if request.form["button"] == "Delete data":
62+
delete_data()
63+
message = "All data deleted"
64+
return render_template("index.html", message=message)
65+
66+
67+
def pagination():
68+
"""Return pagination demonstration page and data generation entrypoint."""
69+
with switch_db(models.Todo, "secondary"):
70+
page_num = int(request.args.get("page") or 1)
71+
todos_page = models.Todo.objects.paginate(page=page_num, per_page=3)
3672

37-
return flask.render_template("pagination.html", todos_page=todos_page)
73+
return render_template("pagination.html", todos_page=todos_page)

0 commit comments

Comments
 (0)