Exemple #1
0
 def make_sort_links_list(self, request_dict):
     """ makes a list of the links for sort options
     """
     if 'sort' in request_dict:
         request_dict.pop('sort')
     order_opts = [
         {'key': 'asc',
          'order': 'ascending'},
         {'key': 'desc',
          'order': 'descending'}
     ]
     for act_sort in self.SORT_OPTIONS:
         if act_sort['opt']:
             # only make sort_options if the 'opt' key is true
             if act_sort['value'] is not None:
                 for order_opt in order_opts:
                     act_sort_val = act_sort['value'] + self.order_sep + order_opt['key']
                     fl = FilterLinks()
                     fl.base_search_link = self.base_search_link
                     fl.base_request_json = json.dumps(request_dict,
                                                       ensure_ascii=False,
                                                       indent=4)
                     fl.spatial_context = self.spatial_context
                     sort_rparams = fl.add_to_request('sort',
                                                      act_sort_val)
                     links = fl.make_request_urls(sort_rparams)
                     current_sort_obj = LastUpdatedOrderedDict()
                     current_sort_obj['id'] = links['html']
                     current_sort_obj['json'] = links['json']
                     current_sort_obj['type'] = act_sort['type']
                     current_sort_obj['label'] = act_sort['label']
                     current_sort_obj['oc-api:sort-order'] = order_opt['order']
                     in_active_list = False
                     for cur_act_sort in self.current_sorting:
                         if act_sort['type'] == cur_act_sort['type'] \
                            and order_opt['order'] == cur_act_sort['oc-api:sort-order']:
                             # the current sort option is ALREADY in use
                             in_active_list = True
                     if in_active_list is False:
                         # only add the sort option if it's not already in use
                         self.sort_links.append(current_sort_obj)
             else:
                 if self.using_default_sorting is False:
                     # only add a link to the default sorting if
                     # we are not currently using it
                     fl = FilterLinks()
                     fl.base_search_link = self.base_search_link
                     fl.base_request_json = json.dumps(request_dict,
                                                       ensure_ascii=False,
                                                       indent=4)
                     fl.spatial_context = self.spatial_context
                     links = fl.make_request_urls(request_dict)
                     current_sort_obj = LastUpdatedOrderedDict()
                     current_sort_obj['id'] = links['html']
                     current_sort_obj['json'] = links['json']
                     current_sort_obj['type'] = act_sort['type']
                     current_sort_obj['label'] = act_sort['label']
                     current_sort_obj['oc-api:sort-order'] = 'descending'
                     self.sort_links.append(current_sort_obj)
Exemple #2
0
 def make_specialized_facet_value_obj(self, solr_facet_key,
                                      solr_facet_value_key,
                                      solr_facet_count):
     """ makes a facet_value obj for specialzied solr faccets """
     fl = FilterLinks()
     fl.base_search_link = self.base_search_link
     fl.base_request_json = self.request_dict_json
     fl.base_r_full_path = self.request_full_path
     fl.spatial_context = self.spatial_context
     new_rparams = fl.add_to_request_by_solr_field(solr_facet_key,
                                                   solr_facet_value_key)
     output = LastUpdatedOrderedDict()
     output['id'] = fl.make_request_url(new_rparams)
     output['json'] = fl.make_request_url(new_rparams, '.json')
     output['label'] = False
     if solr_facet_key == 'item_type':
         if solr_facet_value_key in QueryMaker.TYPE_URIS:
             output['rdfs:isDefinedBy'] = QueryMaker.TYPE_URIS[
                 solr_facet_value_key]
             entity = self.get_entity(output['rdfs:isDefinedBy'])
             if entity is not False:
                 output['label'] = entity.label
     output['count'] = solr_facet_count
     output['slug'] = solr_facet_value_key
     output['data-type'] = 'id'
     return output
 def make_specialized_facet_value_obj(self,
                                      solr_facet_key,
                                      solr_facet_value_key,
                                      solr_facet_count):
     """ makes a facet_value obj for specialzied solr faccets """
     fl = FilterLinks()
     fl.base_search_link = self.base_search_link
     fl.base_request_json = self.request_dict_json
     fl.base_r_full_path = self.request_full_path
     fl.spatial_context = self.spatial_context
     new_rparams = fl.add_to_request_by_solr_field(solr_facet_key,
                                                   solr_facet_value_key)
     output = LastUpdatedOrderedDict()
     output['id'] = fl.make_request_url(new_rparams)
     output['json'] = fl.make_request_url(new_rparams, '.json')
     output['label'] = False
     if solr_facet_key == 'item_type':
         if solr_facet_value_key in QueryMaker.TYPE_URIS:
             output['rdfs:isDefinedBy'] = QueryMaker.TYPE_URIS[solr_facet_value_key]
             entity = self.get_entity(output['rdfs:isDefinedBy'])
             if entity is not False:
                 output['label'] = entity.label
     output['count'] = solr_facet_count
     output['slug'] = solr_facet_value_key
     output['data-type'] = 'id'
     return output
 def make_rel_media_option(self,
                           rel_media_count,
                           rel_media_type,
                           rel_media_options):
     """ makes a facet option for related media """
     if rel_media_count > 0:
         fl = FilterLinks()
         fl.base_search_link = self.base_search_link
         fl.base_request_json = self.request_dict_json
         fl.base_r_full_path = self.request_full_path
         fl.spatial_context = self.spatial_context
         option = LastUpdatedOrderedDict()
         new_rparams = fl.add_to_request_by_solr_field(rel_media_type,
                                                       '1')
         option['id'] = fl.make_request_url(new_rparams)
         option['json'] = fl.make_request_url(new_rparams, '.json')
         if rel_media_type == 'images':
             option['label'] = 'Linked with images'
         elif rel_media_type == 'other-media':
             option['label'] = 'Linked with media (non-image)'
         elif rel_media_type == 'documents':
             option['label'] = 'Linked with documents'
         option['count'] = rel_media_count
         rel_media_options.append(option)
     return rel_media_options
