Delete messages by id
This commit is contained in:
parent
e60184545a
commit
49de777c9f
2 changed files with 15 additions and 9 deletions
|
@ -2,6 +2,8 @@ from django.utils.translation import gettext as _
|
|||
from django.views.generic import ListView
|
||||
from django.http import HttpResponse, JsonResponse
|
||||
from django.core.paginator import Paginator
|
||||
from django.shortcuts import render
|
||||
from django.forms.models import model_to_dict
|
||||
|
||||
from judge.jinja2.gravatar import gravatar
|
||||
from .models import Message
|
||||
|
@ -18,6 +20,7 @@ def format_messages(messages):
|
|||
'author': str(msg.author),
|
||||
'body': msg.body,
|
||||
'image': gravatar(msg.author, 32),
|
||||
'id': msg.id
|
||||
} for msg in messages]
|
||||
return json.dumps(msg_list)
|
||||
|
||||
|
@ -30,9 +33,13 @@ class ChatView(ListView):
|
|||
paginate_by = 50
|
||||
paginator = Paginator(Message.objects.filter(hidden=False), paginate_by)
|
||||
|
||||
def get_queryset(self):
|
||||
return Message.objects.filter(hidden=False)
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
page = request.GET.get('page')
|
||||
if (page == None):
|
||||
# return render(request, 'chat/chat.html', {'message': format_messages(Message.objects.all())})
|
||||
return super().get(request, *args, **kwargs)
|
||||
|
||||
cur_page = self.paginator.get_page(page)
|
||||
|
@ -54,16 +61,16 @@ def delete_message(request):
|
|||
return JsonResponse(ret)
|
||||
|
||||
if request.user.is_staff:
|
||||
author = request.POST.get('author')
|
||||
time = request.POST.get('messtime')
|
||||
messid = int(request.POST.get('messid'))
|
||||
all_mess = Message.objects.all()
|
||||
|
||||
for mess in all_mess:
|
||||
if mess.author.__str__() == author and format_time(mess.time) == time:
|
||||
if mess.id == messid:
|
||||
mess.hidden = True
|
||||
mess.save()
|
||||
new_elt = {'time': format_time(mess.time), 'content': mess.body}
|
||||
ret = new_elt
|
||||
break
|
||||
|
||||
return JsonResponse(ret)
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
loadMessage(data['body'],
|
||||
data['author'],
|
||||
data['time'],
|
||||
data['id'],
|
||||
data['image'],
|
||||
true)
|
||||
$('#chat-box').scrollTop($('#chat-box')[0].scrollHeight);
|
||||
|
@ -28,7 +29,7 @@
|
|||
});
|
||||
}
|
||||
|
||||
function loadMessage(content, user, time, image, isNew) {
|
||||
function loadMessage(content, user, time, messid, image, isNew) {
|
||||
if (isNew) content = encodeHTML(content)
|
||||
li = `<li class="message">
|
||||
<img src="${image}" class="profile-pic">
|
||||
|
@ -39,7 +40,7 @@
|
|||
</a>
|
||||
<span class="time">${time}</span>
|
||||
{% if request.user.is_staff %}
|
||||
<a class="chatbtn_remove_mess" data-messtime="${time}" data-author="${user}">Delete</a>
|
||||
<a class="chatbtn_remove_mess" data-messtime="${time}" data-author="${user}" data-messid="${messid}">Delete</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
<span class="content-message">${content} </span>
|
||||
|
@ -58,9 +59,7 @@
|
|||
(function init_chatlog() {
|
||||
ul = $('#chat-log')
|
||||
{% for msg in message %}
|
||||
{% if not msg.hidden %}
|
||||
loadMessage(`{{msg.body}}`, `{{msg.author}}`, `{{msg.time}}`, `{{gravatar(msg.author, 32)}}`)
|
||||
{% endif %}
|
||||
loadMessage(`{{msg.body}}`, `{{msg.author}}`, `{{msg.time}}`, `{{msg.id}}`, `{{gravatar(msg.author, 32)}}`)
|
||||
{% endfor %}
|
||||
$('#chat-box').scrollTop($('#chat-box')[0].scrollHeight);
|
||||
})()
|
||||
|
@ -84,7 +83,7 @@
|
|||
data = JSON.parse(data)
|
||||
setTimeout( () => {
|
||||
for (msg of data) {
|
||||
loadMessage(msg.body, msg.author, msg.time, msg.image)
|
||||
loadMessage(msg.body, msg.author, msg.time, msg.id, msg.image)
|
||||
}
|
||||
|
||||
loader.hide()
|
||||
|
|
Loading…
Reference in a new issue