Skip to content

Commit d199ea4

Browse files
committed
WIP implementation of refactored packets page
1 parent af36547 commit d199ea4

File tree

3 files changed

+67
-27
lines changed

3 files changed

+67
-27
lines changed

packet/models.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from datetime import datetime
6+
from itertools import chain
67

78
from sqlalchemy import Column, Integer, String, Text, ForeignKey, DateTime, Boolean
89
from sqlalchemy.orm import relationship
@@ -81,6 +82,22 @@ def signatures_received(self):
8182

8283
return SigCounts(eboard, upper, fresh, len(self.misc_signatures))
8384

85+
def did_sign(self, username, is_csh):
86+
"""
87+
:param is_csh: Set to True for CSH accounts and False for freshmen
88+
"""
89+
if is_csh:
90+
for sig in filter(lambda sig: sig.member == username, chain(self.upper_signatures, self.misc_signatures)):
91+
if isinstance(sig, MiscSignature):
92+
return True
93+
else:
94+
return sig.signed
95+
else:
96+
for sig in filter(lambda sig: sig.freshman_username == username, self.fresh_signatures):
97+
return sig.signed
98+
99+
# The user must be a misc CSHer that hasn't signed this packet or an off-floor freshmen
100+
return False
84101

85102
class UpperSignature(db.Model):
86103
__tablename__ = "signature_upper"

packet/routes/shared.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from itertools import chain
2+
from datetime import datetime
23
from flask import render_template, redirect
34

45
from packet import auth, app
5-
from packet.ldap import ldap_is_eboard
6-
from packet.member import current_packets
76
from packet.utils import before_request
87
from packet.models import MiscSignature, Packet
8+
from packet.debug_utils import log_time, log_cache
99

1010

1111
@app.route('/logout')
@@ -49,22 +49,46 @@ def freshman_packet(freshman_username, packet_id, info=None):
4949
upper=filter(lambda sig: not sig.eboard, packet.upper_signatures))
5050

5151

52-
@app.route("/packets")
52+
@app.route("/packets/")
5353
@auth.oidc_auth
5454
@before_request
55+
@log_time
5556
def packets(info=None):
56-
if app.config["REALM"] == "csh":
57-
if info["member_info"]["onfloor"]:
58-
if info["member_info"]["room"] is not None or ldap_is_eboard(info['user_obj']):
59-
open_packets = current_packets(info["uid"], False, True)
60-
else:
61-
open_packets = current_packets(info["uid"], False, False)
62-
else:
63-
open_packets = current_packets(info["uid"], False, False)
64-
else:
65-
open_packets = current_packets(info["uid"], True, info["onfloor"])
57+
@log_time
58+
def query():
59+
return Packet.query.filter(Packet.start < datetime.now(), Packet.end > datetime.now()).all()
60+
61+
open_packets = query()
62+
63+
# Pre-calculate and store the return values of did_sign(), signatures_received(), and signatures_required()
64+
@log_time
65+
def prec_1():
66+
for packet in open_packets:
67+
packet.did_sign_result = packet.did_sign(info["uid"], app.config["REALM"] == "csh")
68+
69+
@log_time
70+
def prec_2():
71+
for packet in open_packets:
72+
packet.signatures_received_result = packet.signatures_received()
73+
74+
@log_time
75+
def prec_3():
76+
for packet in open_packets:
77+
packet.signatures_required_result = packet.signatures_required()
78+
79+
@log_time
80+
def sort():
81+
open_packets.sort(key=lambda packet: packet.signatures_received_result.total, reverse=True)
82+
open_packets.sort(key=lambda packet: packet.did_sign_result, reverse=True)
6683

67-
open_packets.sort(key=lambda x: x.total_signatures, reverse=True)
68-
open_packets.sort(key=lambda x: x.did_sign, reverse=True)
84+
@log_time
85+
def render():
86+
return render_template("active_packets.html", info=info, packets=open_packets)
6987

70-
return render_template("active_packets.html", info=info, packets=open_packets)
88+
prec_1()
89+
prec_2()
90+
prec_3()
91+
sort()
92+
out = render()
93+
log_cache()
94+
return out

packet/templates/active_packets.html

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,30 @@ <h3 class="page-title">Active Packets</h3>
2626
</thead>
2727
<tbody>
2828
{% for packet in packets %}
29-
<tr {% if packet.did_sign %}style="background-color: #4caf505e" {% endif %}>
29+
<tr {% if packet.did_sign_result %}style="background-color: #4caf505e" {% endif %}>
3030
<td>
31-
<a href="/packet/{{ packet.rit_username }}">
31+
<a href="{{ url_for('freshman_packet', freshman_username=packet.freshman_username, packet_id=packet.id) }}">
3232
<img class="eval-user-img"
33-
alt="{{ packet.name }}"
33+
alt="{{ get_rit_name(packet.freshman_username) }}"
3434
src="https://www.gravatar.com/avatar/freshmen?d=mp&f=y"
3535
width="25"
36-
height="25"/> {{ packet.name }}
37-
({{ packet.rit_username }})
36+
height="25"/> {{ get_rit_name(packet.freshman_username) }}
3837
</a>
3938
</td>
40-
<td data-sort="{{ packet.total_signatures }}">
41-
{% if packet.total_signatures == packet.required_signatures %}
39+
<td data-sort="{{ packet.signatures_received_result.total }}">
40+
{% if packet.signatures_received_result.total == packet.signatures_required_result.total %}
4241
💯 {# 100% emoji #}
4342
{% else %}
44-
{{ packet.total_signatures }}/
45-
{{ packet.required_signatures }}
43+
{{ packet.signatures_received_result.total }} /
44+
{{ packet.signatures_required_result.total }}
4645
{% endif %}
4746
</td>
4847
{% if can_sign %}
4948
<td class="sign-packet" align="right">
50-
{% if not packet.did_sign and info.uid != packet.rit_username %}
49+
{% if not packet.did_sign_result and info.uid != packet.rit_username %}
5150
<button class="btn btn-sm btn-primary sign-button"
5251
data-freshman_uid="{{ packet.rit_username }}"
53-
data-freshman_name="{{ packet.name }}">
52+
data-freshman_name="{{ get_rit_name(packet.freshman_username) }}">
5453
Sign
5554
</button>
5655
{% elif info.uid != packet.rit_username %}

0 commit comments

Comments
 (0)