Skip to content

Commit 286967b

Browse files
committed
Simplified URLs and packet lookup
1 parent 6b070a6 commit 286967b

File tree

8 files changed

+27
-20
lines changed

8 files changed

+27
-20
lines changed

packet/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,18 @@ def did_sign(self, username, is_csh):
105105

106106
@classmethod
107107
def open_packets(cls):
108+
"""
109+
Helper method for fetching all currently open packets
110+
"""
108111
return cls.query.filter(cls.start < datetime.now(), cls.end > datetime.now()).all()
109112

113+
@classmethod
114+
def by_id(cls, packet_id):
115+
"""
116+
Helper method for fetching 1 packet by its id
117+
"""
118+
return cls.query.filter_by(id=packet_id).first()
119+
110120
class UpperSignature(db.Model):
111121
__tablename__ = "signature_upper"
112122
packet_id = Column(Integer, ForeignKey("packet.id"), primary_key=True)

packet/routes/api.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@
33
from packet.models import Packet, MiscSignature
44

55

6-
@app.route("/api/v1/sign/<packet_username>/<packet_id>/", methods=["POST"])
6+
@app.route("/api/v1/sign/<packet_id>/", methods=["POST"])
77
@auth.oidc_auth
88
@before_request
9-
def sign(packet_username, packet_id, info):
10-
packet = Packet.query.filter_by(freshman_username=packet_username, id=packet_id).first()
9+
def sign(packet_id, info):
10+
packet = Packet.by_id(packet_id)
1111

1212
if packet is not None and packet.is_open():
1313
if app.config["REALM"] == "csh":
1414
# Check if the CSHer is an upperclassman and if so, sign that row
1515
for sig in filter(lambda sig: sig.member == info["uid"], packet.upper_signatures):
1616
sig.signed = True
1717
db.session.commit()
18-
return "Success: Signed Packet: " + packet_username
18+
return "Success: Signed Packet: " + packet.freshman_username
1919

2020
# The CSHer is a misc so add a new row
2121
db.session.add(MiscSignature(packet=packet, member=info["uid"]))
2222
db.session.commit()
23-
return "Success: Signed Packet: " + packet_username
23+
return "Success: Signed Packet: " + packet.freshman_username
2424
else:
2525
# Check if the freshman is onfloor and if so, sign that row
2626
for sig in filter(lambda sig: sig.freshman_username == info["uid"], packet.fresh_signatures):
2727
sig.signed = True
2828
db.session.commit()
29-
return "Success: Signed Packet: " + packet_username
29+
return "Success: Signed Packet: " + packet.freshman_username
3030

3131
return "Error: Signature not valid. Reason: Unknown"

packet/routes/freshmen.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ def index(info=None):
1616
most_recent_packet = Packet.query.filter_by(freshman_username=info['uid']).order_by(Packet.id.desc()).first()
1717

1818
if most_recent_packet is not None:
19-
return redirect(url_for("freshman_packet", freshman_username=most_recent_packet.freshman_username,
20-
packet_id=most_recent_packet.id), 302)
19+
return redirect(url_for("freshman_packet", packet_id=most_recent_packet.id), 302)
2120
else:
2221
return redirect(url_for("packets"), 302)
2322

@@ -26,9 +25,9 @@ def index(info=None):
2625
@auth.oidc_auth
2726
@before_request
2827
def essays(packet_id, info=None):
29-
packet = Packet.query.filter_by(freshman_username=info['uid'], id=packet_id).first()
28+
packet = Packet.by_id(packet_id)
3029

31-
if packet is not None:
30+
if packet is not None and packet.freshman_username == info["uid"]:
3231
return render_template("essays.html", info=info, packet=packet)
3332
else:
3433
return redirect(url_for("index"), 302)
@@ -38,9 +37,9 @@ def essays(packet_id, info=None):
3837
@auth.oidc_auth
3938
@before_request
4039
def submit_essays(packet_id, info=None):
41-
packet = Packet.query.filter_by(freshman_username=info['uid'], id=packet_id).first()
40+
packet = Packet.by_id(packet_id)
4241

