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