from models import Place, PlaceToken
from django.http import HttpResponse, HttpResponseBadRequest
from django.contrib.gis.geos import Point, Polygon
from django.contrib.gis.measure import D
import json

def lookupId(request, pid):
    try:
        this_place = Place.objects.get(id = pid)
        dispatch = {'name': this_place.name,
                    'tokens': list(PlaceToken.objects.filter(
                    place_id = pid).values('key', 'value'))}
        return HttpResponse(json.dumps(dispatch), content_type = 'application/json')
    except:
        return HttpResponseBadRequest('Place missing')

def lookupKey(request, key):
    dispatch = list(Place.objects.filter(placetoken__key = key).values('id', 'name'))
    return HttpResponse(json.dumps(dispatch), content_type = 'application/json')

def lookupToken(request, key, value):
    dispatch = list(Place.objects.filter(
        placetoken__key = key, placetoken__value = value).values('id', 'name'))
    return HttpResponse(json.dumps(dispatch), content_type = 'application/json')

def lookupBounds(request):
    nwpoint = request.GET.get('nw')
    sepoint = request.GET.get('se')
    if nwpoint.find(',') < 0 or sepoint.find(',') < 0:
        return HttpResponseBadRequest('Malformed coordinates')
    xmin, ymax = [float(x) for x in nwpoint.split(',')]
    xmax, ymin = [float(x) for x in sepoint.split(',')]
    bbox = (xmin, ymin, xmax, ymax)
    dispatch = list(Place.objects.filter(
        mpoint__bbcontains = Polygon.from_bbox(bbox)).values(
        'id', 'name'))
    return HttpResponse(json.dumps(dispatch), content_type = 'application/json')

def lookupAround(request):
    center = request.GET.get('center')
    radius = request.GET.get('radius')
    if center.find(',') < 0:
        return HttpResponseBadRequest('Malformed center coordinates')
    centerx, centery = [float(x) for x in center.split(',')]
    dispatch = list(Place.objects.filter(mpoint__distance_lt=(
            Point(centerx, centery), D(m=radius))).values('id', 'name'))
    return HttpResponse(json.dumps(dispatch), content_type = 'application/json')

def createNew(request):
    # Incoming json payload
    return 1

def editId(request, id):
    # Incoming json payload
    return 1

@@ -141,16 +141,30 @@
  Polygon
+, GEOSGeometry
 %0Afrom dj
@@ -2145,44 +2145,410 @@
     
-# Incoming json payload%0A    return 1
+if request.method == 'POST':%0A        payload = json.loads(request.body)%0A        thisx, thisy = payload.get('mpoint', None).split(',')%0A        pnt = GEOSGeometry('POINT(%25s %25s)' %25 (thisx, thisy))%0A        this_place = Place(name = payload.get('name', None), mpoint = pnt)%0A        this_place.save()%0A        return HttpResponse('Success')%0A    else:%0A        return HttpResponseBadRequest('Missing POST data')
 %0A%0Ade
@@ -2565,16 +2565,17 @@
 equest, 
+p
 id):%0A   
@@ -2579,41 +2579,521 @@
     
-# Incoming json payload%0A    return 1
+if request.method == 'POST':%0A        payload = json.loads(request.body)%0A        this_place = Place.objects.get(id = pid)%0A        this_place.name = payload.get('name', None)%0A        this_token = PlaceToken(place = this_place,%0A                                key = payload.get('key', None),%0A                                value = payload.get('value', None))%0A        this_place.save()%0A        this_token.save()%0A        return HttpResponse('Success')%0A    else:%0A        return HttpResponseBadRequest('Missing POST data')
 %0A

complete createNew and editId