~muirrum/comortas

a056065a812cf7df774ee2c51678af6e68373c28 — Cara Salter 3 years ago a496620
Implement Flask-Security
M fllscoring/__init__.py => fllscoring/__init__.py +17 -13
@@ 31,32 31,36 @@ def create_app():
    # Init extensions
    db.init_app(app)
    login_manager.init_app(app)
    migrator.init_app(app, db) 
    migrator.init_app(app, db)

    # Configure login manager
    login_manager.login_view = "auth.login"
    login_manager.login_message = "You need to be logged in to view that page"
    from fllscoring.models import Users, Role

    user_datastore = SQLAlchemyUserDatastore(db, Users, Role)
    security.init_app(app, user_datastore)

    # Init blueprints
    import fllscoring.auth
    app.register_blueprint(auth.bp)
    with app.app_context():
        # Init blueprints
        import fllscoring.auth

    import fllscoring.meta
    app.register_blueprint(meta.bp)
        app.register_blueprint(auth.bp)

    import fllscoring.profile
    app.register_blueprint(profile.bp)
        import fllscoring.meta

    import fllscoring.setup
    app.register_blueprint(setup.bp)
        app.register_blueprint(meta.bp)

    import fllscoring.scoring
    app.register_blueprint(scoring.bp)
        import fllscoring.profile

        app.register_blueprint(profile.bp)

    return app
        import fllscoring.setup

        app.register_blueprint(setup.bp)

        import fllscoring.scoring

        app.register_blueprint(scoring.bp)

    return app

M fllscoring/auth/__init__.py => fllscoring/auth/__init__.py +1 -3
@@ 53,6 53,4 @@ def logout():

@login_manager.user_loader
def user_loader(user_id):
    return Users.query.filter_by(user_id=user_id).first()


    return Users.query.filter_by(id=user_id).first()

M fllscoring/models.py => fllscoring/models.py +5 -2
@@ 1,17 1,20 @@
from sqlalchemy import ForeignKey
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.dialects.postgresql import JSONB, ARRAY
from sqlalchemy.ext.mutable import MutableDict

from fllscoring import db

from flask_security import UserMixin, RoleMixin

class Users(db.Model,UserMixin):

class Users(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, nullable=False, unique=True)
    email = db.Column(db.String, nullable=False)
    password_hash = db.Column(db.String, nullable=False)
    is_superadmin = db.Column(db.Boolean, default=False, nullable=False)
    active = db.Column(db.Boolean, default=True, nullable=False)
    tournaments = db.Column(ARRAY(db.Integer, dimensions=1))

    def __repr__(self):
        return f"<User {self.username} ({self.id})>"

M fllscoring/setup/__init__.py => fllscoring/setup/__init__.py +25 -7
@@ 21,27 21,45 @@ def new_tournament():
    if form.validate_on_submit():
        print(current_user)
        u = current_user._get_current_object()
        tourney = Tournaments(owner_id=u.user_id, tournament_name=form.tournament_name.data)
        tourney = Tournaments(owner_id=u.id, tournament_name=form.tournament_name.data)
        print(f"Created tourney {tourney}")
        db.session.add(tourney)
        print(f"Added tourney to session")
        db.session.commit()
        if u.tournaments is None:
            u.tournaments = [tourney.id]
        else:
            u.tournaments.append(tourney.id)
        db.session.commit()
        print("Committed")
        return redirect(url_for("setup.home"))
    else:
        for e in form.errors:
            flash(e)

    return render_template("setup/create_tournament.html", form=form, title="New Tournament")
    return render_template(
        "setup/create_tournament.html", form=form, title="New Tournament"
    )


@bp.route("/manage_tournament/<int:id>", methods=["GET", "POST"])
@login_required
def manage_tournament(id):
    tournament = Tournaments.query.filter_by(id=id).first()
    teams = Team.query.filter_by(tournament=id).all()
    if tournament is None:
        abort(404)
    return render_template("setup/tournament_mgmt.html", title=f"Update tournament {tournament.tournament_name}", tournament=tournament, teams=teams)
    perm = ViewTournamentPermission(id)

    if perm.can():
        tournament = Tournaments.query.filter_by(id=id).first()
        teams = Team.query.filter_by(tournament=id).all()
        if tournament is None:
            abort(404)
        return render_template(
            "setup/tournament_mgmt.html",
            title=f"Update tournament {tournament.tournament_name}",
            tournament=tournament,
            teams=teams,
        )
    abort(403)


@bp.route("/delete_tournament/<int:id>", methods=["GET", "POST"])
@login_required