<commit_before>from django.http import HttpResponseRedirect, HttpResponseBadRequest, HttpResponse, Http404
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.utils import simplejson
from django.db import connection, transaction

from strekmann_menu.models import Menu
from strekmann_menu.forms import MenuForm

def list_menu(request, tree_id):
    if request.method == 'POST':
        tree_id = request.POST['tree_id']

    menu = Menu.tree.root_node(tree_id)
    if not menu:
        raise Http404
    
    if request.method == 'POST':
        try:
            # get tree from json-data
            tree = simplejson.loads(request.POST["mptt_tree"])
            
            # format tree, remove root-item and make root-parents = id
            for node in tree:
                if node['parent_id'] == 'root':
                    node['parent_id'] = menu.id
                if node['item_id'] == 'root':
                    tree.remove(node)
            
            cursor = connection.cursor()
            qn = connection.ops.quote_name
            opts = menu._meta
        
            cursor.executemany("""
                UPDATE %(table)s 
                SET %(parent)s = :parent_id,
                    %(left)s = :left,
                    %(right)s = :right,
                    %(level)s = :depth 
                WHERE %(pk)s = :item_id AND %(tree_id)s = %(given_tree_id)s""" % {
                    "table": qn(opts.db_table),
                    'level': qn(opts.get_field(opts.level_attr).column),
                    'left': qn(opts.get_field(opts.left_attr).column),
                    'tree_id': qn(opts.get_field(opts.tree_id_attr).column),
                    'right': qn(opts.get_field(opts.right_attr).column),
                    'parent': qn(opts.get_field(opts.parent_attr).column),
                    'pk': qn(opts.pk.column),
                    'given_tree_id': tree_id,
                }, tree)
            transaction.commit_unless_managed()
            
        except (KeyError, ValueError):
            pass
        <commit_msg>fix bug with tree formating, delete root node after iteration<commit_after>from django.http import HttpResponseRedirect, HttpResponseBadRequest, HttpResponse, Http404
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.utils import simplejson
from django.db import connection, transaction

from strekmann_menu.models import Menu
from strekmann_menu.forms import MenuForm

def list_menu(request, tree_id):
    if request.method == 'POST':
        tree_id = request.POST['tree_id']

    menu = Menu.tree.root_node(tree_id)
    if not menu:
        raise Http404
    
    if request.method == 'POST':
        try:
            # get tree from json-data
            tree = simplejson.loads(request.POST["mptt_tree"])
            
            # format tree, remove root-item and make root-parents = id
            root = None
            for node in tree:
                if node['parent_id'] == 'root':
                    node['parent_id'] = menu.id
                if node['item_id'] == 'root':
                    root = node
            
            if root:
                tree.remove(root)
            
            cursor = connection.cursor()
            qn = connection.ops.quote_name
            opts = menu._meta
        
            cursor.executemany("""
                UPDATE %(table)s 
                SET %(parent)s = :parent_id,
                    %(left)s = :left,
                    %(right)s = :right,
                    %(level)s = :depth 
                WHERE %(pk)s = :item_id AND %(tree_id)s = %(given_tree_id)s""" % {
                    "table": qn(opts.db_table),
                    'level': qn(opts.get_field(opts.level_attr).column),
                    'left': qn(opts.get_field(opts.left_attr).column),
                    'tree_id': qn(opts.get_field(opts.tree_id_attr).column),
                    'right': qn(opts.get_field(opts.right_attr).column),
                    'parent': qn(opts.get_field(opts.parent_attr).column),
                    'pk': qn(opts.pk.column),
                    'given_tree_id': tree_id,
                }, tree)
            transaction.commit_unless_managed()
            
        except (KeyError, ValueError):
            pass
        
{'fraction_non_alphanumeric': 0.10398924249215598, 'fraction_numerical': 0.002689376961004034, 'mean_word_length': 1.7163724893487522, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2507386', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1212, 'n_words': 271}