Skip to content

Commit

Permalink
feat(roster): improve discord_lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
vEnhance committed Sep 15, 2023
1 parent 67921d6 commit 0b60412
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
16 changes: 16 additions & 0 deletions roster/templates/roster/discord_lookup.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
{% extends "layout.html" %}
{% block title %}Discord Lookup{% endblock %}
{% block layout-content %}
{% if lookup != None %}
<h2>Results</h2>
{% if lookup|length == 0 %}
<div class="alert alert-danger">No matches were found.</div>
{% else %}
<ul>
{% for sa,link in lookup.items %}
<li>
<a href="{{ link }}">{{ sa.user.get_full_name }}: {{ sa.extra_info.username }}</a>
</li>
{% endfor %}
</ul>
{% endif %}
<hr />
{% endif %}
<h2>Lookup</h2>
<p>Look up a student by Discord username.</p>
{% include "generic_form.html" with submit_name="Search" %}
{% endblock layout-content %}
37 changes: 16 additions & 21 deletions roster/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,33 +698,28 @@ def link_assistant(request: HttpRequest) -> HttpResponse:

@admin_required
def discord_lookup(request: HttpRequest) -> HttpResponse:
context = {}
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__icontains=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()
lookup: dict[SocialAccount, str] = {}
for sa in SocialAccount.objects.filter(
provider="discord", extra_data__icontains=discord_handle
).select_related("user")[:3]:
student = Student.objects.filter(user=sa.user).order_by("-pk").first()
if student is not None:
return HttpResponseRedirect(student.get_absolute_url())
lookup[sa] = student.get_absolute_url()
else:
return HttpResponseRedirect(
reverse("admin:auth_user_change", args=(user.pk,))
)

lookup[sa] = reverse("admin:auth_user_change", args=(sa.user.pk,))
if len(lookup) == 1:
_, url = list(lookup.items())[0]
return HttpResponseRedirect(url)
context["lookup"] = lookup
else:
context["lookup"] = None
else:
form = DiscordLookupForm()
context = {"form": form}
context["lookup"] = None
context["form"] = form
return render(request, "roster/discord_lookup.html", context)

0 comments on commit 0b60412

Please sign in to comment.