Skip to content

Commit

Permalink
Make sure we reconnect before a DB call.
Browse files Browse the repository at this point in the history
  • Loading branch information
rolando3 committed Mar 23, 2013
1 parent 2443efa commit 29a1117
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
22 changes: 17 additions & 5 deletions migra.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ def __init__(self,name,lat,lng):
self.__lng = lng

def name(self):
return self.__name;
return self.__name

def lat(self):
return self.__lat;
return self.__lat

def lng(self):
return self.__lng;
return self.__lng

class MigraHelper:
def __init__(self):
Expand Down Expand Up @@ -200,7 +200,9 @@ def __init__ ( self ):

global geocoder
geocoder = self

self.__connect()

def __connect ( self ):
try:
import os
pieces = os.environ.get('DATABASE_URL','').split(":")
Expand All @@ -211,11 +213,14 @@ def __init__ ( self ):
except:
import traceback
sys.stderr.write ( 'Cannot connect to database (%s:%s/%s): %s.' % ( host, port, dbname, ''.join(traceback.format_exception( *sys.exc_info())[-2:]).strip().replace('\n',': ') ) )

def geocode ( self, placename ):
""" Look on our database for a stored geocode. If none, return None """
if placename is None: return None

if self.__con.closed == 1:
self.__connect()

sql = 'SELECT lat, lng FROM geocode WHERE placename = %s;'
try:
cur = self.__con.cursor()
Expand All @@ -231,6 +236,10 @@ def geocode ( self, placename ):
return None

def cache ( self, location ):

if self.__con.closed == 1:
self.__connect()

""" Given a location name, store it in the database. Result is irrelevant """
sql = 'INSERT INTO geocode ( placename, lat, lng ) VALUES ( %s, %s, %s );'
try:
Expand All @@ -246,6 +255,9 @@ def cache ( self, location ):
return {'status': { 'message': 'OK', 'code': 0 } }

def countcachedaddresses(self):
if self.__con.closed == 1:
self.__connect()

sql = 'SELECT count(*) FROM geocode;'
cur = self.__con.cursor()
cur.execute(sql)
Expand Down
48 changes: 40 additions & 8 deletions static/js/migra.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ if (!String.prototype.repeat) {
}
}

MigraForms = { View: 1, UploadForm: 2, WalkForm: 3 };

function initialize()
{
//initialize our stuff: the map, its constituent thingies
Expand Down Expand Up @@ -46,8 +48,7 @@ function initialize()
addEventListeners();

Window.stat.actionEnd("Initialized");
//show our upload form.
$('#upload_form_wrapper').show();
showForm(MigraForms.UploadForm);
}

function MigraStatus ()
Expand Down Expand Up @@ -463,11 +464,44 @@ function addEventListeners()
clearMap();
});

$('.pseudobutton').click(function(e) {
window.alert ( this.name );
$('#btn_restart').click(function(e) {
//Might need to clean up data

showForm(MigraForms.UploadForm);
});

$('#btn_choose').click(function(e) {
showForm(MigraForms.WalkForm);
});

}

function showForm ( n )
{
//there are three
if ( n == MigraForms.View )
{
$('#overlay').hide();
$('#walk_form_wrapper').hide();
$('#upload_form_wrapper').hide();
}
else if ( n == MigraForms.UploadForm )
{
$('#overlay').show();
$('#walk_form_wrapper').hide();
$('#upload_form_wrapper').show();
}
else if ( n == MigraForms.WalkForm )
{
$('#overlay').show();
$('#walk_form_wrapper').show();
$('#upload_form_wrapper').hide();
}
else
{
//This is an error!
window.alert ( "What is this bullshit? " + n )
}
}

function processForm(form, e, successfunction )
Expand Down Expand Up @@ -530,8 +564,7 @@ function buildPeopleList(httpData)

));
});
$('#upload_form_wrapper').hide();
$('#walk_form_wrapper').show();
showForm(MigraForms.WalkForm);
Window.stat.actionEnd();
}
else
Expand Down Expand Up @@ -573,8 +606,7 @@ function drawMap ( httpData )
}

//Addresses are being mapped. Take our form away.
$('#walk_form_wrapper').hide();
$('#overlay').hide();
showForm(MigraForms.View);

}

Expand Down
6 changes: 2 additions & 4 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
<div id="header"><h1>Migra ancestry mapping</h1></div>
<div id="map_canvas"></div>
<div id="footer">
<!--
<div id="buttons">
<a id="link1" href="#" class="pseudobutton">Upload a new file</a>
<a id="link2" href="#" class="pseudobutton">Anotherlink</a>
<a id="btn_restart" href="#" class="pseudobutton">Upload a new file</a>
<a id="btn_choose" href="#" class="pseudobutton">Choose a new focus</a>
</div>
-->
<div id="statusBar" style="display: table-row; width: 100%">
<div id="message_pad" class="statusbar_cell" style="width: 50%">Ready.</div>
<div class="statusbar_cell" style="width: 30%">
Expand Down

0 comments on commit 29a1117

Please sign in to comment.