From e4b515c1d5032e0b1a68de6eca60eb357d63aefe Mon Sep 17 00:00:00 2001 From: Erik Kalkoken Date: Tue, 1 Jun 2021 11:38:24 +0000 Subject: [PATCH] Add button to update TS3 groups to admin page --- .../services/modules/teamspeak3/admin.py | 3 ++- .../admin/teamspeak3/authts/change_list.html | 11 +++++++++++ .../services/modules/teamspeak3/tests.py | 18 ++++++++++++++++++ .../services/modules/teamspeak3/urls.py | 14 ++++++++------ .../services/modules/teamspeak3/views.py | 9 +++++++++ docs/features/services/teamspeak3.md | 18 +++++++++++++++--- 6 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 allianceauth/services/modules/teamspeak3/templates/admin/teamspeak3/authts/change_list.html diff --git a/allianceauth/services/modules/teamspeak3/admin.py b/allianceauth/services/modules/teamspeak3/admin.py index 9b562ecc..942498f3 100644 --- a/allianceauth/services/modules/teamspeak3/admin.py +++ b/allianceauth/services/modules/teamspeak3/admin.py @@ -15,6 +15,7 @@ class Teamspeak3UserAdmin(ServicesUserAdmin): @admin.register(AuthTS) class AuthTSgroupAdmin(admin.ModelAdmin): + change_list_template = 'admin/teamspeak3/authts/change_list.html' ordering = ('auth_group__name', ) list_select_related = True @@ -28,7 +29,7 @@ class AuthTSgroupAdmin(admin.ModelAdmin): return [x for x in obj.ts_group.all().order_by('ts_group_id')] _ts_group.short_description = 'ts groups' - #_ts_group.admin_order_field = 'profile__state' + # _ts_group.admin_order_field = 'profile__state' @admin.register(StateGroup) diff --git a/allianceauth/services/modules/teamspeak3/templates/admin/teamspeak3/authts/change_list.html b/allianceauth/services/modules/teamspeak3/templates/admin/teamspeak3/authts/change_list.html new file mode 100644 index 00000000..5911ba5b --- /dev/null +++ b/allianceauth/services/modules/teamspeak3/templates/admin/teamspeak3/authts/change_list.html @@ -0,0 +1,11 @@ +{% extends "admin/change_list.html" %} +{% load i18n static %} + +{% block object-tools-items %} +{{ block.super }} +
  • + + Update TS3 groups + +
  • +{% endblock %} \ No newline at end of file diff --git a/allianceauth/services/modules/teamspeak3/tests.py b/allianceauth/services/modules/teamspeak3/tests.py index 44654ec9..9584a34a 100644 --- a/allianceauth/services/modules/teamspeak3/tests.py +++ b/allianceauth/services/modules/teamspeak3/tests.py @@ -216,6 +216,24 @@ class Teamspeak3ViewsTestCase(TestCase): self.assertEqual(ts3_user.perm_key, '123abc') self.assertTrue(tasks_manager.return_value.__enter__.return_value.update_groups.called) + @mock.patch(MODULE_PATH + '.views.Teamspeak3Tasks') + @mock.patch(MODULE_PATH + '.views.messages') + def test_should_update_ts_groups(self, messages, Teamspeak3Tasks): + # given + self.member.is_superuser = True + self.member.is_staff = True + self.member.save() + self.login() + # when + response = self.client.get(urls.reverse('teamspeak3:admin_update_ts3_groups')) + # then + self.assertRedirects( + response, urls.reverse('admin:teamspeak3_authts_changelist'), + target_status_code=200 + ) + self.assertTrue(messages.info.called) + self.assertTrue(Teamspeak3Tasks.run_ts3_group_update.delay.called) + class Teamspeak3SignalsTestCase(TestCase): def setUp(self): diff --git a/allianceauth/services/modules/teamspeak3/urls.py b/allianceauth/services/modules/teamspeak3/urls.py index 4cc91c6c..1f4282ff 100644 --- a/allianceauth/services/modules/teamspeak3/urls.py +++ b/allianceauth/services/modules/teamspeak3/urls.py @@ -6,12 +6,14 @@ app_name = 'teamspeak3' module_urls = [ # Teamspeak3 service control - url(r'^activate/$', views.activate_teamspeak3, - name='activate'), - url(r'^deactivate/$', views.deactivate_teamspeak3, - name='deactivate'), - url(r'^reset_perm/$', views.reset_teamspeak3_perm, - name='reset_perm'), + url(r'^activate/$', views.activate_teamspeak3, name='activate'), + url(r'^deactivate/$', views.deactivate_teamspeak3, name='deactivate'), + url(r'^reset_perm/$', views.reset_teamspeak3_perm, name='reset_perm'), + url( + r'^admin_update_ts3_groups/$', + views.admin_update_ts3_groups, + name='admin_update_ts3_groups' + ), # Teamspeak Urls url(r'^verify/$', views.verify_teamspeak3, name='verify'), diff --git a/allianceauth/services/modules/teamspeak3/views.py b/allianceauth/services/modules/teamspeak3/views.py index f72ed3e0..5a1226d0 100644 --- a/allianceauth/services/modules/teamspeak3/views.py +++ b/allianceauth/services/modules/teamspeak3/views.py @@ -1,6 +1,7 @@ import logging from django.contrib import messages +from django.contrib.admin.views.decorators import staff_member_required from django.contrib.auth.decorators import login_required, permission_required from django.shortcuts import render, redirect from django.conf import settings @@ -99,3 +100,11 @@ def reset_teamspeak3_perm(request): logger.error("Unsuccessful attempt to reset TS3 permission key for user %s" % request.user) messages.error(request, _('An error occurred while processing your TeamSpeak3 account.')) return redirect("services:services") + + +@login_required +@staff_member_required +def admin_update_ts3_groups(request): + Teamspeak3Tasks.run_ts3_group_update.delay() + messages.info(request, "Started updating TS3 server groups...") + return redirect("admin:teamspeak3_authts_changelist") diff --git a/docs/features/services/teamspeak3.md b/docs/features/services/teamspeak3.md index ef4880f4..80c7dad3 100644 --- a/docs/features/services/teamspeak3.md +++ b/docs/features/services/teamspeak3.md @@ -105,7 +105,9 @@ Click the URL provided to automatically connect to our server. It will prompt yo Now we need to make groups. AllianceAuth handles groups in teamspeak differently: instead of creating groups it creates an association between groups in TeamSpeak and groups in AllianceAuth. Go ahead and make the groups you want to associate with auth groups, keeping in mind multiple TeamSpeak groups can be associated with a single auth group. -Navigate back to the AllianceAuth admin interface (example.com/admin) and under `Services`, select `Auth / TS Groups`. In the top-right corner click `Add`. +Navigate back to the AllianceAuth admin interface (example.com/admin) and under `Teamspeak3`, select `Auth / TS Groups`. + +In the top-right corner click, first click on `Update TS3 Groups` to fetch the newly created server groups from TS3 (this may take a minute to complete). Then click on `Add Auth / TS Group` to link Auth groups with TS3 server groups. The dropdown box provides all auth groups. Select one and assign TeamSpeak groups from the panels below. If these panels are empty, wait a minute for the database update to run, or see the [troubleshooting section](#ts-group-models-not-populating-on-admin-site) below. @@ -119,13 +121,23 @@ To enable advanced permissions, on your client go to the `Tools` menu, `Applicat ### TS group models not populating on admin site -The method which populates these runs every 30 minutes. To populate manually, start a django shell: +The method which populates these runs every 30 minutes. To populate manually you start the process from the admin site or from the Django shell. + +#### Admin Site + +Navigate to the AllianceAuth admin interface and under `Teamspeak3`, select `Auth / TS Groups`. + +Then, in the top-right corner click, click on `Update TS3 Groups` to start the process of fetching the server groups from TS3 (this may take a minute to complete). + +#### Django Shell + +Start a django shell with: ```bash python manage.py shell ``` -And execute the update: +And execute the update as follows: ```python from allianceauth.services.modules.teamspeak3.tasks import Teamspeak3Tasks