Пример #1
0
def schema_nodetype_editcreate(request, graph_slug, nodetype_id=None):
    graph = get_object_or_404(Graph, slug=graph_slug)
    if nodetype_id:
        empty_nodetype = get_object_or_404(NodeType, id=nodetype_id)
    else:
        empty_nodetype = NodeType()
    form = NodeTypeForm(instance=empty_nodetype)
    formset = NodePropertyFormSet(instance=empty_nodetype)
    if request.POST:
        data = request.POST.copy()
        form = NodeTypeForm(data=data, instance=empty_nodetype)
        formset = NodePropertyFormSet(data=data, instance=empty_nodetype)
        if form.is_valid() and formset.is_valid():
            with transaction.commit_on_success():
                node_type = form.save(commit=False)
                node_type.schema = graph.schema
                node_type.save()
                instances = formset.save(commit=False)
                for instance in instances:
                    instance.node = node_type
                    instance.save()
                redirect_url = reverse("schema_edit", args=[graph.slug])
            return redirect(redirect_url)
    return render_to_response('schemas_item_edit.html', {
        "graph":
        graph,
        "item_type_label":
        _("Type"),
        "item_type":
        "node",
        "item_type_id":
        nodetype_id,
        "form":
        form,
        "fields_to_hide": [
            "plural_name", "inverse", "plural_inverse", "arity_source",
            "arity_target", "validation", "inheritance"
        ],
        "item_type_object":
        empty_nodetype,
        "formset":
        formset
    },
                              context_instance=RequestContext(request))
Пример #2
0
def schema_nodetype_editcreate(request, graph_slug, nodetype_id=None):
    graph = get_object_or_404(Graph, slug=graph_slug)
    if nodetype_id:
        empty_nodetype = get_object_or_404(NodeType, id=nodetype_id)
    else:
        empty_nodetype = NodeType()
    form = NodeTypeForm(instance=empty_nodetype)
    formset = NodePropertyFormSet(instance=empty_nodetype)
    if request.POST:
        data = request.POST.copy()
        form = NodeTypeForm(data=data, instance=empty_nodetype)
        formset = NodePropertyFormSet(data=data, instance=empty_nodetype)
        if form.is_valid() and formset.is_valid():
            with transaction.commit_on_success():
                node_type = form.save(commit=False)
                node_type.schema = graph.schema
                node_type.save()
                instances = formset.save(commit=False)
                for instance in instances:
                    instance.node = node_type
                    instance.save()
                redirect_url = reverse("schema_edit", args=[graph.slug])
            return redirect(redirect_url)
    return render_to_response('schemas_item_edit.html',
                              {"graph": graph,
                               "item_type_label": _("Type"),
                               "item_type": "node",
                               "item_type_id": nodetype_id,
                               "form": form,
                               "fields_to_hide": ["plural_name",
                                                  "inverse", "plural_inverse",
                                                  "arity_source",
                                                  "arity_target",
                                                  "validation",
                                                  "inheritance"],
                               "item_type_object": empty_nodetype,
                               "formset": formset},
                              context_instance=RequestContext(request))
