Exemple #1
0
def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    from models import Department, Employee, Role
    Base.metadata.drop_all(bind=engine)
    Base.metadata.create_all(bind=engine)

    # Create the fixtures
    engineering = Department(name='Engineering')
    db_session.add(engineering)
    hr = Department(name='Human Resources')
    db_session.add(hr)

    manager = Role(name='manager')
    db_session.add(manager)
    engineer = Role(name='engineer')
    db_session.add(engineer)

    peter = Employee(name='Peter', department=engineering, role=engineer)
    db_session.add(peter)
    roy = Employee(name='Roy', department=engineering, role=engineer)
    db_session.add(roy)
    tracy = Employee(name='Tracy', department=hr, role=manager)

    # postgresql specific dialects tests
    # tracy.articles = [1, 2, 3, 4]
    # tracy.json_data = {"test_json": "test_json"}
    # tracy.jsonb_data = {"test_jsonb": "test_jsonb"}
    # tracy.hstore_data = {"test_hstore": "test_hstore"}

    db_session.add(tracy)
    db_session.commit()
Exemple #2
0
    def setUpClass(cls):
        #empty database
        db.session.commit()
        db.session.close()
        db.drop_all()
        db.create_all()

        #set up data
        admin_role = Role(name='admin')
        db.session.add(admin_role)
        user_role = Role(name='user')
        db.session.add(user_role)
        admin = Users(username='******',
                      first_name='first',
                      last_name='last',
                      role=admin_role)
        admin.set_password('password')
        db.session.add(admin)
        user = Users(username='******',
                     first_name='first',
                     last_name='last',
                     role=user_role)
        user.set_password('password')
        db.session.add(user)
        db.session.commit()
        db.session.close()
Exemple #3
0
    def setUp(self):
        self.client = app.test_client()
        db.drop_all()
        db.create_all()

        ro1 = Role(role_id=0, role_label="user")

        ro2 = Role(role_id=1, role_label="assignee")

        ro3 = Role(role_id=2, role_label="admin")
        db.session.add_all([ro1, ro2, ro3])
        db.session.commit()

        # Normal user
        user1 = User.register(**TEST_USER1)
        # Assignee user
        user2 = User.register(**TEST_USER2)
        # Admin user
        user3 = User.register(**TEST_USER3)

        user2.role = 1
        user3.role = 2

        db.session.add_all([user1, user2, user3])
        db.session.commit()
Exemple #4
0
def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    from models import Department, Employee, Role
    Base.metadata.drop_all(bind=engine)
    Base.metadata.create_all(bind=engine)

    # Create the fixtures
    one_app = Department(name='One App(人工智能)')
    db_session.add(one_app)
    tire = Department(name='TireO2O(卖轮胎的)')
    db_session.add(tire)
    hr = Department(name='HR(协助人口买卖)')
    db_session.add(hr)

    manager = Role(name='product manager')
    db_session.add(manager)
    engineer = Role(name='engineer')
    db_session.add(engineer)

    hydra = Employee(name='Hydra', department=one_app, role=engineer)
    db_session.add(hydra)
    samuel = Employee(name='Samuel', department=one_app, role=engineer)
    db_session.add(samuel)
    tony = Employee(name='Tony', department=one_app, role=manager)
    db_session.add(tony)
    daisy = Employee(name='Daisy', department=tire, role=manager)
    db_session.add(daisy)
    kyle = Employee(name='Kyle', department=tire, role=engineer)
    db_session.add(kyle)
    xenia = Employee(name='Xenia', department=hr, role=manager)
    db_session.add(xenia)
    db_session.commit()
Exemple #5
0
def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    from models import Department, Employee, Role
    Base.metadata.drop_all(bind=engine)
    Base.metadata.create_all(bind=engine)

    # Create the fixtures
    engineering = Department(name='Engineering')
    db_session.add(engineering)
    hr = Department(name='Human Resources')
    db_session.add(hr)

    manager = Role(name='manager')
    db_session.add(manager)
    engineer = Role(name='engineer')
    db_session.add(engineer)

    peter = Employee(name='Peter', department=engineering, role=engineer)
    db_session.add(peter)
    roy = Employee(name='Roy', department=engineering, role=engineer)
    db_session.add(roy)
    tracy = Employee(name='Tracy', department=hr, role=manager)
    db_session.add(tracy)
    db_session.commit()
