diff --git a/.gitignore b/.gitignore index f62f6fb..9c7f374 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,5 @@ __pycache__ # Django secret.txt +# Autocron own SQLite database file: +autocron.db diff --git a/for_runners/apps.py b/for_runners/apps.py index 9589e01..b6efd75 100644 --- a/for_runners/apps.py +++ b/for_runners/apps.py @@ -1,9 +1,17 @@ +from pathlib import Path + +import autocron from django.apps import AppConfig as BaseAppConfig class AppConfig(BaseAppConfig): name = 'for_runners' - verbose_name = "ForRunners" + verbose_name = 'ForRunners' def ready(self): import for_runners.checks # noqa + + # TODO: Remove after: https://github.com/kbr/autocron/issues/3 + Path('~/.autocron').expanduser().mkdir(parents=True, exist_ok=True) + + autocron.start('for_runner_autocron.sqlite') # activate autocron with his SQLite database diff --git a/for_runners/tasks.py b/for_runners/tasks.py index 80e0845..3a3e201 100644 --- a/for_runners/tasks.py +++ b/for_runners/tasks.py @@ -4,38 +4,40 @@ :license: GNU GPL v3 or above, see LICENSE for more details. """ -# import io -# import logging -# -# from autotask.tasks import delayed_task -# from for_runners.gpx_tools.garmin2gpxpy import garmin2gpxpy -# -# -# log = logging.getLogger(__name__) -# -# -# @delayed_task() -# def generate_gpx_map_task(object_id): -# """ -# Delayed task to generate the map from GPX track -# """ -# log.debug("Generate GPX Map for ID: %r", object_id) -# -# from for_runners.models import GpxModel # import here, because of import-loop -# gpx_instance = GpxModel.objects.get(pk=object_id) -# log.info("Generate GPX Map for: %s" % gpx_instance) -# -# content = gpx_instance.gpx -# gpxpy_instance = garmin2gpxpy(content) -# -# image, plt = generate_map(gpxpy_instance) -# -# temp = io.BytesIO() -# plt.savefig(temp, bbox_inches="tight") -# -# filename = "%s.png" % gpx_instance.get_short_slug() -# -# # Save gpx map file to model instance: -# gpx_instance.map_image.save(filename, temp) -# -# log.info("GPX data saved to %s, ok." % gpx_instance) +import io +import logging + +import autocron + +from for_runners.gpx_tools.garmin2gpxpy import garmin2gpxpy + + +log = logging.getLogger(__name__) + + +@autocron.delay +def generate_gpx_map_task(object_id): + """ + Delayed task to generate the map from GPX track + """ + log.debug("Generate GPX Map for ID: %r", object_id) + + from for_runners.models import GpxModel # import here, because of import-loop + + gpx_instance = GpxModel.objects.get(pk=object_id) + log.info(f"Generate GPX Map for: {gpx_instance}") + + content = gpx_instance.gpx + gpxpy_instance = garmin2gpxpy(content) + + image, plt = generate_map(gpxpy_instance) + + temp = io.BytesIO() + plt.savefig(temp, bbox_inches="tight") + + filename = f"{gpx_instance.get_short_slug()}.png" + + # Save gpx map file to model instance: + gpx_instance.map_image.save(filename, temp) + + log.info(f"GPX data saved to {gpx_instance}, ok.") diff --git a/pyproject.toml b/pyproject.toml index 3998f30..8ab2750 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,7 @@ dependencies = [ "django-tools", # https://github.com/jedie/django-tools/ "django-import-export", # https://github.com/django-import-export/django-import-export "django-admin-sortable2", # https://github.com/jrief/django-admin-sortable2 - "autotask", # https://github.com/kbr/autotask + "autocron", # https://github.com/kbr/autocron "gpxpy", # https://github.com/tkrajina/gpxpy "matplotlib", # http://matplotlib.org/ "svgwrite", # http://github.com/mozman/svgwrite.git diff --git a/requirements.dev.txt b/requirements.dev.txt index 2c8511d..99ce069 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -38,6 +38,10 @@ authlib==1.3.1 \ --hash=sha256:7ae843f03c06c5c0debd63c9db91f9fda64fa62a42a77419fa15fbb7e7a58917 \ --hash=sha256:d35800b973099bbadc49b42b256ecb80041ad56b7fe1216a362c7943c088f377 # via safety +autocron==1.0 \ + --hash=sha256:4c3624308ccfd8f05c3ff4f09f4b87c3c5edffbcc1975ee839945b17ea69a65b \ + --hash=sha256:db73b7b5a10359d88cc2e68c8709ba0abacad36fd3846b579899a423f1b1e66a + # via django-for-runners (pyproject.toml) autoflake==2.3.1 \ --hash=sha256:3ae7495db9084b7b32818b4140e6dc4fc280b712fb414f5b8fe57b0a8e85a840 \ --hash=sha256:c98b75dc5b0a86459c4f01a1d32ac7eb4338ec4317a4469515ff1e687ecd909e @@ -48,9 +52,6 @@ autopep8==2.3.1 \ # via # django-for-runners (pyproject.toml) # manageprojects -autotask==0.5.4 \ - --hash=sha256:d21578ab14adafb0d9be3490a444d261fd2bcdcbb1ee941b28d4b5e5d1a386ad - # via django-for-runners (pyproject.toml) backports-tarfile==1.2.0 \ --hash=sha256:77e284d754527b01fb1e6fa8a1afe577858ebe4e9dad8919e34c862cb399bc34 \ --hash=sha256:d75e02c268746e1b8144c278978b6e98e85de6ad16f8e4b0844a154557eca991 diff --git a/requirements.txt b/requirements.txt index 700eb46..d478d3a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,8 +16,9 @@ attrs==23.2.0 \ # via # cattrs # requests-cache -autotask==0.5.4 \ - --hash=sha256:d21578ab14adafb0d9be3490a444d261fd2bcdcbb1ee941b28d4b5e5d1a386ad +autocron==1.0 \ + --hash=sha256:4c3624308ccfd8f05c3ff4f09f4b87c3c5edffbcc1975ee839945b17ea69a65b \ + --hash=sha256:db73b7b5a10359d88cc2e68c8709ba0abacad36fd3846b579899a423f1b1e66a # via django-for-runners (pyproject.toml) bleach==6.1.0 \ --hash=sha256:0a31f1837963c41d46bbf1331b8778e1308ea0791db03cc4e7357b97cf42a8fe \