Overview
CoroNet stores all vehicle registrations in a CSV-based database system, providing a lightweight yet robust solution for managing vehicle entry records. The system supports full CRUD operations (Create, Read, Update, Delete) with automatic ID assignment and file-based persistence.Data Model
Each vehicle registration contains seven fields:The CSV storage format (defined at app.py:24) provides human-readable data that can be easily imported into spreadsheet applications or other systems.
Storage Architecture
File System Structure
CoroNet maintains two primary storage locations:- data/registros.csv: Main CSV database file containing all registration records (app.py:18)
- uploads/: Directory storing all uploaded vehicle images (app.py:19)
CSV File Management
The system implements three core functions for CSV operations:ensure_csv() - Database Initialization
ensure_csv() - Database Initialization
Guarantees the CSV file exists with proper headers:
- Checks if the CSV file exists (app.py:27)
- Creates new file with headers if missing (app.py:28-29)
- Uses UTF-8 encoding for international character support
- Called before any read/write operation to prevent errors
read_csv() - Data Retrieval
read_csv() - Data Retrieval
Loads all registrations from the CSV file:
- Ensures CSV exists before reading (app.py:32)
- Uses DictReader for dictionary-based row access (app.py:34)
- Returns list of dictionaries, each representing one registration
- Keys correspond to CSV_HEADER field names
write_csv(rows) - Data Persistence
write_csv(rows) - Data Persistence
Writes complete dataset back to CSV file:
- Overwrites entire CSV file with provided rows (app.py:38-41)
- Uses DictWriter for consistent field ordering
- Writes header row before data rows
- Ensures atomic updates (read-modify-write pattern)
CRUD Operations
Create: Adding New Registrations
New registrations are created through the/guardar endpoint:
- Auto-incrementing IDs: New ID calculated as
len(rows) + 1(app.py:113) - Automatic timestamps: Current date/time in
YYYY-MM-DD HH:MM:SSformat (app.py:114) - Image validation: Requires image upload before processing (app.py:94-96)
- Dual OCR detection: Attempts GPT-4o first, falls back to Tesseract (app.py:103-110)
- User feedback: Flash messages confirm successful save (app.py:127)
The system uses a read-modify-write pattern: load all records, append new record, write all records back. This ensures data integrity but may have performance implications for very large datasets.
Read: Viewing Registrations
The/registros endpoint displays all stored registrations:
- Loads entire dataset using
read_csv()(app.py:132) - Passes data to template for rendering (app.py:133)
- Returns list of dictionaries, one per registration
Update: Modifying Registrations
The current implementation does not include an explicit update endpoint. To modify a registration, you would need to delete the existing record and create a new one, or implement a custom update route.
Delete: Removing Registrations
The/eliminar/<id> endpoint handles record deletion:
- List filtering: Creates new list excluding the target ID (app.py:138)
- Image cleanup: Removes associated image file from uploads directory (app.py:139-144)
- Safe deletion: Checks if image file exists before attempting removal (app.py:143)
- Cascading delete: Both database record and physical image file are removed
- User feedback: Confirms successful deletion (app.py:146)
- ID-based Deletion
- Image Cleanup
Records are deleted by ID, which is unique and auto-incremented. The ID is passed as a URL parameter:
Data Integrity
Automatic File Creation
The system automatically creates required directories and files:UTF-8 Encoding
All CSV operations use UTF-8 encoding (app.py:28, 33, 38), supporting international characters in:- Vehicle owner names
- License plates
- Observations/notes
File Path Handling
The system usesos.path.join() for cross-platform compatibility:
\) and Unix-based systems (/).
Performance Considerations
Read-Modify-Write Pattern
Read-Modify-Write Pattern
Every create and delete operation:
- Reads entire CSV into memory
- Modifies the in-memory list
- Writes entire list back to disk
Scaling Recommendations
Scaling Recommendations
For larger deployments, consider:
- Migrating to SQLite or PostgreSQL for indexed queries
- Implementing pagination for the registrations list
- Using a proper ORM like SQLAlchemy
- Adding database connection pooling
- Implementing caching for frequently accessed records
