Skip to content

Commit 65d43ce

Browse files
author
patx
committed
update examples, clean code, add comments
1 parent 2b8caac commit 65d43ce

File tree

4 files changed

+36
-57
lines changed

4 files changed

+36
-57
lines changed

MicroPie.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
class Server:
4646
"""
47-
A lightweight server class providing basic routing, session handling, and
47+
A lightweight class providing basic routing, session handling, and
4848
template rendering using Jinja2. This class uses an internally defined
4949
request handler to manage GET and POST requests.
5050
"""

examples/pastebin-app/app.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,14 @@ def add(self, paste_content):
3535
db.save()
3636
return self.redirect("/paste/{0}".format(pid))
3737

38-
Root().run()
38+
39+
# Create a instance of our MicroPie App
40+
app = Root()
41+
42+
# Run with `gunicorn app-wsgi:wsgi_app`
43+
wsgi_app = app.wsgi_app
44+
45+
# Run with `python3 app.py`
46+
if __name__ == "__main__":
47+
app.run()
48+

examples/pastebin-app/pastebin-wsgi.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

examples/todo-app/app.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
from MicroPie import Server # Import our MicroPie framework
99
from pickledb import PickleDB
1010

11-
# ------------------------------------------------------------------------------
12-
# Database Setup
13-
# ------------------------------------------------------------------------------
11+
12+
# ----------------------------------------------------------------------------
13+
# Database Setup, for production use a better database like sqlite or kenobi
14+
# ----------------------------------------------------------------------------
15+
1416
db = PickleDB("todo.db")
1517

1618
def add_item(content, tags):
@@ -41,9 +43,11 @@ def delete_item(item_id):
4143
db.remove(item_id)
4244
db.save()
4345

44-
# ------------------------------------------------------------------------------
46+
47+
# ----------------------------------------------------------------------------
4548
# ToDoApp Class
46-
# ------------------------------------------------------------------------------
49+
# ----------------------------------------------------------------------------
50+
4751
class ToDoApp(Server):
4852
"""Our ToDo application, based on the MicroPie Server."""
4953

@@ -99,18 +103,20 @@ def add(self):
99103
)
100104
return self.redirect("/")
101105

102-
def delete(self, item_id):
103-
"""
104-
GET /delete/<id> -> Delete an item by id and optionally redirect to a tag.
105-
Requires user to be logged in.
106+
def delete(self, item_id, redirect_tag=None):
107+
"""Delete a document
108+
id --> the id of the document to be deleted
109+
redirect_tag --> if deleted from the tag page redirect back to same
110+
page
106111
"""
107112
if not self.session.get("logged_in"):
108113
return self.redirect("/login")
109114

110-
if item_id:
111-
delete_item(item_id)
112-
113-
return self.redirect("/")
115+
delete_item(item_id)
116+
if redirect_tag:
117+
return self.redirect("/tag/{}".format(redirect_tag))
118+
else:
119+
return self.redirect("/")
114120

115121
def tag(self, tag_value):
116122
"""
@@ -125,12 +131,14 @@ def tag(self, tag_value):
125131
tag_items=matching_tags(tag_value),
126132
)
127133

128-
# ------------------------------------------------------------------------------
134+
135+
# ----------------------------------------------------------------------------
129136
# Main
130-
# ------------------------------------------------------------------------------
137+
# ----------------------------------------------------------------------------
138+
131139
app = ToDoApp()
132140

133-
if __name__ == "__main__"
141+
if __name__ == "__main__":
134142
app.run()
135143
else:
136144
wsgi_app = app.wsgi_app

0 commit comments

Comments
 (0)