Exemple #5
0
 def add_date_fields(self, solr_json):
     """ adds numeric fields with query options """
     date_fields = []
     date_facet_ranges = self.get_solr_ranges(solr_json, 'date')
     if date_facet_ranges is not False:
         for solr_field_key, ranges in date_facet_ranges.items():
             facet_key_list = solr_field_key.split('___')
             slug = facet_key_list[0].replace('_', '-')
             # check to see if the field is a linkded data field
             # if so, it needs some help with making Filter Links
             linked_field = False
             field_entity = self.get_entity(slug)
             if field_entity is not False:
                 self.add_active_facet_field(slug)
                 if field_entity.item_type == 'uri':
                     linked_field = True
             field = self.get_facet_meta(solr_field_key)
             field['oc-api:min-date'] = ranges['start']
             field['oc-api:max-date'] = ranges['end']
             field['oc-api:gap-date'] = ranges['gap']
             field['oc-api:has-range-options'] = []
             i = -1
             qm = QueryMaker()
             for range_min_key in ranges['counts'][::2]:
                 i += 2
                 solr_count = ranges['counts'][i]
                 fl = FilterLinks()
                 fl.base_search_link = self.base_search_link
                 fl.base_request_json = self.request_dict_json
                 fl.base_r_full_path = self.request_full_path
                 fl.spatial_context = self.spatial_context
                 fl.partial_param_val_match = True
                 dt_end = qm.add_solr_gap_to_date(range_min_key,
                                                  ranges['gap'])
                 range_end = qm.convert_date_to_solr_date(dt_end)
                 solr_range = '[' + range_min_key + ' TO ' + range_end + ' ]'
                 new_rparams = fl.add_to_request('prop', solr_range, slug)
                 range_dict = LastUpdatedOrderedDict()
                 range_dict['id'] = fl.make_request_url(new_rparams)
                 range_dict['json'] = fl.make_request_url(
                     new_rparams, '.json')
                 range_dict['label'] = qm.make_human_readable_date(
                     range_min_key) + ' to ' + qm.make_human_readable_date(
                         range_end)
                 range_dict['count'] = solr_count
                 range_dict['oc-api:min-date'] = range_min_key
                 range_dict['oc-api:max-date'] = range_end
                 field['oc-api:has-range-options'].append(range_dict)
             date_fields.append(field)
     if len(date_fields) > 0 and 'facet' in self.act_responses:
         self.json_ld['oc-api:has-date-facets'] = date_fields
