コード例 #1
0
ファイル: forms.py プロジェクト: pythonthings/historymesh
 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()
コード例 #2
0
ファイル: importer.py プロジェクト: pythonthings/historymesh
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
ファイル: importer.py プロジェクト: almereyda/historymesh
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()