Init event checkin structure
This commit is contained in:
parent
83b3eef3bd
commit
302fe4289a
4 changed files with 93 additions and 2 deletions
|
@ -1,9 +1,47 @@
|
|||
|
||||
from flask import Blueprint, render_template
|
||||
from datetime import datetime
|
||||
from flask import Blueprint, current_app, flash, redirect, render_template, url_for
|
||||
from flask_login import login_required, current_user
|
||||
from ulid import ulid
|
||||
|
||||
from acmsite.models import Event, EventCheckin
|
||||
from acmsite import db
|
||||
|
||||
|
||||
bp = Blueprint('dashboard', __name__, url_prefix='/dashboard')
|
||||
|
||||
@bp.route("/")
|
||||
@login_required
|
||||
def home():
|
||||
return render_template('dashboard.html')
|
||||
now = datetime.now()
|
||||
events = db.session.execute(db.select(Event).where(Event.start_time < now,
|
||||
Event.end_time >
|
||||
now)).scalars()
|
||||
return render_template('dashboard.html', events=events)
|
||||
|
||||
@bp.route("/checkin/<string:event_id>")
|
||||
@login_required
|
||||
def checkin(event_id):
|
||||
# actually first check if the event even exists
|
||||
event = db.get_or_404(Event, event_id)
|
||||
# first check if this user has already checked in
|
||||
checkins = db.session.execute(db.select(EventCheckin).where(EventCheckin.user ==
|
||||
current_user.id,
|
||||
EventCheckin.event
|
||||
==event_id)).scalar_one_or_none()
|
||||
|
||||
current_app.logger.debug(checkins)
|
||||
if checkins is None:
|
||||
# There's not a checkin already, let's create one!
|
||||
check = EventCheckin(
|
||||
id = ulid(),
|
||||
user = current_user.id,
|
||||
event = event_id)
|
||||
db.session.add(check)
|
||||
db.session.commit()
|
||||
|
||||
flash(f"Checked in to {event.name} successfully")
|
||||
return redirect(url_for("dashboard.home"))
|
||||
else:
|
||||
flash(f"You've already checked in to {event.name}")
|
||||
return redirect(url_for("dashboard.home"))
|
||||
|
|
|
@ -80,6 +80,12 @@ class Event(db.Model):
|
|||
"end_time": self.end_time.isoformat(),
|
||||
}
|
||||
|
||||
class EventCheckin(db.Model):
|
||||
__tablename__ = "acm_checkins"
|
||||
id = Column(String, primary_key=True)
|
||||
user = Column(String, ForeignKey("acm_users.id"), nullable=False)
|
||||
event = Column(String, ForeignKey("acm_events.id"), nullable=False)
|
||||
|
||||
class Link(db.Model):
|
||||
__tablename__ = "acm_links"
|
||||
id = Column(String, primary_key=True)
|
||||
|
|
|
@ -4,6 +4,18 @@
|
|||
|
||||
<h1>Welcome back, {{ current_user.first_name }}!</h1>
|
||||
|
||||
{% if events %}
|
||||
|
||||
<p>The following events are available for check-in:</p>
|
||||
{% for e in events %}
|
||||
<a href="{{ url_for('dashboard.checkin', event_id=e.id) }}">{{ e.name }}</a>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<p>There are no events available for check-in.</p>
|
||||
{% endif %}
|
||||
|
||||
<hr/>
|
||||
|
||||
<p>For a list of upcoming events, take a look at our <a href="{{
|
||||
url_for('main.events')
|
||||
}}">events
|
||||
|
|
35
migrations/versions/53a76e988b5a_add_event_checkins.py
Normal file
35
migrations/versions/53a76e988b5a_add_event_checkins.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
"""add event checkins
|
||||
|
||||
Revision ID: 53a76e988b5a
|
||||
Revises: 300f24071c14
|
||||
Create Date: 2024-08-25 15:18:22.451548
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '53a76e988b5a'
|
||||
down_revision = '300f24071c14'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('acm_checkins',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('user', sa.String(), nullable=False),
|
||||
sa.Column('event', sa.String(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['event'], ['acm_events.id'], ),
|
||||
sa.ForeignKeyConstraint(['user'], ['acm_users.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('acm_checkins')
|
||||
# ### end Alembic commands ###
|
Loading…
Add table
Reference in a new issue