migrations, work on import script, new model Segment

This commit is contained in:
Tyrel Souza 2015-08-31 23:56:40 -04:00
parent 6043ee127a
commit aa9c14da6e
7 changed files with 79 additions and 10 deletions

View File

@ -1,3 +1,6 @@
from django.test import TestCase
# Create your tests here.
class HomeTest(TestCase):
def test_home(self):
self.client.get("/")

View File

@ -1,3 +1,4 @@
Django==1.8.4
django-extensions
coverage
coverage
gpxpy

View File

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

View File

@ -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

View File

@ -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'),
),
]

View File

@ -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),
),
]

View File

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