2023-08-24 04:14:53 +00:00
|
|
|
from urllib.parse import urljoin
|
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
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
|
2023-08-24 04:14:53 +00:00
|
|
|
from django.conf import settings
|
2020-01-21 06:35:58 +00:00
|
|
|
|
2021-11-21 04:23:03 +00:00
|
|
|
from chat_box.utils import encrypt_url
|
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
from judge.jinja2.gravatar import gravatar
|
|
|
|
from judge.models import Comment, Contest, Organization, Problem, Profile
|
|
|
|
|
|
|
|
|
2022-09-15 07:13:14 +00:00
|
|
|
def _get_user_queryset(term, org_id=None):
|
2022-09-15 07:05:02 +00:00
|
|
|
if org_id:
|
|
|
|
try:
|
|
|
|
qs = Organization.objects.get(id=org_id).members.all()
|
|
|
|
except Exception:
|
|
|
|
raise Http404()
|
|
|
|
else:
|
|
|
|
qs = Profile.objects
|
2022-05-14 17:57:27 +00:00
|
|
|
if term.endswith(" "):
|
2020-01-21 06:35:58 +00:00
|
|
|
qs = qs.filter(user__username=term.strip())
|
|
|
|
else:
|
|
|
|
qs = qs.filter(user__username__icontains=term)
|
|
|
|
return qs
|
|
|
|
|
|
|
|
|
|
|
|
class Select2View(BaseListView):
|
|
|
|
paginate_by = 20
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.request = request
|
2022-05-14 17:57:27 +00:00
|
|
|
self.term = kwargs.get("term", request.GET.get("term", ""))
|
2020-01-21 06:35:58 +00:00
|
|
|
self.object_list = self.get_queryset()
|
|
|
|
context = self.get_context_data()
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
return JsonResponse(
|
|
|
|
{
|
|
|
|
"results": [
|
|
|
|
{
|
|
|
|
"text": smart_text(self.get_name(obj)),
|
|
|
|
"id": obj.pk,
|
|
|
|
}
|
|
|
|
for obj in context["object_list"]
|
|
|
|
],
|
|
|
|
"more": context["page_obj"].has_next(),
|
|
|
|
}
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def get_name(self, obj):
|
|
|
|
return str(obj)
|
|
|
|
|
|
|
|
|
|
|
|
class UserSelect2View(Select2View):
|
2022-09-15 07:05:02 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.org_id = kwargs.get("org_id", request.GET.get("org_id", ""))
|
|
|
|
return super(UserSelect2View, self).get(request, *args, **kwargs)
|
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
def get_queryset(self):
|
2022-05-14 17:57:27 +00:00
|
|
|
return (
|
2022-09-15 07:05:02 +00:00
|
|
|
_get_user_queryset(self.term, self.org_id)
|
2022-05-14 17:57:27 +00:00
|
|
|
.annotate(username=F("user__username"))
|
|
|
|
.only("id")
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def get_name(self, obj):
|
|
|
|
return obj.username
|
|
|
|
|
|
|
|
|
|
|
|
class OrganizationSelect2View(Select2View):
|
|
|
|
def get_queryset(self):
|
|
|
|
return Organization.objects.filter(name__icontains=self.term)
|
|
|
|
|
|
|
|
|
|
|
|
class ProblemSelect2View(Select2View):
|
|
|
|
def get_queryset(self):
|
2022-05-14 17:57:27 +00:00
|
|
|
return (
|
|
|
|
Problem.get_visible_problems(self.request.user)
|
|
|
|
.filter(Q(code__icontains=self.term) | Q(name__icontains=self.term))
|
|
|
|
.distinct()
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ContestSelect2View(Select2View):
|
2023-11-29 02:04:02 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.problem_id = kwargs.get("problem_id", request.GET.get("problem_id", ""))
|
|
|
|
return super(ContestSelect2View, self).get(request, *args, **kwargs)
|
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
def get_queryset(self):
|
2023-11-29 02:04:02 +00:00
|
|
|
q = Contest.get_visible_contests(self.request.user).filter(
|
2022-05-14 17:57:27 +00:00
|
|
|
Q(key__icontains=self.term) | Q(name__icontains=self.term)
|
|
|
|
)
|
2023-11-29 02:04:02 +00:00
|
|
|
if self.problem_id:
|
|
|
|
q = q.filter(problems=self.problem_id)
|
|
|
|
return q
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CommentSelect2View(Select2View):
|
|
|
|
def get_queryset(self):
|
|
|
|
return Comment.objects.filter(page__icontains=self.term)
|
|
|
|
|
|
|
|
|
|
|
|
class UserSearchSelect2View(BaseListView):
|
|
|
|
paginate_by = 20
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return _get_user_queryset(self.term)
|
|
|
|
|
2023-08-24 15:37:04 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.request = request
|
|
|
|
self.kwargs = kwargs
|
2022-05-14 17:57:27 +00:00
|
|
|
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)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
self.object_list = self.get_queryset().values_list(
|
2023-08-24 04:14:53 +00:00
|
|
|
"pk", "user__username", "user__email", "display_rank", "profile_image"
|
2022-05-14 17:57:27 +00:00
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
context = self.get_context_data()
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
return JsonResponse(
|
|
|
|
{
|
|
|
|
"results": [
|
2023-08-24 15:37:04 +00:00
|
|
|
self.get_json_result_from_object(user_tuple)
|
|
|
|
for user_tuple in context["object_list"]
|
2022-05-14 17:57:27 +00:00
|
|
|
],
|
|
|
|
"more": context["page_obj"].has_next(),
|
|
|
|
}
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def get_name(self, obj):
|
|
|
|
return str(obj)
|
|
|
|
|
2023-08-24 15:37:04 +00:00
|
|
|
def get_profile_image_url(self, profile_image):
|
|
|
|
if profile_image:
|
|
|
|
return urljoin(settings.MEDIA_URL, profile_image)
|
|
|
|
return None
|
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
class ContestUserSearchSelect2View(UserSearchSelect2View):
|
|
|
|
def get_queryset(self):
|
2022-05-14 17:57:27 +00:00
|
|
|
contest = get_object_or_404(Contest, key=self.kwargs["contest"])
|
|
|
|
if not contest.is_accessible_by(
|
|
|
|
self.request.user
|
|
|
|
) or not contest.can_see_full_scoreboard(self.request.user):
|
2020-01-21 06:35:58 +00:00
|
|
|
raise Http404()
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
return Profile.objects.filter(
|
|
|
|
contest_history__contest=contest, user__username__icontains=self.term
|
|
|
|
).distinct()
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TicketUserSelect2View(UserSearchSelect2View):
|
|
|
|
def get_queryset(self):
|
2022-05-14 17:57:27 +00:00
|
|
|
return Profile.objects.filter(
|
|
|
|
tickets__isnull=False, user__username__icontains=self.term
|
|
|
|
).distinct()
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssigneeSelect2View(UserSearchSelect2View):
|
|
|
|
def get_queryset(self):
|
2022-05-14 17:57:27 +00:00
|
|
|
return Profile.objects.filter(
|
|
|
|
assigned_tickets__isnull=False, user__username__icontains=self.term
|
|
|
|
).distinct()
|
2023-08-24 15:37:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2024-02-05 21:15:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProblemAuthorSearchSelect2View(UserSearchSelect2View):
|
|
|
|
def get_queryset(self):
|
|
|
|
return Profile.objects.filter(
|
|
|
|
authored_problems__isnull=False, user__username__icontains=self.term
|
|
|
|
).distinct()
|
|
|
|
|
|
|
|
def get_json_result_from_object(self, user_tuple):
|
|
|
|
pk, username, email, display_rank, profile_image = user_tuple
|
|
|
|
return {
|
|
|
|
"text": username,
|
|
|
|
"id": pk,
|
|
|
|
}
|