浏览代码

Revision comments (en cours)

omassot 7 年之前
父节点
当前提交
f0120a1e1d
共有 5 个文件被更改,包括 111 次插入13 次删除
  1. 5 3
      main/models.py
  2. 21 0
      main/static/css/custom.css
  3. 56 8
      main/templates/_comment_div.html
  4. 3 1
      main/urls.py
  5. 26 1
      main/views.py

+ 5 - 3
main/models.py

@@ -3,6 +3,7 @@ import datetime
 import uuid
 
 from django.contrib.auth.models import User
+from django.core import serializers
 from django.db import models, connection
 from django.db.models.aggregates import Sum
 from martor.models import MartorField
@@ -25,7 +26,10 @@ class BaseModel(models.Model):
 
     def comments(self):
         return Comment.objects.filter(obj_uuid = self.uuid)
-    
+
+    def to_json(self):
+        return serializers.serialize('json', [ self, ])
+
 class Project(BaseModel):
     class Meta:
         verbose_name = "projet"
@@ -188,6 +192,4 @@ class Comment(BaseModel):
     author = models.ForeignKey(User, 
                                on_delete=models.PROTECT, 
                                verbose_name="Auteur")
-
-    
     

+ 21 - 0
main/static/css/custom.css

@@ -269,6 +269,12 @@ select[multiple] option {
 
 /* Commentaires */
 
+.comment-header {
+	color: #25a2c3;
+	font-size: 1rem;
+	font-weight: 700;
+}
+
 .comment-section {
 	margin: 0 2.5%; 
 }
@@ -277,6 +283,21 @@ select[multiple] option {
 	height: 50px;
 }
 
+.comment-list {
+	list-style: none;
+	padding-left: 0;	
+}
+
+.comment:first-child {
+	border-top: 0;
+	padding-top: 0;
+}
+
+.comment {
+	border-top: solid 1px #dee1e3;
+	padding: 0.5em 0;
+}
+
 .comment-section .comment-display {
 	border-left: solid 2px #d3c5c5;
 	padding: 5px 20px;

+ 56 - 8
main/templates/_comment_div.html

@@ -3,24 +3,72 @@
 
 {% load martortags %}
 
-<div class="comment-section">
-	<ul class="alt">
+<div class="comment-section" data-obj-uuid="{{ obj.uuid }}">
+	<ul class="comment-list">
 	{% for comment in obj.comments.all %}
-		<li class="flex-col">
-			<div><h5><i class="fa fa-comment"></i> {{ comment.author }}, le {{ comment.created }}:</h5></b></div>
+		<li class="comment flex-col" data-id="{{ comment.id }}">
+			<div class="flex-row">
+				<span class="comment-header"><i class="fa fa-comment"></i> {{ comment.author }}, le {{ comment.created }}</span>
+				<a class="disabled comment-edit-btn" href="" style="margin-left: 20px;"><i class="fa fa-pencil"></i></a>
+				<a class="disabled comment-del-btn" href="" style="margin-left: 10px;"><i class="fa fa-trash"></i></a>
+			</div>
 			<div class="comment-display">
 				{{ comment.content|safe_markdown }}
 			</div>
-		</li>
+	    </li>
 	{% endfor %}
 	</ul>
 	
-	<form action="{% url 'comment' obj_uuid=obj.uuid %}" method="post" accept-charset="utf-8">
+	<form action="{% url 'comment_post' obj_uuid=obj.uuid %}" method="post" accept-charset="utf-8">
 		{% csrf_token %}
-		{{ comment_form.as_p }}
+		{{ comment_form.content }}
 		
     	<div class="flex-row flex-end" style="margin-top: 10px;">
     		<input type="submit">
     	</div>
     </form>
-</div>
+    
+</div>
+
+<!-- <script>
+	
+	// Nouveau commentaire
+    $(".comment-section input[type=submit]").click(function (event) {
+    	event.preventDefault();
+    	
+    	var section = $(this).closest(".comment-section");
+    	var form = section.find("form");
+     	var obj_uuid = section.data("obj-uuid");
+     	var csrf_token= form.find("input[name=csrfmiddlewaretoken]").attr('value');
+     	var target = form.attr('action');
+
+     	var content = form.find("textarea").val();
+        if (!content) {
+            return;
+        }
+        
+        $.ajax({
+            type: "POST",
+            url: target,
+            headers: {"X-CSRFToken": csrf_token},
+            data: '{"content": ' + JSON.stringify(content) + '}',
+            contentType: "application/json; charset=utf-8",
+            dataType: "json",
+            success: function (comment) {
+            	form.find("textarea").val("");
+            	location.reload();
+            },
+            failure: function (response) {
+            	// restore the default behavior
+                console.log(response.responseText);
+            	alert("An error occured (see the console for more informations)");
+            },
+            error: function (response) {
+            	// restore the default behavior
+            	console.log(response.responseText);
+            	alert("An error occured (see the console for more informations)");
+            }
+        });
+    });
+	
+</script> -->

+ 3 - 1
main/urls.py

@@ -33,6 +33,8 @@ urlpatterns = [
     path('reports/', views.reports, name='reports'),
     path('reports/sprints/', views.report_sprints, name='report_sprints'),
     path('reports/projects/', views.report_projects, name='report_projects'),
-    path('comment/<obj_uuid>/', views.comment, name='comment'),
+    path('comment-post/<obj_uuid>/', views.comment_post, name='comment_post'),
+#     path('comment-edit/<int:comment_id>/', views.comment_edit, name='comment_edit'),
+#     path('comment-del/<int:comment_id>/', views.comment_del, name='comment_del'),
     path('search/', views.search, name='search'),
 ]

+ 26 - 1
main/views.py

@@ -1,10 +1,13 @@
 from datetime import datetime
+import json
 
 from django.contrib.auth import logout, login, update_session_auth_hash
 from django.contrib.auth.decorators import login_required
 from django.contrib.auth.forms import PasswordChangeForm
 from django.contrib.auth.models import User
+from django.core import serializers
 from django.core.paginator import Paginator
+from django.http.response import HttpResponse
 from django.shortcuts import render, get_object_or_404, redirect
 
 from main.forms import StoryForm, EpicForm, RegisterForm, ProfileForm, \
@@ -244,7 +247,7 @@ def report_projects(request):
     return render(request, 'reports/report_projects.html', {'epics': epics})
 
 @login_required
-def comment(request, obj_uuid):
+def comment_post(request, obj_uuid):
     comment = Comment()
     comment.obj_uuid = obj_uuid
     comment.author = get_object_or_404(User, username=request.user)
@@ -252,6 +255,28 @@ def comment(request, obj_uuid):
     comment.save()
     return redirect(request.META['HTTP_REFERER'])
 
+# @login_required
+# def comment_post(request, obj_uuid):
+#     data = json.loads(request.body.decode('utf-8'))
+#     comment = Comment()
+#     comment.obj_uuid = obj_uuid
+#     comment.author = get_object_or_404(User, username=request.user)
+#     comment.content = data["content"]
+#     comment.save()
+#     return HttpResponse(comment.to_json(),content_type="application/json")
+# 
+# def comment_edit(request, comment_id):
+#     data = json.loads(request.body.decode('utf-8'))
+#     comment = get_object_or_404(Comment, id=comment_id)
+#     comment.content= data["content"]
+#     comment.save()
+#     return HttpResponse(comment.to_json(),content_type="application/json")
+# 
+# def comment_del(_, comment_id):
+#     comment = get_object_or_404(Comment, id=comment_id)
+#     comment.delete()
+#     return HttpResponse("{}",content_type="application/json")
+
 @login_required
 def search(request):
     qstr = request.GET["q"]