Пример #3
0
def schema_nodetype_editcreate(request, graph_slug, nodetype_id=None):
    graph = get_object_or_404(Graph, slug=graph_slug)
    # We get the modal variable
    as_modal = bool(request.GET.get("asModal", False))
    # We get the schema main view breadcrumb
    schema_main_url = reverse("schema_edit", args=[graph.slug])
    if nodetype_id:
        empty_nodetype = get_object_or_404(NodeType, id=nodetype_id)
        add_url = reverse("schema_nodetype_edit", args=[graph_slug,
                                                        nodetype_id])
    else:
        empty_nodetype = NodeType()
        add_url = reverse("schema_nodetype_create", args=[graph_slug])
    form = NodeTypeForm(instance=empty_nodetype)
    formset = NodePropertyFormSet(instance=empty_nodetype)
    changed_props = request.session.get('schema_changed_props', None)
    deleted_props = request.session.get('schema_deleted_props', None)
    if changed_props:
        del request.session['schema_changed_props']
    if deleted_props:
        del request.session['schema_deleted_props']
    if request.POST:
        data = request.POST.copy()
        as_modal = bool(data.get("asModal", False))
        form = NodeTypeForm(data=data, instance=empty_nodetype)
        formset = NodePropertyFormSet(data=data, instance=empty_nodetype)
        if form.is_valid() and formset.is_valid():
            with transaction.atomic():
                node_type = form.save(commit=False)
                node_type.schema = graph.schema
                # Checking the color, it will create it if doesn't exist
                node_type.get_color()
                node_type.save()
                instances = formset.save(commit=False)
                for instance in instances:
                    instance.node = node_type
                    instance.save()
                schema_modified = False
                if formset.deleted_objects:
                    schema_modified = True
                    deleted_props = []
                    for prop_type in formset.deleted_objects:
                        deleted_props.append({'key': prop_type.key})
                    request.session['schema_deleted_props'] = deleted_props
                if formset.changed_objects:
                    changed_props = []
                    for prop_type, prop_dict in formset.changed_objects:
                        if 'key' in prop_dict:
                            schema_modified = True
                            for data in formset.cleaned_data:
                                if 'key' in data and 'id' in data and \
                                        data['key'] == prop_type.key:
                                    changed_props.append({
                                        'key': data['id'].key,
                                        'new_key': data['key']
                                    })
                    request.session['schema_changed_props'] = changed_props
                if schema_modified:
                    messages.success(request, _("Your changes were saved"))
                    redirect_url = reverse("schema_nodetype_properties_mend",
                                           args=[graph.slug, node_type.id])
                    action = "schema_nodetype_editcreate"
                else:
                    redirect_url = reverse("schema_edit", args=[graph.slug])
                    action = "schema_main"
            if as_modal:
                response = {'type': 'data',
                            'action': action}
                return HttpResponse(json.dumps(response), status=200,
                                    content_type='application/json')
            else:
                return redirect(redirect_url)
    if as_modal:
        base_template = 'empty.html'
        render = render_to_string
    else:
        base_template = 'base.html'
        render = render_to_response
    broader_context = {"graph": graph,
                       "item_type_label": _("Type"),
                       "item_type": "node",
                       "item_type_id": nodetype_id,
                       "form": form,
                       "fields_to_hide": ["plural_name",
                                          "inverse", "plural_inverse",
                                          "arity_source",
                                          "arity_target",
                                          "validation",
                                          "inheritance"],
                       "item_type_object": empty_nodetype,
                       "formset": formset,
                       "base_template": base_template,
                       "add_url": add_url,
                       "schema_main_url": schema_main_url,
                       "as_modal": as_modal}
    response = render('schemas_item_edit.html', broader_context,
                      context_instance=RequestContext(request))
    if as_modal:
        response = {'type': 'html',
                    'action': 'schema_nodetype_editcreate',
                    'html': response}
        return HttpResponse(json.dumps(response), status=200,
                            content_type='application/json')
    else:
        return response
