diff --git a/app/forms.py b/app/forms.py index 40a889e..8f56215 100644 --- a/app/forms.py +++ b/app/forms.py @@ -1,7 +1,8 @@ from flask_wtf import FlaskForm -from wtforms import StringField, PasswordField, BooleanField, SubmitField, TextAreaField, DateTimeField +from wtforms import StringField, PasswordField, BooleanField, SubmitField, TextAreaField, DateTimeField, BooleanField, HiddenField from wtforms.validators import DataRequired, EqualTo, ValidationError, Length from app.models import Player +from pytz import timezone class LoginForm(FlaskForm): username = StringField('Username', validators=[DataRequired()]) @@ -23,6 +24,23 @@ class RegistrationForm(FlaskForm): class CreateGameForm(FlaskForm): game_name = StringField('Game Name', validators=[DataRequired(), Length(min=0, max=64)]) - start_time = DateTimeField(id='datetimepicker1') - end_time = DateTimeField(id='datetimepicker2') - submit = SubmitField('Register') \ No newline at end of file + start_time_disabled = BooleanField('No start time') + start_time = DateTimeField(id='datetimepicker_start', format="%d-%m-%Y %H:%M") + end_time_disabled = BooleanField('No end time') + end_time = DateTimeField(id='datetimepicker_end', format="%d-%m-%Y %H:%M") + timezone = HiddenField(validators=[DataRequired()]) + submit = SubmitField('Register') + + def validate_start_time(self, start_time): + self.date_time_validator(self.start_time_disabled, start_time) + + def validate_end_time(self, end_time): + self.date_time_validator(self.end_time_disabled, end_time) + + def date_time_validator(self, disabled, date_time): + if disabled.data: + date_time.data = None + return + clientzone = timezone(self.timezone.data) + date_time_utc = clientzone.localize(date_time.data).astimezone(timezone('UTC')) + date_time.data = date_time_utc diff --git a/app/models.py b/app/models.py index ce130d8..ed04440 100644 --- a/app/models.py +++ b/app/models.py @@ -6,6 +6,12 @@ from sqlalchemy.sql import func from secrets import token_hex from flask_login import UserMixin +class Role(Enum): + none = 0 + owner = 1 + hunter = 2 + bunny = 3 + class GameState(Enum): initiated = 1 published = 2 @@ -17,7 +23,7 @@ class GamePlayer(db.Model): __tablename__ = 'game_player' game_id = db.Column(db.Integer, db.ForeignKey('game.id'), primary_key=True, nullable=False) player_id = db.Column(db.Integer, db.ForeignKey('player.id'), primary_key=True, nullable=False) - role = db.Column(db.String(16)) + role = db.Column(db.Enum(Role), server_default=Role(0).name) game = db.relationship('Game', back_populates='game_players') player = db.relationship('Player', back_populates='player_games') diff --git a/app/routes.py b/app/routes.py index e78f47f..6357a14 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,7 +1,7 @@ from flask import render_template, flash, redirect, url_for, request from flask_login import login_user, logout_user, current_user, login_required from app import app, db -from app.models import Player, Game +from app.models import Player, Game, Role from app.forms import LoginForm, RegistrationForm, CreateGameForm @app.route('/', methods=['GET']) @@ -48,8 +48,10 @@ def register(): def create_game(): form = CreateGameForm() if form.validate_on_submit(): - game = Game(name=form.game_name.data) - return render_template('create_game.html', title='Create Game!', form=form) + game = Game(name=form.game_name.data, start_time=form.start_time.data, end_time=form.end_time.data) + game.gameplayers.append(GamePlayer(player=current_user, role=Role['owner'])) #check if this works, otherwise use 'owner' + print(game.start_time) + return render_template('create_game.html', title='Create Game', form=form) return render_template('create_game.html', title='Create Game', form=form) @login_required diff --git a/app/templates/create_game.html b/app/templates/create_game.html index e9fd41d..1fb6f7f 100644 --- a/app/templates/create_game.html +++ b/app/templates/create_game.html @@ -2,46 +2,107 @@ {% import 'bootstrap/wtf.html' as wtf %} {% block head %} - {{ super() }} - +{{ super() }} + {% endblock %} {% block app_content %} -