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)
|
||||
|
||||
|
||||
@bp.route("/events", methods=["GET","POST"])
|
||||
@bp.route("/events")
|
||||
@login_required
|
||||
def events():
|
||||
if not current_user.is_admin:
|
||||
|
@ -41,33 +41,6 @@ def events():
|
|||
event_list = Event.query.all()
|
||||
|
||||
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)
|
||||
|
||||
@bp.route("/event/<string:id>")
|
||||
|
@ -99,3 +72,45 @@ def delete_event(id):
|
|||
db.session.commit()
|
||||
|
||||
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>End Time</th>
|
||||
<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>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
@ -52,7 +52,7 @@
|
|||
<h1 class="modal-title fs-5" id="editModalLabel">Event</h1>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</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">
|
||||
{{ form.csrf_token }}
|
||||
<div class="form-floating mb-3 required">
|
||||
|
@ -98,7 +98,7 @@
|
|||
</div>
|
||||
<div class="modal-footer">
|
||||
<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>
|
||||
</form>
|
||||
</div>
|
||||
|
@ -128,6 +128,7 @@
|
|||
<script src="{{ url_for('static', filename='js/jquery-3.6.3.min.js') }}" charset="utf-8"></script>
|
||||
<script charset="utf-8">
|
||||
const deleteButton = document.getElementById("delete")
|
||||
const editButton = document.getElementById("edit-save")
|
||||
|
||||
deleteButton.addEventListener("click", (event) => {
|
||||
button = $(event.relatedTarget)
|
||||
|
@ -167,6 +168,12 @@
|
|||
var name,description,loc,start_time,start_day,end_time,end_day
|
||||
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) {
|
||||
$.get(`/admin/event/${id}`, (data) => {
|
||||
console.log(data)
|
||||
|
|
Loading…
Add table
Reference in a new issue