diff --git a/Dockerfile b/Dockerfile index 37370f1..e7d2754 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,6 @@ EXPOSE 5000 ENV PYTHONUNBUFFERED=1 # Environment variables -ENV CACHE_PATH="" ENV DATABASE_URL="" ENV GOOGLE_MAPS_API_KEY="" ENV MAIL_DEFAULT_SENDER="" @@ -18,7 +17,6 @@ ENV MAIL_PASSWORD="" ENV MAIL_PORT="" ENV MAIL_SERVER="" ENV MAIL_USERNAME="" -ENV PROJECT_ENABLE_CRAWLING="" ENV SECRET_KEY="" ENV SECURITY_PASSWORD_HASH="" ENV SERVER_NAME="" diff --git a/project/__init__.py b/project/__init__.py index 74d7a37..d7ddb54 100644 --- a/project/__init__.py +++ b/project/__init__.py @@ -140,6 +140,7 @@ config_oauth(app) import project.api # Command line +import project.cli.cache import project.cli.dump import project.cli.event import project.cli.seo diff --git a/project/cli/cache.py b/project/cli/cache.py new file mode 100644 index 0000000..f29781b --- /dev/null +++ b/project/cli/cache.py @@ -0,0 +1,17 @@ +import click +from flask.cli import AppGroup + +from project import app, img_path +from project.utils import clear_files_in_dir + +cache_cli = AppGroup("cache") + + +@cache_cli.command("clear-images") +def clear_images(): + click.echo("Clearing images..") + clear_files_in_dir(img_path) + click.echo("Done.") + + +app.cli.add_command(cache_cli) diff --git a/project/utils.py b/project/utils.py index a3bb965..bf3e7fa 100644 --- a/project/utils.py +++ b/project/utils.py @@ -29,6 +29,13 @@ def make_dir(path): os.umask(original_umask) +def clear_files_in_dir(path): + with os.scandir(path) as entries: + for entry in entries: + if entry.is_file() or entry.is_symlink(): + os.remove(entry.path) + + def split_by_crlf(s): return [v for v in s.splitlines() if v] diff --git a/project/views/image.py b/project/views/image.py index 57a30b9..30a6814 100644 --- a/project/views/image.py +++ b/project/views/image.py @@ -23,10 +23,6 @@ def image(id): if "s" in request.args: width = int(request.args["s"]) height = width - elif "w" in request.args: - width = int(request.args["w"]) - elif "h" in request.args: - height = int(request.args["h"]) # Generate file name extension = image.encoding_format.split("/")[-1] if image.encoding_format else "png" diff --git a/tests/cli/test_cache.py b/tests/cli/test_cache.py new file mode 100644 index 0000000..a88a7c0 --- /dev/null +++ b/tests/cli/test_cache.py @@ -0,0 +1,10 @@ +def test_clear_images(client, seeder, app, utils): + user_id, admin_unit_id = seeder.setup_base() + image_id = seeder.upsert_default_image() + + url = utils.get_url("image", id=image_id) + utils.get_ok(url) + + runner = app.test_cli_runner() + result = runner.invoke(args=["cache", "clear-images"]) + assert "Done." in result.output diff --git a/tests/views/test_image.py b/tests/views/test_image.py index 6189742..615675a 100644 --- a/tests/views/test_image.py +++ b/tests/views/test_image.py @@ -6,14 +6,12 @@ from project import img_path @pytest.mark.parametrize("size", [None, 100]) -@pytest.mark.parametrize("width", [None, 100]) -@pytest.mark.parametrize("height", [None, 100]) -def test_read(app, seeder, utils, size, width, height): +def test_read(app, seeder, utils, size): user_id, admin_unit_id = seeder.setup_base() image_id = seeder.upsert_default_image() shutil.rmtree(img_path, ignore_errors=True) - url = utils.get_url("image", id=image_id, s=size, w=width, h=height) + url = utils.get_url("image", id=image_id, s=size) utils.get_ok(url) utils.get_ok(url) # cache