Exemple #6
0
def init_db():

    from models import User, Role

    role_admin = yield Role.objects.get(key='admin')
    if not role_admin:
        role_admin = yield Role(key='admin', name="管理员").save()

    role_salesmanager = yield Role.objects.get(key='salesmanager')
    if not role_salesmanager:
        Role(key='salesmanager', name="销售人员").save()

    role_warehousekeeper = yield Role.objects.get(key='warehousekeeper')
    if not role_warehousekeeper:
        Role(key='warehousekeeper', name="仓库管理员").save()

    user_admin = yield User.objects.get(name='admin')
    if user_admin:
        yield user_admin.delete()
    User(
        name="admin",
        password=User.encode_raw_password('jingan'),
        role=role_admin,
        status='ACTIVE'
    ).save()
Exemple #7
0
def create_role():
    admin_role = Role('Admin')
    mem_role = Role('Member')
    db.session.add(admin_role)
    db.session.add(mem_role)
    db.session.commit()
    return ("Roles Created")
Exemple #8
0
def init_db():
    """Import all modules here that might define models so that
    they will be registered properly on the metadata. Otherwise
    you will have to import them first before calling init_db()
    """
    from models import Department, Employee, Pokemon, Role
    Base.metadata.drop_all(bind=engine)
    Base.metadata.create_all(bind=engine)

    engineering = Department(name="Engineering")
    db_session.add(engineering)
    hr = Department(name="Human Resources")
    db_session.add(hr)

    manager = Role(name="manager")
    db_session.add(manager)
    engineer = Role(name="engineer")
    db_session.add(engineer)

    peter = Employee(name="Peter", department=engineering, role=engineer)
    db_session.add(peter)
    roy = Employee(name="Roy", department=engineering, role=engineer)
    db_session.add(roy)
    tracy = Employee(name="Tracy", department=hr, role=manager)
    db_session.add(tracy)
    db_session.commit()

    charzard = Pokemon(name="Charzard", level=100, type="fire")
    db_session.add(charzard)
    db_session.commit()
async def init_db():
    from models import Department, Employee, Role
    Base.metadata.drop_all(bind=engine)
    Base.metadata.create_all(bind=engine)

    # Create Fixture for department
    engineering = Department(name='Engineering')
    db_session.add(engineering)
    hr = Department(name='Human Resource')
    db_session.add(hr)

    # Create Fixture for Role
    manager = Role(name='manager')
    db_session.add(manager)
    engineer = Role(name='Engineer')
    db_session.add(engineer)

    # Create
    peter = Employee(name='Peter', department=engineering, role=manager)
    db_session.add(peter)
    roy = Employee(name='Roy', department=engineering, role=engineer)
    db_session.add(roy)
    tracy = Employee(name='Tracy', department=hr, role=manager)
    db_session.add(tracy)

    # Insert seed data into database
    db_session.commit()
Exemple #10
0
def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    from models import Department, Employee, Role
    Base.metadata.drop_all(bind=engine)
    Base.metadata.create_all(bind=engine)

    # Create the fixtures
    math = Class(name='Math')
    db_session.add(math)
    biologie = Class(name='Biology')
    db_session.add(biology)

    Student = Role(name='Student')
    db_session.add(manager)
    Teacher = Role(name='Teacher')
    db_session.add(engineer)

    peter = Student(name='Peter', department=biology, role=Student)
    db_session.add(peter)
    roy = Student(name='Roy', department=math, role=Student)
    db_session.add(roy)
    tracy = Teacher(name='Tracy', department=math, role=Teacher)
    db_session.add(tracy)
    db_session.commit()
Exemple #11
0
def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    from models import Department, Employee, Role
    for model in [Department, Employee, Role]:
        if model.exists():
            model.delete_table()
        model.create_table(read_capacity_units=1, write_capacity_units=1, wait=True)

    # Create the fixtures
    engineering = Department(id=str(uuid4()), name='Engineering')
    engineering.save()
    hr = Department(id=str(uuid4()), name='Human Resources')
    hr.save()

    manager = Role(id=str(uuid4()), name='manager')
    manager.save()

    engineer = Role(id=str(uuid4()), name='engineer')
    engineer.save()

    peter = Employee(id=str(uuid4()), name='Peter', department=engineering, role=engineer)
    peter.save()

    roy = Employee(id=str(uuid4()), name='Roy', department=engineering, role=engineer)
    roy.save()

    tracy = Employee(id=str(uuid4()), name='Tracy', department=hr, role=manager)
    tracy.save()
