From 37e5e6a3b3a65e094e3c19d6736cd1f97f99aaf8 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Thu, 24 Aug 2023 10:37:04 -0500 Subject: [PATCH] Fix chat user search --- dmoj/urls.py | 6 +++++ judge/views/select2.py | 58 ++++++++++++++++++++++++++++------------ templates/chat/chat.html | 2 +- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/dmoj/urls.py b/dmoj/urls.py index 191600e..54a7f12 100644 --- a/dmoj/urls.py +++ b/dmoj/urls.py @@ -76,6 +76,7 @@ from judge.views.problem_data import ( from judge.views.register import ActivationView, RegistrationView from judge.views.select2 import ( AssigneeSelect2View, + ChatUserSearchSelect2View, CommentSelect2View, ContestSelect2View, ContestUserSearchSelect2View, @@ -807,6 +808,11 @@ urlpatterns = [ UserSearchSelect2View.as_view(), name="user_search_select2_ajax", ), + url( + r"^user_search_chat$", + ChatUserSearchSelect2View.as_view(), + name="chat_user_search_select2_ajax", + ), url( r"^contest_users/(?P\w+)$", ContestUserSearchSelect2View.as_view(), diff --git a/judge/views/select2.py b/judge/views/select2.py index 065a728..34d0778 100644 --- a/judge/views/select2.py +++ b/judge/views/select2.py @@ -102,6 +102,21 @@ class UserSearchSelect2View(BaseListView): def get_queryset(self): return _get_user_queryset(self.term) + def get_json_result_from_object(self, user_tuple): + pk, username, email, display_rank, profile_image = user_tuple + return { + "text": username, + "id": username, + "gravatar_url": gravatar( + None, + self.gravatar_size, + self.gravatar_default, + self.get_profile_image_url(profile_image), + email, + ), + "display_rank": display_rank, + } + def get(self, request, *args, **kwargs): self.request = request self.kwargs = kwargs @@ -118,23 +133,8 @@ class UserSearchSelect2View(BaseListView): return JsonResponse( { "results": [ - { - "text": username, - "id": username, - "gravatar_url": gravatar( - None, - self.gravatar_size, - self.gravatar_default, - urljoin(settings.MEDIA_URL, profile_image) - if profile_image - else None, - email, - ), - "display_rank": display_rank, - } - for pk, username, email, display_rank, profile_image in context[ - "object_list" - ] + self.get_json_result_from_object(user_tuple) + for user_tuple in context["object_list"] ], "more": context["page_obj"].has_next(), } @@ -143,6 +143,11 @@ class UserSearchSelect2View(BaseListView): def get_name(self, obj): return str(obj) + def get_profile_image_url(self, profile_image): + if profile_image: + return urljoin(settings.MEDIA_URL, profile_image) + return None + class ContestUserSearchSelect2View(UserSearchSelect2View): def get_queryset(self): @@ -169,3 +174,22 @@ class AssigneeSelect2View(UserSearchSelect2View): return Profile.objects.filter( assigned_tickets__isnull=False, user__username__icontains=self.term ).distinct() + + +class ChatUserSearchSelect2View(UserSearchSelect2View): + def get_json_result_from_object(self, user_tuple): + if not self.request.user.is_authenticated: + raise Http404() + pk, username, email, display_rank, profile_image = user_tuple + return { + "text": username, + "id": encrypt_url(self.request.profile.id, pk), + "gravatar_url": gravatar( + None, + self.gravatar_size, + self.gravatar_default, + self.get_profile_image_url(profile_image), + email, + ), + "display_rank": display_rank, + } diff --git a/templates/chat/chat.html b/templates/chat/chat.html index 81ee7b0..9cde791 100644 --- a/templates/chat/chat.html +++ b/templates/chat/chat.html @@ -545,7 +545,7 @@ $('#search-handle').select2({ placeholder: '{{ _('Search by handle...') }}', ajax: { - url: '{{ url('user_search_select2_ajax') }}' + url: '{{ url('chat_user_search_select2_ajax') }}' }, minimumInputLength: 1, escapeMarkup: function (markup) {