Skip to content

Commit

Permalink
Merge pull request #465 from MongoEngine/example-app-new
Browse files Browse the repository at this point in the history
Duplicate example application removed + Allowed to run with docker
  • Loading branch information
insspb committed Jul 1, 2022
2 parents 3492665 + b8bc0d3 commit f274088
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 142 deletions.
25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3.3"
volumes:
db_data: {}

services:
mongo:
image: mongo:4.4
volumes:
- db_data:/data/db/
ports:
- 27017:27017
restart: always

flask:
build:
context: .
dockerfile: ./example_app/compose/flask/Dockerfile
depends_on:
- mongo
command: python ./example_app/app.py
ports:
- 8000:8000
volumes:
- ./:/flask_mongoengine
restart: always
18 changes: 18 additions & 0 deletions docs/example_app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Example app

A simple multi file app - to help get you started.

Completely located in docker container. Require ports 8000, 27015 on host
system to be opened (for development needs).

Usage:

1. From flask-mongoengine root folder just run `docker-compose up`.
2. Wait until dependencies downloaded and containers up and running.
3. Open `http://localhost:8000/` to check the app.
4. Hit `ctrl+c` in terminal window to stop docker-compose.
5. You can use this app for live flask-mongoengine development and testing.

```{note}
Example app files located in ``example_app`` directory.
```
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ You can also use `WTForms <http://wtforms.simplecodes.com/>`_ as model forms for
wtf_forms
session_interface
debug_toolbar
example_app
api/index
AUTHORS
changelog
Expand Down
File renamed without changes.
15 changes: 4 additions & 11 deletions examples/biggerapp/app.py → example_app/app.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import os
import sys

import flask

sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), "../../")))

from flask_debugtoolbar import DebugToolbarExtension
from models import db
from views import index, pagination

app = flask.Flask(__name__)
app.config.from_object(__name__)
app.config["MONGODB_SETTINGS"] = {"DB": "testing"}
app.config["MONGODB_SETTINGS"] = {"DB": "testing", "host": "mongo"}
app.config["TESTING"] = True
app.config["SECRET_KEY"] = "flask+mongoengine=<3"
app.debug = True
Expand All @@ -24,16 +20,13 @@
)
app.config["DEBUG_TB_INTERCEPT_REDIRECTS"] = False

from models import db

db.init_app(app)

DebugToolbarExtension(app)

from views import index, pagination

app.add_url_rule("/", view_func=index)
app.add_url_rule("/pagination", view_func=pagination)

if __name__ == "__main__":
app.run(host="0.0.0.0", port=4000)
app.run(host="0.0.0.0", port=8000)
9 changes: 9 additions & 0 deletions example_app/compose/flask/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python:3.7-slim as dev

ENV PYTHONUNBUFFERED 1
RUN groupadd -r flask && useradd -r -g flask flask
COPY --chown=flask . /flask_mongoengine
RUN pip install --upgrade pip \
&& pip install -r /flask_mongoengine/requirements.txt \
&& pip install -e /flask_mongoengine
WORKDIR /flask_mongoengine
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 5 additions & 3 deletions examples/biggerapp/views.py → example_app/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import flask

from .models import Todo
from models import Todo


def index():
# As a list to test debug toolbar
Todo.objects().delete() # Removes
Todo(title="Simple todo A", text="12345678910").save() # Insert
Todo(title="Simple todo B", text="12345678910").save() # Insert
# Bulk insert
bulk = (Todo(title="Bulk 1"), Todo(title="Bulk 2"))
Todo.objects().insert(bulk)
Todo.objects(title__contains="B").update(set__text="Hello world") # Update
todos = list(Todo.objects[:10])
todos = Todo.objects.all()
Expand All @@ -17,7 +19,7 @@ def index():
def pagination():
Todo.objects().delete()
for i in range(10):
Todo(title="Simple todo {}".format(i), text="12345678910").save() # Insert
Todo(title=f"Simple todo {i}", text="12345678910").save()

page_num = int(flask.request.args.get("page") or 1)
todos_page = Todo.objects.paginate(page=page_num, per_page=3)
Expand Down
14 changes: 0 additions & 14 deletions examples/biggerapp/README.rst

This file was deleted.

14 changes: 0 additions & 14 deletions examples/simpleapp/README.rst

This file was deleted.

56 changes: 0 additions & 56 deletions examples/simpleapp/app.py

This file was deleted.

18 changes: 0 additions & 18 deletions examples/simpleapp/static/style.css

This file was deleted.

11 changes: 0 additions & 11 deletions examples/simpleapp/templates/index.html

This file was deleted.

15 changes: 0 additions & 15 deletions examples/simpleapp/templates/layout.html

This file was deleted.

0 comments on commit f274088

Please sign in to comment.