Skip to content

Commit

Permalink
Merge pull request #219 from EarthSchlange/update_beta_search
Browse files Browse the repository at this point in the history
update beta search
  • Loading branch information
Michael Hiiva authored Sep 4, 2021
2 parents 7c4790f + 6cf723d commit 10c2638
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 11 deletions.
32 changes: 32 additions & 0 deletions agagd/agagd_core/tables/beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

# DJango Imports
import django_tables2 as tables

# AGAGD Column Imports
from agagd_core.tables.core import ChapterColumn
from django.core.exceptions import ObjectDoesNotExist
from django.urls import reverse
from django.utils.safestring import mark_safe
Expand Down Expand Up @@ -180,6 +183,35 @@ class Meta:
template_name = "django_tables2/bootstrap4.html"


class SearchResultsTable(tables.Table):
member_id = tables.LinkColumn(
"member_detail", kwargs={"member_id": tables.A("member_id")}
)
chapter_id = ChapterColumn(verbose_name="Chapter")
players__rating = tables.Column(verbose_name="Rating")
country = tables.LinkColumn(
"country_detail", kwargs={"country_name": tables.A("country")}
)
full_name = tables.LinkColumn(
"member_detail", kwargs={"member_id": tables.A("member_id")}
)

class Meta:
model = agagd_models.Member
fields = ("full_name", "state", "players__rating", "renewal_due", "country")
sequence = (
"full_name",
"players__rating",
"chapter_id",
"country",
"state",
"renewal_due",
"member_id",
)
attrs = default_bootstrap_header_column_attrs
template_name = "django_tables2/bootstrap4.html"


class Top10DanTable(tables.Table):
pin_player = tables.Column(
orderable=False,
Expand Down
48 changes: 37 additions & 11 deletions agagd/agagd_core/views/beta/search.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
from agagd_core.models import Member
from agagd_core.tables.beta import SearchResultsTable
from django.db.models import F, Q
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.template.response import TemplateResponse
from django.urls import reverse
from django.views.generic import TemplateView
from django.views.generic import DetailView
from django_tables2 import RequestConfig


class SearchView(TemplateView):
class SearchView(DetailView):
template_name = "beta.search_page.html"
search_results_template_name = "beta.search_results.html"

def get(self, request):
query = request.GET.get("q", "")

if not query:
return render(request, self.template_name)
return TemplateResponse(request, self.template_name)

if query.isdigit():
member_id = [int(query)]
return HttpResponseRedirect(reverse("member_detail", args=member_id))

return HttpResponseRedirect(f"/search/?q={query}")

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_title"] = "Search -- Players | Games | Tournaments"

return context
member_table_data = (
Member.objects.filter(Q(member_id=F("players__pin_player")))
.filter(full_name__icontains=query)
.values(
"member_id",
"chapter_id",
"renewal_due",
"state",
"players__rating",
"country",
"full_name",
"family_name",
)
.order_by("family_name")
)

self.template_name = self.search_results_template_name

member_results_table = SearchResultsTable(member_table_data)
RequestConfig(request, paginate={"per_page": 25}).configure(
member_results_table
)

context = locals()
context["search_query"] = query
context["member_results_table"] = member_results_table

return TemplateResponse(request, self.template_name, context)
17 changes: 17 additions & 0 deletions agagd/jinja2/beta.search_results.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "beta.base.html" %}

{% block content %}

<div class="container my-4">
<div class="row">
<div class="col mx-0 px-0">
<h3>Search Results for {{ search_query }}</h3>
</div>
</div>
<div class="row">
<div class="col mx-0 mb-2 pb-4 px-0">
{{ member_results_table.as_html(request) }}
</div>
</div>
</div>
{% endblock %}

0 comments on commit 10c2638

Please sign in to comment.