Ejemplo n.º 1
0
 def _get_cache_entity_db(self, identifier):
     """Get an entity object from the database, if successful, cache it."""
     found = False
     entity = Entity()
     entity.get_context = True
     entity.get_thumbnail = True
     found = entity.dereference(identifier)
     if not found:
         # case of linked data slugs
         found = entity.dereference(identifier, identifier)
     if not found:
         return False
     else:
         # cache the entity now
         return self.cache_entity(entity)
Ejemplo n.º 2
0
 def get_field_parent_entity(self, field_num):
     """ Get's a parent entity named for a given field """
     parent_entity_found = False
     self.field_parent_entities[field_num] = False
     parent_anno = ImportFieldAnnotation.objects\
                                        .filter(source_id=self.source_id,
                                                field_num=field_num,
                                                predicate=ImportFieldAnnotation.PRED_CONTAINED_IN)[:1]
     if len(parent_anno) > 0:
         ent = Entity()
         ent.get_context = True
         found = ent.dereference(parent_anno[0].object_uuid)
         if found:
             self.field_parent_entities[field_num] = ent
             parent_entity_found = True
     return parent_entity_found
Ejemplo n.º 3
0
 def _get_cache_entity_db(self, identifier):
     """Get an entity object from the database, if successful, cache it."""
     found = False
     entity = Entity()
     entity.get_context = True
     entity.get_thumbnail = True
     entity.get_ld_data_type = True
     found = entity.dereference(identifier)
     if not found:
         # case of linked data slugs
         found = entity.dereference(identifier, identifier)
     if not found:
         return False
     else:
         # cache the entity now
         return self.cache_entity(entity)
Ejemplo n.º 4
0
 def _get_cache_context_entity_db(self, context):
     """ dereference an item by spatial context path """
     found = False
     entity = Entity()
     entity.get_context = True
     found = entity.context_dereference(context)
     # print('look for ' + context)
     if not found:
         # maybe we're passing a uuid, uri or slug?
         found = entity.dereference(context)
     if not found:
         # case of linked data slugs
         found = entity.dereference(context, context)
     if not found:
         # give up, we can't find it.
         return False
     # cache the entity now
     return self.cache_entity(entity)
Ejemplo n.º 5
0
 def _get_cache_context_entity_db(self, context):
     """ dereference an item by spatial context path """
     found = False
     entity = Entity()
     entity.get_context = True
     found = entity.context_dereference(context)
     # print('look for ' + context)
     if not found:
         # maybe we're passing a uuid, uri or slug?
         found = entity.dereference(context)
     if not found:
         # case of linked data slugs
         found = entity.dereference(context, context)
     if not found:
         # give up, we can't find it.
         return False
     else:
         # cache the entity now
         return self.cache_entity(entity)
Ejemplo n.º 6
0
 def get_context_entity_db(self, identifier):
     """ returns an entity object
         via database calls
     """
     output = False
     entity = Entity()
     entity.get_context = True
     found = entity.dereference(identifier)
     if found is False:
         # case of linked data slugs
         found = entity.dereference(identifier, identifier)
     if found:
         self.context_entities[identifier] = entity
         if entity.uuid is not False:
             self.context_entities[entity.uuid] = entity
         if entity.slug is not False:
             self.context_entities[entity.slug] = entity
         output = entity
     return output
