Updates for GoatHacks 2025 #34
4 changed files with 113 additions and 4 deletions
|
@ -1,11 +1,11 @@
|
||||||
import flask
|
import flask
|
||||||
from flask import Response, render_template, redirect, request, url_for, flash
|
from flask import Response, render_template, redirect, request, url_for, flash, current_app
|
||||||
from flask_login import current_user, login_required
|
from flask_login import current_user, login_required
|
||||||
from goathacks.admin import bp, forms
|
from goathacks.admin import bp, forms
|
||||||
from goathacks import db
|
from goathacks import db
|
||||||
from goathacks.models import Event
|
from goathacks.models import Event
|
||||||
|
|
||||||
import io, qrcode
|
import io, qrcode, datetime
|
||||||
import qrcode.image.pure
|
import qrcode.image.pure
|
||||||
|
|
||||||
@bp.route("/events")
|
@bp.route("/events")
|
||||||
|
@ -20,6 +20,80 @@ def list_events():
|
||||||
|
|
||||||
return render_template("events/list.html", events=events, form=form)
|
return render_template("events/list.html", events=events, form=form)
|
||||||
|
|
||||||
|
@bp.route("/event/<int:id>/delete")
|
||||||
|
@login_required
|
||||||
|
def delete_event(id):
|
||||||
|
if not current_user.is_admin:
|
||||||
|
return {"status": "error", "message": "Unauthorized"}
|
||||||
|
|
||||||
|
event = Event.query.filter_by(id=id).first()
|
||||||
|
|
||||||
|
if event is None:
|
||||||
|
return {"status": "error", "message": "Invalid event ID"}
|
||||||
|
|
||||||
|
db.session.delete(event)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return {"status": "success"}
|
||||||
|
|
||||||
|
@bp.route("/event/<int:id>")
|
||||||
|
@login_required
|
||||||
|
def event(id):
|
||||||
|
if not current_user.is_admin:
|
||||||
|
return {"status": "error", "message": "Unauthorized"}
|
||||||
|
|
||||||
|
event = Event.query.filter_by(id=id).first()
|
||||||
|
|
||||||
|
if event is None:
|
||||||
|
return {"status": "error", "message": "Invalid event ID"}
|
||||||
|
|
||||||
|
return event.create_json()
|
||||||
|
|
||||||
|
@bp.route("/event/<int:id>", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
def update_create_event(id):
|
||||||
|
if not current_user.is_admin:
|
||||||
|
flash("Unauthorized")
|
||||||
|
return redirect(url_for("dashboard.home"))
|
||||||
|
|
||||||
|
name = request.form.get('name')
|
||||||
|
description = request.form.get('description')
|
||||||
|
location = request.form.get('location')
|
||||||
|
start_day = request.form.get('start_day')
|
||||||
|
start_time = request.form.get('start_time')
|
||||||
|
end_day = request.form.get('end_day')
|
||||||
|
end_time = request.form.get('end_time')
|
||||||
|
start = datetime.datetime.combine(datetime.date.fromisoformat(start_day),
|
||||||
|
datetime.time.fromisoformat(start_time))
|
||||||
|
end = datetime.datetime.combine(datetime.date.fromisoformat(end_day),
|
||||||
|
datetime.time.fromisoformat(end_time))
|
||||||
|
|
||||||
|
if id == 0:
|
||||||
|
# new event
|
||||||
|
e = Event(
|
||||||
|
name=name,
|
||||||
|
description=description,
|
||||||
|
location=location,
|
||||||
|
start_time=start,
|
||||||
|
end_time=end)
|
||||||
|
db.session.add(e)
|
||||||
|
db.session.commit()
|
||||||
|
current_app.logger.info(f"{current_user} is creating a new event: {e.name}")
|
||||||
|
else:
|
||||||
|
e = Event.query.filter_by(id=id).first()
|
||||||
|
if e is None:
|
||||||
|
return {"status": "error", "message": "Invalid event ID"}
|
||||||
|
e.name = name
|
||||||
|
e.description = description
|
||||||
|
e.location = location
|
||||||
|
e.start_time = start
|
||||||
|
e.end_time = end
|
||||||
|
db.session.commit()
|
||||||
|
current_app.logger.info(f"{current_user} is updating an existing event: {e.name}")
|
||||||
|
|
||||||
|
|
||||||
|
return redirect(url_for("admin.list_events"))
|
||||||
|
|
||||||
@bp.route("/events/events.json")
|
@bp.route("/events/events.json")
|
||||||
@login_required
|
@login_required
|
||||||
def events_json():
|
def events_json():
|
||||||
|
|
|
@ -84,6 +84,16 @@ class Event(db.Model):
|
||||||
})
|
})
|
||||||
|
|
||||||
return events
|
return events
|
||||||
|
|
||||||
|
def create_json(self):
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"name": self.name,
|
||||||
|
"description": self.description,
|
||||||
|
"location": self.location,
|
||||||
|
"start_time": self.start_time.isoformat(),
|
||||||
|
"end_time": self.end_time.isoformat(),
|
||||||
|
}
|
||||||
|
|
||||||
def get_checkins(self):
|
def get_checkins(self):
|
||||||
checkins = EventCheckins.query.filter_by(event_id=self.id).all()
|
checkins = EventCheckins.query.filter_by(event_id=self.id).all()
|
||||||
|
|
2
goathacks/static/js/jquery-3.6.3.min.js
vendored
Normal file
2
goathacks/static/js/jquery-3.6.3.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
|
@ -34,7 +34,7 @@
|
||||||
<td>{{ event.get_checkins()|length }}</td>
|
<td>{{ event.get_checkins()|length }}</td>
|
||||||
<td><a href='{{ url_for("admin.qrcode_event", id=event.id)
|
<td><a href='{{ url_for("admin.qrcode_event", id=event.id)
|
||||||
}}'>QR Code</a></td>
|
}}'>QR Code</a></td>
|
||||||
<td><a href="#editModal" data-bs-toggle="modal" data-id="{{ e.id}}" >Edit</a></td>
|
<td><a href="#editModal" data-bs-toggle="modal" data-id="{{ event.id}}" >Edit</a></td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
@ -95,7 +95,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
<button type="button" class="btn btn-danger" data-id="0" id="delete">Delete</button>
|
||||||
<button type="submit" class="btn btn-primary" id="edit-save">Save changes</button>
|
<button type="submit" class="btn btn-primary" id="edit-save">Save changes</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
@ -108,6 +108,22 @@
|
||||||
<script charset="utf-8">
|
<script charset="utf-8">
|
||||||
const editButton = document.getElementById("edit-save")
|
const editButton = document.getElementById("edit-save")
|
||||||
|
|
||||||
|
$('#delete').on("click", (event) => {
|
||||||
|
if (window.confirm("Delete this event?")) {
|
||||||
|
console.log("Got OK")
|
||||||
|
deleteButton = document.getElementById("delete")
|
||||||
|
id = deleteButton.dataset.id
|
||||||
|
$.get(`/admin/event/${id}/delete`, (data) => {
|
||||||
|
if (data.status == "error") {
|
||||||
|
window.alert(`Error: ${data.message}`)
|
||||||
|
} else {
|
||||||
|
window.alert("Success")
|
||||||
|
}
|
||||||
|
location.reload()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
$('#editModal').on('show.bs.modal', function(event) {
|
$('#editModal').on('show.bs.modal', function(event) {
|
||||||
var modal = $(this)
|
var modal = $(this)
|
||||||
modal.find('#name').val('')
|
modal.find('#name').val('')
|
||||||
|
@ -125,6 +141,9 @@
|
||||||
saveButton = document.getElementById("edit-save")
|
saveButton = document.getElementById("edit-save")
|
||||||
saveButton.dataset.id = id
|
saveButton.dataset.id = id
|
||||||
|
|
||||||
|
deleteButton = document.getElementById("delete")
|
||||||
|
deleteButton.dataset.id = id
|
||||||
|
|
||||||
editForm = document.getElementById("edit-form")
|
editForm = document.getElementById("edit-form")
|
||||||
editForm.action = "/admin/event/" + id
|
editForm.action = "/admin/event/" + id
|
||||||
|
|
||||||
|
@ -166,5 +185,9 @@
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function padTwoDigits(num) {
|
||||||
|
return num.toString().padStart(2, '0')
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
Loading…
Add table
Reference in a new issue