Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profile search #125

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions app/profile/sort_strategy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from abc import ABC, abstractmethod

from app.api.api import num_shots


class SortStrategy(ABC):
# Returns
# [Catagories]
# Where catagories is the ordered list (Catagory Name, [ (Display value, Sub display line, Username) ,...])
@abstractmethod
def sort_users(self, list_users) -> list:
pass


class AccessStrategy(SortStrategy):
def sort_users(self, list_users):
sorted_users = sorted(list_users, key=lambda x: (str(x.fName), str(x.sName)))
user_groups = {"Student": [], "Coach": [], "Admin": [], "Other": []}
for user in sorted_users:
user_format = (f"{user.fName} {user.sName}", None, user.username)
if user.access == 0:
user_groups["Student"].append(user_format)
elif user.access == 1:
user_groups["Coach"].append(user_format)
elif user.access == 2:
user_groups["Admin"].append(user_format)
else:
user_groups["Other"].append(user_format)
data = []
for key in user_groups:
if len(user_groups[key]) > 0:
data.append((key, user_groups[key]))
return data


class LastNameStrategy(SortStrategy):
def sort_users(self, list_users):
sorted_users = sorted(list_users, key=lambda x: (str(x.sName), str(x.fName)))
users = [(f"{user.fName} {user.sName}", "Funny", user.username) for user in sorted_users]
return [("Users", users)]


class YearStrategy(SortStrategy):
def sort_users(self, list_users):
sorted_users = sorted(list_users, key=lambda x: (str(x.fName), str(x.sName)))
yearGroups = {'Year 12': [], 'Year 11': [], 'Year 10': [], 'Year 9': [], 'Year 8': [],
'Year 7': [], 'Other': []}
for user in sorted_users:
schoolYr = f"Year {user.get_school_year()}"
print(schoolYr)
if schoolYr in yearGroups:
yearGroups[schoolYr].append([f"{user.fName} {user.sName}", None, user.username])
else:
yearGroups['Other'].append([f"{user.fName} {user.sName}", None, user.username])
data = []
for key in yearGroups:
if len(yearGroups[key]) > 0:
data.append((key, yearGroups[key]))
return data


class ShotsStrategy(SortStrategy):
def sort_users(self, list_users):
first_sort = sorted(list_users, key=lambda x: (str(x.sName), str(x.fName)))
users_shots = []
for user in first_sort:
res = num_shots(user.id, user.club.season_start, user.club.season_end)
stages = res["num_stages"]
shots = res["num_shots"]
users_shots.append((user, stages, shots))
users_shots.sort(key=lambda x: (x[1], x[2]), reverse=True)

active_list = []
inactive_list = []
for user, stages, shots in users_shots:
format = (f"{user.fName} {user.sName}", f"Stages {stages} - Shots {shots}", user.username)
if stages == 0:
inactive_list.append(format)
else:
active_list.append(format)
return [("Active Shooters", active_list), ("Inactive Shooters", inactive_list)]
43 changes: 42 additions & 1 deletion app/profile/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import json

from flask import Blueprint, request, redirect, render_template, flash, url_for
from flask import Blueprint, request, redirect, render_template, flash, url_for, abort
from flask import session as flask_session
from flask_login import login_required, current_user

Expand All @@ -10,6 +10,7 @@
from app.models import User, Club
from app.decorators import club_authorised_urlpath, club_exists, is_authorised
from app.profile.forms import updateInfoForm
from app.profile.sort_strategy import YearStrategy, ShotsStrategy, LastNameStrategy, AccessStrategy

profile_bp = Blueprint('profile_bp', __name__)

Expand Down Expand Up @@ -37,6 +38,46 @@ def profile_list(club, club_name):
yearGroups = json.dumps(yearGroups)
return render_template('profile/profile_list.html', users=users, yearGroups=yearGroups, club=club)

@profile_bp.route('/profile_search', methods=['GET'])
def search():
clubID = request.args.get('clubID')
sort_by = request.args.get('sort_by')
club = Club.query.filter_by(id=clubID).first()
if not club:
return abort(400)

if sort_by == "year":
sort_strategy = YearStrategy()
elif sort_by == "shots":
sort_strategy = ShotsStrategy()
elif sort_by == "sname-fname":
sort_strategy = LastNameStrategy()
else:
sort_strategy = AccessStrategy()

data = sort_strategy.sort_users(User.query.filter_by(clubID=clubID).all())
html = """<div class="text-center">"""
print(data)
for category, list_users in data:
html += f"""
<h3>{category}</h3>
<div class="row justify-content-center px-2">"""
for display, subdisplay, username in list_users:
subtitle = ""
if subdisplay:
subtitle = f'<div>{subdisplay}</div>'
html += f"""
<div class="pl-2 pr-2">
<a class="card profile-card mt-3 pl-1 pr-1" href="/profile?username={username}">
<div class="card-body text-center" style="width:300px">
<h6>{display}</h6>
{subtitle}
</div>
</a>
</div>
"""
html += "</div><hr>"
return html

def can_access_profile(user) -> bool:
if current_user.access == 3:
Expand Down
4 changes: 4 additions & 0 deletions app/static/css/template.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/static/css/template.css.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions app/static/css/template.scss
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ a[aria-expanded="true"] {
background: $bg;
}

.profile-card:hover {
border-color: $link-color;
}

/*

::-webkit-scrollbar {
Expand Down
21 changes: 14 additions & 7 deletions app/templates/profile/profile_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ <h1> Profile List </h1>

<!--New Cards by Henry Guo-->
<div class="container-fluid">
<script src="../static/js/profile_list.js"></script>
<form method="get" action="/profile">
<div class="row py-5">
<div class="col-12 text-center">
Expand All @@ -34,12 +33,20 @@ <h1> Profile List </h1>
</div>
</div>
</form>
<div class="row">
<div class="col-12">
<div class="yearGroups text-center" id="yearGroups" >
<!--List of shooters go in here-->
</div>
</div>
<div class="row justify-content-end align-items-center">
<img id="shot-indicator" class="htmx-indicator" style="max-width: 25px"
src="../../static/images/bars.svg"/>
<label class="mb-0 mx-2">Sort By</label>
<select name="sort_by" class="form-select" hx-get="/profile_search?clubID={{ club.id }}" hx-target="#target" hx-trigger="load, change"
hx-indicator="#shot-indicator">
<option selected value="default">Default</option>
<option value="sname-fname">Surname</option>
<option value="year">Year</option>
<option value="shots">Shot Frequency</option>
</select>
</div>
<div id="target">

</div>
</div>

Expand Down
Loading