Exemple #12
0
def init_db():
    # Create the fixtures
    engineering = Department(name='Engineering')
    engineering.save()

    hr = Department(name='Human Resources')
    hr.save()

    manager = Role(name='manager')
    manager.save()

    engineer = Role(name='engineer')
    engineer.save()

    peter = Person(first_name='Peter',
                   last_name='Parker',
                   department=engineering,
                   role=engineer)
    peter.save()

    roy = Person(first_name='Roy',
                 last_name='Royce',
                 department=engineering,
                 role=engineer)
    roy.save()

    tracy = Person(first_name='Tracy',
                   last_name='Monita',
                   department=hr,
                   role=manager)
    tracy.save()
def create_database():
    import string
    import random

    security = current_app.extensions.get('security')

    db.drop_all()
    db.create_all()

    user_role = Role(name='user')
    super_user_role = Role(name='superuser')
    db.session.add(user_role)
    db.session.add(super_user_role)
    db.session.commit()

    test_user = security.datastore.create_user(
        first_name='Admin',
        email='admin',
        password=hash_password('admin'),
        roles=[user_role, super_user_role])

    first_names = [
        'Harry', 'Amelia', 'Oliver', 'Jack', 'Isabella', 'Charlie', 'Sophie',
        'Mia', 'Jacob', 'Thomas', 'Emily', 'Lily', 'Ava', 'Isla', 'Alfie',
        'Olivia', 'Jessica', 'Riley', 'William', 'James', 'Geoffrey', 'Lisa',
        'Benjamin', 'Stacey', 'Lucy'
    ]
    last_names = [
        'Brown', 'Smith', 'Patel', 'Jones', 'Williams', 'Johnson', 'Taylor',
        'Thomas', 'Roberts', 'Khan', 'Lewis', 'Jackson', 'Clarke', 'James',
        'Phillips', 'Wilson', 'Ali', 'Mason', 'Mitchell', 'Rose', 'Davis',
        'Davies', 'Rodriguez', 'Cox', 'Alexander'
    ]

    for i in range(len(first_names)):
        tmp_email = first_names[i].lower() + "." + last_names[i].lower(
        ) + "@example.com"
        tmp_pass = ''.join(
            random.choice(string.ascii_lowercase + string.digits)
            for i in range(10))
        security.datastore.create_user(first_name=first_names[i],
                                       last_name=last_names[i],
                                       email=tmp_email,
                                       password=hash_password(tmp_pass),
                                       roles=[
                                           user_role,
                                       ])
    db.session.commit()

    for _ in range(0, 100):
        _cost = random.randrange(1, 1000)
        _project = Project(name=''.join(
            random.choice(string.ascii_uppercase + string.digits)
            for _ in range(10)),
                           cost=_cost)
        db.session.add(_project)

    db.session.commit()
	def setUp(self):
		self.client = app.test_client()
		db.drop_all()
		db.create_all()

		c1 = Category(
        category_id=0,
        category_label="Technical Issue"
    )

		p1 = Priority(
        priority_id=1,
        priority_label="Medium"
    )

		s1 = Status(
        status_id=0,
        status_label="Submitted"
    )

		ro1 = Role(
        role_id=0,
        role_label="user"
    )

		ro2 = Role(
        role_id=1,
        role_label="assignee"
    )

		ro3 = Role(
        role_id=2,
        role_label="admin"
    )

		db.session.add_all([c1,p1,s1,ro1,ro2,ro3])
		db.session.commit()

		User.query.delete()
		Issue.query.delete()

		user1 = User.register(**TEST_USER1)
		user2 = User.register(**TEST_USER2)
		user1.role = 2
		db.session.add_all([user1,user2])
		db.session.commit()

		issue1 = Issue(title="Test Issue 1", text="Issue 1 description", reporter=user1.id)
		issue2 = Issue(title="Test Issue 2", text="Issue 2 description", reporter=user2.id)
		db.session.add_all([issue1,issue2])
		db.session.commit()

		comment1 = Comment(comment_text="This is a comment", comment_user=2, comment_issue=2)
		db.session.add(comment1)
		db.session.commit()
Exemple #15
0
def create_roles():
    print("create_roles")
    now = datetime.datetime.now()
    if not Role.query.get(1):
        role = Role(id=1, name="Admin", created=now, last_update=now)
        role.save()
    if not Role.query.get(2):
        role = Role(id=2, name="User", created=now, last_update=now)
        role.save()
    DBManager.commitSession()
    return
