def topic(request, id=None): form = TopicForm(initial={'id': id}) comment_form = CommentForm(initial={"topic": id}) if (request.POST): comment_form = CommentForm(request.POST) comment_form.user = request.user.username if comment_form.is_valid(): if comment_form.save(request=request): message = "Comment submitted" return redirect('/topic/{}'.format(id)) else: message = "Failed to save comment" else: pass try: topic = api_client.get(form) comments = api_client.get(form, sub_url="topic/{}/comments".format(id)) except (Exception) as err: message = "Failed to retrieve topic and/or comments" new_comments = {} class node(): def __init__(self, value): self.value = value self.children = [] def add_child(self, obj): self.children.append(obj) nodes = dict((e["id"], node(e)) for e in comments['objects']) for e in comments['objects']: if e["in_reply_to"]: if not nodes.get(e["in_reply_to"], False): nodes[e["in_reply_to"]] = node({"is_hidden": True}) nodes[e["in_reply_to"]].add_child(nodes[e["id"]]) return render(request, 'forum/topic.html', { 'topic': topic, 'comments': nodes, 'form': comment_form })
def topic(request, id=None): form = TopicForm(initial={'id':id}) comment_form = CommentForm(initial={"topic": id}) if (request.POST): comment_form = CommentForm(request.POST) comment_form.user = request.user.username if comment_form.is_valid(): if comment_form.save(request=request): message = "Comment submitted" return redirect('/topic/{}'.format(id)) else: message = "Failed to save comment" else: pass try: topic = api_client.get(form) comments = api_client.get(form, sub_url="topic/{}/comments".format(id)) except (Exception) as err: message = "Failed to retrieve topic and/or comments" new_comments = {} class node(): def __init__(self, value): self.value = value self.children = [] def add_child(self, obj): self.children.append(obj) nodes = dict((e["id"], node(e)) for e in comments['objects']) for e in comments['objects']: if e["in_reply_to"]: if not nodes.get(e["in_reply_to"], False): nodes[e["in_reply_to"]] = node({"is_hidden": True}) nodes[e["in_reply_to"]].add_child(nodes[e["id"]]) return render(request, 'forum/topic.html', {'topic': topic, 'comments': nodes, 'form': comment_form})