Пример #1
0
    def other_story_links(self, instance, story):
        edges = set()
        nodes = set()
        type_str = Edge._type_string_from_model(instance)
        outgoing = Edge.objects.filter(verb="primary", subject_id=instance.id, subject_type=type_str).exclude(story=story)
        second_out = Edge.objects.filter(verb="secondary", subject_id=instance.id, subject_type=type_str, story=story)
        incoming = Edge.objects.filter(verb="primary", object_id=instance.id, object_type=type_str).exclude(story=story)
        second_in = Edge.objects.filter(verb="secondary", object_id=instance.id, object_type=type_str, story=story)

        outgoing = outgoing or second_out
        incoming = incoming or second_in

        for edge in outgoing:
            node = edge.object
            node.horizontal_position = -self.node_separation
            node.story = edge.story

            nodes.add(node)
            edges.add(edge)

        for edge in incoming:
            # The incoming links kink the wrong way on the map. Reversing the link fixes this. We do it in the view
            edge.incoming = True

            node = edge.subject
            node.horizontal_position = self.node_separation
            node.story = edge.story
            nodes.add(node)
            edges.add(edge)
        return nodes,edges
Пример #2
0
def import_from_json(data, clean=False, verbose=False):
    """
    Given an already-parsed JSON object, imports the data within
    into the database.
    """

    # If we're cleaning, clean.
    if clean:
        for model in Node.all_child_classes + [Edge]:
            model.objects.all().delete()

    # First pass: insert the things into the database
    relationships = []
    object_cache = {}
    for thing in data:
        # Get the class it maps to
        try:
            klass = type_map[thing['type']]
        except KeyError:
            raise ValueError("Invalid thing (bad type): %r" % thing)
        # Get the object if it exists
        instance = klass.objects.get_or_create(name=thing['name'])[0]
        # Delete all edges coming off of the object
        instance.outgoing().delete()
        # Store relationships for later use
        for verb, targets in thing['relationships'].items():
            for target in targets:
                relationships.append((instance, target, verb))
        # Cache object
        object_cache[thing['name']] = instance
        if verbose:
            print "Imported %s" % thing['name']
    
    # Second pass: create relationships
    if verbose:
        print "Creating relationships..."
    for subject, object_name, verb in relationships:
        for verb, targets in thing['relationships'].items():
            edge = Edge(verb=verb)
            edge.subject = instance
            try:
                edge.object = object_cache[object_name]
            except KeyError:
                raise ValueError("Relationship from %s to non-existent object %s" % (subject, object_name))
            edge.save()
Пример #3
0
 def save_m2m_real(self):
     try:
         self.instance.incoming("described_by").get(story__isnull=False)
     except Edge.DoesNotExist:
         edge = Edge(verb="described_by")
         edge.subject = self.cleaned_data['subject']
         edge.object = self.instance
         edge.story = self.cleaned_data['story']
         edge.save()
Пример #4
0
def realise_objects(wanted, needed):
    to_get = {}
    for item in needed:
        to_get.setdefault(item.data['type'][0], []).append(item)
              
    for model, items in to_get.iteritems():
        model = Edge._model_from_type_string('core.' + model)

        objs = model.objects.in_bulk([int(item.data['id'][0]) for item in items])
        for item in items:
            item.object = objs[int(item.data['id'][0])]
Пример #5
0
 def get_context_data(self):
     context = get_layout_data()
     context["story"] = self.request.GET.get('story')
     # See if there's currently a node
     current_node = self.request.GET.get("current_node")
     if current_node:
         type_string, pk = current_node.split(":")
         model_class = Edge._model_from_type_string(type_string)
         instance = model_class.objects.get(pk=pk)
         context["current_node"] = instance
         node_story_names = [s.name.lower() for s in instance.stories()]
         if context["story"] not in node_story_names:
             context["story"] = node_story_names[0]
     context['under_map'] = True
     return context
Пример #6
0
    def other_story_links(self, instance, story):
        edges = set()
        nodes = set()
        type_str = Edge._type_string_from_model(instance)
        outgoing = Edge.objects.filter(
            verb="primary", subject_id=instance.id,
            subject_type=type_str).exclude(story=story)
        second_out = Edge.objects.filter(verb="secondary",
                                         subject_id=instance.id,
                                         subject_type=type_str,
                                         story=story)
        incoming = Edge.objects.filter(
            verb="primary", object_id=instance.id,
            object_type=type_str).exclude(story=story)
        second_in = Edge.objects.filter(verb="secondary",
                                        object_id=instance.id,
                                        object_type=type_str,
                                        story=story)

        outgoing = outgoing or second_out
        incoming = incoming or second_in

        for edge in outgoing:
            node = edge.object
            node.horizontal_position = -self.node_separation
            node.story = edge.story

            nodes.add(node)
            edges.add(edge)

        for edge in incoming:
            # The incoming links kink the wrong way on the map. Reversing the link fixes this. We do it in the view
            edge.incoming = True

            node = edge.subject
            node.horizontal_position = self.node_separation
            node.story = edge.story
            nodes.add(node)
            edges.add(edge)
        return nodes, edges
Пример #7
0
def import_from_json(data, clean=False, verbose=False):
    """
    Given an already-parsed JSON object, imports the data within
    into the database.
    """

    # If we're cleaning, clean.
    if clean:
        for model in Node.all_child_classes + [Edge]:
            model.objects.all().delete()

    # First pass: insert the things into the database
    relationships = []
    object_cache = {}
    for thing in data:
        # Get the class it maps to
        try:
            klass = type_map[thing['type']]
        except KeyError:
            raise ValueError("Invalid thing (bad type): %r" % thing)
        # Get the object if it exists
        instance = klass.objects.get_or_create(name=thing['name'])[0]
        # Delete all edges coming off of the object
        instance.outgoing().delete()
        # Store relationships for later use
        for verb, targets in thing['relationships'].items():
            for target in targets:
                relationships.append((instance, target, verb))
        # Cache object
        object_cache[thing['name']] = instance
        if verbose:
            print "Imported %s" % thing['name']

    # Second pass: create relationships
    if verbose:
        print "Creating relationships..."
    for subject, object_name, verb in relationships:
        for verb, targets in thing['relationships'].items():
            edge = Edge(verb=verb)
            edge.subject = instance
            try:
                edge.object = object_cache[object_name]
            except KeyError:
                raise ValueError(
                    "Relationship from %s to non-existent object %s" %
                    (subject, object_name))
            edge.save()
Пример #8
0
 def clean(self, value):
     subject_string, pk = value.split(":")
     subject_model = Edge._model_from_type_string(subject_string)
     return subject_model.objects.get(pk=pk)
Пример #9
0
 def test_model_from_type_string(self):
     self.assertEqual(Person, Edge._model_from_type_string("core.person"))
Пример #10
0
    def test_type_string_generation(self):
        person = Person.objects.get(pk=1)
        self.assertEqual("core.person", Edge._type_string_from_model(person))

        concept = Concept.objects.get(pk=1)
        self.assertEqual("core.concept", Edge._type_string_from_model(concept))
Пример #11
0
 def test_model_from_type_string(self):
     self.assertEqual(Person, Edge._model_from_type_string("core.person"))
Пример #12
0
    def test_type_string_generation(self):
        person = Person.objects.get(pk=1)
        self.assertEqual("core.person", Edge._type_string_from_model(person))

        concept = Concept.objects.get(pk=1)
        self.assertEqual("core.concept", Edge._type_string_from_model(concept))