Beispiel #1
0
def csv_points(request, map_id):
    if request.method == 'POST':
        LTI_LAUNCH = request.session.get("LTI_LAUNCH").values()[0]
        logged_in_user_id = LTI_LAUNCH.get('lis_person_sourcedid')
        logged_in_user_name = LTI_LAUNCH.get('lis_person_name_full')
        datatouse = request.data.dict().keys()[0]
        datatouse = json.loads(datatouse)
        errors = []
        for item in datatouse:
            data = {'title': item[u'title'],
                    'map': map_id,
                    'latitude': item[u'latitude'],
                    'longitude': item[u'longitude'],
                    'description': item[u'description'],
                    'external_url': item[u'externalurl'],
                    'created_by_id': logged_in_user_id,
                    'created_by_full_name': logged_in_user_name,
                    'modified_by_id': logged_in_user_id,
                    'date_created': timezone.now(),
                    'date_modified': timezone.now(),
                    }
            serializer = MarkersSerializer(data=data)
            if serializer.is_valid():
                serializer.save()
            else:
                errors.append(serializer.errors)
        if len(errors) != 0:
            return JSONResponse(errors, status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response(serializer.data, status=status.HTTP_201_CREATED)
Beispiel #2
0
def marker_collection(request, map_id):

    if request.method == 'GET':

        maps = Markers.objects.filter(map_id=map_id)
        serializer = MarkersSerializer(maps, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        LTI_LAUNCH = request.session.get("LTI_LAUNCH").values()[0]
        logged_in_user_id = LTI_LAUNCH.get('lis_person_sourcedid')
        logged_in_user_name = LTI_LAUNCH.get('lis_person_name_full')
        logger.debug(request.user)
        logger.debug(request.user.username)
        logger.debug(logged_in_user_id)
        latitude = request.data.get('latitude')
        longitude = request.data.get('longitude')
        address = request.data.get('address')
        if request.data.get('externalurl'):
            logger.debug("We got it")
            externalurl = request.data.get('externalurl')
        else:
            logger.debug("we didnt get it")
            externalurl = "none_provided"

        # lat/long points take precedence over addresses
        if latitude is not None and longitude is not None:
            data = {'title': request.data.get('title'),
                    'map': map_id,
                    'latitude': request.data.get('latitude'),
                    'longitude': request.data.get('longitude'),
                    'description': request.data.get('description'),
                    'external_url': externalurl,
                    'fileupload': request.data.get('fileupload'),
                    'created_by_id': logged_in_user_id,
                    'created_by_full_name': logged_in_user_name,
                    'modified_by_id': logged_in_user_id,
                    'date_created': timezone.now(),
                    'date_modified': timezone.now(),
                    }

            serializer = MarkersSerializer(data=data)

            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data, status=status.HTTP_201_CREATED)

            else:
                return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

        elif address:
            address = urllib.quote(address.encode("utf-8"))
            url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + \
                address + '&sensor=true'
            data = urllib2.urlopen(url).read()
            json_data = json.loads(data)
            stat = json_data.get('status')

            if stat in 'OK':
                result = json_data['results'][0]
                latitude = result['geometry']['location']['lat']
                longitude = result['geometry']['location']['lng']
                data = {'title': request.data.get('title'),
                        'map': map_id,
                        'latitude': latitude,
                        'longitude': longitude,
                        'description': request.data.get('description'),
                        'external_url': externalurl,
                        'fileupload': request.data.get('fileupload'),
                        'created_by_id': logged_in_user_id,
                        'created_by_full_name': logged_in_user_name,
                        'modified_by_id': logged_in_user_id,
                        'date_created': timezone.now(),
                        'date_modified': timezone.now(),
                        }
                serializer = MarkersSerializer(data=data)
                if serializer.is_valid():
                    serializer.save()
                    return Response({'latitude': latitude, 'longitude': longitude}, status=status.HTTP_201_CREATED)
                else:
                    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
            else:
                errors = ['Address not found!']
                return JSONResponse(errors, status=status.HTTP_400_BAD_REQUEST)
        else:
            errors = [
                'You did not enter either an address or lat/long. Try again.']
            return JSONResponse(errors, status=status.HTTP_400_BAD_REQUEST)