Exemple #6
0
 def add_numeric_fields(self, solr_json):
     """ adds numeric fields with query options """
     num_fields = []
     num_facet_ranges = self.get_solr_ranges(solr_json, 'numeric')
     if num_facet_ranges is not False:
         for solr_field_key, ranges in num_facet_ranges.items():
             facet_key_list = solr_field_key.split('___')
             slug = facet_key_list[0].replace('_', '-')
             # check to see if the field is a linkded data field
             # if so, it needs some help with making Filter Links
             linked_field = False
             field_entity = self.get_entity(slug)
             if field_entity is not False:
                 self.add_active_facet_field(slug)
                 if field_entity.item_type == 'uri':
                     linked_field = True
             field = self.get_facet_meta(solr_field_key)
             field['oc-api:min'] = float(ranges['start'])
             field['oc-api:max'] = float(ranges['end'])
             gap = float(ranges['gap'])
             field['oc-api:gap'] = gap
             field['oc-api:has-range-options'] = []
             i = -1
             for range_min_key in ranges['counts'][::2]:
                 i += 2
                 solr_count = ranges['counts'][i]
                 fl = FilterLinks()
                 fl.base_search_link = self.base_search_link
                 fl.base_request_json = self.request_dict_json
                 fl.base_r_full_path = self.request_full_path
                 fl.spatial_context = self.spatial_context
                 fl.partial_param_val_match = True
                 range_start = float(range_min_key)
                 range_end = range_start + gap
                 solr_range = '[' + str(range_start) + ' TO ' + str(
                     range_end) + ' ]'
                 new_rparams = fl.add_to_request('prop', solr_range, slug)
                 range_dict = LastUpdatedOrderedDict()
                 range_dict['id'] = fl.make_request_url(new_rparams)
                 range_dict['json'] = fl.make_request_url(
                     new_rparams, '.json')
                 range_dict['label'] = str(round(range_start, 3))
                 range_dict['count'] = solr_count
                 range_dict['oc-api:min'] = range_start
                 range_dict['oc-api:max'] = range_end
                 field['oc-api:has-range-options'].append(range_dict)
             num_fields.append(field)
     if len(num_fields) > 0 and 'facet' in self.act_responses:
         self.json_ld['oc-api:has-numeric-facets'] = num_fields
 def add_date_fields(self, solr_json):
     """ adds numeric fields with query options """
     date_fields = []
     date_facet_ranges = self.get_solr_ranges(solr_json, 'date')
     if date_facet_ranges is not False:
         for solr_field_key, ranges in date_facet_ranges.items():
             facet_key_list = solr_field_key.split('___')
             slug = facet_key_list[0].replace('_', '-')
             # check to see if the field is a linkded data field
             # if so, it needs some help with making Filter Links
             linked_field = False
             field_entity = self.get_entity(slug)
             if field_entity is not False:
                 self.add_active_facet_field(slug)
                 if field_entity.item_type == 'uri':
                     linked_field = True
             field = self.get_facet_meta(solr_field_key)
             field['oc-api:min-date'] = ranges['start']
             field['oc-api:max-date'] = ranges['end']
             field['oc-api:gap-date'] = ranges['gap']
             field['oc-api:has-range-options'] = []
             i = -1
             qm = QueryMaker()
             for range_min_key in ranges['counts'][::2]:
                 i += 2
                 solr_count = ranges['counts'][i]
                 fl = FilterLinks()
                 fl.base_search_link = self.base_search_link
                 fl.base_request_json = self.request_dict_json
                 fl.base_r_full_path = self.request_full_path
                 fl.spatial_context = self.spatial_context
                 fl.partial_param_val_match = True
                 dt_end = qm.add_solr_gap_to_date(range_min_key, ranges['gap'])
                 range_end = qm.convert_date_to_solr_date(dt_end)
                 solr_range = '[' + range_min_key + ' TO ' + range_end + ' ]'
                 new_rparams = fl.add_to_request('prop',
                                                 solr_range,
                                                 slug)
                 range_dict = LastUpdatedOrderedDict()
                 range_dict['id'] = fl.make_request_url(new_rparams)
                 range_dict['json'] = fl.make_request_url(new_rparams, '.json')
                 range_dict['label'] = qm.make_human_readable_date(range_min_key) + ' to ' + qm.make_human_readable_date(range_end)
                 range_dict['count'] = solr_count
                 range_dict['oc-api:min-date'] = range_min_key
                 range_dict['oc-api:max-date'] = range_end
                 field['oc-api:has-range-options'].append(range_dict)
             date_fields.append(field)
     if len(date_fields) > 0 and 'facet' in self.act_responses:
         self.json_ld['oc-api:has-date-facets'] = date_fields
 def add_numeric_fields(self, solr_json):
     """ adds numeric fields with query options """
     num_fields = []
     num_facet_ranges = self.get_solr_ranges(solr_json, 'numeric')
     if num_facet_ranges is not False:
         for solr_field_key, ranges in num_facet_ranges.items():
             facet_key_list = solr_field_key.split('___')
             slug = facet_key_list[0].replace('_', '-')
             # check to see if the field is a linkded data field
             # if so, it needs some help with making Filter Links
             linked_field = False
             field_entity = self.get_entity(slug)
             if field_entity is not False:
                 self.add_active_facet_field(slug)
                 if field_entity.item_type == 'uri':
                     linked_field = True
             field = self.get_facet_meta(solr_field_key)
             field['oc-api:min'] = float(ranges['start'])
             field['oc-api:max'] = float(ranges['end'])
             gap = float(ranges['gap'])
             field['oc-api:gap'] = gap
             field['oc-api:has-range-options'] = []
             i = -1
             for range_min_key in ranges['counts'][::2]:
                 i += 2
                 solr_count = ranges['counts'][i]
                 fl = FilterLinks()
                 fl.base_search_link = self.base_search_link
                 fl.base_request_json = self.request_dict_json
                 fl.base_r_full_path = self.request_full_path
                 fl.spatial_context = self.spatial_context
                 fl.partial_param_val_match = True
                 range_start = float(range_min_key)
                 range_end = range_start + gap
                 solr_range = '[' + str(range_start) + ' TO ' + str(range_end) + ' ]'
                 new_rparams = fl.add_to_request('prop',
                                                 solr_range,
                                                 slug)
                 range_dict = LastUpdatedOrderedDict()
                 range_dict['id'] = fl.make_request_url(new_rparams)
                 range_dict['json'] = fl.make_request_url(new_rparams, '.json')
                 range_dict['label'] = str(round(range_start,3))
                 range_dict['count'] = solr_count
                 range_dict['oc-api:min'] = range_start
                 range_dict['oc-api:max'] = range_end
                 field['oc-api:has-range-options'].append(range_dict)
             num_fields.append(field)
     if len(num_fields) > 0 and 'facet' in self.act_responses:
         self.json_ld['oc-api:has-numeric-facets'] = num_fields
 def make_facet_value_obj(self,
                          solr_facet_key,
                          solr_facet_value_key,
                          solr_facet_count):
     """ Makes an last-ordered-dict for a facet """
     facet_key_list = solr_facet_value_key.split('___')
     if len(facet_key_list) == 4:
         # ----------------------------
         # Case where facet values are encoded as:
         # slug___data-type___/uri-item-type/uuid___label
         # ----------------------------
         data_type = facet_key_list[1]
         if 'http://' in facet_key_list[2] or 'https://' in facet_key_list[2]:
             is_linked_data = True
         else:
             is_linked_data = False
         fl = FilterLinks()
         fl.base_search_link = self.base_search_link
         fl.base_request_json = self.request_dict_json
         fl.base_r_full_path = self.request_full_path
         fl.spatial_context = self.spatial_context
         fl.partial_param_val_match = is_linked_data  # allow partial matches of parameters.
         output = LastUpdatedOrderedDict()
         slug = facet_key_list[0]
         new_rparams = fl.add_to_request_by_solr_field(solr_facet_key,
                                                       slug)
         output['id'] = fl.make_request_url(new_rparams)
         output['json'] = fl.make_request_url(new_rparams, '.json')
         if is_linked_data:
             output['rdfs:isDefinedBy'] = facet_key_list[2]
         else:
             output['rdfs:isDefinedBy'] = settings.CANONICAL_HOST + facet_key_list[2]
         output['label'] = facet_key_list[3]
         output['count'] = solr_facet_count
         output['slug'] = slug
         output['data-type'] = data_type
     else:
         # ----------------------------
         # Sepcilized cases of non-encoded facet values
         # ----------------------------
         output = self.make_specialized_facet_value_obj(solr_facet_key,
                                                        solr_facet_value_key,
                                                        solr_facet_count)
     return output