Пример #4
0
def schema_nodetype_editcreate(request, graph_slug, nodetype_id=None):
    graph = get_object_or_404(Graph, slug=graph_slug)
    if nodetype_id:
        empty_nodetype = get_object_or_404(NodeType, id=nodetype_id)
    else:
        empty_nodetype = NodeType()
    form = NodeTypeForm(instance=empty_nodetype)
    formset = NodePropertyFormSet(instance=empty_nodetype)
    changed_props = request.session.get('schema_changed_props', None)
    deleted_props = request.session.get('schema_deleted_props', None)
    if changed_props:
        del request.session['schema_changed_props']
    if deleted_props:
        del request.session['schema_deleted_props']
    if request.POST:
        data = request.POST.copy()
        form = NodeTypeForm(data=data, instance=empty_nodetype)
        formset = NodePropertyFormSet(data=data, instance=empty_nodetype)
        if form.is_valid() and formset.is_valid():
            with transaction.atomic():
                node_type = form.save(commit=False)
                node_type.schema = graph.schema
                # Checking the color, it will create it if doesn't exist
                node_type.get_color()
                node_type.save()
                instances = formset.save(commit=False)
                for instance in instances:
                    instance.node = node_type
                    instance.save()
                schema_modified = False
                if formset.deleted_objects:
                    schema_modified = True
                    deleted_props = []
                    for prop_type in formset.deleted_objects:
                        deleted_props.append({'key': prop_type.key})
                    request.session['schema_deleted_props'] = deleted_props
                if formset.changed_objects:
                    changed_props = []
                    for prop_type, prop_dict in formset.changed_objects:
                        if 'key' in prop_dict:
                            schema_modified = True
                            for data in formset.cleaned_data:
                                if 'key' in data and 'id' in data and \
                                        data['key'] == prop_type.key:
                                    changed_props.append({
                                        'key': data['id'].key,
                                        'new_key': data['key']
                                    })
                    request.session['schema_changed_props'] = changed_props
                if schema_modified:
                    messages.success(request, _("Your changes were saved"))
                    redirect_url = reverse("schema_nodetype_properties_mend",
                                           args=[graph.slug, node_type.id])
                else:
                    redirect_url = reverse("schema_edit", args=[graph.slug])
            return redirect(redirect_url)
    return render_to_response('schemas_item_edit.html',
                              {"graph": graph,
                               "item_type_label": _("Type"),
                               "item_type": "node",
                               "item_type_id": nodetype_id,
                               "form": form,
                               "fields_to_hide": ["plural_name",
                                                  "inverse", "plural_inverse",
                                                  "arity_source",
                                                  "arity_target",
                                                  "validation",
                                                  "inheritance"],
                               "item_type_object": empty_nodetype,
                               "formset": formset},
                              context_instance=RequestContext(request))
Пример #5
0
def schema_nodetype_editcreate(request, graph_slug, nodetype_id=None):
    graph = get_object_or_404(Graph, slug=graph_slug)
    # We get the modal variable
    as_modal = bool(request.GET.get("asModal", False))
    # We get the schema main view breadcrumb
    schema_main_url = reverse("schema_edit", args=[graph.slug])
    if nodetype_id:
        empty_nodetype = get_object_or_404(NodeType, id=nodetype_id)
        add_url = reverse("schema_nodetype_edit",
                          args=[graph_slug, nodetype_id])
    else:
        empty_nodetype = NodeType()
        add_url = reverse("schema_nodetype_create", args=[graph_slug])
    form = NodeTypeForm(instance=empty_nodetype)
    formset = NodePropertyFormSet(instance=empty_nodetype)
    changed_props = request.session.get('schema_changed_props', None)
    deleted_props = request.session.get('schema_deleted_props', None)
    if changed_props:
        del request.session['schema_changed_props']
    if deleted_props:
        del request.session['schema_deleted_props']
    if request.POST:
        data = request.POST.copy()
        as_modal = bool(data.get("asModal", False))
        form = NodeTypeForm(data=data, instance=empty_nodetype)
        formset = NodePropertyFormSet(data=data, instance=empty_nodetype)
        if form.is_valid() and formset.is_valid():
            with transaction.atomic():
                node_type = form.save(commit=False)
                node_type.schema = graph.schema
                # Checking the color, it will create it if doesn't exist
                node_type.get_color()
                node_type.save()
                instances = formset.save(commit=False)
                for instance in instances:
                    instance.node = node_type
                    instance.save()
                schema_modified = False
                if formset.deleted_objects:
                    schema_modified = True
                    deleted_props = []
                    for prop_type in formset.deleted_objects:
                        deleted_props.append({'key': prop_type.key})
                    request.session['schema_deleted_props'] = deleted_props
                if formset.changed_objects:
                    changed_props = []
                    for prop_type, prop_dict in formset.changed_objects:
                        if 'key' in prop_dict:
                            schema_modified = True
                            for data in formset.cleaned_data:
                                if 'key' in data and 'id' in data and \
                                        data['key'] == prop_type.key:
                                    changed_props.append({
                                        'key': data['id'].key,
                                        'new_key': data['key']
                                    })
                    request.session['schema_changed_props'] = changed_props
                if schema_modified:
                    messages.success(request, _("Your changes were saved"))
                    redirect_url = reverse("schema_nodetype_properties_mend",
                                           args=[graph.slug, node_type.id])
                    action = "schema_nodetype_editcreate"
                else:
                    redirect_url = reverse("schema_edit", args=[graph.slug])
                    action = "schema_main"
            if as_modal:
                response = {'type': 'data', 'action': action}
                return HttpResponse(json.dumps(response),
                                    status=200,
                                    content_type='application/json')
            else:
                return redirect(redirect_url)
    if as_modal:
        base_template = 'empty.html'
        render = render_to_string
    else:
        base_template = 'base.html'
        render = render_to_response
    broader_context = {
        "graph":
        graph,
        "item_type_label":
        _("Type"),
        "item_type":
        "node",
        "item_type_id":
        nodetype_id,
        "form":
        form,
        "fields_to_hide": [
            "plural_name", "inverse", "plural_inverse", "arity_source",
            "arity_target", "validation", "inheritance"
        ],
        "item_type_object":
        empty_nodetype,
        "formset":
        formset,
        "base_template":
        base_template,
        "add_url":
        add_url,
        "schema_main_url":
        schema_main_url,
        "as_modal":
        as_modal
    }
    response = render('schemas_item_edit.html',
                      broader_context,
                      context_instance=RequestContext(request))
    if as_modal:
        response = {
            'type': 'html',
            'action': 'schema_nodetype_editcreate',
            'html': response
        }
        return HttpResponse(json.dumps(response),
                            status=200,
                            content_type='application/json')
    else:
        return response
