From 7856cd5ce405d70fc1c6d0eec3fa264e7f360409 Mon Sep 17 00:00:00 2001 From: Peter Pfeufer Date: Sat, 26 Aug 2023 00:47:08 +0200 Subject: [PATCH 1/7] [ADD] Respect and display the new Discord username format when eligible --- .../services/modules/discord/admin.py | 7 ++-- .../services/modules/discord/auth_hooks.py | 40 +++++++++++++++++-- .../modules/discord/tests/test_auth_hooks.py | 20 ++++++++++ 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/allianceauth/services/modules/discord/admin.py b/allianceauth/services/modules/discord/admin.py index 226bf113..4db920bf 100644 --- a/allianceauth/services/modules/discord/admin.py +++ b/allianceauth/services/modules/discord/admin.py @@ -6,6 +6,7 @@ from ...admin import ServicesUserAdmin from . import __title__ from .models import DiscordUser from .utils import LoggerAddTag +from .auth_hooks import DiscordService logger = LoggerAddTag(logging.getLogger(__name__), __title__) @@ -27,6 +28,6 @@ class DiscordUserAdmin(ServicesUserAdmin): @admin.display(description='Discord Username', ordering='username') def _username(self, obj): - if obj.username and obj.discriminator: - return f'{obj.username}#{obj.discriminator}' - return '' + return DiscordService.get_discord_username( + username=obj.username, discriminator=obj.discriminator + ) diff --git a/allianceauth/services/modules/discord/auth_hooks.py b/allianceauth/services/modules/discord/auth_hooks.py index be2956d8..2d6bd204 100644 --- a/allianceauth/services/modules/discord/auth_hooks.py +++ b/allianceauth/services/modules/discord/auth_hooks.py @@ -30,6 +30,29 @@ class DiscordService(ServicesHook): self.access_perm = 'discord.access_discord' self.name_format = '{character_name}' + @staticmethod + def get_discord_username(username:str, discriminator:str) -> str: + """ + Determine the Discord username (Old and new format) + :param username: + :type username: + :param discriminator: + :type discriminator: + :return: + :rtype: + """ + + if username and discriminator: + discord_username = f'{username}#{discriminator}' + + # New Discord user name format + if discriminator == '0': + discord_username = f'@{username}' + else: + discord_username = '' + + return discord_username + def delete_user(self, user: User, notify_user: bool = False) -> None: if self.user_has_account(user): logger.debug('Deleting user %s %s account', user, self.name) @@ -43,10 +66,19 @@ class DiscordService(ServicesHook): user_has_account = True username = request.user.discord.username discriminator = request.user.discord.discriminator - if username and discriminator: - discord_username = f'{username}#{discriminator}' - else: - discord_username = '' + + discord_username = self.get_discord_username( + username=username, discriminator=discriminator + ) + + # if username and discriminator: + # discord_username = f'{username}#{discriminator}' + # + # # New Discord user name format + # if discriminator == '0': + # discord_username = f'@{username}' + # else: + # discord_username = '' else: discord_username = '' user_has_account = False diff --git a/allianceauth/services/modules/discord/tests/test_auth_hooks.py b/allianceauth/services/modules/discord/tests/test_auth_hooks.py index 256f759c..61c07cbf 100644 --- a/allianceauth/services/modules/discord/tests/test_auth_hooks.py +++ b/allianceauth/services/modules/discord/tests/test_auth_hooks.py @@ -150,3 +150,23 @@ class TestDiscordService(NoSocketsTestCase): self.assertTemplateUsed(service.service_ctrl_template) self.assertIn('/discord/reset/', response) self.assertIn('/discord/deactivate/', response) + + def test_new_discord_username_format(self): + """ + Test if we get Discord's new username format + :return: + :rtype: + """ + + # given + username = 'william_riker' + discriminator = '0' # Seems to be returned as 0 for Discord's new username format + + # when + discord_username = DiscordService.get_discord_username( + username=username, discriminator=discriminator + ) + + # then + expected_username = '@william_riker' + self.assertEqual(first=discord_username, second=expected_username) From 66822107e3ed67611dced9c148b8ed4cd4160fcb Mon Sep 17 00:00:00 2001 From: Peter Pfeufer Date: Mon, 28 Aug 2023 21:40:28 +0200 Subject: [PATCH 2/7] [ADD] Language code to page and language selector --- allianceauth/authentication/templates/public/lang_select.html | 2 +- allianceauth/templates/allianceauth/base.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/allianceauth/authentication/templates/public/lang_select.html b/allianceauth/authentication/templates/public/lang_select.html index 90a135d3..6556ad24 100644 --- a/allianceauth/authentication/templates/public/lang_select.html +++ b/allianceauth/authentication/templates/public/lang_select.html @@ -5,7 +5,7 @@