From f0e3510341a41c90da7d37be1196a1a3575e60a7 Mon Sep 17 00:00:00 2001 From: Francesco Luzzana Date: Sun, 27 Feb 2022 22:50:46 +0100 Subject: [PATCH] init, homepage and puzzle page --- .gitignore | 15 ++++++++ .vscode/settings.json | 3 ++ MANIFEST.in | 4 +++ chaospuzzles/__init__.py | 48 +++++++++++++++++++++++++ chaospuzzles/puzzle.py | 29 +++++++++++++++ chaospuzzles/puzzles/katamari/info.json | 7 ++++ chaospuzzles/templates/home.html | 19 ++++++++++ chaospuzzles/templates/puzzle.html | 13 +++++++ setup.py | 13 +++++++ 9 files changed, 151 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 MANIFEST.in create mode 100644 chaospuzzles/__init__.py create mode 100644 chaospuzzles/puzzle.py create mode 100644 chaospuzzles/puzzles/katamari/info.json create mode 100644 chaospuzzles/templates/home.html create mode 100644 chaospuzzles/templates/puzzle.html create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f971665 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +venv/ + +*.pyc +__pycache__/ +.ipynb_checkpoints + +instance/ + +.pytest_cache/ +.coverage +htmlcov/ + +dist/ +build/ +*.egg-info/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0ceb6db --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.formatting.provider": "black" +} diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..d50bebe --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,4 @@ +include chaospuzzles/schema.sql +graft chaospuzzles/static +graft chaospuzzles/templates +global-exclude *.pyc \ No newline at end of file diff --git a/chaospuzzles/__init__.py b/chaospuzzles/__init__.py new file mode 100644 index 0000000..0636d2e --- /dev/null +++ b/chaospuzzles/__init__.py @@ -0,0 +1,48 @@ +import os +from flask import Flask + + +class PrefixMiddleware(object): + def __init__(self, app, prefix=""): + self.app = app + self.prefix = prefix + + def __call__(self, environ, start_response): + + if environ["PATH_INFO"].startswith(self.prefix): + environ["PATH_INFO"] = environ["PATH_INFO"][len(self.prefix) :] + environ["SCRIPT_NAME"] = self.prefix + return self.app(environ, start_response) + else: + start_response("404", [("Content-Type", "text/plain")]) + return ["This url does not belong to the app.".encode()] + + +def create_app(test_config=None): + # create and configure the app + app = Flask(__name__, instance_relative_config=True) + app.config.from_mapping( + SECRET_KEY="dev", + # DATABASE=os.path.join(app.instance_path, 'puzzles'), + ) + + if test_config is None: + # load the instance config, if it exists, when not testing + app.config.from_pyfile("config.py", silent=True) + else: + # load the test config if passed in + app.config.from_mapping(test_config) + + # ensure the instance folder exists + try: + os.makedirs(app.instance_path) + except OSError: + pass + + from . import puzzle + + app.register_blueprint(puzzle.bp) + + app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix="/soupboat/chaospuzzles") + + return app diff --git a/chaospuzzles/puzzle.py b/chaospuzzles/puzzle.py new file mode 100644 index 0000000..7330f35 --- /dev/null +++ b/chaospuzzles/puzzle.py @@ -0,0 +1,29 @@ +import os +import json +from flask import Blueprint, render_template + +bp = Blueprint("puzzle", __name__, url_prefix="/") + + +def list_folders(path): + """Return all the folders in a folder""" + names = [] + for entry in os.scandir(path): + # add to the list only proper files + if not entry.name.startswith(".") and entry.is_dir(): + # remove the extension from the filename + names.append(entry.name) + return names + + +@bp.route("/") +def home(): + puzzles = list_folders("chaospuzzles/puzzles") + return render_template("home.html", puzzles=puzzles) + + +@bp.route("/") +def puzzle(puzzle=None): + with open(f"chaospuzzles/puzzles/{puzzle}/info.json") as data: + info = json.load(data) + return render_template("puzzle.html", puzzle=puzzle, **info) diff --git a/chaospuzzles/puzzles/katamari/info.json b/chaospuzzles/puzzles/katamari/info.json new file mode 100644 index 0000000..4590507 --- /dev/null +++ b/chaospuzzles/puzzles/katamari/info.json @@ -0,0 +1,7 @@ +{ + "name": "katamari", + "rows": 10, + "columns": 15, + "image": "katamari.jpg", + "clusters": [] +} \ No newline at end of file diff --git a/chaospuzzles/templates/home.html b/chaospuzzles/templates/home.html new file mode 100644 index 0000000..9598c57 --- /dev/null +++ b/chaospuzzles/templates/home.html @@ -0,0 +1,19 @@ + + + + + + + Chaos Puzzles Prototype + + +

Chaos Puzzles

+ + + diff --git a/chaospuzzles/templates/puzzle.html b/chaospuzzles/templates/puzzle.html new file mode 100644 index 0000000..c7c1818 --- /dev/null +++ b/chaospuzzles/templates/puzzle.html @@ -0,0 +1,13 @@ + + + + + + + {{puzzle}} + + +

{{puzzle}}

+ {{rows}}, {{columns}}, {{clusters}} + + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1455ce0 --- /dev/null +++ b/setup.py @@ -0,0 +1,13 @@ +from setuptools import find_packages, setup + +setup( + name='Chaos Puzzles', + version='1.0.0', + packages=find_packages(), + include_package_data=True, + zip_safe=False, + install_requires=[ + 'flask', + 'shortuuid' + ], +)