Exemple #16
0
def seed():
    check = Role.query.filter_by(name='basic').all()
    if check:
        print('Roles already exist')
    else:
        role_1 = Role(name='basic', description="Basic User")
        role_2 = Role(name='superuser', description="Super User")
        db.session.add(role_1)
        db.session.add(role_2)
        db.session.commit()
        print("Completed successfully...")
Exemple #17
0
    def run(self):
        admin_role = Role()
        admin_role.name = 'admin'
        admin_role.description = 'Umsjónarhlutverk með aðgang að notendastillingum'

        user_role = Role()
        user_role.name = 'Notandi'
        user_role.description = 'Venjulegur notandi með grunn aðgang'

        db.session.add(admin_role)
        db.session.add(user_role)
        db.session.commit()
Exemple #18
0
def create_roles():
    if not Role.query.get(Role.ADMIN_ROLE_ID):
        role = Role(id=Role.ADMIN_ROLE_ID, name="Admin")
        role.save()
    if not Role.query.get(Role.MANAGER_ROLE_ID):
        role = Role(id=Role.MANAGER_ROLE_ID, name="Manager")
        role.save()
    if not Role.query.get(Role.HELPER_ROLE_ID):
        role = Role(id=Role.HELPER_ROLE_ID, name="Helper")
        role.save()
    DBManager.commitSession()
    return
Exemple #19
0
def init_db():
    db.create_all()

    user_list = [
        {
            'first_name': 'Ben',
            'last_name': 'blake',
            'email': '*****@*****.**'
        },
        {
            'first_name': 'John',
            'last_name': 'Smith',
            'email': '*****@*****.**'
        },
        {
            'first_name': 'hugo',
            'last_name': 'alfred',
            'email': '*****@*****.**'
        },
    ]
    db.session.add(Role(name='user'))
    db.session.add(Role(name='author'))
    db.session.add(Role(name='admin'))
    db.session.commit()

    for user in tqdm(user_list):
        'This is only for populating database.'
        tmp_user = User(
            first_name=user['first_name'],
            last_name=user['last_name'],
            email=user['email'],
            password='******'
        )
        tmp_user.save()

    # Populate database with fake Books
    for i in trange(1, 4):
        db.session.add(
            Book(
                name=f'Wonder Land {i}',
                publish_date=f'2020-02-2{i}',
                price=23.50,
                author_id=2
            )
        )

    db.session.add(UserRoles(user_id=1, role_id=1))
    db.session.add(UserRoles(user_id=2, role_id=2))
    db.session.add(UserRoles(user_id=3, role_id=3))
    db.session.commit()

    print('Database successfully initialized.')
Exemple #20
0
def seed_roles(db):
    print("++ Seeding roles")
    role_usr = Role()
    role_usr.name = "user"
    role_usr.description = "Simple user"

    role_adm = Role()
    role_adm.name = "admin"
    role_adm.description = "Admin user"

    db.session.add(role_usr)
    db.session.add(role_adm)
    db.session.commit()
Exemple #21
0
def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    from models import Department, User, Role, Dataset
    Base.metadata.drop_all(bind=engine)
    Base.metadata.create_all(bind=engine)

    # Create the fixtures
    engineering = Department(name='Engineering')
    db_session.add(engineering)
    hr = Department(name='Human Resources')
    db_session.add(hr)

    manager = Role(name='manager')
    db_session.add(manager)
    engineer = Role(name='engineer')
    db_session.add(engineer)

    peter = User(name='Peter', department=engineering, role=engineer)
    db_session.add(peter)
    roy = User(name='Roy', department=engineering, role=engineer)
    db_session.add(roy)
    tracy = User(name='Tracy', department=hr, role=manager)
    db_session.add(tracy)

    # Dataset
    import random
    from random import randint
    from faker import Faker
    fake = Faker('en_US')
    nPoints = 11

    # data = {'x': [randint(0, 1000) for i in range(nPoints)], 'z': [float(random.randrange(0, 1000))/100 for i in range(nPoints)], 'names': [fake.name() for i in range(nPoints)] }
    data = {
        'x': [int(i) for i in range(nPoints)],
        'z': [float(i) for i in range(nPoints)],
        'names': [fake.name() for i in range(nPoints)]
    }
    test_data1 = Dataset(name='dataset1',
                         description='First dataset',
                         table_name='data1',
                         enabled=True,
                         raw=data)
    db_session.add(test_data1)

    # data = {'x': [randint(0, 1000) for i in range(nPoints)], 'z': [float(random.randrange(0, 1000))/100 for i in range(nPoints)], 'names': [fake.name() for i in range(nPoints)] }
    # test_data2 = Dataset(name='dataset2', description='Second dataset', table_name='data2', enabled=False, raw=data)
    # db_session.add(test_data2)

    db_session.commit()
    def __init__(self):
        sess = DB_Session()
        roles = sess.query(Role).all()
        if (len(roles) == 0):
            normal = Role("normal")
            admin = Role("admin")
            helpdesk = Role("helpdesk")

            sess.add(normal)
            sess.add(admin)
            sess.add(helpdesk)

            sess.commit()
        sess.close()
