From 532137e54cf42f9f5b0458cc82b490a172d2dca3 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Wed, 23 Aug 2023 23:14:53 -0500 Subject: [PATCH] Fix image url in user search --- dmoj/urls.py | 6 ---- judge/jinja2/gravatar.py | 12 ++++---- judge/views/select2.py | 60 ++++++++++------------------------------ templates/chat/chat.html | 2 +- 4 files changed, 21 insertions(+), 59 deletions(-) diff --git a/dmoj/urls.py b/dmoj/urls.py index 54a7f12..191600e 100644 --- a/dmoj/urls.py +++ b/dmoj/urls.py @@ -76,7 +76,6 @@ from judge.views.problem_data import ( from judge.views.register import ActivationView, RegistrationView from judge.views.select2 import ( AssigneeSelect2View, - ChatUserSearchSelect2View, CommentSelect2View, ContestSelect2View, ContestUserSearchSelect2View, @@ -808,11 +807,6 @@ 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/jinja2/gravatar.py b/judge/jinja2/gravatar.py index 2231ab3..c11e683 100644 --- a/judge/jinja2/gravatar.py +++ b/judge/jinja2/gravatar.py @@ -9,14 +9,14 @@ from . import registry @registry.function -def gravatar(profile, size=80, default=None): - assert isinstance(profile, Profile), "profile should be Profile" - profile_image = profile.profile_image +def gravatar(profile, size=80, default=None, profile_image=None, email=None): if profile_image: - return profile_image.url - if default is None: + return profile_image + if profile and profile.profile_image: + return profile.profile_image.url + if default is None and profile: default = profile.mute - email = profile.user.email + email = email or profile.user.email gravatar_url = ( "//www.gravatar.com/avatar/" + hashlib.md5(utf8bytes(email.strip().lower())).hexdigest() diff --git a/judge/views/select2.py b/judge/views/select2.py index 0d1e60a..065a728 100644 --- a/judge/views/select2.py +++ b/judge/views/select2.py @@ -1,8 +1,11 @@ +from urllib.parse import urljoin + from django.db.models import F, Q from django.http import Http404, JsonResponse from django.shortcuts import get_object_or_404 from django.utils.encoding import smart_text from django.views.generic.list import BaseListView +from django.conf import settings from chat_box.utils import encrypt_url @@ -54,7 +57,6 @@ class Select2View(BaseListView): class UserSelect2View(Select2View): def get(self, request, *args, **kwargs): self.org_id = kwargs.get("org_id", request.GET.get("org_id", "")) - print(self.org_id) return super(UserSelect2View, self).get(request, *args, **kwargs) def get_queryset(self): @@ -108,7 +110,7 @@ class UserSearchSelect2View(BaseListView): self.gravatar_default = request.GET.get("gravatar_default", None) 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() @@ -120,11 +122,19 @@ class UserSearchSelect2View(BaseListView): "text": username, "id": username, "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, } - 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(), } @@ -159,45 +169,3 @@ class AssigneeSelect2View(UserSearchSelect2View): return Profile.objects.filter( assigned_tickets__isnull=False, user__username__icontains=self.term ).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) diff --git a/templates/chat/chat.html b/templates/chat/chat.html index 9cde791..81ee7b0 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('chat_user_search_select2_ajax') }}' + url: '{{ url('user_search_select2_ajax') }}' }, minimumInputLength: 1, escapeMarkup: function (markup) {