示例#1
0
def test_roles():
    from users.models import Role
    #db = get_test_db()

    assert Role(db).get(0) == None

    recs = Role(db).select()
    assert recs != None
    assert len(recs) == 3
    assert recs[0].name != None

    rec = Role(db).new()
    rec.name = "Testing"
    rec.description = "A test role"

    recID = Role(db).save(rec)
    rec = Role(db).get(recID)
    assert rec.id == recID
    assert rec.name == 'Testing'
    assert rec.rank == 0

    #Modify the record
    rec.name = "New Test"
    rec.rank = 300
    Role(db).save(rec)
    rec = Role(db).get(rec.id)
    assert rec.name == "New Test"
    assert rec.rank == 300

    db.rollback()
示例#2
0
def setup_database(app):
    with app.app_context():
        from users.models import User
        from users.models import RolesUsers
        from users.models import Role
        from users.models import Techademy
        db.create_all()

        # TODO setup admin
        role = Role()
        role.name = "Admin"
        role.description = "This is the admin role"
        db.session.add(role)
        try:
            db.session.commit()
        except IntegrityError as err:
            db.session.rollback()
            if "UNIQUE constraint" in str(err):
                print("Admin role already exists, this is ok")
            else:
                print("Error with admin role, this needs to be looked into")
        role = Role()
        role.name = "Generic"
        role.description = "Just a viewer of the site"
        db.session.add(role)
        try:
            db.session.commit()
        except IntegrityError as err:
            db.session.rollback()

        # TODO find admin user
        user = User.query.filter_by(username="******").first()
        if user is not None:
            if not user.has_role("Admin"):
                role = Role.query.filter_by(name="Admin").first()
                user.roles.append(role)
                try:
                    db.session.commit()
                except IntegrityError as err:
                    db.session.rollback()
                    if "UNIQUE constraint" in str(err):
                        print("Admin relationship duplicate constraint issue")
                    else:
                        print("Error with user-role relationship")
                print("Added admin role")
            else:
                print("Admin is already admin")