Exemple #10
0
 def make_facet_value_obj(self, solr_facet_key, solr_facet_value_key,
                          solr_facet_count):
     """ Makes an last-ordered-dict for a facet """
     facet_key_list = solr_facet_value_key.split('___')
     if len(facet_key_list) == 4:
         # ----------------------------
         # Case where facet values are encoded as:
         # slug___data-type___/uri-item-type/uuid___label
         # ----------------------------
         data_type = facet_key_list[1]
         if 'http://' in facet_key_list[2] or 'https://' in facet_key_list[
                 2]:
             is_linked_data = True
         else:
             is_linked_data = False
         fl = FilterLinks()
         fl.base_search_link = self.base_search_link
         fl.base_request_json = self.request_dict_json
         fl.base_r_full_path = self.request_full_path
         fl.spatial_context = self.spatial_context
         fl.partial_param_val_match = is_linked_data  # allow partial matches of parameters.
         output = LastUpdatedOrderedDict()
         slug = facet_key_list[0]
         new_rparams = fl.add_to_request_by_solr_field(solr_facet_key, slug)
         output['id'] = fl.make_request_url(new_rparams)
         output['json'] = fl.make_request_url(new_rparams, '.json')
         if is_linked_data:
             output['rdfs:isDefinedBy'] = facet_key_list[2]
         else:
             output[
                 'rdfs:isDefinedBy'] = settings.CANONICAL_HOST + facet_key_list[
                     2]
         output['label'] = facet_key_list[3]
         output['count'] = solr_facet_count
         output['slug'] = slug
         output['data-type'] = data_type
     else:
         # ----------------------------
         # Sepcilized cases of non-encoded facet values
         # ----------------------------
         output = self.make_specialized_facet_value_obj(
             solr_facet_key, solr_facet_value_key, solr_facet_count)
     return output
Exemple #11
0
    def make_paging_links(self, start, rows, ini_request_dict_json):
        """ makes links for paging for start, rows, with
            an initial request dict json string

            a big of a hassle to avoid memory errors with FilterLinks()
        """
        start = int(start)
        start = str(start)
        rows = str(rows)
        fl = FilterLinks()
        fl.base_search_link = self.base_search_link
        fl.base_request_json = ini_request_dict_json
        fl.spatial_context = self.spatial_context
        fl.remove_start_param = False
        start_rparams = fl.add_to_request('start', start)
        start_rparams_json = json.dumps(start_rparams,
                                        ensure_ascii=False,
                                        indent=4)
        fl.base_request_json = start_rparams_json
        new_rparams = fl.add_to_request('rows', rows)
        return fl.make_request_urls(new_rparams)
