Make event editing work
This commit is contained in:
parent
73c5877d75
commit
274d4050ee
2 changed files with 53 additions and 31 deletions
|
@ -31,7 +31,7 @@ def users():
|
||||||
return render_template("admin/users.html", u_list=user_list)
|
return render_template("admin/users.html", u_list=user_list)
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/events", methods=["GET","POST"])
|
@bp.route("/events")
|
||||||
@login_required
|
@login_required
|
||||||
def events():
|
def events():
|
||||||
if not current_user.is_admin:
|
if not current_user.is_admin:
|
||||||
|
@ -41,33 +41,6 @@ def events():
|
||||||
event_list = Event.query.all()
|
event_list = Event.query.all()
|
||||||
|
|
||||||
form = EventForm(request.form)
|
form = EventForm(request.form)
|
||||||
|
|
||||||
if request.method == 'POST':
|
|
||||||
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')
|
|
||||||
print(start_day)
|
|
||||||
print(start_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))
|
|
||||||
|
|
||||||
e = Event(
|
|
||||||
id=ulid.ulid(),
|
|
||||||
name=name,
|
|
||||||
description=description,
|
|
||||||
location=location,
|
|
||||||
start_time=start,
|
|
||||||
end_time=end)
|
|
||||||
db.session.add(e)
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
return render_template("admin/events.html", e_list=event_list, form=form)
|
return render_template("admin/events.html", e_list=event_list, form=form)
|
||||||
|
|
||||||
@bp.route("/event/<string:id>")
|
@bp.route("/event/<string:id>")
|
||||||
|
@ -99,3 +72,45 @@ def delete_event(id):
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return {"status": "success"}
|
return {"status": "success"}
|
||||||
|
|
||||||
|
@bp.route("/event/<string:id>", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
def update_create_event(id):
|
||||||
|
|
||||||
|
|
||||||
|
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(
|
||||||
|
id=ulid.ulid(),
|
||||||
|
name=name,
|
||||||
|
description=description,
|
||||||
|
location=location,
|
||||||
|
start_time=start,
|
||||||
|
end_time=end)
|
||||||
|
db.session.add(e)
|
||||||
|
db.session.commit()
|
||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
return redirect(url_for("admin.events"))
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
<th>Start Time</th>
|
<th>Start Time</th>
|
||||||
<th>End Time</th>
|
<th>End Time</th>
|
||||||
<th><button type="button" class="btn btn-primary" data-bs-toggle="modal"
|
<th><button type="button" class="btn btn-primary" data-bs-toggle="modal"
|
||||||
data-bs-target="#editModal">New</button></th>
|
data-bs-target="#editModal" data-id="0">New</button></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
@ -52,7 +52,7 @@
|
||||||
<h1 class="modal-title fs-5" id="editModalLabel">Event</h1>
|
<h1 class="modal-title fs-5" id="editModalLabel">Event</h1>
|
||||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||||
</div>
|
</div>
|
||||||
<form class="form" role="form" method="post">
|
<form class="form" id="edit-form" action="/admin/events/0" role="form" method="post">
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
{{ form.csrf_token }}
|
{{ form.csrf_token }}
|
||||||
<div class="form-floating mb-3 required">
|
<div class="form-floating mb-3 required">
|
||||||
|
@ -98,7 +98,7 @@
|
||||||
</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-secondary" data-bs-dismiss="modal">Close</button>
|
||||||
<button type="submit" class="btn btn-primary">Save changes</button>
|
<button type="submit" class="btn btn-primary" id="edit-save">Save changes</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
@ -128,6 +128,7 @@
|
||||||
<script src="{{ url_for('static', filename='js/jquery-3.6.3.min.js') }}" charset="utf-8"></script>
|
<script src="{{ url_for('static', filename='js/jquery-3.6.3.min.js') }}" charset="utf-8"></script>
|
||||||
<script charset="utf-8">
|
<script charset="utf-8">
|
||||||
const deleteButton = document.getElementById("delete")
|
const deleteButton = document.getElementById("delete")
|
||||||
|
const editButton = document.getElementById("edit-save")
|
||||||
|
|
||||||
deleteButton.addEventListener("click", (event) => {
|
deleteButton.addEventListener("click", (event) => {
|
||||||
button = $(event.relatedTarget)
|
button = $(event.relatedTarget)
|
||||||
|
@ -167,6 +168,12 @@
|
||||||
var name,description,loc,start_time,start_day,end_time,end_day
|
var name,description,loc,start_time,start_day,end_time,end_day
|
||||||
id = button.data('id')
|
id = button.data('id')
|
||||||
|
|
||||||
|
saveButton = document.getElementById("edit-save")
|
||||||
|
saveButton.dataset.id = id
|
||||||
|
|
||||||
|
editForm = document.getElementById("edit-form")
|
||||||
|
editForm.action = "/admin/event/" + id
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
$.get(`/admin/event/${id}`, (data) => {
|
$.get(`/admin/event/${id}`, (data) => {
|
||||||
console.log(data)
|
console.log(data)
|
||||||
|
|
Loading…
Add table
Reference in a new issue