Skip to content

Commit

Permalink
Backend update to handle new data structure pushed from frontend editing
Browse files Browse the repository at this point in the history
Signed-off-by: ZhuoweiWen <[email protected]>
  • Loading branch information
ZhuoweiWen committed Jul 11, 2024
1 parent d07bf69 commit 55d8e91
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
33 changes: 19 additions & 14 deletions back-end/controllers/celery_controller/celery_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions back-end/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions front-end/components/Editmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions front-end/components/MyEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 55d8e91

Please sign in to comment.