Esempio n. 1
0
def create_node(request):
    node_name_form = NodeForm(request.POST or None)

    nodes = models.Unit.objects.all()

    context = {
        'node_name_form': node_name_form,
        'other_nodes': nodes,
    }

    if request.method == 'POST':
        if not node_name_form.is_valid():
            return render_to_response('edit.html', context=context)
        current_node = models.Unit.objects.create(
            name=node_name_form.cleaned_data['name'])

        actual_weights = {}
        for id, weight in request.POST.iteritems():
            try:
                actual_weights[int(id)] = int(weight)
            except ValueError:
                continue

        service.setup_actual_relations(current_node, actual_weights)
        return redirect('/')

    return render_to_response('edit.html', context)
Esempio n. 2
0
def edit_node(request, node_id):

    current_node = get_object_or_404(models.Unit, id=node_id)
    other_nodes = models.Unit.objects.all().exclude(id=node_id)
    direct_connected_nodes = service.get_direct_relations(current_node).keys()
    relations = service.get_node_relations(current_node)

    node_name_form = NodeForm(request.POST or None, instance=current_node)

    context = {
        'node_name_form': node_name_form,
        'other_nodes': other_nodes,
        'current_node': current_node,
        'direct_connected_nodes': direct_connected_nodes,
        'relations': relations,
    }

    if request.method == 'POST':
        if not node_name_form.is_valid():
            return render_to_response('edit.html', context=context)
        node_name_form.save()

        actual_weights = {}
        for id, weight in request.POST.iteritems():
            try:
                actual_weights[int(id)] = int(weight)
            except ValueError:
                continue

        service.setup_actual_relations(current_node, actual_weights)
        return redirect('/')
    return render_to_response('edit.html', context=context)
Esempio n. 3
0
def create_node(request):
    node_name_form = NodeForm(request.POST or None)

    nodes = models.Unit.objects.all()

    context = {
        'node_name_form': node_name_form,
        'other_nodes': nodes,
    }

    if request.method == 'POST':
        if not node_name_form.is_valid():
            return render_to_response('edit.html', context=context)
        current_node = models.Unit.objects.create(name=node_name_form.cleaned_data['name'])

        actual_weights = {}
        for id, weight in request.POST.iteritems():
            try:
                actual_weights[int(id)] = int(weight)
            except ValueError:
                continue

        service.setup_actual_relations(current_node, actual_weights)
        return redirect('/')

    return render_to_response('edit.html', context)
Esempio n. 4
0
def edit_node(request, node_id):

    current_node = get_object_or_404(models.Unit, id=node_id)
    other_nodes = models.Unit.objects.all().exclude(id=node_id)
    direct_connected_nodes = service.get_direct_relations(current_node).keys()
    relations = service.get_node_relations(current_node)

    node_name_form = NodeForm(request.POST or None, instance=current_node)

    context = {
        'node_name_form': node_name_form,
        'other_nodes': other_nodes,
        'current_node': current_node,
        'direct_connected_nodes': direct_connected_nodes,
        'relations': relations,
    }

    if request.method == 'POST':
        if not node_name_form.is_valid():
            return render_to_response('edit.html', context=context)
        node_name_form.save()

        actual_weights = {}
        for id, weight in request.POST.iteritems():
            try:
                actual_weights[int(id)] = int(weight)
            except ValueError:
                continue

        service.setup_actual_relations(current_node, actual_weights)
        return redirect('/')
    return render_to_response('edit.html', context=context)
Esempio n. 5
0
    def test_creating_new_relation(self):
        node1 = models.Unit.objects.create(name='A')
        node2 = models.Unit.objects.create(name='B')
        node3 = models.Unit.objects.create(name='C')

        models.Relationship.objects.create(start=node1, end=node2, value=1)

        service.setup_actual_relations(node2.id, {node1.id: 2, node3.id:2})

        self.assertEqual(len(models.Relationship.objects.filter(start=node2, end=node3), 1))
Esempio n. 6
0
    def test_deleting_relations(self):
        node1 = models.Unit.objects.create(name='A')
        node2 = models.Unit.objects.create(name='B')
        node3 = models.Unit.objects.create(name='C')
        node4 = models.Unit.objects.create(name='D')

        models.Relationship.objects.create(start=node1, end=node2, value=1)
        models.Relationship.objects.create(start=node2, end=node3, value=1)
        models.Relationship.objects.create(start=node3, end=node4, value=1)

        service.setup_actual_relations(node2, {node3.id: 3})
Esempio n. 7
0
    def test_creating_new_relation(self):
        node1 = models.Unit.objects.create(name='A')
        node2 = models.Unit.objects.create(name='B')
        node3 = models.Unit.objects.create(name='C')

        models.Relationship.objects.create(start=node1, end=node2, value=1)

        service.setup_actual_relations(node2.id, {node1.id: 2, node3.id: 2})

        self.assertEqual(
            len(models.Relationship.objects.filter(start=node2, end=node3), 1))
