Esempio n. 1
0
File: seed.py Progetto: Open-MSS/MSS
def add_user_to_operation(path=None, access_level='admin', emailid=None):
    """ on db level we add all users to the operation TEMPLATE for user handling"""
    if None in (path, emailid):
        return False
    app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    db.init_app(app)
    with app.app_context():
        operation = Operation.query.filter_by(path=path).first()
        if operation:

            user = User.query.filter_by(emailid=emailid).first()
            if user:
                new_permissions = [
                    Permission(user.id, operation.id, access_level)
                ]
                db.session.add_all(new_permissions)
                try:
                    db.session.commit()
                    return True
                except IntegrityError as err:
                    db.session.rollback()
                    logging.debug(f"Error writing to db: {err}")
                db.session.close()
    return False
Esempio n. 2
0
def initialize_managers(app):
    sockio, cm, fm = setup_managers(app)
    # initializing socketio and db
    app.wsgi_app = socketio.Middleware(socketio.server, app.wsgi_app)
    sockio.init_app(app)
    db.init_app(app)
    return app, sockio, cm, fm
Esempio n. 3
0
File: seed.py Progetto: Open-MSS/MSS
def add_operation(operation_name, description):
    app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)
    with app.app_context():
        operation_available = Operation.query.filter_by(
            path=operation_name).first()
        if not operation_available:
            operation = Operation(operation_name, description)
            db.session.add(operation)
            db.session.commit()
            with fs.open_fs(mscolab_settings.MSCOLAB_DATA_DIR) as file_dir:
                if not file_dir.exists(operation_name):
                    file_dir.makedir(operation_name)
                    file_dir.writetext(f'{operation_name}/main.ftml',
                                       mscolab_settings.STUB_CODE)
                    # initiate git
                    r = git.Repo.init(
                        fs.path.join(mscolab_settings.DATA_DIR, 'filedata',
                                     operation_name))
                    r.git.clear_cache()
                    r.index.add(['main.ftml'])
                    r.index.commit("initial commit")
            return True
        else:
            return False
Esempio n. 4
0
File: seed.py Progetto: Open-MSS/MSS
def add_user(email, username, password):
    """
    on db level we add a user
    """
    app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)

    template = f"""
    "MSCOLAB_mailid": "{email}",
    "MSCOLAB_password": "******",
"""
    with app.app_context():
        user_email_exists = User.query.filter_by(emailid=str(email)).first()
        user_name_exists = User.query.filter_by(username=str(username)).first()
        if not user_email_exists and not user_name_exists:
            db_user = User(email, username, password)
            db.session.add(db_user)
            db.session.commit()
            db.session.close()
            logging.info(f"Userdata: {email} {username} {password}")
            logging.info(template)
            return True
        else:
            logging.info(f"{user_name_exists} already in db")
    return False
Esempio n. 5
0
def delete_user(email):
    app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)
    with app.app_context():
        user = User.query.filter_by(emailid=str(email)).first()
        if user:
            print(f"User: {email} deleted from db")
            db.session.delete(user)
            db.session.commit()
        db.session.close()
Esempio n. 6
0
    def setUp(self):
        handle_db_reset()
        db.init_app(self.app)

        self.fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
        self.userdata = 'UV10@uv10', 'UV10', 'uv10'

        assert add_user(self.userdata[0], self.userdata[1], self.userdata[2])
        self.user = get_user(self.userdata[0])
        assert self.user is not None
        assert add_user('UV20@uv20', 'UV20', 'uv20')
        self.user_2 = get_user('UV20@uv20')
Esempio n. 7
0
File: seed.py Progetto: Open-MSS/MSS
def delete_operation(operation_name):
    app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)
    with app.app_context():
        operation = Operation.query.filter_by(path=operation_name).first()
        if operation:
            db.session.delete(operation)
            db.session.commit()
            db.session.close()
            return True
        db.session.close()
        return False
Esempio n. 8
0
 def test_get_recent_oid(self):
     assert add_user(self.userdata[0], self.userdata[1], self.userdata[2])
     assert add_user(self.anotheruserdata[0], self.anotheruserdata[1],
                     self.anotheruserdata[2])
     with self.app.test_client() as test_client:
         db.init_app(self.app)
         user = get_user(self.userdata[0])
         anotheruser = get_user(self.anotheruserdata[0])
         operation, token = self._create_operation(test_client,
                                                   self.userdata)
         op_id = get_recent_op_id(self.fm, user)
         assert op_id == operation.id
         op_id = get_recent_op_id(self.fm, anotheruser)
         assert op_id is None
