Fix chat user search

This commit is contained in:
cuom1999 2023-08-24 10:37:04 -05:00
parent 47d3811aa5
commit 37e5e6a3b3
3 changed files with 48 additions and 18 deletions

View file

@ -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<contest>\w+)$",
ContestUserSearchSelect2View.as_view(),

View file

@ -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,
}

View file

@ -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) {