From e48d34c52c02efc5c22f6ee529a448ec2a6118e9 Mon Sep 17 00:00:00 2001 From: xiaoan <419177984@qq.com> Date: Mon, 6 May 2024 12:35:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=AF=84=E8=AE=BA=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jsmindmulti/settings.py | 2 +- server/models.py | 15 +++++ server/urls.py | 5 +- server/views/jump.py | 10 +++- server/views/mind_map.py | 26 +++++++++ server_auth/lib/auth.py | 17 ++++++ static/html/commont.html | 107 +++++++++++++++++++++++++++++++++++ static/html/dashboard.html | 22 ++++++- static/html/dashboard11.html | 17 +++++- static/js/mind/modal.js | 2 +- 10 files changed, 215 insertions(+), 8 deletions(-) create mode 100644 static/html/commont.html diff --git a/jsmindmulti/settings.py b/jsmindmulti/settings.py index 94b61a2..d626ac5 100644 --- a/jsmindmulti/settings.py +++ b/jsmindmulti/settings.py @@ -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', diff --git a/server/models.py b/server/models.py index 791563b..0b4223c 100644 --- a/server/models.py +++ b/server/models.py @@ -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 diff --git a/server/urls.py b/server/urls.py index 8de0f86..e6b174b 100644 --- a/server/urls.py +++ b/server/urls.py @@ -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') #提交评论处理的路由 ] diff --git a/server/views/jump.py b/server/views/jump.py index 978266b..6d651bd 100644 --- a/server/views/jump.py +++ b/server/views/jump.py @@ -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): """ 分享页面 diff --git a/server/views/mind_map.py b/server/views/mind_map.py index 73190d0..f484d59 100644 --- a/server/views/mind_map.py +++ b/server/views/mind_map.py @@ -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): """ diff --git a/server_auth/lib/auth.py b/server_auth/lib/auth.py index f6edc76..cc8bd14 100644 --- a/server_auth/lib/auth.py +++ b/server_auth/lib/auth.py @@ -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 + + diff --git a/static/html/commont.html b/static/html/commont.html new file mode 100644 index 0000000..5a343e6 --- /dev/null +++ b/static/html/commont.html @@ -0,0 +1,107 @@ + + + + + 文章详情 + + + + + + + + + + + + + + +
{# 提交评论区域的div #} +
发表评论
+ +

+
+ + +
{# 评论展示区域的div #} +
评论列表
+
+ {% for comment in comment_list %} {# 循环展示评论的数据 #} +
+

第{{ forloop.counter}}楼 -> By:{{ comment.user.username }} -> {{ comment.comment_time }} ->

{# 在此处定义一个回复按钮,用户实现子评论,并且自定义属性username和pk,用于下面回复功能的实现 #} + {% if comment.pre_comment_id %} {# 判断评论是否有父级评论 #} +

原评论内容:{{ comment.pre_comment.comment_content }}

{# 如果有父级评论,则在中间显示父级评论的评论内容 #} + {% endif %} +

评论内容:{{ comment.comment_content }}

+
+
+ {% endfor %} +
+
+ + + {# 提交评论的JS,发送Ajax请求 #} + + + + \ No newline at end of file diff --git a/static/html/dashboard.html b/static/html/dashboard.html index 3f2b070..fe0e630 100644 --- a/static/html/dashboard.html +++ b/static/html/dashboard.html @@ -106,6 +106,7 @@ if (page_tag === 'b9b101e') { sub_btn = `协同脑图 协同编辑 +评论 删除` @@ -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(`分享成功,请复制以下链接进行分享:
${window.location.protocol}//${window.location.host}/${url}
`) + show_share_url: function (u) { + const url = `2_pinglun?id=${u}` + $('#share_mind_modal .modal-body').html(`评论链接生成成功,请打开下面链接进行评论:
${window.location.protocol}//${window.location.host}/${url}
`) $('#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 @@ + diff --git a/static/html/dashboard11.html b/static/html/dashboard11.html index 2187aa4..33ce848 100644 --- a/static/html/dashboard11.html +++ b/static/html/dashboard11.html @@ -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 diff --git a/static/js/mind/modal.js b/static/js/mind/modal.js index 78aef54..b21f7f9 100644 --- a/static/js/mind/modal.js +++ b/static/js/mind/modal.js @@ -108,7 +108,7 @@ const modal = `