diff options
author | Jogi Hofmüller <jogi@mur.at> | 2011-08-03 11:19:44 +0000 |
---|---|---|
committer | Jogi Hofmüller <jogi@mur.at> | 2011-08-03 11:19:44 +0000 |
commit | 502b6a813a6f90c2e7adb5781b1beecd559014d9 (patch) | |
tree | 6c46b328e6a106bc770ad77650c0b6a14fb525e5 | |
parent | cleanup (diff) |
- added functionality for location
- user's default location is save
- map centers on user's location iff available
git-svn-id: https://svn.spreadspace.org/mur.sat@96 7de4ea59-55d0-425e-a1af-a3118ea81d4c
-rw-r--r-- | tools/mmd/MmdCommands.py | 10 | ||||
-rw-r--r-- | tools/mmd/MmdDb.py | 12 | ||||
-rw-r--r-- | tools/mmd/MmdLocation.py | 39 | ||||
-rw-r--r-- | tools/mmd/MmdLocationList.py | 43 | ||||
-rw-r--r-- | tools/mmd/MmdSession.py | 3 | ||||
-rw-r--r-- | tools/mmd/MmdUser.py | 40 | ||||
-rw-r--r-- | tools/mmd/MmdWidgets.py | 39 |
7 files changed, 156 insertions, 30 deletions
diff --git a/tools/mmd/MmdCommands.py b/tools/mmd/MmdCommands.py index 5355783..c1dbeb9 100644 --- a/tools/mmd/MmdCommands.py +++ b/tools/mmd/MmdCommands.py @@ -46,6 +46,7 @@ def register (session): callsign = session.getQsCallsign () longitude = session.getQsLongitude () latitude = session.getQsLatitude () + location_name = session.getQsLocationName () password = session.getQsPassword () confirm = session.getQsConfirm () @@ -54,7 +55,11 @@ def register (session): return indexWidget (dataWidget (), errorWidget (error_message), statusWidget (session), session) user = User () - code = user.create (session.session_id, firstname, lastname, email, callsign, longitude, latitude, password) + code = user.create (session.session_id, firstname, lastname, email, callsign, password) + + if longitude != '' and latitude != '' and location_name != '': + user.addLocation (location_name, longitude, latitude, 'yes') + message = '''From: noreply <noreply@mur.at> To: "{0} {1}" <{2}> Subject: MMD registration confirmation request @@ -124,7 +129,8 @@ def index (session): """ display the index page """ - return indexWidget (dataWidget (), osmWidget (), statusWidget (session), session) + satellite = {'longitude': 15.4426, 'latitude': 47.06576} + return indexWidget (dataWidget (), osmWidget (satellite, session.user), statusWidget (session), session) def mmdtest (env, cookie): body = '' diff --git a/tools/mmd/MmdDb.py b/tools/mmd/MmdDb.py index 1db8f17..7d915cf 100644 --- a/tools/mmd/MmdDb.py +++ b/tools/mmd/MmdDb.py @@ -101,6 +101,18 @@ class Db: self.cursor.execute ('SELECT * FROM location WHERE user_id=?', (user_id,)) return self.cursor.fetchone () + def locationListByUserId (self, user_id): + self.cursor.execute ('SELECT id FROM location WHERE user_id=?', (user_id,)) + return self.cursor.fetchall () + + def locationFindId (self, location_id): + self.cursor.execute ('SELECT * FROM location WHERE id=?', (location_id,)) + return self.cursor.fetchone () + + def locationDeleteId (self, location_id): + self.cursor.execute ('DELETE FROM location WHERE id=?', (location_id,)) + self.conn.commit () + def close (self): self.cursor.close () self.conn.close () diff --git a/tools/mmd/MmdLocation.py b/tools/mmd/MmdLocation.py index 67a1a74..e3212bb 100644 --- a/tools/mmd/MmdLocation.py +++ b/tools/mmd/MmdLocation.py @@ -14,8 +14,15 @@ def checkLonLat (lonlat): class Location: - def __init__ (self): + def __init__ (self, location_id = False): self.db = Db () + if location_id: + self.location_id = location_id + self.load () + else: + self.name = 'Graz/Austria' + self.longitude = 15.44226 + self.latitude = 47.06576 def create (self, name, longitude, latitude, is_default, user_id): self.name = name @@ -36,19 +43,31 @@ class Location: if not l: return False self.name = l['name'] - self.longitude = int (l['longitude']) - self.latitude = int (l['latitude']) + self.longitude = float (l['longitude']) + self.latitude = float (l['latitude']) self.is_default = l['is_default'] self.location_id = l['id'] + def load (self, location_id = False): + if location_id: + self.location_id = location_id + l = self.db.locationFindId (self.location_id) + if not l: + return False + self.name = l['name'] + self.longitude = float (l['longitude']) + self.latitude = float (l['latitude']) + self.is_default = l['is_default'] + self.user_id = l['user_id'] + return True + + def delete (self, location_id = False): + if location_id: + self.location_id = location_id + self.db.locationDeleteId (self.location_id) + if __name__ == "__main__": - l = Location () - try: - assert l, 'Location instantiation failed' - assert l.create ('Graz', '14.5', '47.3', 'yes', '1'), 'Location create failed' - assert l.findUserId (1), 'Location find user by id failed' - except AssertionError as e: - print 'Test failed: {0}'.format (e) + pass # vim: tw=0 ts=2 expandtab # EOF diff --git a/tools/mmd/MmdLocationList.py b/tools/mmd/MmdLocationList.py new file mode 100644 index 0000000..fd793f7 --- /dev/null +++ b/tools/mmd/MmdLocationList.py @@ -0,0 +1,43 @@ +""" +Location List class for MURSAT1 Mission Dashboard +""" +from cgi import parse_qs, escape +from MmdDb import Db +from MmdLocation import Location + +class LocationList: + + def __init__ (self, user_id = False): + self.db = Db () + self.locations = [] + if user_id: + self.user_id = user_id + location_ids = self.db.locationListByUserId (user_id) + for l in location_ids: + self.locations.append (Location (l['id'])) + + def count (self): + return len (self.locations) + + def getDefaultLocation (self): + for l in self.locations: + if l.is_default == 'yes': + return l + return False + + def addLocation (self, name, longitude, latitude, is_default): + location = Location () + if location.create (name, longitude, latitude, is_default, self.user_id): + self.locations.append (location) + return True + return False + + def delete (self): + for location in self.locations: + location.delete () + +if __name__ == "__main__": + pass + +# vim: tw=0 ts=2 expandtab +# EOF diff --git a/tools/mmd/MmdSession.py b/tools/mmd/MmdSession.py index d831fba..801f089 100644 --- a/tools/mmd/MmdSession.py +++ b/tools/mmd/MmdSession.py @@ -90,6 +90,9 @@ class Session: def getQsCode (self): return self.getField ('code') + def getQsLocationName (self): + return self.getField ('location_name') + def getField (self, field): try: return escape (self.qs.get (field)[0]) diff --git a/tools/mmd/MmdUser.py b/tools/mmd/MmdUser.py index d9597b1..feb8f5a 100644 --- a/tools/mmd/MmdUser.py +++ b/tools/mmd/MmdUser.py @@ -6,15 +6,16 @@ import smtplib import time from cgi import parse_qs, escape from MmdDb import Db -from MmdLocation import Location +from MmdLocationList import LocationList class User: def __init__ (self, email = 'Anonymous'): self.db = Db () self.email = email + self.location_list = LocationList () - def create (self, user_id, firstname, lastname, email, callsign, longitude, latitude, password): + def create (self, user_id, firstname, lastname, email, callsign, password): password_hash = hashlib.sha1 (password).hexdigest () code = hashlib.sha1 ('{0}{1}{2}{3}'.format (user_id, firstname, lastname, email)).hexdigest () regtimeout = int (time.strftime ('%s')) + (3600 * 24) @@ -23,8 +24,13 @@ class User: self.email = email self.firstname = firstname self.lastname = lastname + self.user_id = user_id return code + def addLocation (self, name, longitude, latitude, is_default): + self.location_list = LocationList (self.user_id) + return self.location_list.addLocation (name, longitude, latitude, is_default) + def findPending (self, code): u = self.db.userFindPending (code) if not u: @@ -45,15 +51,17 @@ class User: self.lastname = u['lastname'] return True - def load (self, email): - u = self.db.userFindEmail (email) + def load (self, email = False): + if email: + self.email = email + u = self.db.userFindEmail (self.email) if not u: return False - self.email = email + self.user_id = u['id'] self.firstname = u['firstname'] self.lastname = u['lastname'] self.password = u['password'] - # include loading of location data here + self.location_list = LocationList (self.user_id) return True def checkPassword (self, password): @@ -62,8 +70,15 @@ class User: return False def cancel (self, code): + self.deleteLocationList () self.db.userCancel (code) + def deleteLocationList (self): + self.location_list.delete () + + def getDefaultLocation (self): + return self.location_list.getDefaultLocation () + def sendEmail (self, message): server = smtplib.SMTP ('localhost') try: @@ -76,12 +91,19 @@ class User: if __name__ == "__main__": user = User () + email = 'the6@mur.at' try: assert user, 'no user object' - code = user.create (1, 'jogi', 'hofmueller', 'jogi@mur.at', '', 14.4, 47.2, 'blah') + code = user.create (1, 'jogi', 'hofmueller', email, '', 14.4, 47.2, 'blah') assert code, 'could not create user' - assert user.load ('jogi@mur.at'), 'could not load user data from db' - print 'passed all tests' + assert user.load (email), 'could not load user data from db' + print 'User:', user.firstname, user.lastname, user.email + assert user.addLocation ('Graz', '14.4', '47.2', 'yes'), 'could not add location' + location = user.getDefaultLocation () + assert location, 'could not get default location' + print 'Location:', location.name, location.longitude, location.latitude, location.is_default + user.cancel (code) + print 'passed all tests and removed objects from database' except AssertionError as error: print 'Test failed: {0}'.format (error) diff --git a/tools/mmd/MmdWidgets.py b/tools/mmd/MmdWidgets.py index ec77eb0..4f0d582 100644 --- a/tools/mmd/MmdWidgets.py +++ b/tools/mmd/MmdWidgets.py @@ -3,6 +3,7 @@ web widgets for MURSAT1 Dashboard """ import time +from MmdLocation import Location def loginFormWidget (): html = ''' @@ -49,10 +50,12 @@ def registerFormWidget (): <tr><td class="formfield">Lastname*: </td><td><input type="text" name="lastname" /></td></tr> <tr><td class="formfield">Email*: </td><td><input type="text" name="email" /></td></tr> <tr><td class="formfield">Callsign: </td><td><input type="text" name="callsign" /></td></tr> - <tr><td class="formfield">Longitude: </td><td><input type="text" name="latitude" /></td></tr> - <tr><td class="formfield">Latitude: </td><td><input type="text" name="longitude" /></td></tr> <tr><td class="formfield">Password*: </td><td><input type="password" name="password" /></td></tr> <tr><td class="formfield">Confirm Password*: </td><td><input type="password" name="confirm" /></td></tr> + <tr><td colspan="2">You can enter your prefered location if you wish</td></tr> + <tr><td class="formfield">Name: </td><td><input type="text" name="location_name" /></td></tr> + <tr><td class="formfield">Longitude: </td><td><input type="text" name="latitude" /></td></tr> + <tr><td class="formfield">Latitude: </td><td><input type="text" name="longitude" /></td></tr> <!-- <tr><td class="formfield"><input type="submit" value="Register" disabled="disabled" /></td><td></td></tr> --> <tr><td class="formfield"><input type="submit" value="register" /></td><td></td></tr> </table> @@ -96,7 +99,10 @@ def confirmRegistrationWidget (user): Thank you for participating in this project by registerting here! We are looking forward to receiving your reports from MURSAT1. </div> <div> - Sessions here stay valid for 24 hours after your last click. If your session expires, just use your email and password to logon again using this link: <a href="http://hofos.at/mmd/?cmd=login">http://hofos.at/mmd/?cmd=login</a>. + Sessions here stay valid for 48 hours after your last click. If your session expires, just use your email and password to logon again using this link: <a href="http://hofos.at/mmd/?cmd=login">http://hofos.at/mmd/?cmd=login</a>. + </div> + <div> + Greetings from the MURSAT1 team </div> '''.format (user.firstname, user.lastname) @@ -145,14 +151,18 @@ def logWidget (): return html -def osmWidget (longitude = 15.4426, latitude = 47.06576): +def osmWidget (satellite, user): + user_location = user.getDefaultLocation () + if not user_location: + user_location = Location () html = ''' <div id="osm"></div> <script src="http://www.openlayers.org/api/OpenLayers.js"></script> <script> var from = new OpenLayers.Projection ("EPSG:4326"); var to = new OpenLayers.Projection ("EPSG:900913"); - var lonlat = new OpenLayers.LonLat ({0}, {1}).transform (from, to); + var satellite = new OpenLayers.LonLat ({0}, {1}).transform (from, to); + var user_location = new OpenLayers.LonLat ({2}, {3}).transform (from, to); // create a map map = new OpenLayers.Map ("osm"); @@ -161,12 +171,14 @@ def osmWidget (longitude = 15.4426, latitude = 47.06576): // set a marker var markers = new OpenLayers.Layer.Markers ("Markers"); map.addLayer (markers); - markers.addMarker (new OpenLayers.Marker (lonlat)); + markers.addMarker (new OpenLayers.Marker (satellite)); + markers.addMarker (new OpenLayers.Marker (user_location)); // center the map - map.setCenter (lonlat, 2); + map.setCenter (user_location, 2); </script> - '''.format (longitude, latitude) + <!-- lon: {2} lat: {3} --> + '''.format (satellite['longitude'], satellite['latitude'], user_location.longitude, user_location.latitude) return html @@ -189,6 +201,9 @@ def statusWidget (session): def indexWidget (lcol, rcol, status, debug_info = False): if debug_info: + user_location = debug_info.user.getDefaultLocation () + if not user_location: + user_location = Location () debug = ''' <pre class="debug"> Debug Info @@ -200,6 +215,9 @@ def indexWidget (lcol, rcol, status, debug_info = False): Expires: {4} Renewal: {5} Now: {6} + User's Location Name: {7} + Longitude: {8} + Latitude: {9} </pre> '''.format ( debug_info.session_id, @@ -208,7 +226,10 @@ def indexWidget (lcol, rcol, status, debug_info = False): debug_info.status, debug_info.expires, debug_info.renewal, - time.strftime ('%s')) + time.strftime ('%s'), + user_location.name, + user_location.longitude, + user_location.latitude) else: debug = '' |