Skip to content

Commit

Permalink
feat(roster): lookup by discord username
Browse files Browse the repository at this point in the history
This is super annoying to test so we'll just test on prod lol
  • Loading branch information
vEnhance committed Sep 15, 2023
1 parent 62a1a0c commit 8072ded
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions otisweb/templates/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ <h3>Admin controls</h3>
<li>
<a href="{% url "admin-palace-list" %}">Ruby Palace</a>
</li>
<li>
<a href="{% url "discord-lookup" %}">Discordo</a>
</li>
<li>Giga Chart</li>
<ul>
<li>
Expand Down
4 changes: 4 additions & 0 deletions roster/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,7 @@ class LinkAssistantForm(forms.Form):
),
label="Student to claim",
)


class DiscordLookupForm(forms.Form):
discord_handle = forms.CharField()
6 changes: 6 additions & 0 deletions roster/templates/roster/discord_lookup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "layout.html" %}
{% block title %}Discord Lookup{% endblock %}
{% block layout-content %}
<p>Look up a student by Discord username.</p>
{% include "generic_form.html" with submit_name="Search" %}
{% endblock layout-content %}
1 change: 1 addition & 0 deletions roster/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]
35 changes: 35 additions & 0 deletions roster/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
AdvanceForm,
CurriculumForm,
DecisionForm,
DiscordLookupForm,
InquiryForm,
UserForm,
)
Expand Down Expand Up @@ -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)

0 comments on commit 8072ded

Please sign in to comment.