Exemple #12
0
 def make_rel_media_option(self, rel_media_count, rel_media_type,
                           rel_media_options):
     """ makes a facet option for related media """
     if rel_media_count > 0:
         fl = FilterLinks()
         fl.base_search_link = self.base_search_link
         fl.base_request_json = self.request_dict_json
         fl.base_r_full_path = self.request_full_path
         fl.spatial_context = self.spatial_context
         option = LastUpdatedOrderedDict()
         new_rparams = fl.add_to_request_by_solr_field(rel_media_type, '1')
         option['id'] = fl.make_request_url(new_rparams)
         option['json'] = fl.make_request_url(new_rparams, '.json')
         if rel_media_type == 'images':
             option['label'] = 'Linked with images'
         elif rel_media_type == 'other-media':
             option['label'] = 'Linked with media (non-image)'
         elif rel_media_type == 'documents':
             option['label'] = 'Linked with documents'
         option['count'] = rel_media_count
         rel_media_options.append(option)
     return rel_media_options
    def make_paging_links(self, start, rows, ini_request_dict_json):
        """ makes links for paging for start, rows, with
            an initial request dict json string

            a big of a hassle to avoid memory errors with FilterLinks()
        """
        start = int(start)
        start = str(start)
        rows = str(rows)
        fl = FilterLinks()
        fl.base_search_link = self.base_search_link
        fl.base_request_json = ini_request_dict_json
        fl.spatial_context = self.spatial_context
        fl.remove_start_param = False
        start_rparams = fl.add_to_request('start',
                                          start)
        start_rparams_json = json.dumps(start_rparams,
                                        ensure_ascii=False,
                                        indent=4)
        fl.base_request_json = start_rparams_json
        new_rparams = fl.add_to_request('rows',
                                        rows)
        return fl.make_request_urls(new_rparams)
