Exemple #1
0
def newLayer(request):
    if request.method == 'POST':
        layerObject = json.loads(request.raw_post_data)
        if 'externalUrl' in layerObject:
            form = LibraryLayerUrlForm(layerObject)
            layer = form.save()
        else:
            print >> sys.stderr, request.raw_post_data
            return jsonErrorResponse('did not get expected fields')
        layer.save()
        return jsonResponse({'result': json.loads(layer.json)})
    else:
        return HttpResponseNotAllowed(('POST',))
Exemple #2
0
def newLayer(request):
    if request.method == 'POST':
        layerObject = json.loads(request.raw_post_data)
        if 'externalUrl' in layerObject:
            form = LibraryLayerUrlForm(layerObject)
            layer = form.save()
        else:
            print >> sys.stderr, request.raw_post_data
            return jsonErrorResponse('did not get expected fields')
        layer.save()
        return jsonResponse({'result': json.loads(layer.json)})
    else:
        return HttpResponseNotAllowed(('POST', ))
Exemple #3
0
 def setJson(self, newdata):
     if isinstance(newdata, basestring):
         newdata = json.loads(newdata)
     old_data = json.loads(self.json)
     old_data.update(newdata)
     self.json = json.dumps(old_data)
     for k in (
         'name',
         'description',
         'url',
         'mapsetjson'
     ):
         if k in old_data:
             setattr(self, k, old_data[k])
     self.save()
Exemple #4
0
def mapSetSave(request):
    """
    Update or create a new mapset from json properties.
    (See MapSet.fromJSON for the schema)
    """
    if request.method == 'POST':
        json_data = json.loads(request.raw_post_data)

        mapset = MapSet.fromJSON(json_data)
        mapset.save()

        # if 'extensions' in json_data:
        #     extensions = json_data['extensions']
        #     for extension in extensions.keys():
        #         e = Extension(
        at
        #             url = extensions[extension])
        #         mapset.extension_set.add(e)
        #         e.save()

        # if 'children' in json_data:
        #     children = json_data['children']
        #     for child in children:
        #         c = MapSetLayer.fromJSON(child)
        #         mapset.mapsetlayer_set.add(c)
        #         c.save()

    return HttpResponse("OK")
 def validate(self, dataStr):
     try:
         data = json.loads(dataStr)
     except ValueError, valerr:
         print valerr
         raise JsonRpc2ComplianceException(
             "Could not parse data into json object: %s" % str(valerr))
Exemple #6
0
 def json(self, value):
     obj = json.loads(value)
     for k in obj.keys():
         if k in self.json_fields:
             setattr(self, k, obj[k])
         else:
             raise AttributeError("Tried to set a JSON value with a key (%s) not listed in LibaryLayer.json_fields" % k)
Exemple #7
0
 def to_python(self, value):
     if value in ('', None):
         return None
     elif isinstance(value, (str, unicode)):
         return json.loads(value)
     else:
         return value
Exemple #8
0
    def handleRpcCall(self, messages):
        for msg in messages:
            try:
                call = json.loads(msg)
                callId = call['id']
            except:  # pylint: disable=W0702
                self.rpcStream.send(json.dumps({'result': None,
                                                'error': 'malformed request'}))

            try:
                method = call['method']
                _params = call['params']
                if method == 'info':
                    result = self.handleInfo()
                else:
                    raise ValueError('unknown method %s' % method)
                self.rpcStream.send(json.dumps({'result': result,
                                                'error': None,
                                                'id': callId}))
            except:  # pylint: disable=W0702
                self.logException('handling rpc message')
                errClass, errObject = sys.exc_info()[:2]
                errText = '%s.%s: %s' % (errClass.__module__,
                                         errClass.__name__,
                                         str(errObject))
                self.rpcStream.send(json.dumps({'result': None,
                                                'error': errText,
                                                'id': callId}))
