Example #1
0
    def get_nodes(self, class_name, params):
        """ get nodes """
        # determine if response is going to be JSON or GeoJSON
        if 'geojson' in class_name.lower():
            response_format = 'geojson'
            SerializerClass = OpenLaborGeoSerializer
        else:
            response_format = 'json'
            SerializerClass = OpenLaborSerializer

        layer_name = self.layer.name
        cache_key = 'layer_%s_nodes.%s' % (self.layer.id, response_format)
        serialized_nodes = cache.get(cache_key, False)

        if serialized_nodes is False:
            try:
                response = requests.get(self.get_url,
                                        verify=self.config.get(
                                            'verify_SSL', True))
            except requests.exceptions.ConnectionError as e:
                return {
                    'error': _('external layer not reachable'),
                    'exception': list(e.message)
                }

            try:
                response.data = json.loads(response.content)
            except json.scanner.JSONDecodeError as e:
                return {
                    'error':
                    _('external layer is experiencing some issues because it returned invalid data'
                      ),
                    'exception':
                    list(e)
                }

            nodes = []

            # loop over all the entries and convert to nodeshot format
            for job in response.data:
                # skip records which do not have geographic information
                if not job.get('latitude', False) or not job.get(
                        'longitude', False):
                    continue
                # convert response in nodeshot format
                node_dictionary = self.to_nodeshot(job)
                # create Node model instance (needed for rest_framework serializer)
                node = Node(**node_dictionary)
                node.layer_name = layer_name  # hack to avoid too many queries to get layer name each time
                nodes.append(node)

            # serialize with rest framework to achieve consistency
            serialized_nodes = SerializerClass(nodes, many=True).data
            cache.set(cache_key, serialized_nodes, 86400)  # cache for 1 day

        return serialized_nodes
Example #2
0
 def get_nodes(self, class_name, params):
     """ get nodes """
     # determine if response is going to be JSON or GeoJSON
     if 'geojson' in class_name.lower():
         response_format = 'geojson'
         SerializerClass = OpenLaborGeoSerializer
     else:
         response_format = 'json'
         SerializerClass = OpenLaborSerializer
     
     layer_name = self.layer.name
     cache_key = 'layer_%s_nodes.%s' % (self.layer.id, response_format)
     serialized_nodes = cache.get(cache_key, False)
     
     if serialized_nodes is False:            
         try:
             response = requests.get(
                 self.get_url,
                 verify=self.config.get('verify_SSL', True)
             )
         except requests.exceptions.ConnectionError as e:
             return {
                 'error': _('external layer not reachable'),
                 'exception': list(e.message)
             }
         
         try:
             response.data = json.loads(response.content)
         except json.scanner.JSONDecodeError as e:
             return {
                 'error': _('external layer is experiencing some issues because it returned invalid data'),
                 'exception': list(e)
             }
         
         nodes = []
         
         # loop over all the entries and convert to nodeshot format
         for job in response.data:
             # skip records which do not have geographic information
             if not job.get('latitude', False) or not job.get('longitude', False):
                 continue
             # convert response in nodeshot format
             node_dictionary = self.to_nodeshot(job)
             # create Node model instance (needed for rest_framework serializer)
             node = Node(**node_dictionary)
             node.layer_name = layer_name  # hack to avoid too many queries to get layer name each time
             nodes.append(node)
         
         # serialize with rest framework to achieve consistency
         serialized_nodes = SerializerClass(nodes, many=True).data
         cache.set(cache_key, serialized_nodes, 86400)  # cache for 1 day
     
     return serialized_nodes