Skip to content

Commit

Permalink
robustified code to work when not called from the web application
Browse files Browse the repository at this point in the history
  • Loading branch information
tamarmot committed Jun 23, 2014
1 parent 11be427 commit cb0a80f
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions geocamTrack/views.py
Expand Up @@ -16,7 +16,7 @@
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django.core.urlresolvers import reverse
import pytz
import iso8601
Expand All @@ -32,7 +32,11 @@


def getModel(key):
found = getattr(geocamTrack, key)
try:
found = getattr(geocamTrack, key)
except AttributeError:
found = None

if not found:
value = model_dict[key]
model = getModelByName(value)
Expand Down Expand Up @@ -672,9 +676,17 @@ def getClosestPosition(track, timestamp, max_time_difference_seconds=60):
"""
Look up the closest location, with a 1 minute default maximum difference.
"""
foundPosition = None

try:
foundPosition = getModel('PAST_POSITION_MODEL').objects.get(track=track, timestamp=timestamp)
foundPositions = getModel('PAST_POSITION_MODEL').objects.filter(track=track, timestamp=timestamp)
# take the first one.
if foundPositions:
foundPosition = foundPositions[0]
except ObjectDoesNotExist:
pass

if not foundPosition:
tablename = getModel('PAST_POSITION_MODEL')._meta.db_table
query = "select * from " + tablename + " where " + "track_id = '" + str(track.id) + "' order by abs(timestampdiff(second, '" + timestamp.isoformat() + "' , timestamp)) limit 1"
posAtTime = (getModel('PAST_POSITION_MODEL').objects.raw(query))
Expand Down

0 comments on commit cb0a80f

Please sign in to comment.