Exemple #14
0
 def add_filters_json(self, request_dict):
     """ adds JSON describing search filters """
     fl = FilterLinks()
     fl.base_search_link = self.base_search_link
     filters = []
     string_fields = []  # so we have an interface for string searches
     i = 0
     for param_key, param_vals in request_dict.items():
         if param_key == 'path':
             if param_vals:
                 i += 1
                 f_entity = self.m_cache.get_entity(param_vals)
                 label = http.urlunquote_plus(param_vals)
                 act_filter = LastUpdatedOrderedDict()
                 act_filter['id'] = '#filter-' + str(i)
                 act_filter['oc-api:filter'] = 'Context'
                 act_filter['label'] = label.replace('||', ' OR ')
                 if f_entity:
                     act_filter['rdfs:isDefinedBy'] = f_entity.uri
                 # generate a request dict without the context filter
                 rem_request = fl.make_request_sub(request_dict,
                                                   param_key,
                                                   param_vals)
                 act_filter['oc-api:remove'] = fl.make_request_url(rem_request)
                 act_filter['oc-api:remove-json'] = fl.make_request_url(rem_request, '.json')
                 filters.append(act_filter)
         else:
             for param_val in param_vals:
                 i += 1
                 remove_geodeep = False
                 act_filter = LastUpdatedOrderedDict()
                 act_filter['id'] = '#filter-' + str(i)
                 if self.hierarchy_delim in param_val:
                     all_vals = param_val.split(self.hierarchy_delim)
                 else:
                     all_vals = [param_val]
                 if param_key == 'proj':
                     # projects, only care about the last item in the parameter value
                     act_filter['oc-api:filter'] = 'Project'
                     label_dict = self.make_filter_label_dict(all_vals[-1])
                     act_filter['label'] = label_dict['label']
                     if len(label_dict['entities']) == 1:
                         act_filter['rdfs:isDefinedBy'] = label_dict['entities'][0].uri
                 elif param_key == 'prop':
                     # prop, the first item is the filter-label
                     # the last is the filter
                     act_filter['label'] = False
                     if len(all_vals) < 2:
                         act_filter['oc-api:filter'] = 'Description'
                         act_filter['oc-api:filter-slug'] = all_vals[0]
                     else:
                         filt_dict = self.make_filter_label_dict(all_vals[0])
                         act_filter['oc-api:filter'] = filt_dict['label']
                         if 'slug' in filt_dict:
                             act_filter['oc-api:filter-slug'] = filt_dict['slug']
                         if filt_dict['data-type'] == 'string':
                             act_filter['label'] = 'Search Term: \'' + all_vals[-1] + '\''
                     if act_filter['label'] is False:
                         label_dict = self.make_filter_label_dict(all_vals[-1])
                         act_filter['label'] = label_dict['label']
                 elif param_key == 'type':
                     act_filter['oc-api:filter'] = 'Open Context Type'
                     if all_vals[0] in QueryMaker.TYPE_MAPPINGS:
                         type_uri = QueryMaker.TYPE_MAPPINGS[all_vals[0]]
                         label_dict = self.make_filter_label_dict(type_uri)
                         act_filter['label'] = label_dict['label']
                     else:
                         act_filter['label'] = all_vals[0]
                 elif param_key == 'q':
                     act_filter['oc-api:filter'] = self.TEXT_SEARCH_TITLE
                     act_filter['label'] = 'Search Term: \'' + all_vals[0] + '\''
                 elif param_key == 'id':
                     act_filter['oc-api:filter'] = 'Identifier Lookup'
                     act_filter['label'] = 'Identifier: \'' + all_vals[0] + '\''
                 elif param_key == 'form-chronotile':
                     act_filter['oc-api:filter'] = 'Time of formation, use, or life'
                     chrono = ChronoTile()
                     dates = chrono.decode_path_dates(all_vals[0])
                     if isinstance(dates, dict):
                         act_filter['label'] = 'Time range: ' + str(dates['earliest_bce'])
                         act_filter['label'] += ' to ' + str(dates['latest_bce'])
                 elif param_key == 'form-start':
                     act_filter['oc-api:filter'] = 'Earliest formation, use, or life date'
                     try:
                         val_date = int(float(all_vals[0]))
                     except:
                         val_date = False
                     if val_date is False:
                         act_filter['label'] = '[Invalid year]'
                     elif val_date < 0:
                         act_filter['label'] = str(val_date * -1) + ' BCE'
                     else:
                         act_filter['label'] = str(val_date) + ' CE'
                 elif param_key == 'form-stop':
                     act_filter['oc-api:filter'] = 'Latest formation, use, or life date'
                     try:
                         val_date = int(float(all_vals[0]))
                     except:
                         val_date = False
                     if val_date is False:
                         act_filter['label'] = '[Invalid year]'
                     elif val_date < 0:
                         act_filter['label'] = str(val_date * -1) + ' BCE'
                     else:
                         act_filter['label'] = str(val_date) + ' CE'
                 elif param_key == 'disc-geotile':
                     act_filter['oc-api:filter'] = 'Location of discovery or observation'
                     act_filter['label'] = self.make_geotile_filter_label(all_vals[0])
                     remove_geodeep = True
                 elif param_key == 'disc-bbox':
                     act_filter['oc-api:filter'] = 'Location of discovery or observation'
                     act_filter['label'] = self.make_bbox_filter_label(all_vals[0])
                     remove_geodeep = True
                 elif param_key == 'images':
                     act_filter['oc-api:filter'] = 'Has related media'
                     act_filter['label'] = 'Linked to images'
                 elif param_key == 'other-media':
                     act_filter['oc-api:filter'] = 'Has related media'
                     act_filter['label'] = 'Linked to media (other than images)'
                 elif param_key == 'documents':
                     act_filter['oc-api:filter'] = 'Has related media'
                     act_filter['label'] = 'Linked to documents'
                 elif param_key == 'dc-subject':
                     act_filter['oc-api:filter'] = 'Has subject metadata'
                     label_dict = self.make_filter_label_dict(all_vals[-1])
                     if len(label_dict['label']) > 0:
                         act_filter['label'] = label_dict['label']
                     if 'tdar' == all_vals[-1] or 'tdar*' == all_vals[-1]:
                         act_filter['label'] = 'tDAR defined metadata record(s)'
                     if len(label_dict['entities']) == 1:
                         act_filter['rdfs:isDefinedBy'] = label_dict['entities'][0].uri
                         if label_dict['entities'][0].vocabulary is not False:
                             act_filter['label'] += ' in ' + label_dict['entities'][0].vocabulary
                 elif param_key == 'dc-spatial':
                     act_filter['oc-api:filter'] = 'Has spatial metadata'
                     label_dict = self.make_filter_label_dict(all_vals[-1])
                     if len(label_dict['label']) > 0:
                         act_filter['label'] = label_dict['label']
                     if len(label_dict['entities']) == 1:
                         act_filter['rdfs:isDefinedBy'] = label_dict['entities'][0].uri
                         if label_dict['entities'][0].vocabulary is not False:
                             act_filter['label'] += ' in ' + label_dict['entities'][0].vocabulary
                 elif param_key == 'dc-coverage':
                     act_filter['oc-api:filter'] = 'Has coverage / period metadata'
                     label_dict = self.make_filter_label_dict(all_vals[-1])
                     if len(label_dict['label']) > 0:
                         act_filter['label'] = label_dict['label']
                     if len(label_dict['entities']) == 1:
                         act_filter['rdfs:isDefinedBy'] = label_dict['entities'][0].uri
                         if label_dict['entities'][0].vocabulary is not False:
                             act_filter['label'] += ' in ' + label_dict['entities'][0].vocabulary
                 elif param_key == 'dc-temporal':
                     act_filter['oc-api:filter'] = 'Has temporal coverage'
                     label_dict = self.make_filter_label_dict(all_vals[-1])
                     if len(label_dict['label']) > 0:
                         act_filter['label'] = label_dict['label']
                         if len(label_dict['entities']) == 1: 
                             if label_dict['entities'][0].entity_type == 'vocabulary':
                                 act_filter['label'] = 'Concepts defined by: ' + label_dict['label']
                         elif 'periodo' in all_vals[-1]:
                             act_filter['label'] = 'PeriodO defined concepts'
                     if len(label_dict['entities']) == 1:
                         act_filter['rdfs:isDefinedBy'] = label_dict['entities'][0].uri
                         if label_dict['entities'][0].vocabulary is not False\
                            and label_dict['entities'][0].vocabulary != label_dict['label']:
                             act_filter['label'] += ' in ' + label_dict['entities'][0].vocabulary
                 elif param_key == 'obj':
                     act_filter['oc-api:filter'] = 'Links (in some manner) to object'
                     label_dict = self.make_filter_label_dict(all_vals[-1])
                     if len(label_dict['label']) > 0:
                         act_filter['label'] = label_dict['label']
                     if len(label_dict['entities']) == 1:
                         act_filter['rdfs:isDefinedBy'] = label_dict['entities'][0].uri
                         if label_dict['entities'][0].vocabulary is not False:
                             act_filter['label'] += ' in ' + label_dict['entities'][0].vocabulary
                 elif param_key == 'dc-isReferencedBy':
                     act_filter['oc-api:filter'] = 'Is referenced by'
                     label_dict = self.make_filter_label_dict(all_vals[-1])
                     if len(label_dict['label']) > 0:
                         act_filter['label'] = label_dict['label']
                     if len(label_dict['entities']) == 1:
                         act_filter['rdfs:isDefinedBy'] = label_dict['entities'][0].uri
                         if label_dict['entities'][0].vocabulary is not False\
                            and label_dict['entities'][0].vocab_uri != label_dict['entities'][0].uri:
                             act_filter['label'] += ' in ' + label_dict['entities'][0].vocabulary
                 elif param_key == 'linked' and all_vals[-1] == 'dinaa-cross-ref':
                     act_filter['oc-api:filter'] = 'Has cross references'
                     act_filter['label'] = 'Links to, or with, DINAA curated site files'
                 else:
                     act_filter = False
                 if act_filter is not False:
                     rem_request = fl.make_request_sub(request_dict,
                                                       param_key,
                                                       param_val)
                     if 'geodeep' in rem_request and remove_geodeep:
                         rem_request.pop('geodeep', None)    
                     act_filter['oc-api:remove'] = fl.make_request_url(rem_request)
                     act_filter['oc-api:remove-json'] = fl.make_request_url(rem_request, '.json')
                     filters.append(act_filter)
     return filters