Esempio n. 9
0
 def setup(self):
     self.sockets = []
     self.file_message_counter = [0] * 2
     self.app = APP
     self.app.config[
         'SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
     self.app.config['MSCOLAB_DATA_DIR'] = mscolab_settings.MSCOLAB_DATA_DIR
     self.app.config['UPLOAD_FOLDER'] = mscolab_settings.UPLOAD_FOLDER
     self.app, _, cm, fm = initialize_managers(self.app)
     self.fm = fm
     self.cm = cm
     db.init_app(self.app)
     with self.app.app_context():
         self.user = User.query.filter_by(id=8).first()
Esempio n. 10
0
    def setUp(self):
        handle_db_reset()
        db.init_app(self.app)
        self.fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
        self.operation_name = "XYZ"
        self.description = "Template"
        self.userdata_0 = 'UV0@uv0', 'UV0', 'uv0'
        self.userdata_1 = "UV1@uv1", "UV1", "UV1"
        self.userdata_2 = "UV2@v2", "V2", "v2"

        assert add_user(self.userdata_0[0], self.userdata_0[1], self.userdata_0[2])
        assert add_operation(self.operation_name, self.description)
        assert add_user_to_operation(path=self.operation_name, emailid=self.userdata_0[0])
        self.user = User(self.userdata_0[0], self.userdata_0[1], self.userdata_0[2])
Esempio n. 11
0
File: seed.py Progetto: Open-MSS/MSS
def add_all_users_to_all_operations(access_level='collaborator'):
    """ on db level we add all users as collaborator to all operations """
    app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)
    with app.app_context():
        all_operations = Operation.query.all()
        all_path = [operation.path for operation in all_operations]
        db.session.close()
    for path in all_path:
        access_level = 'collaborator'
        if path == "TEMPLATE":
            access_level = 'admin'
        add_all_users_default_operation(path=path, access_level=access_level)
Esempio n. 12
0
File: seed.py Progetto: Open-MSS/MSS
def add_all_users_default_operation(path='TEMPLATE',
                                    description="Operation to keep all users",
                                    access_level='admin'):
    """ on db level we add all users to the operation TEMPLATE for user handling"""
    app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    db.init_app(app)
    with app.app_context():
        operation_available = Operation.query.filter_by(path=path).first()
        if not operation_available:
            operation = Operation(path, description)
            db.session.add(operation)
            db.session.commit()
            with fs.open_fs(mscolab_settings.MSCOLAB_DATA_DIR) as file_dir:
                if not file_dir.exists(path):
                    file_dir.makedir(path)
                    file_dir.writetext(f'{path}/main.ftml',
                                       mscolab_settings.STUB_CODE)
                    # initiate git
                    r = git.Repo.init(
                        fs.path.join(mscolab_settings.DATA_DIR, 'filedata',
                                     path))
                    r.git.clear_cache()
                    r.index.add(['main.ftml'])
                    r.index.commit("initial commit")

        operation = Operation.query.filter_by(path=path).first()
        op_id = operation.id
        user_list = User.query \
            .join(Permission, (User.id == Permission.u_id) & (Permission.op_id == op_id), isouter=True) \
            .add_columns(User.id, User.username) \
            .filter(Permission.u_id.is_(None))

        new_u_ids = [user.id for user in user_list]
        new_permissions = []
        for u_id in new_u_ids:
            if Permission.query.filter_by(u_id=u_id,
                                          op_id=op_id).first() is None:
                new_permissions.append(
                    Permission(u_id, operation.id, access_level))
        db.session.add_all(new_permissions)
        try:
            db.session.commit()
            return True
        except IntegrityError as err:
            db.session.rollback()
            logging.debug(f"Error writing to db: {err}")
        db.session.close()
Esempio n. 13
0
    def setUp(self):
        handle_db_reset()
        db.init_app(self.app)

        self.fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
        self.userdata = 'UV11@uv11', 'UV11', 'uv11'
        self.userdata2 = 'UV12@uv12', 'UV12', 'uv12'

        assert add_user(self.userdata[0], self.userdata[1], self.userdata[2])
        assert add_user(self.userdata2[0], self.userdata2[1], self.userdata2[2])

        self.user = get_user(self.userdata[0])
        self.user2 = get_user(self.userdata2[0])
        assert self.user is not None
        self.file_message_counter = [0] * 2
        self._example_data()
Esempio n. 14
0
    def setUp(self):
        handle_db_reset()
        db.init_app(self.app)
        self.userdata = 'UV10@uv10', 'UV10', 'uv10'
        self.anotheruserdata = 'UV20@uv20', 'UV20', 'uv20'
        self.fm = FileManager(self.app.config["MSCOLAB_DATA_DIR"])
        self.userdata = 'UV10@uv10', 'UV10', 'uv10'

        assert add_user(self.userdata[0], self.userdata[1], self.userdata[2])
        self.user = get_user(self.userdata[0])
        assert self.user is not None
        assert add_user(self.anotheruserdata[0], self.anotheruserdata[1],
                        self.anotheruserdata[2])
        self.anotheruser = get_user(self.anotheruserdata[0])
        assert add_user('UV30@uv30', 'UV30', 'uv30')
        self.vieweruser = get_user('UV30@uv30')
        assert add_user('UV40@uv40', 'UV40', 'uv40')
        self.collaboratoruser = get_user('UV40@uv40')

        self._example_data()
