Esempio n. 1
0
    def process_subject(self, subject, mapped_type):
        """Prepare a document from a Subject by browsing the RDF graph
        :param subject: subject URIRef
        :return dict
        """
        title = self.graph.value(subject, DC.title)
        if not title:
            return None
        else:
            title = title.toPython()

        if subject in self.merged_things:
            logger.info('Ignoring {subject} -- merged with Thing already'.format(subject=subject.toPython()))
            return None

        doc = {}
        doc['name'] = title
        doc['id'] = self._get_formatted_oxpoints_id(subject)

        # storing all the types of the Thing
        types = []
        if not isinstance(mapped_type, list):
            types.append(mapped_type)
        else:
            types.extend(mapped_type)

        sort_label = self.graph.value(subject, OpenVocab.SORT_LABEL)
        if sort_label:
            doc['name_sort'] = sort_label.toPython()
        else:
            doc['name_sort'] = title

        ids = set()
        ids.add(doc['id'])
        ids.update(self._get_identifiers_for_subject(subject))

        parent_of = set()
        child_of = set()

        main_site = self.graph.value(subject, OxPoints.primaryPlace)
        main_site_id = None

        # attempt to merge a Thing and its Site if it has one
        if main_site:
            site_title = self.graph.value(main_site, DC.title)
            if site_title:
                site_title = site_title.toPython()

                # if the main_site has the same name that the Thing, then merge
                # them and do not import the Site by itself
                if site_title == title:
                    main_site_id = self._get_formatted_oxpoints_id(main_site)
                    ids.add(main_site_id)
                    ids.update(self._get_identifiers_for_subject(main_site))

                    # try to add the type of the site to the Thing
                    # e.g. Sheldonian is both a Department and a Building after merge
                    main_site_type = self.graph.value(main_site, RDF.type)
                    if main_site_type in INDEXED_TYPES:
                        main_site_mapped_types = INDEXED_TYPES.get(main_site_type)
                        if type(main_site_mapped_types) is list:
                            types.extend(main_site_mapped_types)
                        else:
                            types.append(main_site_mapped_types)

                    self.merged_things.append(main_site)
                    # adding accessibility data of the site to the doc
                    # this happens when a building == an organisation
                    # e.g. Sackler Library -- makes sense to merge accessibility data
                    doc.update(self._handle_accessibility_data(main_site))
                    doc.update(self._handle_mapped_properties(main_site))
                    if self.static_files_dir:
                        doc['files'] = self._handle_files(main_site)

                    # primary place is itself
                    doc['primary_place'] = doc['id']

            if not main_site_id:
                # Thing and its main site haven't been merged
                # adding a relation between the site and the thing
                parent_of.add(self._get_formatted_oxpoints_id(main_site))
                doc['primary_place'] = self._get_formatted_oxpoints_id(main_site)

        location = find_location(self.graph, subject)
        if location:
            lat, lon = location
            doc['location'] = "{lat},{lon}".format(lat=lat, lon=lon)

        shape = find_shape(self.graph, subject)
        if shape:
            doc['shape'] = shape

        doc[self.identifier_key] = list(ids)

        doc.update(self._handle_alternative_names(subject))

        doc.update(self._handle_hidden_names(subject))

        doc.update(self._handle_address_data(subject))

        doc.update(self._handle_social_accounts(subject))

        doc.update(self._handle_mapped_properties(subject))

        doc.update(self._handle_courses(subject))

        if '_accessibility_has_access_guide_information' not in doc:
            # no access info from main site, attempt to get from the thing directly
            doc.update(self._handle_accessibility_data(subject))

        # only import images if a static files dir has been defined
        if self.static_files_dir:
            if 'files' in doc:
                doc['files'].extend(self._handle_files(subject))
            else:
                doc['files'] = self._handle_files(subject)

        parent_of.update(self._find_inverse_relations(subject, Org.subOrganizationOf))
        parent_of.update(self._find_relations(subject, Org.hasSite))
        parent_of.update(self._find_inverse_relations(subject, DCTERMS.isPartOf))
        parent_of.discard(main_site_id)
        if parent_of:
            doc['parent_of'] = list(parent_of)

        child_of.update(self._find_relations(subject, Org.subOrganizationOf))
        child_of.update(self._find_inverse_relations(subject, Org.hasSite))
        child_of.update(self._find_relations(subject, DCTERMS.isPartOf))
        if child_of:
            doc['child_of'] = list(child_of)

        doc['type'] = types

        return doc
 def test_shape_site(self):
     result = find_shape(self.graph, URIRef('http://oxpoints/site1'))
     self.assertEqual(self.site_shape_value, result)
 def test_no_shape(self):
     result = find_shape(self.graph, URIRef('http://oxpoints/sublibrary1'))
     self.assertIsNone(result)
 def test_do_not_go_to_site(self):
     result = find_shape(self.graph, URIRef('http://oxpoints/building1'))
     self.assertIsNone(result)
 def test_shape(self):
     result = find_shape(self.graph, URIRef('http://oxpoints/room1'))
     self.assertIsNotNone(result)
     self.assertEqual(self.building_shape_value, result)