Skip to content
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
7 changes: 7 additions & 0 deletions packet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
50 changes: 49 additions & 1 deletion packet/routes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<username>", 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/<username>/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/<packet_id>", 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/<packet_id>/", methods=["POST"])
@packet_auth
@before_request
Expand Down