43-
if packet is not None:
42+
if packet is not None and packet.is_open() and packet.freshman_username == info["uid"]:
4443
packet.info_eboard = request.form.get("info_eboard", None)
4544
packet.info_events = request.form.get("info_events", None)
4645
packet.info_achieve = request.form.get("info_achieve", None)

packet/routes/shared.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ def logout():
1515
return redirect("http://csh.rit.edu")
1616

1717

18-
@app.route("/packet/<freshman_username>/<packet_id>/")
18+
@app.route("/packet/<packet_id>/")
1919
@auth.oidc_auth
2020
@before_request
21-
def freshman_packet(freshman_username, packet_id, info=None):
22-
packet = Packet.query.filter_by(freshman_username=freshman_username, id=packet_id).first()
21+
def freshman_packet(packet_id, info=None):
22+
packet = Packet.by_id(packet_id)
2323

2424
if packet is None:
2525
return "Invalid packet or freshman", 404

packet/static/js/signing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $(document).ready(function () {
1313
.then((willSign) => {
1414
if (willSign) {
1515
$.ajax({
16-
url: "/api/v1/sign/" + packetData.freshman_uid + "/" + packetData.packet_id + "/",
16+
url: "/api/v1/sign/" + packetData.packet_id + "/",
1717
method: "POST",
1818
success: function (data) {
1919
swal("Congratulations or I'm sorry\nYou've signed " + packetData.freshman_name + "'s packet.", {

packet/templates/active_packets.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h3 class="page-title">Active Packets</h3>
2828
{% for packet in packets %}
2929
<tr {% if packet.did_sign_result %}style="background-color: #4caf505e" {% endif %}>
3030
<td>
31-
<a href="{{ url_for('freshman_packet', freshman_username=packet.freshman_username, packet_id=packet.id) }}">
31+
<a href="{{ url_for('freshman_packet', packet_id=packet.id) }}">
3232
<img class="eval-user-img"
3333
alt="{{ get_rit_name(packet.freshman_username) }}"
3434
src="https://www.gravatar.com/avatar/freshmen?d=mp&f=y"
@@ -48,7 +48,6 @@ <h3 class="page-title">Active Packets</h3>
4848
<td class="sign-packet" align="right">
4949
{% if not packet.did_sign_result and info.uid != packet.rit_username %}
5050
<button class="btn btn-sm btn-primary sign-button"
51-
data-freshman_uid="{{ packet.freshman_username }}"
5251
data-packet_id="{{ packet.id }}"
5352
data-freshman_name="{{ get_rit_name(packet.freshman_username) }}">
5453
Sign

packet/templates/packet.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ <h3>{{ get_rit_name(packet.freshman_username) }}</h3>
1212
<div class="col">
1313
{% if can_sign and not did_sign %}
1414
<button class="btn btn-primary sign-button"
15-
data-freshman_uid="{{ packet.freshman_username }}"
1615
data-packet_id="{{ packet.id }}"
1716
data-freshman_name="{{ get_rit_name(packet.freshman_username) }}">Sign
1817
</button>

packet/templates/upperclassman.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ <h3 class="page-title">{{ get_csh_name(member) }}</h3>
2323
{# Using inline style because of how themeswitcher loads the css theme :( #}
2424
<tr {% if packet.did_sign_result %}style="background-color: #4caf505e" {% endif %}>
2525
<td>
26-
<a href="{{ url_for('freshman_packet', freshman_username=packet.freshman_username, packet_id=packet.id) }}">
26+
<a href="{{ url_for('freshman_packet', packet_id=packet.id) }}">
2727
<img class="eval-user-img"
2828
alt="{{ get_rit_name(packet.freshman_username) }}"
2929
src="https://www.gravatar.com/avatar/freshmen?d=mp&f=y"

0 commit comments

Comments
 (0)