Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mscolab flighttrack template handling #2652

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion mslib/mscolab/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from mslib.utils.verify_waypoint_data import verify_waypoint_data
from mslib.mscolab.models import db, Operation, Permission, User, Change, Message
from mslib.mscolab.conf import mscolab_settings
from flask import Flask, request, jsonify


class FileManager:
Expand Down Expand Up @@ -103,7 +104,11 @@ def create_operation(self, path, description, user, last_used=None, content=None
if content is not None:
operation_file.write(content)
else:
operation_file.write(mscolab_settings.STUB_CODE)
content = mscolab_settings.STUB_CODE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to remove the constant STUB_CODE and use only the new_flighttrack_template which the user provides in her msui_settings.json

# Convert list to string
if isinstance(content, list):
content = '\n'.join(content)
operation_file.write(content)
operation_path = fs.path.combine(self.data_dir, operation.path)
r = git.Repo.init(operation_path)
r.git.clear_cache()
Expand Down Expand Up @@ -146,6 +151,35 @@ def list_operations(self, user, skip_archived=False):
})
return operations

@app.route('/create_operation', methods=['POST'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs in the server code, a @verify_user

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okk

def create_operation_route():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all routes should be in the server code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okk

data = request.form
path = data.get('path')
description = data.get('description', 'Initial flight track')
content = data.get('content')
category = data.get('category', 'default')
active = data.get('active', 'True') == 'True'
token = data.get('token')

user = verify_user_token(token)

if not user:
return jsonify({"success": False, "message": "Invalid token"}), 401

success = mscolab.create_operation(
path=path,
description=description,
user=user,
content=content,
category=category,
active=active
)

if success:
return jsonify({"success": True, "message": f"Operation '{path}' created successfully"})
else:
return jsonify({"success": False, "message": "Failed to create operation"})

def is_member(self, u_id, op_id):
"""
op_id: operation id
Expand Down
3 changes: 2 additions & 1 deletion mslib/mscolab/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def add_operation(operation_name, description):
with fs.open_fs(mscolab_settings.OPERATIONS_DATA) 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)
content = mscolab_settings.STUB_CODE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a duplication of the var?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry this is a mistake.

file_dir.writetext(f'{operation_name}/main.ftml', content)
# initiate git
r = git.Repo.init(fs.path.join(mscolab_settings.DATA_DIR, 'filedata', operation_name))
r.git.clear_cache()
Expand Down
30 changes: 16 additions & 14 deletions mslib/msui/mscolab.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ def browse():
self.add_proj_dialog = add_operation_ui.Ui_addOperationDialog()
self.add_proj_dialog.setupUi(self.proj_diag)
self.add_proj_dialog.f_content = None
self.add_proj_dialog.buttonBox.accepted.connect(self.add_operation)
self.add_proj_dialog.buttonBox.accepted.connect(self.create_local_operation)
Copy link
Member

@ReimarBauer ReimarBauer Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use the existing method and modify it so that it does the new aspect.

Copy link
Contributor Author

@annapurna-gupta annapurna-gupta Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To pass MSUIDefaultConfig.new_flighttrack_template to the server, I’ve created this function that initially triggers when the user clicks on the MSColab operation button. This function will check for initial data, include the template, and then pass it to the add_operation function, which will handle the operation as expected...this is what i am trying to do

Copy link
Member

@ReimarBauer ReimarBauer Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that looks way to complicated
the server has

@APP.route('/create_operation', methods=["POST"])
@verify_user
def create_operation()

And to that you can send content

content = request.form.get('content', None)

That means when the user is not using a ftml file to upload a local file the default content sended is the content based on the user local settings, of course when you send it. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you add content always with the users new_flight_template to the data in
https://github.com/Open-MSS/MSS/blob/develop/mslib/msui/mscolab.py#L1031

You have only to convert the data from new_flight_template to a ftml str syntax.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there for flightracks the template is used to create a waypoint table. When you have a waypoint table you can format it to different types.

https://github.com/Open-MSS/MSS/blob/develop/mslib/msui/msui_mainwindow.py#L750

self.add_proj_dialog.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(False)
self.add_proj_dialog.path.textChanged.connect(check_and_enable_operation_accept)
self.add_proj_dialog.description.textChanged.connect(check_and_enable_operation_accept)
Expand Down Expand Up @@ -1027,21 +1027,23 @@ def add_operation(self):
if self.add_proj_dialog.f_content is not None:
data["content"] = self.add_proj_dialog.f_content
try:
response = self.conn.request_post("create_operation", data)
response = self.create_local_operation(path, description, category)

if response == "True":
QMessageBox.information(
self.ui, "Creation successful",
"Your operation was created successfully.",
)
op_id = self.get_recent_op_id()
self.new_op_id = op_id
self.conn.handle_new_operation(op_id)
self.signal_operation_added.emit(op_id, path)
else:
self.show_error_message(f"Operation creation failed: {response}")
except requests.exceptions.RequestException as ex:
raise MSColabConnectionError(f"Some error occurred ({ex})! Please reconnect.")
if response.text == "True":
QMessageBox.information(
self.ui, "Creation successful",
"Your operation was created successfully.",
)
op_id = self.get_recent_op_id()
self.new_op_id = op_id
self.conn.handle_new_operation(op_id)
self.signal_operation_added.emit(op_id, path)
else:
self.error_dialog = QtWidgets.QErrorMessage()
self.error_dialog.showMessage('The path already exists')
except Exception as ex:
self.show_error_message(f"Operation creation failed: {str(ex)}")

@verify_user_token
def get_recent_op_id(self):
Expand Down
35 changes: 35 additions & 0 deletions mslib/msui/msui_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,41 @@ def close_selected_flight_track(self):
if ret == QtWidgets.QMessageBox.Yes:
self.listFlightTracks.takeItem(self.listFlightTracks.currentRow())

def create_local_operation(self, operation_name):
if not operation_name:
return "Operation name is required"

existing_ops = self.list_operations(self.user)
if any(op["path"] == operation_name for op in existing_ops):
return "An operation with this name already exists"

initial_template = MSUIDefaultConfig.new_flighttrack_template
try:
model = ft.WaypointsTableModel(waypoints=initial_template)
xml_doc = model.get_xml_doc()
initial_content = xml_doc.toprettyxml(indent=" ", newl="\n")
except SyntaxError:
return "Invalid flight track template"

try:
response = requests.post(
f"{self.mscolab_server_url}/create_operation",
data={
"path": operation_name,
"content": initial_content,
"description": description,
"category": category,
"active": "True",
"token": self.token
},
timeout=10
)
response.raise_for_status()
except requests.exceptions.RequestException as e:
return f"Failed to connect to server: {e}"

return response.text

def create_view_handler(self, _type):
if self.local_active:
self.create_view(_type, self.active_flight_track)
Expand Down
2 changes: 1 addition & 1 deletion tests/_test_mscolab/test_file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def test_upload_file(self):
def test_get_file(self):
with self.app.test_client():
flight_path, operation = self._create_operation(flight_path="operation7")
assert self.fm.get_file(operation.id, self.user).startswith('<?xml version="1.0" encoding="utf-8"?>')
assert self.fm.get_file(operation.id, self.user).startswith('Nagpur')

def test_get_all_changes(self):
with self.app.test_client():
Expand Down
2 changes: 1 addition & 1 deletion tests/_test_mscolab/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def test_get_operation_by_id(self):
response = test_client.get('/get_operation_by_id', data={"token": token,
"op_id": operation.id})
assert response.status_code == 200
assert "<ListOfWaypoints>" in response.data.decode('utf-8')
assert "Nagpur\\nDelhi" in response.data.decode('utf-8')

def test_get_operations(self):
assert add_user(self.userdata[0], self.userdata[1], self.userdata[2])
Expand Down
Loading