diff --git a/home/tests.py b/home/tests.py index 7ce503c..98e21bc 100644 --- a/home/tests.py +++ b/home/tests.py @@ -1,3 +1,6 @@ from django.test import TestCase # Create your tests here. +class HomeTest(TestCase): + def test_home(self): + self.client.get("/") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 6be7f9e..fce310e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ Django==1.8.4 django-extensions -coverage \ No newline at end of file +coverage +gpxpy \ No newline at end of file diff --git a/tracking/admin.py b/tracking/admin.py index 6b54242..18d5bc7 100644 --- a/tracking/admin.py +++ b/tracking/admin.py @@ -1,13 +1,17 @@ from django.contrib import admin -from tracking.models import Track, Point +from tracking.models import Track, Point, Segment class TrackAdmin(admin.ModelAdmin): pass +class SegmentAdmin(admin.ModelAdmin): + pass + class PointAdmin(admin.ModelAdmin): pass # Register your models here. admin.site.register(Track, TrackAdmin) +admin.site.register(Segment, SegmentAdmin) admin.site.register(Point, PointAdmin) \ No newline at end of file diff --git a/tracking/management/commands/import_gpx.py b/tracking/management/commands/import_gpx.py index 62a81c2..30dd9b5 100644 --- a/tracking/management/commands/import_gpx.py +++ b/tracking/management/commands/import_gpx.py @@ -1,8 +1,8 @@ __author__ = 'tyrel' from django.core.management.base import BaseCommand, CommandError from django.contrib.auth.models import User -from tracking.models import Track, Point -from xml.etree import ElementTree +from tracking.models import Track, Segment, Point +import gpxpy import os @@ -24,9 +24,33 @@ class Command(BaseCommand): self.stdout.write("Importing for user %s" % user.username) if os.path.isdir(filepath): - self.stdout("OK, path") + self.import_directory(user, filepath) elif os.path.isfile(filepath): - self.stdout("OK, file") + self.import_file(user, filepath) + else: raise CommandError("{0} is neither an existing file, nor a path.".format(filepath)) + + def import_file(self, user, filename): + with open(filename, 'r') as f: + gpx = gpxpy.parse(f) + + for g_track in gpx.tracks: + for g_segment in g_track.segments: + for g_point in g_segment.points: + print 'Point at ({0},{1}) -> {2} -> {3}'.format(g_point.latitude, g_point.longitude, g_point.elevation) + + + + + def import_directory(self, user, directory): + pass + + + +def etree_to_dict(t): + d = {t.tag : map(etree_to_dict, t.getchildren())} + d.update(('@' + k, v) for k, v in t.attrib.iteritems()) + d['text'] = t.text + return d \ No newline at end of file diff --git a/tracking/migrations/0001_initial.py b/tracking/migrations/0001_initial.py index d667ea0..3852cf7 100644 --- a/tracking/migrations/0001_initial.py +++ b/tracking/migrations/0001_initial.py @@ -20,6 +20,12 @@ class Migration(migrations.Migration): ('elevation', models.DecimalField(max_digits=20, decimal_places=2)), ], ), + migrations.CreateModel( + name='Segment', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ], + ), migrations.CreateModel( name='Track', fields=[ @@ -30,8 +36,13 @@ class Migration(migrations.Migration): ], ), migrations.AddField( - model_name='point', + model_name='segment', name='track', field=models.ForeignKey(to='tracking.Track'), ), + migrations.AddField( + model_name='point', + name='segment', + field=models.ForeignKey(to='tracking.Segment'), + ), ] diff --git a/tracking/migrations/0002_auto_20150901_0355.py b/tracking/migrations/0002_auto_20150901_0355.py new file mode 100644 index 0000000..eacda5b --- /dev/null +++ b/tracking/migrations/0002_auto_20150901_0355.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('tracking', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='point', + name='latitude', + field=models.DecimalField(max_digits=13, decimal_places=10), + ), + migrations.AlterField( + model_name='point', + name='longitude', + field=models.DecimalField(max_digits=12, decimal_places=10), + ), + ] diff --git a/tracking/models.py b/tracking/models.py index e3b89ec..c155196 100644 --- a/tracking/models.py +++ b/tracking/models.py @@ -12,12 +12,14 @@ class Track(models.Model): def finish(self): return self.point_set.order_by("-time")[0].time +class Segment(models.Model): + track = models.ForeignKey(Track) class Point(models.Model): time = models.DateTimeField() - latitude = models.FloatField() - longitude = models.FloatField() + latitude = models.DecimalField(max_digits=13, decimal_places=10) + longitude = models.DecimalField(max_digits=12, decimal_places=10) elevation = models.DecimalField(max_digits=20, decimal_places=2) - track = models.ForeignKey(Track) + segment = models.ForeignKey(Segment)