Esempio n. 1
0
def add_spot(request):
    """
    View to add a new spot to upspot.
  """
    if request.method == 'GET':
        spots = Spot.objects.filter(owner=request.user)
        return render(request, 'park/spot.html', {'spots': spots})
    elif request.method == 'POST':
        owner = request.user
        lat = float(request.POST.get("lat"))
        lng = float(request.POST.get("lng"))
        sv_lat = float(request.POST.get("sv_lat"))
        sv_lng = float(request.POST.get("sv_lng"))
        address = request.POST.get("address")
        spot_number = request.POST.get("spot_number")
        instructions = request.POST.get("instructions")
        new_spot = Spot()
        new_spot.owner = owner
        new_spot.location = Point(lng, lat)
        new_spot.sv_location = Point(sv_lng, sv_lat)
        new_spot.address = address
        new_spot.spot_number = spot_number
        new_spot.instructions = instructions
        new_spot.save()
        # Find geohash to get grid value and retrieve geobucket.
        spot_geohash = geohash_encode(lat, lng)[:6]
        geobucket, _ = GeoBucket.objects.get_or_create(geohash=spot_geohash)
        # Add a spot to the given geobucket and save.
        geobucket.spot()
        geobucket.save()
        return redirect('/park/spots')
Esempio n. 2
0
    def spot_with_id(self, request, pk=None):
        """
        Creates spot with beacon id
        POST params - user, spot_timestamp, lat, lng, namespace_id, instance_id
        """
        request_dict = json.loads(request.body)[0]
        try:
            user = str(request_dict['user'])
        except (KeyError, TypeError):
            return return_bad_request('parameter user missing or incorrect',
                                      request_dict)

        try:
            spot_timestamp = int(request_dict['spot_timestamp'])
        except (KeyError, TypeError):
            return return_bad_request(
                'parameter spot_timestamp missing or incorrect', request_dict)

        try:
            lat = float(request_dict['lat'])
        except (KeyError, TypeError):
            return return_bad_request('parameter lat missing or incorrect',
                                      request_dict)

        try:
            lng = float(request_dict['lng'])
        except (KeyError, TypeError):
            return return_bad_request('parameter lng missing or incorrect',
                                      request_dict)

        try:
            namespace_id = str(request_dict['namespace_id'])
        except (KeyError, TypeError):
            return return_bad_request(
                'parameter namespace_id missing or incorrect', request_dict)

        try:
            instance_id = str(request_dict['instance_id'])
        except (KeyError, TypeError):
            return return_bad_request(
                'parameter instance_id missing or incorrect', request_dict)

        values = {
            'user': user,
            'spot_timestamp': spot_timestamp,
            'lat': lat,
            'lng': lng,
        }
        spot = Spot(**values)
        spot.save(namespace_id=namespace_id, instance_id=instance_id)
        return return_request_ok()