diff --git a/conftest.py b/conftest.py index 1d7f9fd69..f145f6dc2 100644 --- a/conftest.py +++ b/conftest.py @@ -125,6 +125,9 @@ def pytest_generate_tests(metafunc): # mscolab data directory MSCOLAB_DATA_DIR = fs.path.join(DATA_DIR, 'filedata') +# In the unit days when Operations get archived because not used +ARCHIVE_THRESHOLD = 30 + # To enable logging set to True or pass a logger object to use. SOCKETIO_LOGGER = True diff --git a/docs/samples/config/mscolab/mscolab_settings.py.sample b/docs/samples/config/mscolab/mscolab_settings.py.sample index b521805f7..48bfc3951 100644 --- a/docs/samples/config/mscolab/mscolab_settings.py.sample +++ b/docs/samples/config/mscolab/mscolab_settings.py.sample @@ -26,6 +26,9 @@ """ import os +# In the unit days when Operations get archived because not used +ARCHIVE_THRESHOLD = 30 + # To enable logging set to True or pass a logger object to use. SOCKETIO_LOGGER = False diff --git a/mslib/mscolab/conf.py b/mslib/mscolab/conf.py index e0e50db56..6cb60f05e 100644 --- a/mslib/mscolab/conf.py +++ b/mslib/mscolab/conf.py @@ -33,6 +33,9 @@ class default_mscolab_settings: # expire token in seconds # EXPIRATION = 86400 + # In the unit days when Operations get archived because not used + ARCHIVE_THRESHOLD = 30 + # To enable logging set to True or pass a logger object to use. SOCKETIO_LOGGER = False diff --git a/mslib/mscolab/file_manager.py b/mslib/mscolab/file_manager.py index c16e5a10c..620ba658e 100644 --- a/mslib/mscolab/file_manager.py +++ b/mslib/mscolab/file_manager.py @@ -104,10 +104,17 @@ def list_operations(self, user, skip_archived=False): operations = [] permissions = Permission.query.filter_by(u_id=user.id).all() for permission in permissions: + operation = Operation.query.filter_by(id=permission.op_id).first() + if operation.last_used is not None and ( + datetime.datetime.utcnow() - operation.last_used).days > mscolab_settings.ARCHIVE_THRESHOLD: + # outdated OPs get archived + self.update_operation(permission.op_id, "active", False, user) + # new query to get uptodate data if skip_archived: operation = Operation.query.filter_by(id=permission.op_id, active=skip_archived).first() else: operation = Operation.query.filter_by(id=permission.op_id).first() + if operation is not None: operations.append({ "op_id": permission.op_id, diff --git a/mslib/mscolab/server.py b/mslib/mscolab/server.py index 25a38fe4e..b3a2488ca 100644 --- a/mslib/mscolab/server.py +++ b/mslib/mscolab/server.py @@ -45,7 +45,7 @@ from werkzeug.utils import secure_filename from mslib.mscolab.conf import mscolab_settings -from mslib.mscolab.models import Change, MessageType, User, Operation, db +from mslib.mscolab.models import Change, MessageType, User, db from mslib.mscolab.sockets_manager import setup_managers from mslib.mscolab.utils import create_files, get_message_dict from mslib.utils import conditional_decorator @@ -59,7 +59,6 @@ migrate = Migrate(APP, db, render_as_batch=True) auth = HTTPBasicAuth() -ARCHIVE_THRESHOLD = 30 try: from mscolab_auth import mscolab_auth @@ -512,38 +511,21 @@ def get_operation_details(): def set_last_used(): # ToDo refactor move to file_manager op_id = request.form.get('op_id', None) + user = g.user days_ago = int(request.form.get('days', 0)) - operation = Operation.query.filter_by(id=int(op_id)).first() - operation.last_used = datetime.datetime.utcnow() - datetime.timedelta(days=days_ago) - temp_operation_active = operation.active - if days_ago > ARCHIVE_THRESHOLD: - operation.active = False + fm.update_operation(int(op_id), 'last_used', + datetime.datetime.utcnow() - datetime.timedelta(days=days_ago), + user) + if days_ago > mscolab_settings.ARCHIVE_THRESHOLD: + fm.update_operation(int(op_id), "active", False, user) else: - operation.active = True - db.session.commit() - # Reload Operation List - if temp_operation_active != operation.active: + fm.update_operation(int(op_id), "active", True, user) token = request.args.get('token', request.form.get('token', False)) json_config = {"token": token} sockio.sm.update_operation_list(json_config) return jsonify({"success": True}), 200 -@APP.route('/update_last_used', methods=["POST"]) -@verify_user -def update_last_used(): - # ToDo refactor move to file_manager - operations = Operation.query.filter().all() - for operation in operations: - if operation.last_used is not None and \ - (datetime.datetime.utcnow() - operation.last_used).days > 30: - operation.active = False - else: - operation.active = True - db.session.commit() - return jsonify({"success": True}), 200 - - @APP.route('/undo', methods=["POST"]) @verify_user def undo_ftml(): diff --git a/mslib/msui/mscolab.py b/mslib/msui/mscolab.py index d36bd6f6e..67b06097d 100644 --- a/mslib/msui/mscolab.py +++ b/mslib/msui/mscolab.py @@ -584,12 +584,6 @@ def after_login(self, emailid, url, r): # create socket connection here try: self.conn = sc.ConnectionManager(self.token, user=self.user, mscolab_server_url=self.mscolab_server_url) - # Update Last Used - data = { - "token": self.token - } - r = requests.post(f"{self.mscolab_server_url}/update_last_used", data=data, - timeout=tuple(config_loader(dataset="MSCOLAB_timeout"))) except Exception as ex: logging.debug("Couldn't create a socket connection: %s", ex) show_popup(self.ui, "Error", "Couldn't create a socket connection. Maybe the MSColab server is too old. " @@ -1683,17 +1677,6 @@ def set_active_op_id(self, item): self.ui.workLocallyCheckbox.setChecked(False) self.ui.workLocallyCheckbox.blockSignals(False) - # Disable Activate Operation Button - # self.ui.actionUnarchiveOperation.setEnabled(False) - - # set last used date for operation - data = { - "token": self.token, - "op_id": item.op_id, - } - requests.post(f'{self.mscolab_server_url}/set_last_used', data=data, - timeout=tuple(config_loader(dataset="MSCOLAB_timeout"))) - # set active_op_id here self.active_op_id = item.op_id self.access_level = item.access_level diff --git a/tests/_test_mscolab/test_server.py b/tests/_test_mscolab/test_server.py index e4e0a79de..db53b4012 100644 --- a/tests/_test_mscolab/test_server.py +++ b/tests/_test_mscolab/test_server.py @@ -348,15 +348,6 @@ def test_set_last_used(self): data = json.loads(response.data.decode('utf-8')) assert data["success"] is True - def test_update_last_used(self): - assert add_user(self.userdata[0], self.userdata[1], self.userdata[2]) - with self.app.test_client() as test_client: - operation, token = self._create_operation(test_client, self.userdata) - response = test_client.post('/update_last_used', data={"token": token}) - assert response.status_code == 200 - data = json.loads(response.data.decode('utf-8')) - assert data["success"] is True - def test_get_users_without_permission(self): assert add_user(self.userdata[0], self.userdata[1], self.userdata[2]) unprevileged_user = 'UV20@uv20', 'UV20', 'uv20'