agentsclimarketplace

Flask mongodb update with file replacement

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/english_gpt4_8/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.From its SKILL.md

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill flask-mongodb-update-with-file-replacement

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

SKILL.md

2.4 KB, 419 tokens by cl100k_base, as published. Nobody here has run it

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

  1. Route Definition: Create a PUT route accepting an ID (e.g., /annonce/update/<annonce_id>).
  2. ID Validation: Validate the ID using ObjectId.is_valid.
  3. Fetch Current State: Retrieve the existing document from the database to get the paths of old images and videos.
  4. Handle Multipart Data: Parse request.form for JSON data (often in a 'data' field) and request.files for new images/videos.
  5. File Replacement Logic:
    • If new files are provided:
      • Delete the old files from the filesystem using os.remove (handle OSError gracefully).
      • Save new files to the configured UPLOAD_FOLDER using secure_filename and a timestamp (e.g., datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")).
      • Update the data dictionary with the new file paths.
  6. Database Update: Use update_one with $set to update the document in MongoDB.
  7. 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 request.files first.

Triggers

  • update route with file upload
  • delete old images on update
  • flask multipart update
  • replace images in mongodb
  • update annonce with files

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.