多级评论
from django.template import Library
from django.utils.safestring import mark_safe
register = Library()
@register.simple_tag
def truncate_upload_img(img_src):
print(dir(img_src))
print(img_src.name)
return img_src.name.lstrip("/uploads/")
@register.simple_tag
def render_paginator_btn(articles,page):
current_page = articles.number
if abs(current_page - page) <= 5 :
ele = """
{page}""".format(page=page)return mark_safe(ele)
return ''
def build_comment_tree(comment_dic,obj):
"""递归的去把每个评论放到合适的层级里面"""
for k,v in comment_dic.items():
if obj.p_node == k: #代表找他了它父节点,把自己加到k下面
print("[%s]找到了父节点[%s]" %(obj,k))
comment_dic[k][obj] = {}
else: #开始进行深度查询
print("没找到,进入下一层查找",obj)
build_comment_tree(comment_dic[k], obj)
def build_comment_html(comment_dic,margin_arg):
"""循环评论的字典, 拼接html"""
comment_eles = ''
for k,v in comment_dic.items():
comment_eles += '''
.format(user=k.user,
date=k.date.strftime('%Y-%m-%d %H:%M:%S'),
comment=k.comment,
margin=margin_arg)
if v:
comment_eles += build_comment_html(comment_dic[k],margin_arg+20)
return comment_eles
@register.simple_tag
def load_comments(article_obj):
#1.先把数据库里所有的这篇文章的评论查出来,转成字典
#2.递归的循环这个字典,生成html评论元素
comment_dic = {
}
comment_objs = article_obj.comment_set.all().order_by('date') #列表
for obj in comment_objs:
if not obj.p_node:#判断obj有没有p_node,没有的话,那它自己就是顶级评论
comment_dic[obj] = {}
else: #有父亲节点
build_comment_tree(comment_dic,obj)
print(comment_dic)
comment_list = sorted(comment_dic.items(),key=lambda x:x[0].date)
print("comment objs:",comment_list)
comment_html = """"""
for comment_branch in comment_list:
margin_arg=0
branch_ele = """
format(user=comment_branch[0].user,
date=comment_branch[0].date.strftime('%Y-%m-%d %H:%M:%S'),
comment=comment_branch[0].comment,
margin=margin_arg)
comment_html += branch_ele
#开始构建它的子级评论的元素
comment_html += build_comment_html(comment_branch[1],margin_arg+20)
return mark_safe(comment_html)
webqq
from django.shortcuts import render,HttpResponse
from django.views.decorators.csrf import csrf_exempt
from webqq.msg_handler import MsgHandler
import queue,json
# Create your views here.
MSG_QUEUES = {}
def dashboard(request):
print("user--",request.user.userproifle)
return render(request,"webqq/dashboard.html")
#@csrf_exempt
def msg_api(request):
msg_obj = MsgHandler(request, MSG_QUEUES)
if request.method == "POST":
msg_obj.msg_send()
return HttpResponse(json.dumps({"msg_send_status": 1}))
else:
msg_data = msg_obj.msg_recv()
return HttpResponse(json.dumps(msg_data))
1 {% extends 'index.html' %}2
3 {% block extra-head-resources %}4
5
6 {% endblock %}7
8
9 {% block container %}10
11 {% csrf_token %}12
13
14
15
c16
g17
18
19
20
21
22
23
24
25 {% for contact in request.user.userproifle.friends.all %}26
29
30
31 {% endfor %}32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 正在与聊天48
49
50
51
52 body53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 {% endblock %}72
73
74 {% block bottom-js %}75
76
77
78 $(document).ready(function () {79
80 csrfmiddlewaretoken = $("input[name='csrfmiddlewaretoken']").val();81
82
83 $("body").delegate("textarea", "keydown",function(e){84 if(e.which == 13) {//Enter key down85 //send msg button clicked86 var msg_text = $("textarea").val();87 if ($.trim(msg_text).length >0){88 //console.log(msg_text);89 SendMsg(msg_text);90 }91 //no wait the send_msg's call confirm msg
92 AddSentMsgIntoBox(msg_text);93 $("textarea").val('');94 }95 });//end body96
97
98
99
100 /*setInterval(function () {101 LoadNewMsgs();102 },1000)*/
103
104 LoadNewMsgs();105
106
107
108
109 });//end doc ready110
111
112 function LoadNewMsgs() {113
114
115
116 $.get("{% url 'get_msg' %}",function(callback){117 console.log("get_msg callback:",callback);118 returnLoadNewMsgs();119 });//end get120 }121
122 function SendMsg(msg) {123 console.log("going tosend msg" +msg);124
125
126
127 var msg_data ={128 'csrfmiddlewaretoken':csrfmiddlewaretoken,129 'from':"{{ request.user.userproifle.id }}",130 'to': $("#chat_panel_header_text").attr("contact_id"),131 'data':msg132 }133 $.post("{% url 'msg_api' %}", msg_data ,function(callback){134
135 var callback = JSON.parse(callback); //json反序列化136 console.log("msg_send_status:",callback.msg_send_status);137 if ( callback.msg_send_status != 1){138 alert("消息发送失败:"+msg);139 }140 });//end post141
142
143 }144 function AddSentMsgIntoBox(msg_text) {145 var d =new Date();146 var msg_ele = "
150
151 $('.chat_panel_body').animate({152 scrollTop: $('.chat_panel_body')[0].scrollHeight}, 500)153
154 }155 function OpenSession(ele) {156 var contact_id = $(ele).attr("contact_id");157 var contact_name = $(ele).attr("contact_name");158 $(ele).addClass("active");159 $(ele).siblings().removeClass("active");160 $("#chat_panel_header_text").text(contact_name);161 $("#chat_panel_header_text").parent().removeClass("hidden");162 $("#chat_panel_header_text").attr("contact_id",contact_id);163
164 }165
166
167
168
169
170
171
172
173
174 {% endblock %}