def login(): errors = {} FORM_ARGS = { 'email': FORM_USER_VALID['email'], 'password': FORM_USER_VALID['password'], } auth_token = None if request.method == 'POST': print("POST") try: parsed_args = parser.parse(FORM_ARGS, location="form") pprint(parsed_args) auth_token = app.action.user.user_get_auth_token( app_cls.service, parsed_args) #resp.set_cookie('auth_token', auth_token) print(f"--> auth_token {auth_token}") # FIXME: register auth_token cookie # FIXME: redirect except DataError as e: errors = e.args[0]['form'] print("ERROR", errors) except app.action.user.ErrWrongPassword as e: print("ERROR", "wrong password") errors = {"user": ["Wrong password or user does not exists."]} except app.action.user.ErrNotVerified as e: print("ERROR", "not verified") errors = {"user": ["User not verified, please check your email."]} resp = make_response(render_template('login.html', errors=errors)) if auth_token: resp = make_response(redirect('/')) resp.set_cookie('auth_token', auth_token) return resp
def signup(): errors = {} successes = {} FORM_ARGS = { 'name': FORM_USER_VALID['name'], 'email': FORM_USER_VALID['email'], 'password': FORM_USER_VALID['password'], } if request.method == 'POST': print("POST") try: parsed_args = parser.parse(FORM_ARGS, location="form") pprint(parsed_args) user = app.action.user.user_create(app_cls.service, parsed_args) url = send_message_verify(user.email, user.recovery) print(user.recovery) successes = { "user": [ f"We've sent you an email to {user.email} to verify your account. (debug: {url})" ] } except DataError as e: errors = e.args[0]['form'] print("ERROR", errors) except app.action.user.ErrDuplicate as e: print("ERROR", "duplicate") errors = {"user": ["This user already exists (with this email)."]} return render_template('signup.html', errors=errors, successes=successes)
def forget_code(code): errors = {} successes = {} args = base64.b64decode(code.encode('utf-8')).decode('utf-8') email, recovery = args.split("/") print(f"|{email}|, |{recovery}|") FORM_ARGS = { 'password': FORM_USER_VALID['password'], } if request.method == 'POST': try: parsed_args = parser.parse(FORM_ARGS, location="form") pprint(parsed_args) app.action.user.user_try_forget( app_cls.service, { "email": email, "recovery": recovery, "password": parsed_args["password"] }) successes = {"user": [f"Account for {email} is now active."]} except app.action.user.ErrWrongRecovery as e: print("ERROR", "wrong recovery") errors = {"user": [f"Wrong recovery code for {email}."]} except app.action.user.ErrTimeout as e: print("ERROR", "timeout") errors = {"user": [f"Timeout (more than 24h) for {email}."]} return render_template('forget_code.html', email=email, errors=errors, successes=successes)
def tournament_edit(idx): errors = {} successes = {} FORM_ARGS = { 'title': FORM_TOURNAMENT_VALID['title'], 'max_users': FORM_TOURNAMENT_VALID['max_users'], 'location': FORM_TOURNAMENT_VALID['location'], 'readme': FORM_TOURNAMENT_VALID['readme'], 'date_start': FORM_TOURNAMENT_VALID['date_start'], 'date_deadline': FORM_TOURNAMENT_VALID['date_deadline'], } from app.models import Tournament tournament = (Tournament.query.filter(Tournament.id == idx).first()) parsed_args = tournament.__dict__ # check if author is editing auth_token = request.cookies.get('auth_token') user = app.action.user.user_instance_from_token(app_cls, auth_token) if user.id != tournament.owner_id: return redirect(f"/tournament_display/{idx}") # if posted -> edit if request.method == 'POST': try: parsed_args = parser.parse(FORM_ARGS, location="form") parsed_args['user'] = user pprint(parsed_args) cur_date = datetime.datetime.utcnow() if parsed_args['date_deadline'] > parsed_args['date_start']: raise ErrFormDate if parsed_args['date_deadline'] < cur_date: raise ErrFormDate tournament = app.action.tournament.tournament_edit( app_cls.service, parsed_args, tournament) successes = {"tournament": [f"The event was edited."]} except DataError as e: errors = e.args[0]['form'] print("ERROR", errors) except app.action.tournament.ErrDuplicate as e: print("ERROR", "duplicate") errors = { "tournament": ["This event already exists (with this title)."] } except ErrFormDate as e: print("ERROR", "date") errors = { "tournament": [ "Wrong datetime span (should be: current <= deadline <= start)." ] } return render_template('tournament_edit.html', tournament=tournament, errors=errors, successes=successes, parsed_args=parsed_args)
def tournament_new(): errors = {} successes = {} FORM_ARGS = { 'title': FORM_TOURNAMENT_VALID['title'], 'max_users': FORM_TOURNAMENT_VALID['max_users'], 'location': FORM_TOURNAMENT_VALID['location'], 'readme': FORM_TOURNAMENT_VALID['readme'], 'date_start': FORM_TOURNAMENT_VALID['date_start'], 'date_deadline': FORM_TOURNAMENT_VALID['date_deadline'], } parsed_args = request.form.to_dict(flat=True) # print(request.form.to_dict(flat=True)) if request.method == 'POST': try: auth_token = request.cookies.get('auth_token') user = app.action.user.user_instance_from_token( app_cls, auth_token) parsed_args = parser.parse(FORM_ARGS, location="form") parsed_args['user'] = user pprint(parsed_args) cur_date = datetime.datetime.utcnow() if parsed_args['date_deadline'] > parsed_args['date_start']: raise ErrFormDate if parsed_args['date_deadline'] < cur_date: raise ErrFormDate tournament = app.action.tournament.tournament_create( app_cls.service, parsed_args) successes = {"tournament": [f"The event was created."]} except DataError as e: errors = e.args[0]['form'] print("ERROR", errors) except app.action.tournament.ErrDuplicate as e: print("ERROR", "duplicate") errors = { "tournament": ["This event already exists (with this title)."] } except ErrFormDate as e: print("ERROR", "date") errors = { "tournament": [ "Wrong datetime span (should be: current <= deadline <= start)." ] } return render_template('tournament_new.html', errors=errors, successes=successes, parsed_args=parsed_args)
def tournament_join(idx): errors = {} successes = {} parsed_args = {} FORM_ARGS = { 'license': FORM_TOURNAMENT_VALID['license'], 'rating': FORM_TOURNAMENT_VALID['rating'], } from app.models import Tournament tournament = (Tournament.query.filter(Tournament.id == idx).first()) auth_token = request.cookies.get('auth_token') user = app.action.user.user_instance_from_token(app_cls, auth_token) if request.method == 'POST': try: parsed_args = parser.parse(FORM_ARGS, location="form") parsed_args['user'] = user pprint(parsed_args) cur_date = datetime.datetime.utcnow() if tournament.date_deadline < cur_date: raise ErrFormDate app.action.tournament.tournament_join(app_cls.service, parsed_args, tournament, user) successes = {"tournament": [f"User joined the list."]} except DataError as e: errors = e.args[0]['form'] print("ERROR", errors) except ErrFormDate as e: print("ERROR", "date") errors = {"tournament": ["Deadline passed."]} except app.action.tournament.ErrAlreadyJoined as e: print("ERROR", "already") errors = {"tournament": ["User already on the list."]} return render_template('tournament_join.html', tournament=tournament, errors=errors, successes=successes, parsed_args=parsed_args)
def forget(): errors = {} successes = {} FORM_ARGS = { 'email': FORM_USER_VALID['email'], } if request.method == 'POST': try: parsed_args = parser.parse(FORM_ARGS, location="form") pprint(parsed_args) user = app.action.user.user_new_recovery(app_cls.service, parsed_args) url = send_message_forget(user.email, user.recovery) successes = { "user": [ f"We've sent you an email to {user.email} to recover your account. (debug: {url})" ] } except DataError as e: errors = e.args[0]['form'] print("ERROR", errors) except Exception as e: print("HACKER/DDOS?", e) return render_template('forget.html', errors=errors, successes=successes)
def match_new(idx): from app.models import Match, User match = (Match.query.filter(Match.id == idx).first()) from app.models import Tournament tournament = (Tournament.query.filter( Tournament.id == match.tournament_id).first()) auth_token = request.cookies.get('auth_token') user = app.action.user.user_instance_from_token(app_cls, auth_token) #################################################################### errors = {} successes = {} FORM_ARGS = { 'score_1': FORM_TOURNAMENT_VALID['score_1'], 'score_2': FORM_TOURNAMENT_VALID['score_2'], } parsed_args = {} user_1 = None user_2 = None if match.user_id_1: _user = (User.query.filter(User.id == match.user_id_1).first()) user_1 = _user if match.user_id_2: _user = (User.query.filter(User.id == match.user_id_2).first()) user_2 = _user if user.id not in [match.user_id_1, match.user_id_2]: return redirect(f'/tournament_display/{tournament.id}') ##### # fixme unpack score for user! results_by_user = None if user.id is match.user_id_1: results_by_user = match.results_by_user_id_1 if user.id is match.user_id_2: results_by_user = match.results_by_user_id_2 if results_by_user: parsed_args['score_1'], parsed_args['score_2'] = map( int, results_by_user.split("@")) if request.method == 'POST': try: parsed_args = parser.parse(FORM_ARGS, location="form") pprint(parsed_args) results_by_user = str(parsed_args['score_1']) + "@" + str( parsed_args['score_2']) ########################################### if user.id is match.user_id_1: match.results_by_user_id_1 = results_by_user if user.id is match.user_id_2: match.results_by_user_id_2 = results_by_user if match.results_by_user_id_1 == match.results_by_user_id_2: match.results = match.results_by_user_id_1 # FIXME: set winner WHO IS winner_id = None loser_id = None if parsed_args['score_1'] > parsed_args['score_2']: winner_id = match.user_id_1 loser_id = match.user_id_2 else: winner_id = match.user_id_2 loser_id = match.user_id_1 if match.next_match_id: match_next = (Match.query.filter( Match.tournament_id == match.tournament_id).filter( Match.match_id == match.next_match_id).first()) if match.match_id % 2 == 1: match_next.user_id_1 = winner_id else: match_next.user_id_2 = winner_id if match_next.flag == 1: # FIXME: for 3th place match_next_losers = \ (Match.query.filter(Match.tournament_id == match.tournament_id).filter(Match.match_id == match_next.next_match_id).first()) if match.match_id % 2 == 1: match_next_losers.user_id_1 = loser_id else: match_next_losers.user_id_2 = loser_id app_cls.service.db.session.commit() ########################################### successes = {"match": [f"The score was recorded."]} except DataError as e: errors = e.args[0]['form'] print("ERROR", errors) """ results = db.Column(db.Text) results_by_user_id_1 = db.Column(db.Text) results_by_user_id_2 = db.Column(db.Text) #return redirect(f'/tournament_display/{tournament.id}') """ return render_template('match_new.html', match=match, tournament=tournament, user_1=user_1, user_2=user_2, errors=errors, successes=successes, parsed_args=parsed_args)