Skip to content

Commit 4c2a4bf

Browse files
mxmeinholddevinmatte
authored andcommitted
Packet GET routes for Conditional (#185)
* Create `packets/{username}` route * Create `/packet/{packet_id}` route * Create newest packet by user route * Remove superflous `as_dict()` * Remove extra `beforerequest`s * Correct for pylint * Correct GET capitalization * Replace as_dict() with vars() again
1 parent 6636bbf commit 4c2a4bf

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

packet/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class Freshman(db.Model):
4242
# One freshman can have multiple packets if they repeat the intro process
4343
packets = relationship("Packet", order_by="desc(Packet.id)")
4444

45+
@classmethod
46+
def by_username(cls, username: str):
47+
"""
48+
Helper method to retrieve a freshman by their RIT username
49+
"""
50+
return cls.query.filter_by(rit_username=username).first()
51+
4552

4653
class Packet(db.Model):
4754
__tablename__ = "packet"

packet/routes/api.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,58 @@
77
from packet.context_processors import get_rit_name
88
from packet.mail import send_report_mail
99
from packet.utils import before_request, packet_auth, notify_slack
10-
from packet.models import Packet, MiscSignature, NotificationSubscription
10+
from packet.models import Packet, MiscSignature, NotificationSubscription, Freshman
1111
from packet.notifications import packet_signed_notification, packet_100_percent_notification
1212

1313

14+
@app.route("/api/v1/packets/<username>", methods=["GET"])
15+
@packet_auth
16+
def get_packets_by_user(username: str) -> dict:
17+
"""
18+
Return a dictionary of packets for a freshman by username, giving packet start and end date by packet id
19+
"""
20+
frosh = Freshman.by_username(username)
21+
22+
return {packet.id: {
23+
'start': packet.start,
24+
'end': packet.end,
25+
} for packet in frosh.packets}
26+
27+
28+
@app.route("/api/v1/packets/<username>/newest", methods=["GET"])
29+
@packet_auth
30+
def get_newest_packet_by_user(username: str) -> dict:
31+
"""
32+
Return a user's newest packet
33+
"""
34+
frosh = Freshman.by_username(username)
35+
36+
packet = frosh.packets[-1]
37+
38+
return {
39+
packet.id: {
40+
'start': packet.start,
41+
'end': packet.end,
42+
'required': vars(packet.signatures_required()),
43+
'received': vars(packet.signatures_received()),
44+
}
45+
}
46+
47+
48+
@app.route("/api/v1/packet/<packet_id>", methods=["GET"])
49+
@packet_auth
50+
def get_packet_by_id(packet_id: int) -> dict:
51+
"""
52+
Return the scores of the packet in question
53+
"""
54+
55+
packet = Packet.by_id(packet_id)
56+
57+
return {
58+
'required': vars(packet.signatures_required()),
59+
'received': vars(packet.signatures_received()),
60+
}
61+
1462
@app.route("/api/v1/sign/<packet_id>/", methods=["POST"])
1563
@packet_auth
1664
@before_request

0 commit comments

Comments
 (0)