Esempio n. 8
0
    def test_deleting_relations(self):
        node1 = models.Unit.objects.create(name='A')
        node2 = models.Unit.objects.create(name='B')
        node3 = models.Unit.objects.create(name='C')
        node4 = models.Unit.objects.create(name='D')

        models.Relationship.objects.create(start=node1, end=node2, value=1)
        models.Relationship.objects.create(start=node2, end=node3, value=1)
        models.Relationship.objects.create(start=node3, end=node4, value=1)

        service.setup_actual_relations(node2, {node3.id: 3})
Esempio n. 9
0
    def test_deleting_not_actual_relations(self):
        node1 = models.Unit.objects.create(name='A')
        node2 = models.Unit.objects.create(name='B')
        node3 = models.Unit.objects.create(name='C')
        node4 = models.Unit.objects.create(name='D')

        models.Relationship.objects.create(start=node1, end=node2, value=2)
        models.Relationship.objects.create(start=node2, end=node3, value=12)
        models.Relationship.objects.create(start=node2, end=node4, value=1)

        service.setup_actual_relations(node2, {node1.id: 5, node3.id: 3})

        self.assertEqual(len(models.Relationship.objects.all()), 2)
Esempio n. 10
0
    def test_creating_and_updating_new_relations(self):
        node1 = models.Unit.objects.create(name='A')
        node2 = models.Unit.objects.create(name='B')
        node3 = models.Unit.objects.create(name='C')
        node4 = models.Unit.objects.create(name='D')

        models.Relationship.objects.create(start=node1, end=node2, value=1)

        service.setup_actual_relations(node2, {node3.id: 2, node4.id: 3})

        self.assertEqual(len(models.Relationship.objects.all()), 2)
        self.assertEqual(models.Relationship.objects.get(start=node2, end=node3).value, 2)
        self.assertEqual(models.Relationship.objects.get(start=node2, end=node4).value, 3)
Esempio n. 11
0
    def test_deleting_not_actual_relations(self):
        node1 = models.Unit.objects.create(name='A')
        node2 = models.Unit.objects.create(name='B')
        node3 = models.Unit.objects.create(name='C')
        node4 = models.Unit.objects.create(name='D')

        models.Relationship.objects.create(start=node1, end=node2, value=2)
        models.Relationship.objects.create(start=node2, end=node3, value=12)
        models.Relationship.objects.create(start=node2, end=node4, value=1)

        service.setup_actual_relations(node2, {node1.id: 5, node3.id: 3})

        self.assertEqual(len(models.Relationship.objects.all()), 2)
Esempio n. 12
0
    def test_updating_actual_weight(self):
        node1 = models.Unit.objects.create(name='A')
        node2 = models.Unit.objects.create(name='B')
        node3 = models.Unit.objects.create(name='C')
        node4 = models.Unit.objects.create(name='D')

        models.Relationship.objects.create(start=node1, end=node2, value=1)
        models.Relationship.objects.create(start=node2, end=node3, value=1)
        models.Relationship.objects.create(start=node2, end=node4, value=1)

        service.setup_actual_relations(node2, {node1.id: 2, node4.id: 3})

        self.assertEqual(models.Relationship.objects.get(end=node1.id).value, 2)
        self.assertEqual(models.Relationship.objects.get(end=node4.id).value, 3)
Esempio n. 13
0
    def test_creating_and_updating_new_relations(self):
        node1 = models.Unit.objects.create(name='A')
        node2 = models.Unit.objects.create(name='B')
        node3 = models.Unit.objects.create(name='C')
        node4 = models.Unit.objects.create(name='D')

        models.Relationship.objects.create(start=node1, end=node2, value=1)

        service.setup_actual_relations(node2, {node3.id: 2, node4.id: 3})

        self.assertEqual(len(models.Relationship.objects.all()), 2)
        self.assertEqual(
            models.Relationship.objects.get(start=node2, end=node3).value, 2)
        self.assertEqual(
            models.Relationship.objects.get(start=node2, end=node4).value, 3)
Esempio n. 14
0
    def test_updating_actual_weight(self):
        node1 = models.Unit.objects.create(name='A')
        node2 = models.Unit.objects.create(name='B')
        node3 = models.Unit.objects.create(name='C')
        node4 = models.Unit.objects.create(name='D')

        models.Relationship.objects.create(start=node1, end=node2, value=1)
        models.Relationship.objects.create(start=node2, end=node3, value=1)
        models.Relationship.objects.create(start=node2, end=node4, value=1)

        service.setup_actual_relations(node2, {node1.id: 2, node4.id: 3})

        self.assertEqual(
            models.Relationship.objects.get(end=node1.id).value, 2)
        self.assertEqual(
            models.Relationship.objects.get(end=node4.id).value, 3)