""" 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): self.db = Db () 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 = int (l['longitude']) self.latitude = int (l['latitude']) self.is_default = l['is_default'] self.location_id = l['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) # vim: tw=0 ts=2 expandtab # EOF