# your code goes here
def commentsAll(request):
c = {}
c.update(csrf(request))
link_all = Link.objects.all()
filter_descendant = list(Link.objects.values('descendant'))
#print filter_descendant
list_sortig_ancestor = []
for item in link_all:
if item.ancestor not in list_sortig_ancestor:
list_sortig_ancestor.append(item.ancestor)
#делаем копию, в которую будем складывать всё
list_comment_id = list_sortig_ancestor[:]
#print list_sortig_ancestor
new_link_all = []
for item in link_all:
new_link_all.append('{0}'.format(item.ancestor))
#делаем из строк числа для того чтобы потом узнать сколько раз число входит
number_list_ancestor = []
for item in new_link_all:
if item == 'None':
break
else: number_list_ancestor.append(int(item))
list_item_descendant_for = []
clearx = []
for itemsort in list_sortig_ancestor:
length_descendand = number_list_ancestor.count(itemsort) #узнали длину cсколько раз входит
#print u'сколько раз предок', length_descendand
for itemlink in link_all:
if (itemlink.ancestor == itemsort):
list_item_descendant_for.append(itemlink.descendant) #добивили в массив потомков
#print itemlink.descendant
#print 'list_item_descendant_for', list_item_descendant_for
if (length_descendand == len(list_item_descendant_for)): #если длины массивов помоквов и количество эл. в линкалл совпадет
#print u'равно'
i = list_comment_id.index(itemsort)
i = i + 1
list_comment_id.insert(i, list_item_descendant_for) #list_comment_id[i:i] = list_item_descendant_for
list_item_descendant_for = clearx[:]
elif itemlink.ancestor is not itemsort:
continue
#print list_sortig_ancestor
print list_comment_id
new_list_comment = []
for item in list_sortig_ancestor:
if item not in new_list_comment:
new_list_comment.append(item)
for i in link_all:
if i.ancestor == item:
if i.descendant not in new_list_comment:
index = new_list_comment.index(item)
index = index + 1
new_list_comment.insert(index, i.descendant)
while new_list_comment.count(None) != 0:
new_list_comment.remove(None)
list_link_all = []
for i in link_all:
if i.descendant == None:
i.descendant = 0
new = [i.ancestor,i.descendant]
list(new)
list_link_all.append(new)
#print list_link_all
print new_list_comment
all_comments = Comments.objects.all()
v = RequestContext(request, {'all_comments': all_comments, 'new_list_comment': new_list_comment, 'list_link_all': list_link_all })
return render_to_response('comments_all.html', v)