Exemple #9
0
    def handleRpcCall(self, messages):
        for msg in messages:
            try:
                call = json.loads(msg)
                callId = call['id']
            except:  # pylint: disable=W0702
                self.rpcStream.send(json.dumps({'result': None,
                                                'error': 'malformed request'}))

            try:
                method = call['method']
                _params = call['params']
                if method == 'info':
                    result = self.handleInfo()
                else:
                    raise ValueError('unknown method %s' % method)
                self.rpcStream.send(json.dumps({'result': result,
                                                'error': None,
                                                'id': callId}))
            except:  # pylint: disable=W0702
                self.logException('handling rpc message')
                errClass, errObject = sys.exc_info()[:2]
                errText = '%s.%s: %s' % (errClass.__module__,
                                         errClass.__name__,
                                         str(errObject))
                self.rpcStream.send(json.dumps({'result': None,
                                                'error': errText,
                                                'id': callId}))
Exemple #10
0
    def handleRpcCall(self, messages):
        for msg in messages:
            try:
                call = json.loads(msg)
                callId = call['id']
            except:  # pylint: disable=W0702
                self.rpcStream.send(json.dumps({'result': None,
                                                'error': 'malformed request'}))

            try:
                method = call['method']
                _params = call['params']
                if method == 'info':
                    result = self.handleInfo()
                else:
                    raise ValueError('unknown method %s' % method)
                self.rpcStream.send(json.dumps({'result': result,
                                                'error': None,
                                                'id': callId}))
            except:  # pylint: disable=W0702
                errClass, errObject, errTB = sys.exc_info()[:3]
                errText = '%s.%s: %s' % (errClass.__module__,
                                         errClass.__name__,
                                         str(errObject))
                logging.warning(''.join(traceback.format_tb(errTB)))
                logging.warning(errText)
                logging.warning('while handling rpc message: %s', msg)
                self.rpcStream.send(json.dumps({'result': None,
                                                'error': errText,
                                                'id': callId}))
Exemple #11
0
def mapSetSave(request):
    """
    Update or create a new mapset from json properties.
    (See MapSet.fromJSON for the schema)
    """
    if request.method == 'POST':
        json_data = json.loads(request.raw_post_data)

        mapset = MapSet.fromJSON(json_data)
        mapset.save()

        # if 'extensions' in json_data:
        #     extensions = json_data['extensions']
        #     for extension in extensions.keys():
        #         e = Extension(
        at
        #             url = extensions[extension])
        #         mapset.extension_set.add(e)
        #         e.save()

        # if 'children' in json_data:
        #     children = json_data['children']
        #     for child in children:
        #         c = MapSetLayer.fromJSON(child)
        #         mapset.mapsetlayer_set.add(c)
        #         c.save()

    return HttpResponse("OK")
Exemple #12
0
 def sendDjango(self, modelInstance, topic=None, topicSuffix=None):
     dataText = self.serializer.serialize([modelInstance])
     data = json.loads(dataText)[0]
     if topic is None:
         topic = data['model'].encode('utf-8')
         if topicSuffix is not None:
             topic += topicSuffix
     self.sendJson(topic, {'data': data})
Exemple #13
0
 def sendDjango(self, modelInstance, topic=None, topicSuffix=None):
     dataText = self.serializer.serialize([modelInstance])
     data = json.loads(dataText)[0]
     if topic is None:
         topic = data['model'].encode('utf-8')
         if topicSuffix is not None:
             topic += topicSuffix
     self.sendJson(topic, {'data': data})
Exemple #14
0
    def getGenerator(self):
        image = self.getImage()

        if self.transform:
            return (quadTree.WarpedQuadTreeGenerator(
                self.id, image, json.loads(self.transform)))
        else:
            return quadTree.SimpleQuadTreeGenerator(self.id, image)
Exemple #15
0
 def read(self, dflt=None):
     if os.path.exists(self.path):
         text = open(self.path, 'rb').read()
         try:
             return json.loads(text)
         except:
             print >> sys.stderr, 'ERROR: JSON file %s, with size %s, appears to be corrupted, will ignore and overwrite' % (self.path, len(text))
             return dflt
     else:
         return dflt
Exemple #16
0
    def getGenerator(self):
        image = self.getImage()

        if self.transform:
            return (quadTree.WarpedQuadTreeGenerator
                    (self.id,
                     image,
                     json.loads(self.transform)))
        else:
            return quadTree.SimpleQuadTreeGenerator(self.id,
                                                    image)
