""" Location class for MURSAT1 Mission Dashboard """ import hashlib from cgi import parse_qs, escape from MmdDb import Db def checkLonLat (lonlat): if type (lonlat) != type (0.0): lonlat = float (lonlat) if lonlat < 0.0 or lonlat > 360.0: return False return lonlat class Location: 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 self.is_default = is_default self.longitude = checkLonLat (longitude) self.latitude = checkLonLat (latitude) if not self.longitude: self.longitude = 15.44226 if not self.latitude: self.latitude = 47.06576 self.location_id = hashlib.sha1 ('{0}{1}{2}'.format (user_id, self.longitude, self.latitude)).hexdigest () return self.db.locationCreate (self.location_id, self.name, self.longitude, self.latitude, user_id, self.is_default) def findUserId (self, user_id): l = self.db.locationFindUserId (user_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.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__": pass # vim: tw=0 ts=2 expandtab # EOF