diff --git a/packet/models.py b/packet/models.py index b251661c..dc60d570 100644 --- a/packet/models.py +++ b/packet/models.py @@ -42,6 +42,13 @@ class Freshman(db.Model): # One freshman can have multiple packets if they repeat the intro process packets = relationship("Packet", order_by="desc(Packet.id)") + @classmethod + def by_username(cls, username: str): + """ + Helper method to retrieve a freshman by their RIT username + """ + return cls.query.filter_by(rit_username=username).first() + class Packet(db.Model): __tablename__ = "packet" diff --git a/packet/routes/api.py b/packet/routes/api.py index 5f576117..1b48c99f 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -7,10 +7,58 @@ from packet.context_processors import get_rit_name from packet.mail import send_report_mail from packet.utils import before_request, packet_auth, notify_slack -from packet.models import Packet, MiscSignature, NotificationSubscription +from packet.models import Packet, MiscSignature, NotificationSubscription, Freshman from packet.notifications import packet_signed_notification, packet_100_percent_notification +@app.route("/api/v1/packets/", methods=["GET"]) +@packet_auth +def get_packets_by_user(username: str) -> dict: + """ + Return a dictionary of packets for a freshman by username, giving packet start and end date by packet id + """ + frosh = Freshman.by_username(username) + + return {packet.id: { + 'start': packet.start, + 'end': packet.end, + } for packet in frosh.packets} + + +@app.route("/api/v1/packets//newest", methods=["GET"]) +@packet_auth +def get_newest_packet_by_user(username: str) -> dict: + """ + Return a user's newest packet + """ + frosh = Freshman.by_username(username) + + packet = frosh.packets[-1] + + return { + packet.id: { + 'start': packet.start, + 'end': packet.end, + 'required': vars(packet.signatures_required()), + 'received': vars(packet.signatures_received()), + } + } + + +@app.route("/api/v1/packet/", methods=["GET"]) +@packet_auth +def get_packet_by_id(packet_id: int) -> dict: + """ + Return the scores of the packet in question + """ + + packet = Packet.by_id(packet_id) + + return { + 'required': vars(packet.signatures_required()), + 'received': vars(packet.signatures_received()), + } + @app.route("/api/v1/sign//", methods=["POST"]) @packet_auth @before_request