Editing and creating events!
This commit is contained in:
parent
74394abdfe
commit
9d08c27135
6 changed files with 160 additions and 1 deletions
|
@ -7,6 +7,7 @@ from goathacks.models import User
|
||||||
bp = Blueprint("admin", __name__, url_prefix="/admin")
|
bp = Blueprint("admin", __name__, url_prefix="/admin")
|
||||||
|
|
||||||
from goathacks import db, mail as app_mail
|
from goathacks import db, mail as app_mail
|
||||||
|
from goathacks.admin import events
|
||||||
|
|
||||||
@bp.route("/")
|
@bp.route("/")
|
||||||
@login_required
|
@login_required
|
||||||
|
|
69
goathacks/admin/events.py
Normal file
69
goathacks/admin/events.py
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
from flask import render_template, redirect, request, url_for, flash
|
||||||
|
from flask_login import current_user, login_required
|
||||||
|
from goathacks.admin import bp, forms
|
||||||
|
from goathacks import db
|
||||||
|
from goathacks.models import Event
|
||||||
|
|
||||||
|
@bp.route("/events")
|
||||||
|
@login_required
|
||||||
|
def list_events():
|
||||||
|
if not current_user.is_admin:
|
||||||
|
return redirect(url_for("dashboard.home"))
|
||||||
|
|
||||||
|
events = Event.query.all()
|
||||||
|
|
||||||
|
return render_template("events/list.html", events=events)
|
||||||
|
|
||||||
|
@bp.route("/events/new", methods=["GET", "POST"])
|
||||||
|
@login_required
|
||||||
|
def new_event():
|
||||||
|
if not current_user.is_admin:
|
||||||
|
return redirect(url_for("dashboard.home"))
|
||||||
|
|
||||||
|
form = forms.EventForm(request.form)
|
||||||
|
if request.method == 'POST':
|
||||||
|
name = request.form.get("name")
|
||||||
|
description = request.form.get("description")
|
||||||
|
location = request.form.get("location")
|
||||||
|
start_time = request.form.get("start_time")
|
||||||
|
end_time = request.form.get("end_time")
|
||||||
|
category = request.form.get("category")
|
||||||
|
|
||||||
|
event = Event(
|
||||||
|
name = name,
|
||||||
|
description = description,
|
||||||
|
location = location,
|
||||||
|
start_time = start_time,
|
||||||
|
end_time = end_time,
|
||||||
|
category = category
|
||||||
|
)
|
||||||
|
|
||||||
|
db.session.add(event)
|
||||||
|
db.session.commit()
|
||||||
|
flash("Created event")
|
||||||
|
return redirect(url_for("admin.list_events"))
|
||||||
|
|
||||||
|
|
||||||
|
return render_template("events/new_event.html", form=form)
|
||||||
|
|
||||||
|
@bp.route("/events/edit/<int:id>", methods=["GET", "POST"])
|
||||||
|
@login_required
|
||||||
|
def edit_event(id):
|
||||||
|
if not current_user.is_admin:
|
||||||
|
return redirect(url_for("dashboard.home"))
|
||||||
|
|
||||||
|
event = Event.query.filter_by(id=id).one()
|
||||||
|
if event is None:
|
||||||
|
flash("Event does not exist")
|
||||||
|
return redirect(url_for("admin.list_events"))
|
||||||
|
|
||||||
|
form = forms.EventForm(request.form)
|
||||||
|
if request.method == 'POST':
|
||||||
|
form.populate_obj(event)
|
||||||
|
db.session.commit()
|
||||||
|
flash("Updated event")
|
||||||
|
return redirect(url_for("admin.list_events"))
|
||||||
|
else:
|
||||||
|
form = forms.EventForm(obj=event)
|
||||||
|
|
||||||
|
return render_template("events/new_event.html", form=form)
|
12
goathacks/admin/forms.py
Normal file
12
goathacks/admin/forms.py
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
from flask_wtf import FlaskForm
|
||||||
|
from wtforms import StringField, DateTimeField, SubmitField, TextAreaField
|
||||||
|
from wtforms.validators import DataRequired
|
||||||
|
|
||||||
|
class EventForm(FlaskForm):
|
||||||
|
name = StringField("Name", validators=[DataRequired()])
|
||||||
|
description = TextAreaField("Description")
|
||||||
|
location = StringField("Location", validators=[DataRequired()])
|
||||||
|
start_time = DateTimeField("Start Time", validators=[DataRequired()])
|
||||||
|
end_time = DateTimeField("End Time", validators=[DataRequired()])
|
||||||
|
category = StringField("Category")
|
||||||
|
submit = SubmitField("Submit")
|
|
@ -15,7 +15,7 @@ def workshop_checkin(id):
|
||||||
return redirect(url_for("dashboard.home"))
|
return redirect(url_for("dashboard.home"))
|
||||||
|
|
||||||
checkin = EventCheckins.query.filter_by(event_id=id,
|
checkin = EventCheckins.query.filter_by(event_id=id,
|
||||||
user_id=current_user.id).one()
|
user_id=current_user.id).first()
|
||||||
if checkin is not None:
|
if checkin is not None:
|
||||||
flash("You've already checked into this event!")
|
flash("You've already checked into this event!")
|
||||||
return redirect(url_for("dashboard.home"))
|
return redirect(url_for("dashboard.home"))
|
||||||
|
|
38
goathacks/templates/events/list.html
Normal file
38
goathacks/templates/events/list.html
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
|
||||||
|
<div style="height: 100%; color: white;">
|
||||||
|
<h2>Events</h2>
|
||||||
|
<table class="table table-striped table-hover table-condensed" style="color:
|
||||||
|
white;">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>ID</th>
|
||||||
|
<th>Name</th>
|
||||||
|
<th>Location</th>
|
||||||
|
<th>Start</th>
|
||||||
|
<th>End</th>
|
||||||
|
<th>Category</th>
|
||||||
|
<th>Checked in</th>
|
||||||
|
<th><a href="{{url_for('admin.new_event')}}">New</a></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for event in events %}
|
||||||
|
<tr id="{{event.id}}-row">
|
||||||
|
<td>{{ event.id }}</td>
|
||||||
|
<td>{{ event.name }}</td>
|
||||||
|
<td>{{ event.location }}</td>
|
||||||
|
<td>{{ event.start_time }}</td>
|
||||||
|
<td>{{ event.end_time }}</td>
|
||||||
|
<td>{{ event.category }}</td>
|
||||||
|
<td>{{ event.get_checkins()|length }}</td>
|
||||||
|
<td><a href="{{url_for('admin.edit_event', id=event.id)}}">Edit</a></td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% endblock %}
|
39
goathacks/templates/events/new_event.html
Normal file
39
goathacks/templates/events/new_event.html
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div style="height: 100%;">
|
||||||
|
<div id="registration-banner" class="parallax-container valign-wrapper">
|
||||||
|
<div class="section">
|
||||||
|
<h3 class="header-center text-darken-2">Create/Edit Event</h3>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="container">
|
||||||
|
<div class="section" style="background-color: #974355; padding: 20px;">
|
||||||
|
<form method="post">
|
||||||
|
{{ form.csrf_token }}
|
||||||
|
<div>
|
||||||
|
{{ form.name}}<br/> {{ form.name.label }}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
{{ form.description}}<br/>{{form.description.label}}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
{{ form.location}}<br/>{{form.location.label}}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
{{form.start_time}}<br/>{{form.start_time.label}}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
{{form.end_time}}<br/>{{form.end_time.label}}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
{{form.category}}<br/>{{form.category.label}}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
{{form.submit}}
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
Loading…
Add table
Reference in a new issue