Exemple #17
0
 def read(self, dflt=None):
     if os.path.exists(self.path):
         text = open(self.path, 'rb').read()
         try:
             return json.loads(text)
         except:
             print >> sys.stderr, 'ERROR: JSON file %s, with size %s, appears to be corrupted, will ignore and overwrite' % (
                 self.path, len(text))
             return dflt
     else:
         return dflt
Exemple #18
0
 def handleMessages(self, messages):
     for msg in messages:
         if hasAttachments(msg):
             self.logMessageWithAttachments(msg)
         else:
             self.logMessage(msg)
         if msg.startswith('central.heartbeat.'):
             try:
                 _topic, body = msg.split(':', 1)
                 self.handleHeartbeat(json.loads(body))
             except:  # pylint: disable=W0702
                 self.logException('handling heartbeat')
Exemple #19
0
def mapSetSet(request, userName, shortName):
    if request.method == 'POST':
        json_data = json.loads(request.raw_post_data)
        mapset = MapSet.fromJSON(userName, shortName, json_data)
        mapset.save()
        return HttpResponse("OK")
    else:
        # get: user is requesting map set
        s = get_object_or_404(MapSet,
                              author__username=userName,
                              shortName=shortName)
        return jsonResponse(s.json, raw=True)
Exemple #20
0
 def handleMessages(self, messages):
     for msg in messages:
         if hasAttachments(msg):
             self.logMessageWithAttachments(msg)
         else:
             self.logMessage(msg)
         if msg.startswith('central.heartbeat.'):
             try:
                 _topic, body = msg.split(':', 1)
                 self.handleHeartbeat(json.loads(body))
             except:  # pylint: disable=W0702
                 self.logException('handling heartbeat')
Exemple #21
0
def mapSetSet(request, userName, shortName):
    if request.method == 'POST':
        json_data = json.loads(request.raw_post_data)
        mapset = MapSet.fromJSON(userName, shortName, json_data)
        mapset.save()
        return HttpResponse("OK")
    else:
        # get: user is requesting map set
        s = get_object_or_404(MapSet,
                              author__username=userName,
                              shortName=shortName)
        return jsonResponse(s.json, raw=True)
Exemple #22
0
 def handleMessages(self, messages):
     for msg in messages:
         self.logMessage(msg)
         if msg.startswith('central.heartbeat.'):
             try:
                 _topic, body = msg.split(':', 1)
                 self.handleHeartbeat(json.loads(body))
             except:  # pylint: disable=W0702
                 errClass, errObject, errTB = sys.exc_info()[:3]
                 errText = '%s.%s: %s' % (errClass.__module__,
                                          errClass.__name__,
                                          str(errObject))
                 logging.warning(''.join(traceback.format_tb(errTB)))
                 logging.warning(errText)
                 logging.warning('[error while handling heartbeat %s]', msg)
Exemple #23
0
def layerUpload(request):
    if request.method == 'POST':
        form = LibraryLayerUploadForm(request.POST, request.FILES)
        if form.is_valid():
            layer = form.save()
            if layer.externalUrl:
                layer.name = os.path.basename(layer.externalUrl)
            elif layer.localCopy:
                pass  # figure this out
            layer.setJson()
            layer.save()
            return jsonResponse({'result': json.loads(layer.json)})
        else:
            return jsonFormErrorsResponse(form)
    else:
        return HttpResponseNotAllowed(('POST', ))
Exemple #24
0
def layerUpload(request):
    if request.method == 'POST':
        form = LibraryLayerUploadForm(request.POST, request.FILES)
        if form.is_valid():
            layer = form.save()
            if layer.externalUrl:
                layer.name = os.path.basename(layer.externalUrl)
            elif layer.localCopy:
                pass # figure this out
            layer.setJson()
            layer.save()
            return jsonResponse({'result': json.loads(layer.json)})
        else:
            return jsonFormErrorsResponse(form)
    else:
        return HttpResponseNotAllowed(('POST',))
