diff --git a/otisweb/templates/sidebar.html b/otisweb/templates/sidebar.html index 9ad95832..335857ea 100644 --- a/otisweb/templates/sidebar.html +++ b/otisweb/templates/sidebar.html @@ -156,6 +156,9 @@
Look up a student by Discord username.
+ {% include "generic_form.html" with submit_name="Search" %} +{% endblock layout-content %} diff --git a/roster/urls.py b/roster/urls.py index 3be6b84d..5a529e05 100644 --- a/roster/urls.py +++ b/roster/urls.py @@ -28,4 +28,5 @@ path(r"mystery-unlock/harder/", views.unlock_rest_of_mystery, kwargs={"delta": 2}), path(r"instructors/", views.StudentAssistantList.as_view(), name="instructors"), path(r"link-assistant/", views.link_assistant, name="link-assistant"), + path(r"discord-lookup/", views.discord_lookup, name="discord-lookup"), ] diff --git a/roster/views.py b/roster/views.py index a8fb7e87..d54b4462 100644 --- a/roster/views.py +++ b/roster/views.py @@ -57,6 +57,7 @@ AdvanceForm, CurriculumForm, DecisionForm, + DiscordLookupForm, InquiryForm, UserForm, ) @@ -693,3 +694,37 @@ def link_assistant(request: HttpRequest) -> HttpResponse: } return render(request, "roster/link_assistant.html", context) + + +@admin_required +def discord_lookup(request: HttpRequest) -> HttpResponse: + if request.method == "POST": + form = DiscordLookupForm(request.POST) + if form.is_valid(): + discord_handle = form.cleaned_data["discord_handle"] + try: + sa = SocialAccount.objects.get( + provider="discord", + extra_data__contains=[{"username": discord_handle}], + ) + except SocialAccount.DoesNotExist: + messages.error(request, f"Could not find {discord_handle}.") + except SocialAccount.MultipleObjectsReturned: + messages.error( + request, + f"Somehow found multiple social accounts for {discord_handle}.", + ) + else: + user = sa.user + student = Student.objects.filter(user=user).order_by("-pk").first() + if student is not None: + return HttpResponse(redirect_to=student.get_absolute_url()) + else: + return HttpResponse( + reverse("admin:auth_user_change", args=(user.pk,)) + ) + + else: + form = DiscordLookupForm() + context = {"form": form} + return render(request, "roster/discord_lookup.html", context)