Example #1
0
    def _add_item_identifier(self, address):
        """Adds an item identifier to this topic.

        This method performs only the actual database operation to add
        the item identifier, and is only called after appropriate
        checking in add_item_identifier().

        :param address: external form of a locator
        :type address: string

        """
        ii = ItemIdentifier(address=address,
                            containing_topic_map=self.topic_map)
        ii.save()
        self.item_identifiers.add(ii)
Example #2
0
    def _add_item_identifier (self, address):
        """Adds an item identifier to this topic.

        This method performs only the actual database operation to add
        the item identifier, and is only called after appropriate
        checking in add_item_identifier().

        :param address: external form of a locator
        :type address: string
        
        """
        ii = ItemIdentifier(address=address,
                            containing_topic_map=self.topic_map)
        ii.save()
        self.item_identifiers.add(ii)
Example #3
0
    def add_item_identifier(self, item_identifier):
        """Adds an item identifier.

        It is not allowed to have two `Construct`s in the same
        `TopicMap` with the same item identifier. If the two objects
        are `Topic`s, then they must be merged. If at least one of the
        two objects is not a `Topic`, an `IdentityConstraintException`
        must be reported.

        :param item_identifier: the item identifier to be added
        :type item_identifier: `Locator`
        :raises `IdentityConstraintException`: if another construct
          has an item identifier which is equal to `item_identifier`
        
        """
        if item_identifier is None:
            raise ModelConstraintException(
                self, 'The item identifier may not be None')
        address = item_identifier.to_external_form()
        topic_map = self.get_topic_map()
        try:
            ii = ItemIdentifier.objects.get(address=address,
                                            containing_topic_map=topic_map)
            construct = ii.get_construct()
            if construct == self:
                return
            # While an ItemIdentifier may legitimately (in the
            # database's terms) exist while not being associated with
            # any constructs, the only valid ways to remove an item
            # identifier from a construct (remove_item_identifier()
            # and remove()) delete the item identifier. Therefore,
            # raise an exception without checking that construct is
            # not None.
            raise IdentityConstraintException(
                self, ii.get_construct(), item_identifier,
                'This item identifier is already associated with another construct'
            )
        except ItemIdentifier.DoesNotExist:
            ii = ItemIdentifier(address=address,
                                containing_topic_map=topic_map)
            ii.save()
            self.item_identifiers.add(ii)
Example #4
0
    def create_topic_by_item_identifier (self, item_identifier):
        """Returns a `Topic` instance with the specified item identifier.

        This method returns either an existing `Topic` or creates a
        new `Topic` instance with the specified item identifier.

        If a topic with the specified item identifier exists in the
        topic map, that topic is returned. If a topic with a subject
        identifier equal to the specified item identifier exists, the
        specified item identifier is added to that topic and the topic
        is returned. If neither a topic with the specified item
        identifier nor with a subject identifier equal to the subject
        identifier exists, a topic with the item identifier is
        created.

        :param item_identifier: the item identifier the topic should contain
        :type item_identifier: `Locator`
        :rtype: `Topic`

        """
        if item_identifier is None:
            raise ModelConstraintException(
                self, 'The item identifier may not be None')
        reference = item_identifier.to_external_form()
        try:
            topic = self.topic_constructs.get(
                item_identifiers__address=reference)
        except Topic.DoesNotExist:
            try:
                topic = self.topic_constructs.get(
                    subject_identifiers__address=reference)
            except Topic.DoesNotExist:
                topic = Topic(topic_map=self)
                topic.save()
            ii = ItemIdentifier(address=reference, containing_topic_map=self)
            ii.save()
            topic.item_identifiers.add(ii)
        return topic            
Example #5
0
    def create_topic_by_item_identifier(self, item_identifier):
        """Returns a `Topic` instance with the specified item identifier.

        This method returns either an existing `Topic` or creates a
        new `Topic` instance with the specified item identifier.

        If a topic with the specified item identifier exists in the
        topic map, that topic is returned. If a topic with a subject
        identifier equal to the specified item identifier exists, the
        specified item identifier is added to that topic and the topic
        is returned. If neither a topic with the specified item
        identifier nor with a subject identifier equal to the subject
        identifier exists, a topic with the item identifier is
        created.

        :param item_identifier: the item identifier the topic should contain
        :type item_identifier: `Locator`
        :rtype: `Topic`

        """
        if item_identifier is None:
            raise ModelConstraintException(
                self, 'The item identifier may not be None')
        reference = item_identifier.to_external_form()
        try:
            topic = self.topic_constructs.get(
                item_identifiers__address=reference)
        except Topic.DoesNotExist:
            try:
                topic = self.topic_constructs.get(
                    subject_identifiers__address=reference)
            except Topic.DoesNotExist:
                topic = Topic(topic_map=self)
                topic.save()
            ii = ItemIdentifier(address=reference, containing_topic_map=self)
            ii.save()
            topic.item_identifiers.add(ii)
        return topic
Example #6
0
    def create_topic (self, proxy=Topic):
        """Returns a `Topic` instance with an automatically generated
        item identifier.

        This method never returns an existing `Topic` but creates a
        new one with an automatically generated item identifier.

        Returns the newly created `Topic` instance with an automatically
        generated item identifier.

        :param proxy: Django proxy model class
        :type proxy: class
        :rtype: `Topic`

        """
        topic = proxy(topic_map=self)
        topic.save()
        address = 'http://%s/tmapi/iid/auto/%d' % \
            (Site.objects.get_current().domain, topic.id)
        ii = ItemIdentifier(address=address, containing_topic_map=self)
        ii.save()
        topic.item_identifiers.add(ii)
        return topic
Example #7
0
    def create_topic(self, proxy=Topic):
        """Returns a `Topic` instance with an automatically generated
        item identifier.

        This method never returns an existing `Topic` but creates a
        new one with an automatically generated item identifier.

        Returns the newly created `Topic` instance with an automatically
        generated item identifier.

        :param proxy: Django proxy model class
        :type proxy: class
        :rtype: `Topic`

        """
        topic = proxy(topic_map=self)
        topic.save()
        address = 'http://%s/tmapi/iid/auto/%d' % \
            (Site.objects.get_current().domain, topic.id)
        ii = ItemIdentifier(address=address, containing_topic_map=self)
        ii.save()
        topic.item_identifiers.add(ii)
        return topic