Exemple #23
0
def create_role():
    # 1.开发者
    developer = Role(name='开发者管理员', desc='开发人员,拥有最高权限')
    developer.permission = CMSPermission.DEVELOPER
    # 2.教师
    teacher = Role(name='教师', desc='查看学生数据,添加问题')
    teacher.permission = CMSPermission.TEACHER
    # 3.学生
    student = Role(name='学生', desc='学习技能,使用CSits')
    student.permission = CMSPermission.STUDENT

    db.session.add_all([developer, teacher, student])
    db.session.commit()
    print('角色注册成功!')
    def test_patch_role_by_invalid_age_interval(self):
        new_movie = Movie(**AppTestCase.test_movie)
        new_movie.insert()
        movie_id = new_movie.id

        new_role = Role(movie_id=movie_id, **AppTestCase.test_role)
        new_role.insert()
        role_id = new_role.id

        change_data = {
            'min_age': 25,
            'max_age': 20,
        }

        res = self.client().patch(f'/roles/{role_id}',
                                  json=change_data,
                                  headers=HEADER)
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 422)
        self.assertFalse(data['success'])
        err_message = 'Min age can not be greater than max age'
        self.assertEqual(data['message'], err_message)

        new_movie.delete()
    def test_patch_role_to_provide_actor_id(self):
        new_movie = Movie(**AppTestCase.test_movie)
        new_movie.insert()
        movie_id = new_movie.id

        new_role = Role(movie_id=movie_id, **AppTestCase.test_role)
        new_role.insert()
        role_id = new_role.id

        new_actor = Actor(**AppTestCase.test_actor)
        new_actor.insert()
        actor_id = new_actor.id

        res = self.client().patch(f'/roles/{role_id}',
                                  json={'actor_id': actor_id},
                                  headers=HEADER)
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertTrue(data['success'])
        self.assertEqual(data['id'], role_id)
        role = Role.query.get(role_id)
        self.assertEqual(role.actor_id, actor_id)

        new_movie.delete()
        new_actor.delete()
Exemple #26
0
def load_roles():

    role = Role()
    role.name = "Client"
    role.save()

    print("Roles loaded")
Exemple #27
0
def register_post():
    email_tem = request.form['email']
    password_tem = request.form['password']
    captcha_tem = request.form['captcha'].upper()
    captcha_str = request.form['captcha_str']
    img_name = request.form['img_name']
    if captcha_tem != captcha_str:
        return render_template('register.html',
                               state='验证码错误',
                               email_re=request.form['email'],
                               img_name=img_name,
                               captcha_str=captcha_str)
    email = db.session.query(Role).filter_by(email=email_tem).first()
    if email:
        return render_template('register.html',
                               state='此用户已注册!',
                               email_re=request.form['email'],
                               img_name=img_name,
                               captcha_str=captcha_str)
    user_add = Role(email=email_tem, password=password_tem)
    db.session.add(user_add)
    db.session.commit()
    return render_template('register.html',
                           state='注册成功',
                           email_re=request.form['email'],
                           img_name=img_name,
                           captcha_str=captcha_str)
Exemple #28
0
def find_or_create_role(name, label):
    """ Find existing role or create new role """
    role = Role.query.filter(Role.name == name).first()
    if not role:
        role = Role(name=name, label=label)
        db.session.add(role)
    return role
Exemple #29
0
 def create(self, validated_data):
     slug = validated_data['name'].lower().replace('-','_').strip()
     slug = slug.replace(' ','_')
     new_permission = Role(name=validated_data['name'],slug = slug)
     new_permission.save()
     # add permission
     return new_permission
def create_role(request: userRole, db: Session = Depends(get_db)):
    id_uid = id_uuids()
    role = Role(id=id_uid, name=request.name)
    db.add(role)
    db.commit()
    db.refresh(role)
    return role