Ejemplo n.º 7
0
 def add_to_request(self, param, new_value, add_to_value=None):
     """ adds to the new request object a parameter and value """
     if self.base_request_json is not False:
         # start of with JSON encoded base request parameters
         new_rparams = json.loads(self.base_request_json)
     elif self.base_r_full_path is not False:
         # start of with parsing a URL string
         new_rparams = self.make_base_params_from_url(self.base_r_full_path)
     elif self.base_request is not False:
         # start with a dictionary object of the base request
         # for some reason this often leads to memory errors
         new_rparams = self.base_request
     else:
         new_rparams = {}
     if 'start' in new_rparams and self.remove_start_param:
         # remove paging information when composing a new link
         new_rparams.pop('start', None)
     if param == 'path':
         entity = Entity()
         entity.get_context = True
         found = entity.dereference(new_value)
         if found:
             # convert the (slug) value into a context path
             new_value = entity.context
     if param not in new_rparams:
         if param == 'path':
             new_rparams[param] = new_value
         else:
             new_rparams[param] = [new_value]
     else:
         if param == 'path':
             new_rparams['path'] = new_value
         else:
             if add_to_value is not None:
                 new_list = []
                 old_found = False
                 for old_val in new_rparams[param]:
                     old_prefix = self.remove_solr_part(old_val)
                     first_last_old_val = False
                     if self.hierarchy_delim in old_val:
                         old_val_ex = old_val.split(self.hierarchy_delim)
                         if len(old_val_ex) > 2:
                             first_last_old_val = old_val_ex[0]
                             first_last_old_val += self.hierarchy_delim
                             first_last_old_val += old_val_ex[-1]
                     if old_val == add_to_value:
                         old_found = True
                         new_list_val = old_val + self.hierarchy_delim + new_value
                     elif old_prefix == add_to_value:
                         old_found = True
                         new_list_val = old_prefix + self.hierarchy_delim + new_value
                     elif first_last_old_val == add_to_value:
                         old_found = True
                         new_list_val = old_prefix + self.hierarchy_delim + new_value
                     else:
                         new_list_val = old_val
                     new_list.append(new_list_val)
                 if old_found is False:
                     if self.partial_param_val_match:
                         for old_val in new_rparams[param]:
                             if add_to_value in old_val:
                                 old_found = True
                                 old_prefix = self.remove_solr_part(old_val)
                                 new_list_val = old_prefix + self.hierarchy_delim + new_value
                                 # add the new item
                                 new_list.append(new_list_val)
                                 # remove the old
                                 new_list.remove(old_val)
                 new_rparams[param] = new_list
                 if old_found is False:
                     new_rparams[param].append(new_value)
             else:
                 new_rparams[param].append(new_value)
     return new_rparams
Ejemplo n.º 8
0
 def add_to_request(self,
                    param,
                    new_value,
                    add_to_value=None):
     """ adds to the new request object a parameter and value """
     if self.base_request_json is not False:
         # start of with JSON encoded base request parameters
         new_rparams = json.loads(self.base_request_json)
     elif self.base_r_full_path is not False:
         # start of with parsing a URL string
         new_rparams = self.make_base_params_from_url(self.base_r_full_path)
     elif self.base_request is not False:
         # start with a dictionary object of the base request
         # for some reason this often leads to memory errors
         new_rparams = self.base_request
     else:
         new_rparams = {}
     if 'start' in new_rparams and self.remove_start_param:
         # remove paging information when composing a new link
         new_rparams.pop('start', None)
     if param == 'path':
         entity = Entity()
         entity.get_context = True
         found = entity.dereference(new_value)
         if found:
             # convert the (slug) value into a context path
             new_value = entity.context
     if param not in new_rparams:
         if param == 'path':
             new_rparams[param] = new_value
         else:
             new_rparams[param] = [new_value]
     else:
         if param == 'path':
             new_rparams['path'] = new_value
         else:
             if add_to_value is not None:
                 new_list = []
                 old_found = False
                 for old_val in new_rparams[param]:
                     old_prefix = self.remove_solr_part(old_val)
                     first_last_old_val = False
                     if self.hierarchy_delim in old_val:
                         old_val_ex = old_val.split(self.hierarchy_delim)
                         if len(old_val_ex) > 2:
                             first_last_old_val = old_val_ex[0]
                             first_last_old_val += self.hierarchy_delim
                             first_last_old_val += old_val_ex[-1]
                     if old_val == add_to_value:
                         old_found = True
                         new_list_val = old_val + self.hierarchy_delim + new_value
                     elif old_prefix == add_to_value:
                         old_found = True
                         new_list_val = old_prefix + self.hierarchy_delim + new_value
                     elif first_last_old_val == add_to_value:
                         old_found = True
                         new_list_val = old_prefix + self.hierarchy_delim + new_value
                     else:
                         new_list_val = old_val
                     new_list.append(new_list_val)
                 if old_found is False:
                     if self.partial_param_val_match:
                         for old_val in new_rparams[param]:
                             if add_to_value in old_val:
                                 old_found = True
                                 old_prefix = self.remove_solr_part(old_val)
                                 new_list_val = old_prefix + self.hierarchy_delim + new_value
                                 # add the new item
                                 new_list.append(new_list_val)
                                 # remove the old
                                 new_list.remove(old_val)
                 new_rparams[param] = new_list
                 if old_found is False:
                     new_rparams[param].append(new_value)
             else:
                 new_rparams[param].append(new_value)
     return new_rparams