Exemple #15
0
 def add_text_fields(self):
     """ adds text fields with query options """
     text_fields = []
     # first add a general key-word search option
     fl = FilterLinks()
     fl.base_search_link = self.base_search_link
     fl.base_request_json = self.request_dict_json
     fl.base_r_full_path = self.request_full_path
     fl.spatial_context = self.spatial_context
     q_request_dict = self.request_dict
     if 'q' not in q_request_dict:
         q_request_dict['q'] = []
     param_vals = q_request_dict['q']
     if len(param_vals) < 1:
         search_term = None
     else:
         search_term = param_vals[0]
     field = LastUpdatedOrderedDict()
     field['id'] = '#textfield-keyword-search'
     field['label'] = 'General Keyword Search'
     field['oc-api:search-term'] = search_term
     if search_term is False or search_term is None:
         new_rparams = fl.add_to_request_by_solr_field('q', '{SearchTerm}')
         field['oc-api:template'] = fl.make_request_url(new_rparams)
         field['oc-api:template-json'] = fl.make_request_url(
             new_rparams, '.json')
     else:
         param_search = param_vals[0].replace(search_term, '{SearchTerm}')
         rem_request = fl.make_request_sub(q_request_dict, 'q', search_term,
                                           '{SearchTerm}')
         field['oc-api:template'] = fl.make_request_url(rem_request)
         field['oc-api:template-json'] = fl.make_request_url(
             rem_request, '.json')
     text_fields.append(field)
     # now add an option looking in properties
     if 'prop' in self.request_dict:
         param_vals = self.request_dict['prop']
         for param_val in param_vals:
             if self.hierarchy_delim in param_val:
                 all_vals = param_val.split(self.hierarchy_delim)
             else:
                 all_vals = [param_val]
             if len(all_vals) < 2:
                 check_field = all_vals[0]
                 search_term = None  # no search term
             else:
                 check_field = all_vals[
                     -2]  # penultimate value is the field
                 search_term = all_vals[-1]  # last value is search term
             check_dict = self.make_filter_label_dict(check_field)
             if check_dict['data-type'] == 'string':
                 fl = FilterLinks()
                 fl.base_search_link = self.base_search_link
                 fl.base_request_json = self.request_dict_json
                 fl.base_r_full_path = self.request_full_path
                 fl.spatial_context = self.spatial_context
                 field = LastUpdatedOrderedDict()
                 field['id'] = '#textfield-' + check_dict['slug']
                 field['label'] = check_dict['label']
                 field['oc-api:search-term'] = search_term
                 if len(check_dict['entities']) == 1:
                     field['rdfs:isDefinedBy'] = check_dict['entities'][
                         0].uri
                 if search_term is False or search_term is None:
                     param_search = param_val + self.hierarchy_delim + '{SearchTerm}'
                     new_rparams = fl.add_to_request_by_solr_field(
                         'prop', param_search)
                     field['oc-api:template'] = fl.make_request_url(
                         new_rparams)
                     field['oc-api:template-json'] = fl.make_request_url(
                         new_rparams, '.json')
                 else:
                     param_search = param_val.replace(
                         search_term, '{SearchTerm}')
                     rem_request = fl.make_request_sub(
                         self.request_dict, 'prop', param_val, param_search)
                     field['oc-api:template'] = fl.make_request_url(
                         rem_request)
                     field['oc-api:template-json'] = fl.make_request_url(
                         rem_request, '.json')
                 text_fields.append(field)
     if len(text_fields) > 0:
         self.json_ld['oc-api:has-text-search'] = text_fields
 def add_text_fields(self):
     """ adds text fields with query options """
     text_fields = []
     # first add a general key-word search option
     fl = FilterLinks()
     fl.base_search_link = self.base_search_link
     fl.base_request_json = self.request_dict_json
     fl.base_r_full_path = self.request_full_path
     fl.spatial_context = self.spatial_context
     q_request_dict = self.request_dict
     if 'q' not in q_request_dict:
         q_request_dict['q'] = []
     param_vals = q_request_dict['q']
     if len(param_vals) < 1:
         search_term = None
     else:
         search_term = param_vals[0]
     field = LastUpdatedOrderedDict()
     field['id'] = '#textfield-keyword-search'
     field['label'] = 'General Keyword Search'
     field['oc-api:search-term'] = search_term
     if search_term is False or search_term is None:
         new_rparams = fl.add_to_request_by_solr_field('q',
                                                       '{SearchTerm}')
         field['oc-api:template'] = fl.make_request_url(new_rparams)
         field['oc-api:template-json'] = fl.make_request_url(new_rparams, '.json')
     else:
         param_search = param_vals[0].replace(search_term, '{SearchTerm}')
         rem_request = fl.make_request_sub(q_request_dict,
                                           'q',
                                           search_term,
                                           '{SearchTerm}')
         field['oc-api:template'] = fl.make_request_url(rem_request)
         field['oc-api:template-json'] = fl.make_request_url(rem_request, '.json')
     text_fields.append(field)
     # now add an option looking in properties
     if 'prop' in self.request_dict:
         param_vals = self.request_dict['prop']
         for param_val in param_vals:
             if self.hierarchy_delim in param_val:
                 all_vals = param_val.split(self.hierarchy_delim)
             else:
                 all_vals = [param_val]
             if len(all_vals) < 2:
                 check_field = all_vals[0]
                 search_term = None  # no search term
             else:
                 check_field = all_vals[-2]  # penultimate value is the field
                 search_term = all_vals[-1]  # last value is search term
             check_dict = self.make_filter_label_dict(check_field)
             if check_dict['data-type'] == 'string':
                 fl = FilterLinks()
                 fl.base_search_link = self.base_search_link
                 fl.base_request_json = self.request_dict_json
                 fl.base_r_full_path = self.request_full_path
                 fl.spatial_context = self.spatial_context
                 field = LastUpdatedOrderedDict()
                 field['id'] = '#textfield-' + check_dict['slug']
                 field['label'] = check_dict['label']
                 field['oc-api:search-term'] = search_term
                 if len(check_dict['entities']) == 1:
                     field['rdfs:isDefinedBy'] = check_dict['entities'][0].uri
                 if search_term is False or search_term is None:
                     param_search = param_val + self.hierarchy_delim + '{SearchTerm}'
                     new_rparams = fl.add_to_request_by_solr_field('prop',
                                                                   param_search)
                     field['oc-api:template'] = fl.make_request_url(new_rparams)
                     field['oc-api:template-json'] = fl.make_request_url(new_rparams, '.json')
                 else:
                     param_search = param_val.replace(search_term, '{SearchTerm}')
                     rem_request = fl.make_request_sub(self.request_dict,
                                                       'prop',
                                                       param_val,
                                                       param_search)
                     field['oc-api:template'] = fl.make_request_url(rem_request)
                     field['oc-api:template-json'] = fl.make_request_url(rem_request, '.json')
                 text_fields.append(field)
     if len(text_fields) > 0:
         self.json_ld['oc-api:has-text-search'] = text_fields