Skip to content

Commit

Permalink
File delete update and bug fix
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 55d8e91 commit b6aeb15
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 52 deletions.
19 changes: 12 additions & 7 deletions back-end/controllers/celery_controller/celery_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ def toggle_tiles(self, markers, userid, folderid, polygonfeatures):
status_code = 0
session = Session()
try:
file_editfile_link_set = set()
user_folder = folder_ops.get_folder_with_id(userid=userid, folderid=folderid, session=session)
if user_folder:
# Process each polygon feature
Expand All @@ -227,18 +226,24 @@ def toggle_tiles(self, markers, userid, folderid, polygonfeatures):
new_editfile = editfile_ops.create_editfile(filename=editfile_name, content=feature_binary, folderid=folderid, session=session)
session.commit()

# Link this editfile with relevant files
file_ids = set()
if markers[index]:
for filename in markers[index][0]['editedFile']:
fileVal = file_ops.get_file_with_name(filename=filename, folderid=user_folder.id, session=session)
file_ids.add(
fileVal.id
)

for file_id in file_ids:
file_editfile_link_ops.link_file_and_editfile(file_id, new_editfile.id, session)

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
13 changes: 13 additions & 0 deletions back-end/controllers/database_controller/file_editfile_link_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ def link_file_and_editfile(file_id, editfile_id, session):
session.commit()
return new_link

def unlink_file_and_editfile(file_id, editfile_id, session):
# Query to find the existing link between the file and editfile
link = session.query(file_editfile_link).filter(
file_editfile_link.file_id == file_id,
file_editfile_link.editfile_id == editfile_id
).first()

# If a link exists, delete it from the session and commit the change
if link:
session.delete(link)
session.commit()



def get_editfiles_for_file(file_id, session):
file_instance = session.query(file).filter_by(id=file_id).one()
Expand Down
31 changes: 9 additions & 22 deletions back-end/controllers/database_controller/file_ops.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import psycopg2
from database.sessions import ScopedSession, Session
from database.models import file, kml_data
from database.models import file, kml_data, file_editfile_link
from threading import Lock
from datetime import datetime
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
Expand Down Expand Up @@ -216,33 +216,20 @@ def delete_file(fileid, session=None):
owns_session = True

try:
file_to_del = get_file_with_id(fileid, session)
file_to_del = get_file_with_id(fileid, session)
if file_to_del:
# Split name and extension
base_name, ext = file_to_del.name.rsplit('.', 1)

# Check if the file is an edit file with the new naming scheme
if re.match(".*-edit\d*/$", ext):
orig_file_ext = ext.split('-edit')[0]
kml_entries = session.query(kml_data).filter(kml_data.file_id == fileid).all()
orig_file = get_file_with_name(base_name + '.' + orig_file_ext, file_to_del.folder_id, session)
if orig_file:
for kml_entry in kml_entries:
kml_entry.file_id = orig_file.id
kml_entry.served = True
kml_entry.coveredLocations = orig_file.name
session.add(kml_entry)
session.commit()
# If not an edit file, check for related edits
elif ext in ['.kml', '.geojson']:
related_edits = get_files_with_prefix(file_to_del.folder_id, f"{base_name + ext}-edit", session)
for edit in related_edits:
session.delete(edit)

# Delete all associated editfile links first
links = session.query(file_editfile_link).filter(file_editfile_link.file_id == fileid).all()
for link in links:
session.delete(link)

# Proceed to delete the file
session.delete(file_to_del)
if owns_session:
session.commit()


except SQLAlchemyError as e:
if owns_session:
session.rollback()
Expand Down
2 changes: 0 additions & 2 deletions front-end/components/Editmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,6 @@ 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 All @@ -623,7 +622,6 @@ function Editmap() {

selectedPolygonsRef.current.push(updatedPoints);
setSelectedPolygons(selectedPolygonsRef.current);
console.log(polygonFeature);
setSelectedPolygonsArea((prevAreas) => [
...prevAreas, polygonFeature
]);
Expand Down
28 changes: 7 additions & 21 deletions front-end/components/Upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ export default function Upload() {
event.preventDefault();

const formData = new FormData();
formData.append("deadline", `${newDeadline.year}-${newDeadline.month}-01`);
formData.append("deadline", `${newDeadline.year}-${newDeadline.month}-03`);
files.forEach((fileDetails) => {
const allowedExtensions = ["kml", "geojson", "csv", "kmz"];
const allowedExtensions = ["kml", "geojson", "csv"];
const fileExtension = fileDetails.file.name
.split(".")
.pop()
.toLowerCase();

if (!allowedExtensions.includes(fileExtension)) {
toast.error(
"Invalid File Format. Please upload a KML, GeoJSON, CSV, or KMZ file.",
"Invalid File Format. Please upload a KML, GeoJSON, or CSV file.",
{
position: toast.POSITION.TOP_RIGHT,
autoClose: 10000,
Expand Down Expand Up @@ -325,14 +325,10 @@ export default function Upload() {


const handleNewDeadlineChange = ({ month, year }) => {
const formattedDeadline = `${month}/${year}`;
// Check if the deadline exists
const exists = folders.some(folder => folder.deadline === formattedDeadline);
if (exists) {
setOpenConfirmDialog(true); // Open confirmation dialog if exists
} else {
setNewDeadline({ month, year });
}


setNewDeadline({ month, year });

};

const confirmCreation = () => {
Expand Down Expand Up @@ -470,16 +466,6 @@ export default function Upload() {
</FormControl>
</>
)}
<Dialog open={openConfirmDialog} onClose={() => setOpenConfirmDialog(false)}>
<DialogTitle>Confirm</DialogTitle>
<div>
A filing for this deadline already exists. Are you sure you want to create another?
</div>
<DialogActions>
<Button onClick={() => setOpenConfirmDialog(false)}>Cancel</Button>
<Button onClick={confirmCreation}>Confirm</Button>
</DialogActions>
</Dialog>
</div>
<ButtonGroup
variant="contained"
Expand Down

0 comments on commit b6aeb15

Please sign in to comment.