Skip to content

Commit

Permalink
moved server request for last_used to the file_manager (#2041)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReimarBauer authored Sep 28, 2023
1 parent afc9e27 commit f0a6659
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 52 deletions.
3 changes: 3 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/samples/config/mscolab/mscolab_settings.py.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions mslib/mscolab/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions mslib/mscolab/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 8 additions & 26 deletions mslib/mscolab/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -59,7 +59,6 @@
migrate = Migrate(APP, db, render_as_batch=True)
auth = HTTPBasicAuth()

ARCHIVE_THRESHOLD = 30

try:
from mscolab_auth import mscolab_auth
Expand Down Expand Up @@ -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():
Expand Down
17 changes: 0 additions & 17 deletions mslib/msui/mscolab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. "
Expand Down Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions tests/_test_mscolab/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit f0a6659

Please sign in to comment.