Exemple #25
0
 def to_python(self, value):
     if value == '':
         return Extras()
     elif type(value) == Extras:
         return value
     else:
         extras = Extras()
         try:
             values = json.loads(value)
             assert type(values) == dict, 'expected a dictionary object, found a %s' % type(values).__name__
             for key in values.keys():
                 assert type(key) in (unicode, str), 'expected unicode keys, found a %s' % type(key).__name__
             extras.__dict__ = values  # pylint: disable=W0201
         except (ValueError, AssertionError), e:
             raise ValidationError('Invalid JSON data in ExtrasField: %s' % e)
         return extras
Exemple #26
0
 def to_python(self, value):
     if value == '':
         return DotDict()
     elif type(value) == DotDict:
         return value
     else:
         extras = DotDict()
         try:
             values = convertToDotDictRecurse(json.loads(value))
             assert isinstance(values, DotDict), 'expected a DotDict object, found a %s' % type(values).__name__
             for key in values.iterkeys():
                 assert type(key) in (unicode, str), 'expected unicode or str keys, found a %s' % type(key).__name__
             extras = values
         except (ValueError, AssertionError), e:
             raise ValidationError('Invalid JSON data in ExtrasDotField: %s' % e)
         return extras
Exemple #27
0
def postPosition(request):
    if request.method == 'GET':
        return HttpResponseNotAllowed('Please post a resource position as a GeoJSON Feature.')
    else:
        try:
            featureDict = json.loads(request.raw_post_data)
        except ValueError:
            return HttpResponse('Malformed request, expected resources position as a GeoJSON Feature',
                                status=400)

        # create or update Resource
        properties = featureDict['properties']
        featureUserName = properties['userName']
        matchingUsers = User.objects.filter(username=featureUserName)
        if matchingUsers:
            user = matchingUsers[0]
        else:
            user = User.objects.create_user(featureUserName, '*****@*****.**' % featureUserName, '12345')
            user.first_name = featureUserName
            user.is_active = False
            user.save()
        resource, created = Resource.objects.get_or_create(uuid=featureDict['id'],
                                                           defaults=dict(user=user))
        if resource.user.username != featureUserName:
            resource.user = user
            resource.save()

        # create or update ResourcePosition
        coordinates = featureDict['geometry']['coordinates']
        timestamp = iso8601.parse_date(properties['timestamp']).replace(tzinfo=None)
        attrs = dict(timestamp=timestamp,
                     longitude=coordinates[0],
                     latitude=coordinates[1])
        if len(coordinates) >= 3:
            attrs['altitude'] = coordinates[2]
        rp, created = ResourcePosition.objects.get_or_create(resource=resource,
                                                             defaults=attrs)
        if not created:
            for field, val in attrs.iteritems():
                setattr(rp, field, val)
            rp.save()

        # add a PastResourcePosition historical entry
        PastResourcePosition(resource=resource, **attrs).save()

        return HttpResponse(dumps(dict(result='ok')),
                            mimetype='application/json')
Exemple #28
0
    def handleRequest(self, rawRequest):
        try:
            request = json.loads(rawRequest)
        except ValueError:
            self.writeError(rawRequest, None, -32700, 'invalid json')
            return

        version = request.get('jsonrpc', None)
        if version != '2.0':
            self.writeError(rawRequest, None, -32600,
                            'request does not have jsonrpc == "2.0"')
            return

        requestId = request.get('id', None)
        if requestId is None:
            self.writeError(rawRequest, None, -32600, 'request has no id')
            return

        params = request.get('params', None)
        if not isinstance(params, dict):
            self.writeError(rawRequest, requestId, -32600,
                            'params field must be a dict')
            return

        methodName = request.get('method', None)
        if isinstance(methodName, (str, unicode)):
            handlerName = 'handle_' + methodName
            handler = getattr(self, handlerName, None)
        else:
            handler = None
        if handler is None or not callable(handler):
            self.writeError(rawRequest, requestId, -32601,
                            'unknown method %s' % methodName)
            return

        try:
            result = handler(**params)
        except:  # pylint: disable=W0702
            errClass, errObject, errTB = sys.exc_info()[:3]
            traceback.print_tb(errTB)
            print >> sys.stderr, '%s.%s: %s' % (
                errClass.__module__, errClass.__name__, str(errObject))
            self.writeError(rawRequest, requestId, -32000,
                            'internal server error')
            return

        self.writeSuccess(requestId, result)
