Skip to content

Commit

Permalink
Use autotask to generate map in background
Browse files Browse the repository at this point in the history
  • Loading branch information
jedie committed Jun 12, 2018
1 parent a2f50fa commit c7fc7b2
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 23 deletions.
21 changes: 7 additions & 14 deletions for_runners/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import io
import logging

from autotask.tasks import delayed_task
from django import forms
from django.conf.urls import url
from django.contrib import admin, messages
from django.db import models
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.views import generic

# https://github.com/jedie/django-for-runners
from for_runners.forms import UploadGpxFileForm
from for_runners.gpx_tools.garmin2gpxpy import garmin2gpxpy
Expand Down Expand Up @@ -67,21 +67,14 @@ def post(self, request, *args, **kwargs):
class ProcessGpxDataView(generic.View):

def get(self, request, object_id):
instance = GpxModel.objects.get(pk=object_id)
log.debug("Process GPX %s" % instance)

content = instance.gpx
gpxpy_instance = garmin2gpxpy(content)

image, plt = generate_map(gpxpy_instance)

temp = io.BytesIO()
plt.savefig(temp, bbox_inches="tight")
"""
Create delayed task to generate the map of the GPX Track
"""
messages.info(request, "GPX Map will be generated in background")

# Save gpx map file to model instance:
instance.map_image.save("gpx", temp)
gpx_instance = GpxModel.objects.get(pk=object_id)
gpx_instance.schedule_generate_map()

messages.success(request, "GPX data saved to %s, ok." % instance)
return HttpResponseRedirect("../")


Expand Down
19 changes: 11 additions & 8 deletions for_runners/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@
from django.db import models
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from filer.fields.file import FilerFileField
from filer.utils.loader import load_model

# https://github.com/jedie/django-tools
from django_tools.models import UpdateInfoBaseModel, UpdateTimeBaseModel

from filer.fields.file import FilerFileField
from filer.utils.loader import load_model
# https://github.com/jedie/django-for-runners
from for_runners.geo import reverse_geo
from for_runners.gpx import get_identifier, parse_gpx
from for_runners.gpx_tools.humanize import human_seconds
from for_runners.managers import GpxModelManager
from for_runners.svg import gpx2svg_string
from for_runners.tasks import generate_gpx_map_task

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -139,8 +137,6 @@ class GpxModel(UpdateTimeBaseModel):
on_delete=models.SET_NULL
)



points_no = models.PositiveIntegerField(
help_text=_("Number of points in GPX"),
null=True, blank=True,
Expand Down Expand Up @@ -201,6 +197,8 @@ class GpxModel(UpdateTimeBaseModel):
def save(self, *args, **kwargs):
if self.gpx:
self.calculate_values()
if not self.map_image:
self.schedule_generate_map()

super().save(*args, **kwargs)

Expand Down Expand Up @@ -272,6 +270,12 @@ def get_gpxpy_instance(self):
gpxpy_instance = parse_gpx(content=self.gpx)
return gpxpy_instance

def schedule_generate_map(self):
"""
Create delayed task to generate the map of the GPX Track
"""
generate_gpx_map_task(object_id=self.pk)

def calculate_values(self):
gpxpy_instance = self.get_gpxpy_instance()
self.points_no = gpxpy_instance.get_points_no()
Expand Down Expand Up @@ -343,7 +347,6 @@ def calculate_values(self):
self.heart_rate_avg = statistics.median(heart_rates)
self.heart_rate_max = max(heart_rates)


def __str__(self):
parts = [self.start_time, self.event, self.short_start_address]
result = " ".join([str(part) for part in parts if part])
Expand Down
39 changes: 39 additions & 0 deletions for_runners/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
created 12.06.2018 by Jens Diemer <opensource@jensdiemer.de>
:copyleft: 2018 by the django-for-runners team, see AUTHORS for more details.
: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
from for_runners.gpx_tools.gpxpy2map import generate_map


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")

# Save gpx map file to model instance:
gpx_instance.map_image.save("gpx", temp)

log.info("GPX data saved to %s, ok." % gpx_instance)
2 changes: 1 addition & 1 deletion for_runners/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
:license: GNU GPL v3 or above, see LICENSE for more details.
"""

__version__ = '0.0.2'
__version__ = '0.0.3'
7 changes: 7 additions & 0 deletions for_runners_test_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@

'debug_toolbar', # https://github.com/jazzband/django-debug-toolbar/

'autotask', # https://bitbucket.org/kbr/autotask

'cms', # https://github.com/divio/django-cms
'menus', # Part of Django-CMS

Expand All @@ -76,6 +78,9 @@
"for_runners_test_project.test_app",
)

# https://bitbucket.org/kbr/autotask
AUTOTASK_IS_ACTIVE = True

ROOT_URLCONF = 'for_runners_test_project.urls'
WSGI_APPLICATION = 'for_runners_test_project.wsgi.application'

Expand Down Expand Up @@ -140,6 +145,8 @@
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, "..", "test_project_db.sqlite3"),
# 'NAME': ":memory:"
# https://docs.djangoproject.com/en/dev/ref/databases/#database-is-locked-errors
'timeout': 30,
}
}

Expand Down
5 changes: 5 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ django-tools
# https://github.com/jedie/django-cms-tools
django-cms-tools

# https://pypi.org/project/autotask/
# https://bitbucket.org/kbr/autotask
autotask

# https://github.com/tkrajina/gpxpy
#gpxpy
# use branch for: https://github.com/tkrajina/gpxpy/issues/117
Expand All @@ -35,3 +39,4 @@ svgwrite
# https://pypi.org/project/geopy/
# https://github.com/geopy/geopy
geopy

0 comments on commit c7fc7b2

Please sign in to comment.