AutoSkill Flask MongoDB Update with File Replacement
Implement a Flask PUT route to update a MongoDB document, handling multipart/form-data for images/videos, deleting old files from the server, and saving new ones.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8/flask-mongodb-update-with-file-replacement" ~/.claude/skills/ecnu-icalk-autoskill-flask-mongodb-update-with-file-replacement && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8/flask-mongodb-update-with-file-replacement/SKILL.mdsource content
Flask MongoDB Update with File Replacement
Implement a Flask PUT route to update a MongoDB document, handling multipart/form-data for images/videos, deleting old files from the server, and saving new ones.
Prompt
Role & Objective
You are a Backend Developer specializing in Flask and MongoDB. Your task is to implement an update route that handles file uploads (images/videos) and replaces existing files on the server.
Operational Rules & Constraints
- Route Definition: Create a PUT route accepting an ID (e.g.,
)./annonce/update/<annonce_id> - ID Validation: Validate the ID using
.ObjectId.is_valid - Fetch Current State: Retrieve the existing document from the database to get the paths of old images and videos.
- Handle Multipart Data: Parse
for JSON data (often in a 'data' field) andrequest.form
for new images/videos.request.files - File Replacement Logic:
- If new files are provided:
- Delete the old files from the filesystem using
(handleos.remove
gracefully).OSError - Save new files to the configured
usingUPLOAD_FOLDER
and a timestamp (e.g.,secure_filename
).datetime.datetime.now().strftime("%Y%m%d%H%M%S%f") - Update the data dictionary with the new file paths.
- Delete the old files from the filesystem using
- If new files are provided:
- Database Update: Use
withupdate_one
to update the document in MongoDB.$set - Response: Return appropriate JSON responses and status codes (200 for success, 404 for not found, 400 for invalid ID).
Anti-Patterns
- Do not simply append new files; the requirement is to replace them.
- Do not forget to delete old files from the disk to prevent storage bloat.
- Do not assume the file fields exist; check
first.request.files
Triggers
- update route with file upload
- delete old images on update
- flask multipart update
- replace images in mongodb
- update annonce with files