import settings from django.shortcuts import render_to_response from locations.models import * from locations.xmlrpc import * from django.http import HttpResponse from django.contrib.auth.models import User from django.db.models import Q import simplejson import time def index(request): addlocationform = LocationForm(auto_id="addlocation_") editlocationform = LocationForm(auto_id="editlocation_") return render_to_response("locations/index.html", {"addlocationform": addlocationform, "editlocationform": editlocationform, "user": request.user, "userauthed": request.user.is_authenticated()}) def search(request): # Handle search requests query = request.GET['q'] if not query or query == "": return HttpResponse(simplejson.encode(GetLocationList())) else: q = Q() for qs in query.split(" "): q &= Q(name__contains=qs) | Q(locname__contains=qs) try: locations = Location.objects.get(q) except: return HttpResponse("[]") results = [] for location in locations: results.append({ 'id': location.id, 'lat': location.lat, 'lon': location.lon, 'name': location.name, 'city': location.locname, 'website': location.website }) return HttpResponse(simplejson.encode(results)) def setlocation(request): if not request.has_key('locationid'): return HttpResponse(simplejson.encode({'error': 'Must supply location ID'})) locationid = request.GET['locationid'] vars = {} for a in ('name', 'locname', 'latitude', 'longitude', 'website', 'access'): if request.GET.has_key(a): vars[a] = request.GET[a] else: vars[a] = None SetLocation(locationid, **vars) def getlocation(request): if not request.GET['locationid']: return HttpResponse(simplejson.encode({'error': 'Must supply location ID'})) return HttpResponse(simplejson.encode(GetLocationDetails(request.GET['locationid']))) def addlocation(request): # Handle Add Location requests response = {'saved': False, 'error': "Not handled"} locationform = LocationForm(request.GET) if not request.user.is_authenticated(): response["error"] = "You must be logged in to register locations." return HttpResponse(simplejson.encode(response)) if locationform.is_valid(): location = locationform.save(commit=False) location.manager = request.user location.save() response["saved"] = True return HttpResponse(simplejson.encode(response)) else: response["error"] = "Must fill required fields." response["newtable"] = locationform.as_table() return HttpResponse(simplejson.encode(response)) def locationdetails(request): # Return very detailed information about a location. if not request.GET.has_key('locationid'): return HttpResponse(simplejson.encode({'error': 'Must supply ID'})) return HttpResponse(simplejson.encode(GetLocationDetails(request.GET['locationid']))) def addequipment(request): return HttpResponse("") def addequipmenttype(request): return HttpResponse("") def locationoutput(request): if type(request) == int: locationid = request else: locationid = request.GET['locationid'] location = GetLocationDetails(locationid) datetime = time.ctime() return {"location": location, "time": datetime} def locationtoxml(request): return render_to_response("locations/location.xml", locationoutput(request)) def locationtoyaml(request): return render_to_response("locations/location.yaml", locationoutput(request))