Esempio n. 15
0
File: seed.py Progetto: Open-MSS/MSS
def seed_data():
    app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)

    with app.app_context():
        # create users
        users = [{
            'username': '******',
            'id': 8,
            'password': '******',
            'emailid': 'a'
        }, {
            'username': '******',
            'id': 9,
            'password': '******',
            'emailid': 'b'
        }, {
            'username': '******',
            'id': 10,
            'password': '******',
            'emailid': 'c'
        }, {
            'username': '******',
            'id': 11,
            'password': '******',
            'emailid': 'd'
        }, {
            'username': '******',
            'id': 12,
            'password': '******',
            'emailid': 'test1'
        }, {
            'username': '******',
            'id': 13,
            'password': '******',
            'emailid': 'test2'
        }, {
            'username': '******',
            'id': 14,
            'password': '******',
            'emailid': 'test3'
        }, {
            'username': '******',
            'id': 15,
            'password': '******',
            'emailid': 'test4'
        }, {
            'username': '******',
            'id': 16,
            'password': '******',
            'emailid': 'mscolab_user'
        }, {
            'username': '******',
            'id': 17,
            'password': '******',
            'emailid': 'merge_waypoints_user'
        }]
        for user in users:
            db_user = User(user['emailid'], user['username'], user['password'])
            db_user.id = user['id']
            db.session.add(db_user)

        # create operations
        operations = [{
            'id': 1,
            'path': 'one',
            'description': 'a, b',
            'category': 'default'
        }, {
            'id': 2,
            'path': 'two',
            'description': 'b, c',
            'category': 'default'
        }, {
            'id': 3,
            'path': 'three',
            'description': 'a, c',
            'category': 'default'
        }, {
            'id': 4,
            'path': 'four',
            'description': 'd',
            'category': 'default'
        }, {
            'id': 5,
            'path': 'Admin_Test',
            'description': 'Operation for testing admin window',
            'category': 'default'
        }, {
            'id': 6,
            'path': 'test_mscolab',
            'description': 'Operation for testing mscolab main window',
            'category': 'default'
        }]
        for operation in operations:
            db_operation = Operation(operation['path'],
                                     operation['description'],
                                     operation['category'])
            db_operation.id = operation['id']
            db.session.add(db_operation)

        # create permissions
        permissions = [{
            'u_id': 8,
            'op_id': 1,
            'access_level': "creator"
        }, {
            'u_id': 9,
            'op_id': 1,
            'access_level': "collaborator"
        }, {
            'u_id': 9,
            'op_id': 2,
            'access_level': "creator"
        }, {
            'u_id': 10,
            'op_id': 2,
            'access_level': "collaborator"
        }, {
            'u_id': 10,
            'op_id': 3,
            'access_level': "creator"
        }, {
            'u_id': 8,
            'op_id': 3,
            'access_level': "collaborator"
        }, {
            'u_id': 10,
            'op_id': 1,
            'access_level': "viewer"
        }, {
            'u_id': 11,
            'op_id': 4,
            'access_level': 'creator'
        }, {
            'u_id': 8,
            'op_id': 4,
            'access_level': 'admin'
        }, {
            'u_id': 13,
            'op_id': 3,
            'access_level': 'viewer'
        }, {
            'u_id': 12,
            'op_id': 5,
            'access_level': 'creator'
        }, {
            'u_id': 12,
            'op_id': 3,
            'access_level': 'collaborator'
        }, {
            'u_id': 15,
            'op_id': 5,
            'access_level': 'viewer'
        }, {
            'u_id': 14,
            'op_id': 3,
            'access_level': 'collaborator'
        }, {
            'u_id': 15,
            'op_id': 3,
            'access_level': 'collaborator'
        }, {
            'u_id': 16,
            'op_id': 6,
            'access_level': 'creator'
        }, {
            'u_id': 17,
            'op_id': 6,
            'access_level': 'admin'
        }]
        for perm in permissions:
            db_perm = Permission(perm['u_id'], perm['op_id'],
                                 perm['access_level'])
            db.session.add(db_perm)
        db.session.commit()
        db.session.close()

    with fs.open_fs(mscolab_settings.MSCOLAB_DATA_DIR) as file_dir:
        file_paths = [
            'one', 'two', 'three', 'four', 'Admin_Test', 'test_mscolab'
        ]
        for file_path in file_paths:
            file_dir.makedir(file_path)
            file_dir.writetext(f'{file_path}/main.ftml',
                               mscolab_settings.STUB_CODE)
            # initiate git
            r = git.Repo.init(
                fs.path.join(mscolab_settings.DATA_DIR, 'filedata', file_path))
            r.git.clear_cache()
            r.index.add(['main.ftml'])
            r.index.commit("initial commit")
Esempio n. 16
0
 def setUp(self):
     db.init_app(self.app)
     handle_db_init()
     assert Operation.query.all() == []
     assert User.query.all() == []
     assert Permission.query.all() == []
Esempio n. 17
0
 def setUp(self):
     handle_db_reset()
     db.init_app(self.app)
     self.userdata = 'UV10@uv10', 'UV10', 'uv10'