Exemple #29
0
    def handleRequest(self, rawRequest):
        try:
            request = json.loads(rawRequest)
        except ValueError:
            self.writeError(rawRequest, None, -32700, 'invalid json')
            return

        version = request.get('jsonrpc', None)
        if version != '2.0':
            self.writeError(rawRequest, -32600, 'request does not have jsonrpc == "2.0"')
            return

        requestId = request.get('id', None)
        if requestId is None:
            self.writeError(rawRequest, -32600, 'request has no id')
            return

        params = request.get('params', None)
        if not isinstance(params, dict):
            self.writeError(rawRequest, requestId, -32600, 'params field must be a dict')
            return

        methodName = request.get('method', None)
        if isinstance(methodName, (str, unicode)):
            handlerName = 'handle_' + methodName
            handler = getattr(self, handlerName, None)
        else:
            handler = None
        if handler is None or not callable(handler):
            self.writeError(rawRequest, requestId, -32601, 'unknown method %s' % repr(method))
            return

        try:
            result = handler(**params)
        except:  # pylint: disable=W0702
            errClass, errObject, errTB = sys.exc_info()[:3]
            traceback.print_tb(errTB)
            print >> sys.stderr, '%s.%s: %s' % (errClass.__module__,
                                                errClass.__name__,
                                                str(errObject))
            self.writeError(rawRequest, requestId, -32000, 'internal server error')
            return

        self.writeSuccess(requestId, result)
Exemple #30
0
def mapset_pre_save(sender, instance, raw, *args, **kwargs):
    '''
    Pre-save signal handler.
    Ensure that a shortName is assigned before save.
    Add any boilerplate to the stored JSON representation.
    '''
    if not instance.shortName:
        instance.shortName = sender.shortNameFromName(instance.name)

    # Set URL
    json_rep = json.loads(instance.json)
    json_rep['url'] = getattr(settings, 'BASE_URL', '') + reverse( 'mapset_resource', kwargs={'username':instance.author.username, 'shortname':instance.shortName} )

    # Set some default values for MapSetJSON properties
    json_rep.setdefault(u'extensions', {u'kml': u'http://mapmixer.org/mapsetjson/ext/kml/0.1/', u'geojson': u'http://mapmixer.org/mapsetjson/ext/geojson/0.1/'} )
    json_rep.setdefault(u'mapsetjson', u'0.1')
    json_rep.setdefault(u'type', u'Document')

    instance.json = json.dumps(json_rep)
Exemple #31
0
 def reversePts(self, toPts):
     """
     Helper needed for fitRpcToModel. 
     Does v = T(u).
         @v is a 3 x n matrix of n 3D points in WGS84 (lon, lat, alt)
         @u is a 2 x n matrix of n 2D points in image (px, py)
     """
     transformDict  = json.loads(self.transform)
     tform =  transform.makeTransform(transformDict)
     pixels = None
     for column in toPts.T:
         # convert column (3D pts in WGS84) to gmap meters
         lonlat = column[:2]
         gmap_meters = transform.lonLatToMeters(lonlat)
         px, py = tform.reverse(gmap_meters)
         newCol = np.array([[px],[py]])
         if pixels is None:
             pixels = newCol
         else:
             pixels = np.column_stack((pixels, newCol))
     return pixels        
Exemple #32
0
 def to_python(self, value):
     if value == '':
         return Extras()
     elif type(value) == Extras:
         return value
     else:
         extras = Extras()
         try:
             values = json.loads(value)
             assert type(
                 values
             ) == dict, 'expected a dictionary object, found a %s' % type(
                 values).__name__
             for key in values.keys():
                 assert type(key) in (
                     unicode, str
                 ), 'expected unicode keys, found a %s' % type(key).__name__
             extras.__dict__ = values  # pylint: disable=W0201
         except (ValueError, AssertionError), e:
             raise ValidationError('Invalid JSON data in ExtrasField: %s' %
                                   e)
         return extras
