Added view to clear the chatbox.

This commit is contained in:
Norman Jäckel 2016-10-17 16:56:19 +02:00
parent 7965fd3fce
commit 639748bb3b
4 changed files with 50 additions and 10 deletions

View File

@ -22,6 +22,7 @@ Core:
- Moved custom slides to own app "topics". Renamed it to "Topic". - Moved custom slides to own app "topics". Renamed it to "Topic".
- Added support for multiple projectors. - Added support for multiple projectors.
- Added an overlay for the current list of speakers. - Added an overlay for the current list of speakers.
- Added button to clear the chatbox.
Motions: Motions:
- Added origin field. - Added origin field.

View File

@ -1298,7 +1298,7 @@ angular.module('OpenSlidesApp.core.site', [
}); });
$scope.clearChatHistory = function () { $scope.clearChatHistory = function () {
console.log("Clear"); $http.post('/rest/core/chatmessage/clear/');
}; };
} }
]) ])

View File

@ -15,25 +15,27 @@ from django.core.urlresolvers import get_resolver
from django.db.models import F from django.db.models import F
from django.http import Http404, HttpResponse from django.http import Http404, HttpResponse
from django.utils.timezone import now from django.utils.timezone import now
from django.utils.translation import ugettext as _
from openslides import __version__ as version from .. import __version__ as version
from openslides.utils import views as utils_views from ..utils import views as utils_views
from openslides.utils.collection import Collection, CollectionElement from ..utils.autoupdate import inform_deleted_data
from openslides.utils.plugins import ( from ..utils.collection import Collection, CollectionElement
from ..utils.plugins import (
get_plugin_description, get_plugin_description,
get_plugin_verbose_name, get_plugin_verbose_name,
get_plugin_version, get_plugin_version,
) )
from openslides.utils.rest_api import ( from ..utils.rest_api import (
ModelViewSet, ModelViewSet,
Response, Response,
SimpleMetadata, SimpleMetadata,
ValidationError, ValidationError,
ViewSet, ViewSet,
detail_route, detail_route,
list_route,
) )
from openslides.utils.search import search from ..utils.search import search
from .access_permissions import ( from .access_permissions import (
ChatMessageAccessPermissions, ChatMessageAccessPermissions,
ConfigAccessPermissions, ConfigAccessPermissions,
@ -667,13 +669,18 @@ class ChatMessageViewSet(ModelViewSet):
""" """
if self.action in ('list', 'retrieve'): if self.action in ('list', 'retrieve'):
result = self.get_access_permissions().check_permissions(self.request.user) result = self.get_access_permissions().check_permissions(self.request.user)
else: elif self.action in ('metadata', 'create'):
# We do not want anonymous users to use the chat even the anonymous # We do not want anonymous users to use the chat even the anonymous
# group has the permission core.can_use_chat. # group has the permission core.can_use_chat.
result = ( result = (
self.action in ('metadata', 'create') and
self.request.user.is_authenticated() and self.request.user.is_authenticated() and
self.request.user.has_perm('core.can_use_chat')) self.request.user.has_perm('core.can_use_chat'))
elif self.action == 'clear':
result = (
self.request.user.has_perm('core.can_use_chat') and
self.request.user.has_perm('core.can_manage_chat'))
else:
result = False
return result return result
def perform_create(self, serializer): def perform_create(self, serializer):
@ -683,6 +690,22 @@ class ChatMessageViewSet(ModelViewSet):
""" """
serializer.save(user=self.request.user) serializer.save(user=self.request.user)
@list_route(methods=['post'])
def clear(self, request):
"""
Deletes all chat messages.
"""
# Collect all chat messages with their collection_string and id
chatmessages = ChatMessage.objects.all()
args = []
for chatmessage in chatmessages:
args.append(chatmessage.get_collection_string())
args.append(chatmessage.pk)
chatmessages.delete()
# Trigger autoupdate and setup response.
inform_deleted_data(*args)
return Response({'detail': _('All chat messages deleted successfully.')})
# Special API views # Special API views

View File

@ -1,4 +1,5 @@
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APIClient from rest_framework.test import APIClient
from openslides.core.config import config from openslides.core.config import config
@ -145,3 +146,18 @@ class TestConfigDBQueries(TestCase):
""" """
with self.assertNumQueries(60): with self.assertNumQueries(60):
self.client.get(reverse('config-list')) self.client.get(reverse('config-list'))
class ChatMessageViewSet(TestCase):
"""
Tests requests to deal with chat messages.
"""
def setUp(self):
admin = User.objects.get(pk=1)
self.client.force_login(admin)
ChatMessage.objects.create(message='test_message_peechiel8IeZoohaem9e', user=admin)
def test_clear_chat(self):
response = self.client.post(reverse('chatmessage-clear'))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(ChatMessage.objects.all().count(), 0)