Fix image url in user search

This commit is contained in:
cuom1999 2023-08-23 23:14:53 -05:00
parent fdbfa01f6b
commit 532137e54c
4 changed files with 21 additions and 59 deletions

View file

@ -76,7 +76,6 @@ from judge.views.problem_data import (
from judge.views.register import ActivationView, RegistrationView from judge.views.register import ActivationView, RegistrationView
from judge.views.select2 import ( from judge.views.select2 import (
AssigneeSelect2View, AssigneeSelect2View,
ChatUserSearchSelect2View,
CommentSelect2View, CommentSelect2View,
ContestSelect2View, ContestSelect2View,
ContestUserSearchSelect2View, ContestUserSearchSelect2View,
@ -808,11 +807,6 @@ urlpatterns = [
UserSearchSelect2View.as_view(), UserSearchSelect2View.as_view(),
name="user_search_select2_ajax", name="user_search_select2_ajax",
), ),
url(
r"^user_search_chat$",
ChatUserSearchSelect2View.as_view(),
name="chat_user_search_select2_ajax",
),
url( url(
r"^contest_users/(?P<contest>\w+)$", r"^contest_users/(?P<contest>\w+)$",
ContestUserSearchSelect2View.as_view(), ContestUserSearchSelect2View.as_view(),

View file

@ -9,14 +9,14 @@ from . import registry
@registry.function @registry.function
def gravatar(profile, size=80, default=None): def gravatar(profile, size=80, default=None, profile_image=None, email=None):
assert isinstance(profile, Profile), "profile should be Profile"
profile_image = profile.profile_image
if profile_image: if profile_image:
return profile_image.url return profile_image
if default is None: if profile and profile.profile_image:
return profile.profile_image.url
if default is None and profile:
default = profile.mute default = profile.mute
email = profile.user.email email = email or profile.user.email
gravatar_url = ( gravatar_url = (
"//www.gravatar.com/avatar/" "//www.gravatar.com/avatar/"
+ hashlib.md5(utf8bytes(email.strip().lower())).hexdigest() + hashlib.md5(utf8bytes(email.strip().lower())).hexdigest()

View file

@ -1,8 +1,11 @@
from urllib.parse import urljoin
from django.db.models import F, Q from django.db.models import F, Q
from django.http import Http404, JsonResponse from django.http import Http404, JsonResponse
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.utils.encoding import smart_text from django.utils.encoding import smart_text
from django.views.generic.list import BaseListView from django.views.generic.list import BaseListView
from django.conf import settings
from chat_box.utils import encrypt_url from chat_box.utils import encrypt_url
@ -54,7 +57,6 @@ class Select2View(BaseListView):
class UserSelect2View(Select2View): class UserSelect2View(Select2View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
self.org_id = kwargs.get("org_id", request.GET.get("org_id", "")) self.org_id = kwargs.get("org_id", request.GET.get("org_id", ""))
print(self.org_id)
return super(UserSelect2View, self).get(request, *args, **kwargs) return super(UserSelect2View, self).get(request, *args, **kwargs)
def get_queryset(self): def get_queryset(self):
@ -108,7 +110,7 @@ class UserSearchSelect2View(BaseListView):
self.gravatar_default = request.GET.get("gravatar_default", None) self.gravatar_default = request.GET.get("gravatar_default", None)
self.object_list = self.get_queryset().values_list( self.object_list = self.get_queryset().values_list(
"pk", "user__username", "user__email", "display_rank" "pk", "user__username", "user__email", "display_rank", "profile_image"
) )
context = self.get_context_data() context = self.get_context_data()
@ -120,11 +122,19 @@ class UserSearchSelect2View(BaseListView):
"text": username, "text": username,
"id": username, "id": username,
"gravatar_url": gravatar( "gravatar_url": gravatar(
email, self.gravatar_size, self.gravatar_default None,
self.gravatar_size,
self.gravatar_default,
urljoin(settings.MEDIA_URL, profile_image)
if profile_image
else None,
email,
), ),
"display_rank": display_rank, "display_rank": display_rank,
} }
for pk, username, email, display_rank in context["object_list"] for pk, username, email, display_rank, profile_image in context[
"object_list"
]
], ],
"more": context["page_obj"].has_next(), "more": context["page_obj"].has_next(),
} }
@ -159,45 +169,3 @@ class AssigneeSelect2View(UserSearchSelect2View):
return Profile.objects.filter( return Profile.objects.filter(
assigned_tickets__isnull=False, user__username__icontains=self.term assigned_tickets__isnull=False, user__username__icontains=self.term
).distinct() ).distinct()
class ChatUserSearchSelect2View(BaseListView):
paginate_by = 20
def get_queryset(self): # TODO: add block
return _get_user_queryset(self.term)
def get(self, request, *args, **kwargs):
if not self.request.user.is_authenticated:
raise Http404()
self.request = request
self.kwargs = kwargs
self.term = kwargs.get("term", request.GET.get("term", ""))
self.gravatar_size = request.GET.get("gravatar_size", 128)
self.gravatar_default = request.GET.get("gravatar_default", None)
self.object_list = self.get_queryset().values_list(
"pk", "user__username", "user__email", "display_rank"
)
context = self.get_context_data()
return JsonResponse(
{
"results": [
{
"text": username,
"id": encrypt_url(request.profile.id, pk),
"gravatar_url": gravatar(
email, self.gravatar_size, self.gravatar_default
),
"display_rank": display_rank,
}
for pk, username, email, display_rank in context["object_list"]
],
"more": context["page_obj"].has_next(),
}
)
def get_name(self, obj):
return str(obj)

View file

@ -545,7 +545,7 @@
$('#search-handle').select2({ $('#search-handle').select2({
placeholder: '{{ _('Search by handle...') }}', placeholder: '{{ _('Search by handle...') }}',
ajax: { ajax: {
url: '{{ url('chat_user_search_select2_ajax') }}' url: '{{ url('user_search_select2_ajax') }}'
}, },
minimumInputLength: 1, minimumInputLength: 1,
escapeMarkup: function (markup) { escapeMarkup: function (markup) {