From 55d8e91af9f7b7dc4916b00e15b51530a9a16553 Mon Sep 17 00:00:00 2001 From: ZhuoweiWen Date: Wed, 10 Jul 2024 20:32:12 -0400 Subject: [PATCH] Backend update to handle new data structure pushed from frontend editing Signed-off-by: ZhuoweiWen --- .../celery_controller/celery_tasks.py | 33 +++++++++++-------- back-end/routes.py | 1 + front-end/components/Editmap.js | 1 + front-end/components/MyEdit.js | 9 +++-- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/back-end/controllers/celery_controller/celery_tasks.py b/back-end/controllers/celery_controller/celery_tasks.py index ea6dc5e..d9d5277 100755 --- a/back-end/controllers/celery_controller/celery_tasks.py +++ b/back-end/controllers/celery_controller/celery_tasks.py @@ -216,24 +216,29 @@ def toggle_tiles(self, markers, userid, folderid, polygonfeatures): user_folder = folder_ops.get_folder_with_id(userid=userid, folderid=folderid, session=session) if user_folder: # Process each polygon feature - for feature in polygonfeatures: - # Convert the feature to a binary GeoJSON format + for index, feature in enumerate(polygonfeatures): + filenames = '-'.join(set( + filename for marker in markers[index] for filename in marker['editedFile'] + )) + formatted_datetime = datetime.now().strftime(DATETIME_FORMAT) + editfile_name = f"edit_on_{filenames}_at_{formatted_datetime}" + feature_binary = json.dumps(feature).encode('utf-8') - # Create an editfile for the polygon feature - new_editfile = editfile_ops.create_editfile(filename=f"edit-{datetime.now().strftime(DATETIME_FORMAT)}", content=feature_binary, folderid=folderid, session=session) + new_editfile = editfile_ops.create_editfile(filename=editfile_name, content=feature_binary, folderid=folderid, session=session) session.commit() # Link this editfile with relevant files - for marker in markers: - # Identify relevant kml_data entries and link them to the new editfile - kml_data_entries = session.query(kml_data).join(file).filter(kml_data.location_id == marker['id'], file.folder_id == user_folder.id).all() - for entry in kml_data_entries: - file_instance = session.query(file).filter_by(id=entry.file_id).first() - if file_instance: - if (file_instance.id, new_editfile.id) not in file_editfile_link_set: - file_editfile_link_ops.link_file_and_editfile(file_instance.id, new_editfile.id, session) - file_editfile_link_set.add((file_instance.id, new_editfile.id)) - session.delete(entry) + for marker in markers[index]: + # Query kml_data_entries based on location_id and filenames from editedFile + for filename in marker['editedFile']: + kml_data_entries = session.query(kml_data).join(file).filter(kml_data.location_id == marker['id'], file.folder_id == user_folder.id, file.name == filename).all() + for entry in kml_data_entries: + file_instance = session.query(file).filter_by(id=entry.file_id).first() + if file_instance: + if (file_instance.id, new_editfile.id) not in file_editfile_link_set: + file_editfile_link_ops.link_file_and_editfile(file_instance.id, new_editfile.id, session) + file_editfile_link_set.add((file_instance.id, new_editfile.id)) + session.delete(entry) session.commit() else: diff --git a/back-end/routes.py b/back-end/routes.py index 01f36db..12cfc78 100755 --- a/back-end/routes.py +++ b/back-end/routes.py @@ -343,6 +343,7 @@ def toggle_markers(): if folderid == -1: return jsonify({'error': 'Invalid folder id'}), 400 identity = get_jwt_identity() + task = toggle_tiles.apply_async(args=[markers, identity['id'], folderid, polygonfeatures]) return jsonify({'Status': "OK", 'task_id': task.id}), 200 except NoAuthorizationError: diff --git a/front-end/components/Editmap.js b/front-end/components/Editmap.js index c8e19f9..b5ef085 100755 --- a/front-end/components/Editmap.js +++ b/front-end/components/Editmap.js @@ -606,6 +606,7 @@ function Editmap() { allPoints.forEach(point => { const editedFile = Array.from(point.coveredBy).filter(file => checkedState[Array.from(point.coveredBy).sort().join(", ")]?.[file]); point.editedFile = new Set(editedFile); + console.log(point.editedFile); // Update feature state on the map for each point map.current.setFeatureState({ source: "custom", diff --git a/front-end/components/MyEdit.js b/front-end/components/MyEdit.js index 3327e76..7ce5b1f 100644 --- a/front-end/components/MyEdit.js +++ b/front-end/components/MyEdit.js @@ -107,11 +107,16 @@ const MyEdit = () => { const toggleMarkers = async () => { setIsLoading(true); try { - const polygonPoints = selectedPolygons.flatMap(polygon => - polygon.slice(1) // Skip the first element (timestamp) and take the rest (points) + const polygonPoints = selectedPolygons.map(polygon => + polygon.slice(1).map(point => ({ + id: point.id, + served: point.served, + editedFile: Array.isArray(point.editedFile) ? point.editedFile : Array.from(point.editedFile) // Ensure editedFile is serialized correctly + })) ); + const requestBody = { marker: polygonPoints, polygonfeatures: selectedPolygonsArea,