Пример #1
0
    def _add_relation(self, datum, network):
        """
        checks to see whether an edge exists for relation.concept
        if not, create it.
        check if the node is in network
        if not, add it.
        """
        logger.debug(u'Handling relation with source {0}.'
                                                   .format(datum['source']))

        # TODO: Problem. How to tell which appellations are referenced by
        #  the relation? Maybe text-position, but what if two appellations
        #  have the same position?
        #  Temporary solution is to assume that there is one appellation
        #  per concept....
        source = self.these_appellations[datum['source']]
        target = self.these_appellations[datum['target']]
        
        # Create appellation for predicate.
        # TODO: Find a better way to handle incomplete data.
        try:
            predicate_concept = retrieve_concept(datum['attributes']['predicate'])
        except KeyError:
            predicate_concept = retrieve_concept('http://www.digitalhps.org/concepts/CON42732c04-a08f-476b-97fb-646bb73cb54c')
        predicate = Appellation(concept=predicate_concept)
        predicate.save()
        
        # Check for text position.
        predicate = self._handle_text_position(predicate, datum)

        logger.debug(u'Relation: {0} - {1} - {2}'
                                             .format(source, predicate, target))
        relation = Relation(source=source,
                            target=target,
                            predicate=predicate)
        relation.save()

        source_node = self.these_nodes[datum['source']]
        target_node = self.these_nodes[datum['target']]

        try:
            edge = Edge.objects.get(source=source_node.id,
                                    target=target_node.id,
                                    concept=predicate_concept.id)
            logger.debug("edge found")
        except Edge.DoesNotExist:
            logger.debug("edge does not exist")
            edge = Edge(source=source_node,
                        target=target_node,
                        concept=predicate_concept)
            edge.save()
        edge.relations.add(relation.id)
        edge.save()
        
        if edge not in network.edges.all():
            logger.debug(u'Edge not in Network {0}; adding.'
                                                          .format(network.name))
            network.edges.add(edge.id)

        return relation
Пример #2
0
    def save_model(self, request, obj, form, change):
        """
        Concepts are added from a known ConceptAuthority.
        """

        if change:  # Use built-in save_model when editing Concepts.
            return super(ConceptAdmin, self) \
                                         .save_model(request, obj, form, change)
        else:  # Retrieve the concept by URI from a ConceptAuthority.
            uri = form.cleaned_data['uri']
            try:
                concept = retrieve_concept(uri)

                logging.debug("Concept with uri {0} retrieved".format(uri))
                message = "Concept {0} retrieved successfully." \
                                                           .format(concept.name)
                self.message_user(request, message, level=messages.SUCCESS)
            except ValueError:
                logging.debug("Concept with uri {0} not found".format(uri))
                message = "No such concept in selected ConceptAuthority"
                self.message_user(request, message, level=messages.ERROR)
            except RuntimeError:
                logging.debug("No valid ConceptAuthority.")
                message = "No valid ConceptAuthority available."
                self.message_user(request, message, level=messages.ERROR)
Пример #3
0
def retrieve(request, uri):
    """
    Get or retrieve JSON data about a :class:`.Concept` by ``uri``.
    
    If no :class:`.Concept` for that ``uri`` exits, reaches out to a known
    :class:`.ConceptAuthority` and attempts to retrieve it.
    
    Parameters
    ----------
    uri : str
        A URI, presumably belonging to some :class:`.ConceptAuthority`\.
    """

    try:
        concept = retrieve_concept(uri)
    except RuntimeError:
        return HttpResponse("No ConceptAuthority selected.", status=403)
    except ValueError:
        raise Http404

    # Return the Concept as JSON.
    response_data = { 'id': concept.id,
                      'uri': concept.uri,
                      'type': concept.type.uri,
                      'location': concept.location_id,
                      'equalto': concept.equalto,
                      'similarto': concept.similarto }
    
    jdata = simplejson.dumps(response_data)
    return HttpResponse(jdata, "application/json")
Пример #4
0
    def test_concept_has_type(self):
        """
        When creating a new Concept, should also create a ConceptType and 
        associate it via Concept.type.
        """

        concept = retrieve_concept(cp_concept)
        self.assertIsInstance(concept.type, ConceptType)
        self.assertEqual(concept.type.uri, cp_concept_type_uri)
Пример #5
0
    def test_retrieve_legit_concept(self):
        """
        If a legit concept from the ConceptAuthority is provided, then should
        get a Concept object with uri = provided uri.
        """

        concept = retrieve_concept(cp_concept)
        self.assertIsInstance(concept, Concept)
        self.assertEqual(concept.uri, cp_concept)
Пример #6
0
    def test_retrieve_legit_concept(self):
        """
        If a legit concept from the ConceptAuthority is provided, then should
        get a Concept object with uri = provided uri.
        """

        concept = retrieve_concept(cp_concept)
        self.assertIsInstance(concept, Concept)
        self.assertEqual(concept.uri, cp_concept)
Пример #7
0
    def test_concept_has_type(self):
        """
        When creating a new Concept, should also create a ConceptType and 
        associate it via Concept.type.
        """

        concept = retrieve_concept(cp_concept)
        self.assertIsInstance(concept.type, ConceptType)
        self.assertEqual(concept.type.uri, cp_concept_type_uri)