Exemple #33
0
def layerJson(request, layerId):
    layer = get_object_or_404(LibraryLayer, id=layerId)
    if request.method in ('PUT', 'POST'):
        layerObject = json.loads(request.raw_post_data)
        if 'id' in layerObject:
            assert layerObject['id'] == layerId
        else:
            layerObject['id'] = layerId
        form = LibraryLayerMetaForm(layerObject)
        if form.is_valid():
            updatedLayer = form.save(commit=False)
            keepFields = ('id', 'externalUrl', 'localCopy')
            for f in keepFields:
                setattr(updatedLayer, f, getattr(layer, f))
            updatedLayer.complete = True
            updatedLayer.save()
            return jsonResponse(updatedLayer.json, raw=True)
        else:
            return jsonFormErrorsResponse(form)
    elif request.method == 'GET':
        return jsonResponse(layer.json, raw=True)
    else:
        return HttpResponseNotAllowed(('PUT', 'POST', 'GET'))
Exemple #34
0
 def to_python(self, value):
     if value == '':
         return DotDict()
     elif type(value) == DotDict:
         return value
     else:
         extras = DotDict()
         try:
             values = convertToDotDictRecurse(json.loads(value))
             assert isinstance(
                 values,
                 DotDict), 'expected a DotDict object, found a %s' % type(
                     values).__name__
             for key in values.iterkeys():
                 assert type(key) in (
                     unicode, str
                 ), 'expected unicode or str keys, found a %s' % type(
                     key).__name__
             extras = values
         except (ValueError, AssertionError), e:
             raise ValidationError(
                 'Invalid JSON data in ExtrasDotField: %s' % e)
         return extras
Exemple #35
0
def layerJson(request, layerId):
    layer = get_object_or_404(LibraryLayer, id=layerId)
    if request.method in ('PUT', 'POST'):
        layerObject = json.loads(request.raw_post_data)
        if 'id' in layerObject:
            assert layerObject['id'] == layerId
        else:
            layerObject['id'] = layerId
        form = LibraryLayerMetaForm(layerObject)
        if form.is_valid():
            updatedLayer = form.save(commit=False)
            keepFields = ('id', 'externalUrl', 'localCopy')
            for f in keepFields:
                setattr(updatedLayer, f, getattr(layer, f))
            updatedLayer.complete = True
            updatedLayer.save()
            return jsonResponse(updatedLayer.json, raw=True)
        else:
            return jsonFormErrorsResponse(form)
    elif request.method == 'GET':
        return jsonResponse(layer.json, raw=True)
    else:
        return HttpResponseNotAllowed(('PUT', 'POST', 'GET'))
Exemple #36
0
 def djangoHandler(topic, body):
     obj = json.loads(body)
     dataText = json.dumps([obj['data']])
     modelInstance = self.deserializer(dataText)[0]
     return handler(topic, modelInstance)
 def djangoHandler(topicPrefix, body):
     obj = json.loads(body)
     dataText = json.dumps([obj['data']])
     modelInstance = list(self.deserializer(dataText))[0]
     return handler(topicPrefix, modelInstance.object)
 def jsonHandler(topicPrefix, body):
     return handler(topicPrefix,
                    convertToDotDictRecurse(json.loads(body)))
Exemple #39
0
 def json(self, value):
     for k,v in json.loads(value):
         setattr(self, k, v)
Exemple #40
0
 def jsonHandler(topic, body):
     return handler(topic, json.loads(body))
Exemple #41
0
 def getAllLayersInJson(cls):
     return json.dumps([json.loads(layer.json)
                        for layer in LibraryLayer.objects.all()
                        if layer.complete],
                       indent=4)
 def validate(self, dataStr):
     try:
         data = json.loads(dataStr)
     except ValueError, valerr:
         print valerr
         raise JsonRpc2ComplianceException("Could not parse data into json object: %s" % str(valerr))
Exemple #43
0
 def jsonHandler(topicPrefix, body):
     return handler(topicPrefix, convertToDotDictRecurse(json.loads(body)))
Exemple #44
0
 def djangoHandler(topicPrefix, body):
     obj = json.loads(body)
     dataText = json.dumps([obj['data']])
     modelInstance = list(self.deserializer(dataText))[0]
     return handler(topicPrefix, modelInstance.object)