From ad2b011d1578fa47cb2ccb868615418d789bc7bf Mon Sep 17 00:00:00 2001 From: Max Meinhold Date: Mon, 7 Oct 2019 23:19:38 -0400 Subject: [PATCH 1/8] Create `packets/{username}` route --- packet/models.py | 7 +++++++ packet/routes/api.py | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) 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..72e64afb 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -7,10 +7,25 @@ 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 +@before_request +def get_packets_by_user(username: str, info) -> 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/sign//", methods=["POST"]) @packet_auth @before_request From b5a868170966e6d87a1b93bd9b6e8df3f23b1c97 Mon Sep 17 00:00:00 2001 From: Max Meinhold Date: Mon, 7 Oct 2019 23:32:01 -0400 Subject: [PATCH 2/8] Create `/packet/{packet_id}` route --- packet/models.py | 14 ++++++++++++++ packet/routes/api.py | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/packet/models.py b/packet/models.py index dc60d570..c00b3091 100644 --- a/packet/models.py +++ b/packet/models.py @@ -31,6 +31,20 @@ def __init__(self, upper, fresh, misc): self.member_total = upper + self.misc_capped self.total = upper + fresh + self.misc_capped + def as_dict(self) -> dict: + """ + Return a dictionary representing these sig counts + """ + + return { + 'upper': self.upper, + 'fresh': self.fresh, + 'misc': self.misc, + 'misc_capped': self.misc_capped, + 'member_total': self.member_total, + 'total': self.total, + } + class Freshman(db.Model): __tablename__ = "freshman" diff --git a/packet/routes/api.py b/packet/routes/api.py index 72e64afb..f4008d31 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -26,6 +26,21 @@ def get_packets_by_user(username: str, info) -> dict: } for packet in frosh.packets} +@app.route("/api/v1/packet/", methods=["get"]) +@packet_auth +@before_request +def get_packet_by_id(packet_id: int, info) -> dict: + """ + Return the scores of the packet in question + """ + + packet = Packet.by_id(packet_id) + + return { + 'required': packet.signatures_required().as_dict(), + 'received': packet.signatures_received().as_dict(), + } + @app.route("/api/v1/sign//", methods=["POST"]) @packet_auth @before_request From a6217a9c386240e63ffd134996802475e1d68c15 Mon Sep 17 00:00:00 2001 From: Max Meinhold Date: Mon, 7 Oct 2019 23:48:06 -0400 Subject: [PATCH 3/8] Create newest packet by user route --- packet/routes/api.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packet/routes/api.py b/packet/routes/api.py index f4008d31..54738cfe 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -26,6 +26,27 @@ def get_packets_by_user(username: str, info) -> dict: } for packet in frosh.packets} +@app.route("/api/v1/packets//newest", methods=["get"]) +@packet_auth +@before_request +def get_newest_packet_by_user(username: str, info) -> 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': packet.signatures_required().as_dict(), + 'received': packet.signatures_received().as_dict(), + } + } + + @app.route("/api/v1/packet/", methods=["get"]) @packet_auth @before_request From 7bd8619513369f1304f6eb22fda46dab135ec3a1 Mon Sep 17 00:00:00 2001 From: Max Meinhold Date: Tue, 8 Oct 2019 00:31:21 -0400 Subject: [PATCH 4/8] Remove superflous `as_dict()` --- packet/models.py | 14 -------------- packet/routes/api.py | 4 ++-- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/packet/models.py b/packet/models.py index c00b3091..dc60d570 100644 --- a/packet/models.py +++ b/packet/models.py @@ -31,20 +31,6 @@ def __init__(self, upper, fresh, misc): self.member_total = upper + self.misc_capped self.total = upper + fresh + self.misc_capped - def as_dict(self) -> dict: - """ - Return a dictionary representing these sig counts - """ - - return { - 'upper': self.upper, - 'fresh': self.fresh, - 'misc': self.misc, - 'misc_capped': self.misc_capped, - 'member_total': self.member_total, - 'total': self.total, - } - class Freshman(db.Model): __tablename__ = "freshman" diff --git a/packet/routes/api.py b/packet/routes/api.py index 54738cfe..8ce7224c 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -58,8 +58,8 @@ def get_packet_by_id(packet_id: int, info) -> dict: packet = Packet.by_id(packet_id) return { - 'required': packet.signatures_required().as_dict(), - 'received': packet.signatures_received().as_dict(), + 'required': vars(packet.signatures_required()), + 'received': vars(packet.signatures_received()), } @app.route("/api/v1/sign//", methods=["POST"]) From 04d983d59aa2ee51a0da1f09e807353ce327e182 Mon Sep 17 00:00:00 2001 From: Max Meinhold Date: Tue, 8 Oct 2019 00:31:49 -0400 Subject: [PATCH 5/8] Remove extra `beforerequest`s --- packet/routes/api.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packet/routes/api.py b/packet/routes/api.py index 8ce7224c..2c7e8bb8 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -28,8 +28,7 @@ def get_packets_by_user(username: str, info) -> dict: @app.route("/api/v1/packets//newest", methods=["get"]) @packet_auth -@before_request -def get_newest_packet_by_user(username: str, info) -> dict: +def get_newest_packet_by_user(username: str) -> dict: """ Return a user's newest packet """ @@ -49,8 +48,7 @@ def get_newest_packet_by_user(username: str, info) -> dict: @app.route("/api/v1/packet/", methods=["get"]) @packet_auth -@before_request -def get_packet_by_id(packet_id: int, info) -> dict: +def get_packet_by_id(packet_id: int) -> dict: """ Return the scores of the packet in question """ @@ -64,8 +62,7 @@ def get_packet_by_id(packet_id: int, info) -> dict: @app.route("/api/v1/sign//", methods=["POST"]) @packet_auth -@before_request -def sign(packet_id, info): +def sign(packet_id): packet = Packet.by_id(packet_id) if packet is not None and packet.is_open(): From f8f8036c70374c7f3d97f99da8eaf6a7e42503f0 Mon Sep 17 00:00:00 2001 From: Max Meinhold Date: Tue, 8 Oct 2019 13:28:25 -0400 Subject: [PATCH 6/8] Correct for pylint --- packet/routes/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packet/routes/api.py b/packet/routes/api.py index 2c7e8bb8..e5633aa2 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -13,8 +13,7 @@ @app.route("/api/v1/packets/", methods=["get"]) @packet_auth -@before_request -def get_packets_by_user(username: str, info) -> dict: +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 """ @@ -62,7 +61,8 @@ def get_packet_by_id(packet_id: int) -> dict: @app.route("/api/v1/sign//", methods=["POST"]) @packet_auth -def sign(packet_id): +@before_request +def sign(packet_id, info): packet = Packet.by_id(packet_id) if packet is not None and packet.is_open(): From 7044de45ff57c90e1c1f0515542cf937a36b73dc Mon Sep 17 00:00:00 2001 From: Max Meinhold Date: Tue, 8 Oct 2019 13:30:16 -0400 Subject: [PATCH 7/8] Correct GET capitalization --- packet/routes/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packet/routes/api.py b/packet/routes/api.py index e5633aa2..3ac9384b 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -11,7 +11,7 @@ from packet.notifications import packet_signed_notification, packet_100_percent_notification -@app.route("/api/v1/packets/", methods=["get"]) +@app.route("/api/v1/packets/", methods=["GET"]) @packet_auth def get_packets_by_user(username: str) -> dict: """ @@ -25,7 +25,7 @@ def get_packets_by_user(username: str) -> dict: } for packet in frosh.packets} -@app.route("/api/v1/packets//newest", methods=["get"]) +@app.route("/api/v1/packets//newest", methods=["GET"]) @packet_auth def get_newest_packet_by_user(username: str) -> dict: """ @@ -45,7 +45,7 @@ def get_newest_packet_by_user(username: str) -> dict: } -@app.route("/api/v1/packet/", methods=["get"]) +@app.route("/api/v1/packet/", methods=["GET"]) @packet_auth def get_packet_by_id(packet_id: int) -> dict: """ From e58eb4fdca07f04f2e99557bc3666fb4c149388f Mon Sep 17 00:00:00 2001 From: Max Meinhold Date: Tue, 8 Oct 2019 14:14:08 -0400 Subject: [PATCH 8/8] Replace as_dict() with vars() again --- packet/routes/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packet/routes/api.py b/packet/routes/api.py index 3ac9384b..1b48c99f 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -39,8 +39,8 @@ def get_newest_packet_by_user(username: str) -> dict: packet.id: { 'start': packet.start, 'end': packet.end, - 'required': packet.signatures_required().as_dict(), - 'received': packet.signatures_received().as_dict(), + 'required': vars(packet.signatures_required()), + 'received': vars(packet.signatures_received()), } }