Пример #8
0
def retrieve(request, uri):
    try:
        concept = retrieve_concept(uri)
    except RuntimeError:
        return HttpResponse("No ConceptAuthority selected.", status=403)
    except ValueError:
        raise Http404

    # Return the Concept as JSON.
    response_data = { 'id': concept.id,
                      'uri': concept.uri,
                      'type': concept.type,
                      'location': concept.location_id,
                      'equalto': concept.equalto,
                      'similarto': concept.similarto }
    
    jdata = simplejson.dumps(response_data)
    return HttpResponse(jdata, "application/json")
Пример #9
0
def handle_item(repo, item):
    cred = repo.credential
    manager = RepositoryManager(cred)
    
    # Ignore items without bitstreams.
    if item['primary_bitstream'] in [ None, '-1' ]:
        return None
    
    try:
        text = Text.objects.get(uri=item['uri'])
        exists = True
    except Text.DoesNotExist:
        exists = False
    
    if not exists:
        # Get bitstream.
        bitstream = manager.get_bitstream(item['primary_bitstream'])
        
        # Get Creators.
        creators = []
        for creator in item['creators']:
            creators.append(retrieve_concept(creator))


        text = Text(    uri = item['uri'],
                        title = item['title'],
                        dateCreated = handle_date(item['dateCreated']),
                        dateDigitized = handle_date(item['dateDigitized']),
                        content = bitstream,
                        filename = item['uri'],
                        length = len(bitstream) )
        text.save()
        for creator in creators:
            text.creator.add(creator)
        text.save()
        
        return text
    return None
Пример #10
0
    def _add_appellation(self, datum, network):
        """
        checks to see whether a node exists for appellation.concept
        if not, create it.
        check if the node is in network
        if not, add it.
        """
        logger.debug(u'Handling appellation for {0}'.format(datum['id']))

        concept = retrieve_concept(datum['attributes']['concept'])
        logger.debug(u'Found concept {0}'.format(concept.name))

        # TODO: this is going to cause problems when editing. ID? Prevent?
        appellation = Appellation(concept=concept)
        appellation.save()
        
        # TODO: better way to do this (see relations block, below).
        self.these_appellations[datum['id']] = appellation

        # Check for, and attach, a TextPosition.
        instance = self._handle_text_position(appellation, datum)
        
        # Get Node and attach Appellation.
        node = Node.objects.get_unique(concept)
        node.appellations.add(appellation.id)
        node.save()
        
        self.these_nodes[datum['id']] = node
        
        # Check to see whether Node is in Network. If not, add it.
        if node not in network.nodes.all():
            logger.debug(u'Node for {0} not in Network {1}'
                                            .format(concept.name, network.name))
            network.nodes.add(node.id)

        return appellation
Пример #11
0
    def _add_appellation(self, datum, network):
        """
        checks to see whether a node exists for appellation.concept
        if not, create it.
        check if the node is in network
        if not, add it.
        """
        logger.debug(u'Handling appellation for {0}'.format(datum['id']))

        concept = retrieve_concept(datum['attributes']['concept'])
        logger.debug(u'Found concept {0}'.format(concept.name))

        # TODO: this is going to cause problems when editing. ID? Prevent?
        appellation = Appellation(concept=concept)
        appellation.save()

        # TODO: better way to do this (see relations block, below).
        self.these_appellations[datum['id']] = appellation

        # Check for, and attach, a TextPosition.
        instance = self._handle_text_position(appellation, datum)

        # Get Node and attach Appellation.
        node = Node.objects.get_unique(concept)
        node.appellations.add(appellation.id)
        node.save()

        self.these_nodes[datum['id']] = node

        # Check to see whether Node is in Network. If not, add it.
        if node not in network.nodes.all():
            logger.debug(u'Node for {0} not in Network {1}'.format(
                concept.name, network.name))
            network.nodes.add(node.id)

        return appellation
Пример #12
0
    def _add_relation(self, datum, network):
        """
        checks to see whether an edge exists for relation.concept
        if not, create it.
        check if the node is in network
        if not, add it.
        """
        logger.debug(u'Handling relation with source {0}.'.format(
            datum['source']))

        # TODO: Problem. How to tell which appellations are referenced by
        #  the relation? Maybe text-position, but what if two appellations
        #  have the same position?
        #  Temporary solution is to assume that there is one appellation
        #  per concept....
        source = self.these_appellations[datum['source']]
        target = self.these_appellations[datum['target']]

        # Create appellation for predicate.
        # TODO: Find a better way to handle incomplete data.
        try:
            predicate_concept = retrieve_concept(
                datum['attributes']['predicate'])
        except KeyError:
            predicate_concept = retrieve_concept(
                'http://www.digitalhps.org/concepts/CON42732c04-a08f-476b-97fb-646bb73cb54c'
            )
        predicate = Appellation(concept=predicate_concept)
        predicate.save()

        # Check for text position.
        predicate = self._handle_text_position(predicate, datum)

        logger.debug(u'Relation: {0} - {1} - {2}'.format(
            source, predicate, target))
        relation = Relation(source=source, target=target, predicate=predicate)
        relation.save()

        source_node = self.these_nodes[datum['source']]
        target_node = self.these_nodes[datum['target']]

        try:
            edge = Edge.objects.get(source=source_node.id,
                                    target=target_node.id,
                                    concept=predicate_concept.id)
            logger.debug("edge found")
        except Edge.DoesNotExist:
            logger.debug("edge does not exist")
            edge = Edge(source=source_node,
                        target=target_node,
                        concept=predicate_concept)
            edge.save()
        edge.relations.add(relation.id)
        edge.save()

        if edge not in network.edges.all():
            logger.debug(u'Edge not in Network {0}; adding.'.format(
                network.name))
            network.edges.add(edge.id)

        return relation