def show_game(jam_slug, game_id): comment_form = WriteComment() jam = Jam.query.filter_by(slug=jam_slug).first_or_404() game = Game.query.filter_by(is_deleted=False, id=game_id).filter_by(jam=jam).first_or_404() if current_user.is_authenticated and comment_form.validate_on_submit(): comment = Comment(comment_form.text.data, game, current_user) db.session.add(comment) db.session.commit() # notify the team for user in game.team.members: if user.notify_game_comment: body = render_template("emails/comment.txt", recipient=user, comment=comment) mail.send_message( subject= f"{current_user.username} commented on {game.title}", recipients=[user.email], body=body) flash("Your comment has been posted.", "success") return redirect(game.url()) rating = Rating.query.filter_by(game_id=game.id, user_id=current_user.get_id()).first() return render_template('jam/game/info.html', game=game, form=comment_form, rating=rating)
def reset_request(): if current_user.is_authenticated: flash("You are already logged in.", "info") return redirect(url_for("index")) error = None form = ResetPassword() if form.validate_on_submit(): # thanks to the UsernameValidator we cam assume the username exists user = User.query.filter( func.lower(User.username) == func.lower( form.username.data)).first() user.token = randint(0, sys.maxsize) db.session.commit() body = render_template("emails/account/reset_password.txt", recipient=user) mail.send_message( subject=f"{app.config['LONG_NAME']}: Reset your password", recipients=[user.email], body=body) flash("Your password has been reset, check your email.", "success") return render_template('account/reset_request.html', form=form, error=error)
def inviteUser(self, user, sender): # sender: which user sent the invitation if not user.notify_team_invitation: return None if self.getInvitation(user): i = self.getInvitation(user) # already invited else: i = Invitation(self, user) db.session.add(i) db.session.commit() body = render_template("emails/invitation.txt", team=self, sender=sender, recipient=user, invitation=i) mail.send_message(subject=app.config["LONG_NAME"] +": You have been invited to " + self.name, recipients=[user.email], body=body) return i
def login(): login_form = UserLogin() register_form = UserRegistration() if login_form.validate_on_submit(): username = login_form.username.data password = login_form.password.data remember_me = login_form.remember_me.data user = User.query.filter( func.lower(User.username) == func.lower(username)).first() if login_user(user, remember_me): flash("You have been logged in.", "success") if user.invitations.count(): markup = f'You have {user.invitations.count()} team invitations' markup += f'- click <a href="{url_for("invitations")}">here</a> to view them.' flash(Markup(markup), "info") return redirect(request.args.get("next") or url_for('index')) # Tell Flask-Principal the identity changed identity_changed.send(current_app._get_current_object(), identity=Identity(user.id)) else: flash("Login failed, user not validated. Check your email!", "error") return redirect(url_for("verify_status", username=username)) elif register_form.validate_on_submit(): username = register_form.username.data.strip() password = register_form.password.data email = register_form.email.data new_user = User(username, password, email) body = render_template("emails/account/verification.txt", recipient=new_user, email_changed=False) mail.send_message( subject=f"Welcome to {app.config['LONG_NAME']}, {username}", recipients=[new_user.email], body=body) db.session.add(new_user) db.session.commit() flash( "Your account has been created, check your email address for a link to verify your account.", "success") return redirect(url_for('verify_status', username=username)) return render_template('account/login.html', login_form=login_form, register_form=register_form)
def contact_user(username): user = User.query.filter_by(is_deleted = False, username = username).first_or_404() if user == current_user or user.pm_mode == "disabled": abort(403) form = ContactUserForm() if form.validate_on_submit(): message = form.message.data body = render_template("emails/account/message.txt", recipient=user, sender=current_user, message=message) mail.send_message(subject=app.config["LONG_NAME"] + ": New message from " + current_user.username, recipients=[user.email], reply_to=current_user.email, body=body) flash("Message successfully sent", "success") return render_template("account/contact.html", user = user, form = form)
def verify_send(): if request.method == 'GET': return redirect(url_for('index')) username = request.form.get('username', "") user = User.query.filter_by(username = username).first_or_404() if user.is_verified: flash("%s's account is already validated." % user.username.capitalize(), "info") return redirect(url_for('index')) body=render_template("emails/account/verification.txt", recipient=user) mail.send_message(subject="Welcome to " + app.config["LONG_NAME"] + ", " + username, recipients=[user.new_email], body=body) flash("Verification has been resent, check your email", "success") return redirect(url_for('verify_status', username=username))
def reset_request(): if current_user.is_authenticated(): flash("You are already logged in.", "info") return redirect(url_for("index")) error = None form = ResetPassword() if form.validate_on_submit(): # thanks to the UsernameValidator we cam assume the username exists user = User.query.filter_by(username=form.username.data).first() user.token = randint(0, sys.maxint) db.session.commit() body = render_template("emails/account/reset_password.txt", recipient=user) mail.send_message(subject=app.config["LONG_NAME"] + ": Reset your password", recipients=[user.email], body=body) flash("Your password has been reset, check your email.", "success") return render_template("account/reset_request.html", form=form, error=error)
def login(): login_form = UserLogin() register_form = UserRegistration() if login_form.validate_on_submit(): username = login_form.username.data password = login_form.password.data remember_me = login_form.remember_me.data user = User.query.filter_by(username=username).first() if login_user(user, remember_me): flash("You were logged in.", "success") if user.invitations.count(): flash( Markup( 'You have %s team invitations - click <a href="%s">here</a> to view them.' % (user.invitations.count(), url_for("invitations")) ), "info", ) return redirect(request.args.get("next") or url_for("index")) # Tell Flask-Principal the identity changed identity_changed.send(current_app._get_current_object(), identity=Identity(user.id)) else: flash("Login failed, user not validated", "error") return redirect(url_for("verify_status", username=username)) elif register_form.validate_on_submit(): username = register_form.username.data.strip() password = register_form.password.data email = register_form.email.data new_user = User(username, password, email) body = render_template("emails/account/verification.txt", recipient=new_user, email_changed=False) mail.send_message( subject="Welcome to " + app.config["LONG_NAME"] + ", " + username, recipients=[new_user.email], body=body ) db.session.add(new_user) db.session.commit() flash("Your account has been created, confirm your email to verify.", "success") return redirect(url_for("verify_status", username=username)) return render_template("account/login.html", login_form=login_form, register_form=register_form)
def show_game(jam_slug, game_id): comment_form = WriteComment() jam = Jam.query.filter_by(slug = jam_slug).first_or_404() game = Game.query.filter_by(is_deleted = False, id = game_id).filter_by(jam = jam).first_or_404() if current_user.is_authenticated() and comment_form.validate_on_submit(): comment = Comment(comment_form.text.data, game, current_user) db.session.add(comment) db.session.commit() # notify the team for user in game.team.members: if user.notify_game_comment: body = render_template("emails/comment.txt", recipient=user, comment=comment) mail.send_message(subject=current_user.username + " commented on " + game.title, recipients=[user.email], body=body) flash("Your comment has been posted.", "success") return redirect(game.url()) rating = Rating.query.filter_by(game_id = game.id, user_id = current_user.get_id()).first() return render_template('jam/game/info.html', game = game, form = comment_form, rating = rating)
def settings(): user = current_user form = SettingsForm(obj=user) logout = False if form.validate_on_submit(): user.ability_programmer = form.ability_programmer.data user.ability_gamedesigner = form.ability_gamedesigner.data user.ability_2dartist = form.ability_2dartist.data user.ability_3dartist = form.ability_3dartist.data user.ability_composer = form.ability_composer.data user.ability_sounddesigner = form.ability_sounddesigner.data user.abilities_extra = form.abilities_extra.data user.real_name = form.real_name.data user.about = form.about.data user.website = form.website.data user.pm_mode = form.pm_mode.data user.avatar = form.avatar.data user.notify_new_jam = form.notify_new_jam.data user.notify_jam_start = form.notify_jam_start.data user.notify_jam_finish = form.notify_jam_finish.data user.notify_game_comment = form.notify_game_comment.data user.notify_team_invitation = form.notify_team_invitation.data user.notify_newsletter = form.notify_newsletter.data if user.location != form.location.data and form.location.data: if user.setLocation(form.location.data): flash(f"Location was set to: {user.location_display}", "success") else: flash("Could not find the location you entered.", "error") if not form.location.data: user.setLocation("") if form.old_password.data and form.new_password.data and form.new_password2.data: if not verify_password(user.password, form.old_password.data): flash( "Your password is incorrect. The password was not changed.", "error") else: user.password = hash_password(form.new_password.data) flash("Your password was changed", "success") if user.email != form.email.data and form.email.data: user.new_email = form.email.data user.is_verified = False same_email = User.query.filter_by(email=user.new_email).all() if not (len(same_email) == 0 or (len(same_email) == 1 and same_email[0] == user)): flash( "This email address is already in use by another account.", "error") return redirect(url_for("settings")) body = render_template("emails/account/verification.txt", recipient=user, email_changed=True) mail.send_message( subject=f"{app.config['LONG_NAME']}: email verification", recipients=[user.new_email], body=body) logout = True flash( "Your email address has changed. Please check your inbox for the verification.", "info") db.session.commit() flash("Your settings were saved.", "success") if logout: return redirect(url_for("logout")) else: return redirect(url_for("settings")) return render_template('account/settings.html', form=form)
def settings(): user = current_user form = SettingsForm(obj=user) logout = False if form.validate_on_submit(): user.ability_programmer = form.ability_programmer.data user.ability_gamedesigner = form.ability_gamedesigner.data user.ability_2dartist = form.ability_2dartist.data user.ability_3dartist = form.ability_3dartist.data user.ability_composer = form.ability_composer.data user.ability_sounddesigner = form.ability_sounddesigner.data user.abilities_extra = form.abilities_extra.data user.real_name = form.real_name.data user.about = form.about.data user.website = form.website.data user.pm_mode = form.pm_mode.data user.avatar = form.avatar.data user.notify_new_jam = form.notify_new_jam.data user.notify_jam_start = form.notify_jam_start.data user.notify_jam_finish = form.notify_jam_finish.data user.notify_game_comment = form.notify_game_comment.data user.notify_team_invitation = form.notify_team_invitation.data user.notify_newsletter = form.notify_newsletter.data if user.location != form.location.data and form.location.data: if user.setLocation(form.location.data): flash("Location was set to: " + user.location_display, "success") else: flash("Could not find the location you entered.", "error") if not form.location.data: user.setLocation("") if form.old_password.data and form.new_password.data and form.new_password2.data: if not verify_password(user.password, form.old_password.data): flash("Your password is incorrect. The password was not changed.", "error") else: user.password = hash_password(form.new_password.data) flash("Your password was changed", "success") if user.email != form.email.data and form.email.data: user.new_email = form.email.data user.is_verified = False same_email = User.query.filter_by(email=user.new_email).all() if not (len(same_email) == 0 or (len(same_email) == 1 and same_email[0] == user)): flash("This email address is already in use by another account.", "error") return redirect(url_for("settings")) body = render_template("emails/account/verification.txt", recipient=user, email_changed=True) mail.send_message( subject=app.config["LONG_NAME"] + ": eMail verification", recipients=[user.new_email], body=body ) logout = True flash("Your email address has changed. Please check your inbox for the verification.", "info") db.session.commit() flash("Your settings were saved.", "success") if logout: return redirect(url_for("logout")) else: return redirect(url_for("settings")) return render_template("account/settings.html", form=form)