Пример #6
0
def schema_nodetype_editcreate(request, graph_slug, nodetype_id=None):
    graph = get_object_or_404(Graph, slug=graph_slug)
    if nodetype_id:
        empty_nodetype = get_object_or_404(NodeType, id=nodetype_id)
    else:
        empty_nodetype = NodeType()
    form = NodeTypeForm(instance=empty_nodetype)
    formset = NodePropertyFormSet(instance=empty_nodetype)
    changed_props = request.session.get('schema_changed_props', None)
    deleted_props = request.session.get('schema_deleted_props', None)
    if changed_props:
        del request.session['schema_changed_props']
    if deleted_props:
        del request.session['schema_deleted_props']
    if request.POST:
        data = request.POST.copy()
        form = NodeTypeForm(data=data, instance=empty_nodetype)
        formset = NodePropertyFormSet(data=data, instance=empty_nodetype)
        if form.is_valid() and formset.is_valid():
            with transaction.commit_on_success():
                node_type = form.save(commit=False)
                node_type.schema = graph.schema
                # Checking the color
                if 'color' not in node_type.get_options():
                    color = node_type.schema.get_color()
                    node_type.set_option('color', color)
                    node_type.schema.save()
                node_type.save()
                instances = formset.save(commit=False)
                for instance in instances:
                    instance.node = node_type
                    instance.save()
                schema_modified = False
                if formset.deleted_objects:
                    schema_modified = True
                    deleted_props = []
                    for prop_type in formset.deleted_objects:
                        deleted_props.append({'key': prop_type.key})
                    request.session['schema_deleted_props'] = deleted_props
                if formset.changed_objects:
                    changed_props = []
                    for prop_type, prop_dict in formset.changed_objects:
                        if 'key' in prop_dict:
                            schema_modified = True
                            for data in formset.cleaned_data:
                                if 'key' in data and 'id' in data and \
                                        data['key'] == prop_type.key:
                                    changed_props.append({
                                        'key': data['id'].key,
                                        'new_key': data['key']
                                    })
                    request.session['schema_changed_props'] = changed_props
                if schema_modified:
                    messages.success(request, _("Your changes were saved"))
                    redirect_url = reverse("schema_nodetype_properties_mend",
                                           args=[graph.slug, node_type.id])
                else:
                    redirect_url = reverse("schema_edit", args=[graph.slug])
            return redirect(redirect_url)
    return render_to_response('schemas_item_edit.html', {
        "graph":
        graph,
        "item_type_label":
        _("Type"),
        "item_type":
        "node",
        "item_type_id":
        nodetype_id,
        "form":
        form,
        "fields_to_hide": [
            "plural_name", "inverse", "plural_inverse", "arity_source",
            "arity_target", "validation", "inheritance"
        ],
        "item_type_object":
        empty_nodetype,
        "formset":
        formset
    },
                              context_instance=RequestContext(request))