File system & Handler
This module manages file operations within the Plantask system, including upload, deletion, renaming, and batch zipping. It integrates with the database and activity log to track file-related actions.
📁 FILE MANAGEMENT WORKFLOW (Pyramid + SQLAlchemy)
1. 📤 File Upload (Single)
▶ Entry Point:
Route: file_upload_page
Method: POST
Function: file_upload_page(request)
🔄 Flow:
Extract Form Data:
entity_type
(task, microtask, project, profile_picture)entity_id
(ID to associate file with)file
(uploaded file)view_name
(optional, for logs)
Create
FileUploadService
instanceCall
handle_upload(...)
in the serviceSaves file to disk
Creates DB record in
File
Associates with corresponding entity via:
TasksFile
MicrotasksFile
Project.project_image_id
User.user_image_id
Logs activity in
ActivityLog
Flash message + redirect based on
entity_type
→ to the appropriate view (task, microtask, etc.)
2. 📤 File Upload (Multiple)
▶ Entry Point:
Route: multi_upload
Method: POST
Function: multi_upload(request)
🔄 Flow:
Get list of uploaded files from
multi_files
(POST)Create
FileUploadService
instanceCall
handle_multiple_uploads_as_file(...)
(Not shown in your code, but presumably similar tohandle_upload
in structure)Flash success/failure
Redirect to
file_list_page
3. 📄 File Display
▶ Entry Point:
Route: file_list_page
Method: GET
Function: file_list_page(request)
🔄 Flow:
Queries all active files:
File.active == True
Passes them to the template:
templates/test_file_service.jinja2
4. ⬇ File Download (GET or POST)
▶ Option A:
Route: file_crud
Method: GET
Function: file_crud(request)
▶ Option B:
Route: download_file
Method: POST
Function: handle_download_file(request)
🔄 Flow (Both):
Extract
file_id
and checkaction == 'download'
Create
FileUploadService
instanceCall
download_file(file_id)
Query
File
whereactive == True
Check file exists on disk
Return file path + name
Return
FileResponse
to trigger browser download
5. ❌ File Deletion (Soft Delete)
▶ Entry Point:
Route: delete_file_page
Method: POST
Function: delete_file_page(request)
🔄 Flow:
Extract
file_id
andview_name
from POSTCreate
FileUploadService
instanceCall
delete_file(file_id, ...)
Sets
File.active = False
Logs activity in
ActivityLog
Flash message + redirect to
file_list_page
🔧 SERVICE LOGIC: FileUploadService
FileUploadService
✅ save_file_to_disk(file_storage)
save_file_to_disk(file_storage)
Saves to
upload_dir
Only allows certain extensions (
ALLOWED_FILES
)Returns metadata (filename, path, extension, URL)
✅ handle_upload(...)
handle_upload(...)
Uses
save_file_to_disk
Creates a
File
recordAssociates with entity (via
TasksFile
,ProfilePicture
, etc.)Logs action via
ActivityLog
Returns upload result
✅ delete_file(...)
delete_file(...)
Queries file by ID
If found and active → sets
active = False
Logs deletion
Commits transaction
✅ download_file(file_id)
download_file(file_id)
Looks up file by ID
Ensures it's active and file exists
Returns path and filename for download
Last updated