提交评论功能

This commit is contained in:
2024-05-06 12:35:09 +08:00
parent 645aafcadb
commit e48d34c52c
10 changed files with 215 additions and 8 deletions
+1 -1
View File
@@ -95,7 +95,7 @@ WSGI_APPLICATION = 'jsmindmulti.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'jsmind',
'NAME': 'jira3',
'USER': 'root',
'PASSWORD': 'guoan123',
'HOST': '127.0.0.1',
+15
View File
@@ -117,3 +117,18 @@ class Task(AbstractModel):
def __str__(self):
return self.title
class Comment(models.Model): # 定义评论模型
# article = models.ForeignKey(to=Article, on_delete=models.DO_NOTHING, verbose_name='评论文章')
pinglun_id = models.IntegerField(verbose_name='评论id')
comment_content = models.TextField(verbose_name='评论内容')
user = models.ForeignKey(MindUser, on_delete=models.CASCADE) #评论人id
comment_time = models.DateTimeField(auto_now_add=True, verbose_name='评论时间')
pre_comment = models.ForeignKey('self', on_delete=models.DO_NOTHING, null=True,
verbose_name='父评论id') # 父级评论,如果没有父级则为空NULL, "self"表示外键关联自己
class Meta:
db_table = 'comment_tb'
verbose_name = '评论'
verbose_name_plural = verbose_name
+4 -1
View File
@@ -23,6 +23,7 @@ urlpatterns = [
path('reng_wu',to_rengwu), # 到任务界面
path('ke_cheng',to_kecheng), # 到课程界面
path('2_write',to_write), # 到协同边界页面
path('2_pinglun',to_pinglun), # 评论
path('2_star', to_star), # 到收藏界面
path('2_recycle', to_recycle), # 回收站
@@ -39,6 +40,7 @@ urlpatterns = [
path('star', JsMindStarView.as_view()), # 收藏思维导图
path('bianji', JsMindbianjiView.as_view()), # 跳转到协同编辑页面
path('pinglun', JsMindpinglunView.as_view()), # 跳转到评论页面
# ---------------------------------------------------
path('register', MindUserView.as_view()), # 用户注册
path('login', LoginView.as_view()), # 用户登录
@@ -52,6 +54,7 @@ urlpatterns = [
path('post_task/', post_task, name='post_task'),
path('post_kecheng/', post_kecheng, name='post_kecheng'),
path('come_kecheng/',come_kecheng, name='come_kecheng')
path('come_kecheng/',come_kecheng, name='come_kecheng'),
path('comment_control/',comment_control,name='comment_control') #提交评论处理的路由
]
+9 -1
View File
@@ -1,7 +1,7 @@
import json
from django.shortcuts import render
from server.models import Task,StudentandTercher
from server.models import Task,StudentandTercher,Comment
from server_auth.models import MindUser
@@ -21,6 +21,14 @@ def to_baiban(request):
"path": test_date}
return render(request, "html/baiban.html", param)
def to_pinglun(request):
test_date = request.GET.get('id')
comment_list = Comment.objects.filter(pinglun_id=test_date) # 从数据库找出该文章的评论数据对象
param = {"path": test_date,
"comment_list": comment_list}
print(test_date)
return render(request, "html/commont.html", param)
def to_share(request):
"""
分享页面
+26
View File
@@ -1,6 +1,7 @@
import uuid
from datetime import datetime as dt
from django.http import JsonResponse
from django_filters.rest_framework import *
from rest_framework.generics import *
from rest_framework.views import APIView
@@ -301,6 +302,31 @@ class JsMindbianjiView(LoginRequiredJSONMixin, ListAPIView):
return build_response(msg="操作成功", data=res)
class JsMindpinglunView(LoginRequiredJSONMixin, ListAPIView):
"""
思维导图分享view
"""
serializer_class = CooperationMindShareSerializer
filter_backends = [DjangoFilterBackend]
filter_class = CooperationMindShareFilter
pagination_class = DefaultNumberPagination
def post(self, request):
"""
点击协同编辑
"""
post_body = request.POST
id = post_body.get('id')
user = request.user
if id is None:
return build_response(code=CODE_FAILED, msg='参数错误')
if user.id is None:
return build_response(code=CODE_FAILED, msg='用户未登录')
print("----------------------------------------error")
res = f"http://127.0.0.1:8000/2_pinglun?test={id}"
return build_response(msg="操作成功", data=res)
class JsMindStarView(LoginRequiredJSONMixin, ListAPIView):
"""
+17
View File
@@ -2,6 +2,7 @@ from django import http
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.backends import ModelBackend # 验证基类
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import JsonResponse
from django.shortcuts import redirect
from django.urls import reverse
from rest_framework.views import APIView
@@ -190,3 +191,19 @@ def come_kecheng(request):
return http.JsonResponse({'code': CODE_SUCCESS, 'msg': '发布成功'})
def comment_control(request): # 提交评论的处理函数
comment_content = request.POST.get('comment_content')
article_id = request.POST.get('article_id')
pid = request.POST.get('pid')
id = MindUser.objects.get(username=request.user.username).id
author_id = id # 获取当前用户的ID
Comment.objects.create(comment_content=comment_content, pre_comment_id=pid, pinglun_id=article_id,
user_id=author_id) # 将提交的数据保存到数据库中
article = list(
Comment.objects.values('id', 'comment_content', 'pre_comment_id', 'pinglun_id', 'user_id',
'comment_time')) # 以键值对的形式取出评论对象,并且转化为列表list类型
return JsonResponse(article, safe=False) # JsonResponse返回JSON字符串,自动序列化,如果不是字典类型,则需要添加safe参数为False
+107
View File
@@ -0,0 +1,107 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文章详情</title>
<script src="/static/js/jquery-1.11.3.min.js"></script>
<script src="/static/js/jsmind.js"></script>
<script src="/static/js/jsmind.draggable.js"></script>
<script src="/static/js/jsmind.screenshot.js"></script>
<script src="/static/js/jsmind.menu.js"></script>
<script src="/static/js/jsmind.coop.js?d="></script>
<script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="/static/js/mind/c_m_m.js"></script>
<script src="https://cdn.jsdelivr.net/npm/feather-icons@4.28.0/dist/feather.min.js"></script> <!-- 图标相关-->
<style>
.article_detail{
width: 80%;
margin-left: 100px;
}
.comment_post{
width: 80%;
margin-left: 100px;
}
.comment_show{
width: 80%;
margin-left: 100px;
}
#commentform_title {
background-repeat: no-repeat;
padding: 0 0 0 25px;
margin-bottom: 10px;
}
.feedback_area_title {
border-bottom: 1px solid #ddd;
font-size: 14px;
font-weight: bold;
margin: 20px 0 10px;
}
#p{
border: deepskyblue 1px dashed;
background-color: antiquewhite;
}
</style>
</head>
<body>
<div class="comment_post"> {# 提交评论区域的div #}
<div id="commentform_title">发表评论</div>
<textarea rows="10" cols="60" id="comment_content"></textarea>
<p><button>提交评论</button></p>
</div>
<div class="comment_show"> {# 评论展示区域的div #}
<div class="feedback_area_title">评论列表</div>
<div class="comment_list">
{% for comment in comment_list %} {# 循环展示评论的数据 #}
<div>
<p>第{{ forloop.counter}}楼 -> By:{{ comment.user.username }} -> {{ comment.comment_time }} -> <button class="reply" username={{ comment.user.username }} pk={{ comment.pk }}>回复</button></p> {# 在此处定义一个回复按钮,用户实现子评论,并且自定义属性username和pk,用于下面回复功能的实现 #}
{% if comment.pre_comment_id %} {# 判断评论是否有父级评论 #}
<p id="p">原评论内容:{{ comment.pre_comment.comment_content }}</p> {# 如果有父级评论,则在中间显示父级评论的评论内容 #}
{% endif %}
<p>评论内容:{{ comment.comment_content }}</p>
<hr>
</div>
{% endfor %}
</div>
</div>
{# 提交评论的JS,发送Ajax请求 #}
<script>
var pid = "" {# 设置一个变量pid默认为空,用于后面作为数据库存储的父级评论的ID,如果没有父级评论则为空,子评论有父级评论 #}
{# 提交评论按钮的点击事件 #}
$(".comment_post p button").click(function (){
$.ajax({
url: '/comment_control/',
headers: {"X-CSRFToken": $.cookie("csrftoken")},
type: 'post',
data: {
comment_content: $("#comment_content").val(), {# 提交的数据内容data #}
article_id: {{ path }},
pid: pid
},
success: function (res){ {# 本例中返回的数据仅仅用于在控制台打印而已 #}
console.log(res) {# 控制台打印返回的数据 #}
$("#comment_content").val("") {# 提交完成后,清空评论输入框的内容 #}
pid = "" {# 子评论提交完成后,将pid默认设置为空,恢复为默认的父评论 #}
}
})
})
{# 回复按钮的点击事件 #}
$(".reply").click(function (){
$("#comment_content").focus() {# 回复按钮的事件,点击时,将光标聚集到评论输入框中 #}
var val = "@" + $(this).attr("username") + "\n" {# $(this)指代".reply"标签本身,获取这个标签的username值 #}
$("#comment_content").val(val) {# 回复时,自动在输入框加入:@要回复的人 #}
pid = $(this).attr("pk") {# 当点击回复时,父评论ID不再为空,此时修改为:对应评论的主键值ID #}
})
</script>
</body>
</html>
+19 -3
View File
@@ -106,6 +106,7 @@
if (page_tag === 'b9b101e') {
sub_btn = `<a class="dropdown-item" href="javascript:void(0);" onclick="window.open('2_d?id=${item.id}&s_p=b9b101e')">协同脑图</a>
<a class="dropdown-item" href="javascript:void(0);" onclick="mind.open_bianji(${item.id})">协同编辑</a>
<a class="dropdown-item" href="javascript:void(0);" onclick="mind.open_pinglun(${item.id})">评论</a>
<a class="dropdown-item" href="javascript:void(0);" onclick="mind.del_mind(${item.id},'card_${uuid}')">删除</a>`
@@ -144,9 +145,9 @@
});
},
/**显示分享链接*/
show_share_url: function (u, h) {
const url = `s_2_d?u=${u}&h=${h}`
$('#share_mind_modal .modal-body').html(`分享成功,请复制以下链接进行分享:<div>${window.location.protocol}//${window.location.host}/${url}</div>`)
show_share_url: function (u) {
const url = `2_pinglun?id=${u}`
$('#share_mind_modal .modal-body').html(`评论链接生成成功,请打开下面链接进行评论:<div>${window.location.protocol}//${window.location.host}/${url}</div>`)
$('#share_mind_modal_a').click()
},
/**分页组件*/
@@ -233,6 +234,20 @@
async: false
});
},
open_pinglun: function (mind_id) {
const me = this
$.ajax({
type: "post",
url: "pinglun",
headers: {"X-CSRFToken": $.cookie("csrftoken")},
data: {'id': mind_id},
dataType: "json",
success: function (resp) {
me.show_share_url(mind_id)
},
async: false
});
},
/**取消分享*/
share_cancel: function (mind_id) {
const me = this
@@ -671,6 +686,7 @@
<!-- 右侧内容 end-->
</div>
</div>
<!--导航 和 内容 end-->
</body>
<script src="/static/js/mind/modal.js"></script>
+16 -1
View File
@@ -200,6 +200,7 @@
async: false
});
},
/**分享脑图*/
share_mind: function (mind_id) {
const me = this
@@ -230,7 +231,21 @@
async: false
});
},
/**评论*/
open_pinglun: function (mind_id) {
const me = this
$.ajax({
type: "post",
url: "bianji",
headers: {"X-CSRFToken": $.cookie("csrftoken")},
data: {'id': mind_id},
dataType: "json",
success: function (resp) {
me.show_share_url(`${resp.data.u}`, `${resp.data.h}`)
},
async: false
});
},
/**取消分享*/
share_cancel: function (mind_id) {
const me = this
+1 -1
View File
@@ -108,7 +108,7 @@ const modal = `
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">分享成功</h5>
<h5 class="modal-title">评论生成成功</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>