from locations.models import * from math import * # TODO: Authentication!!! # TODO: Better error handling def GetLocationList(): """Returns a list of locations.""" locations = Location.objects.all() results = [] for location in locations: results.append({ 'id': location.id, 'lat': location.lat, 'lon': location.lon, 'name': location.name, 'locname': location.locname, 'website': location.website }) return results def GetLocationDetails(locationid): location = Location.objects.get(id=locationid) if location.id: vals = {"location": location} equipcnt = location.equipment_set.count() if equipcnt > 0: equip = [] for equipment in location.equipment_set.iterator(): caps = [] for capability in equipment.type.capabilities.iterator(): caps.append(capability.name) equip.append({"type": equipment.type.name, "maker": equipment.type.maker, "capabilities": caps, "notes": equipment.notes}) vals['equipment'] = equip return vals else: return {} def GetEquipmentTypes(): equiptypes = EquipmentType.objects.all() results = [] for e in equiptypes.iterator: caps = [] for capability in e.capabilities.iterator(): caps.append(capability.name) results.append({'name': e.name, 'maker': e.maker, 'capabilities': caps}) return results def GetCapabilities(): caps = EquipmentCapability.objects.all() ret = [] for a in caps.iterator(): ret.append({'id': a.id, 'name': a.name}) return ret def AddEquipmentType(name, maker, capabilities): # Register an equipment type et = EquipmentType(name=name, maker=maker) et.save() for a in capabilities: if type(a) is int: et.capabilities.add(a) et.save() return {'saved': True} def AddEquipment(locationid, equipmenttype, notes): # Register equipment eq = Equipment(type=equipmenttype, location=locationid, notes=notes) eq.save() return {'saved': True} def AddLocation(name, locname, lat, lon, website): # Add a location. manager = 1 # FIXME: Here we should make manager be the same as the authed user. location = Location(name=name, locname=locname, lat=lat, lon=lon, website=website, manager=manager) location.save() return {'location': location, 'saved': True} def SetLocation(locationid, name, locname, lat, lon, website): location = Location(locationid=locationid) if name: location.name = name if locname: location.locname = locname if latitude: location.latitude = lat if longitude: location.longitude = lon if website: location.website = website location.save() def AddCapability(capability): # Add a capability class cap = EquipmentCapability(name=capability) cap.save() return {'saved': True, 'id': cap.id} def FindEquipmentByCapability(capid): # Return a list of equipment, and each equipment type contains a list of locations where it exists. return {} def Haversine(lon1, lat1, lon2, lat2): # TODO: Move this to some utilities thing? """ Find the distance in km between two points on the surface of the earth using the haversine formula. http://mathforum.org/library/drmath/view/51879.html http://www.mpi.nl/world/persons/private/micdunn/misc/haversine.py """ lon1 = radians(lon1) lon2 = radians(lon2) lat1 = radians(lat1) lat2 = radians(lat2) dlon = lon2 - lon1 dlat = lat2 - lat1 a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2 c = 2 * atan2(sqrt(a), sqrt(1-a)) km = 6367 * c return km