How mewset/lagerhantering works
Project Type and Purpose
• Full-stack Flask web application for managing spare-parts inventory.
• Modular, layered architecture with REST API, UI templates, JSON storage, scheduled backup and updater services.
Key Components (to include
A. Presentation Layer (Client)
• Browser / HTTP client
• Static assets: static/css/style.css, static/js/*.js
• HTML templates: templates/index.html, admin.html, dashboard.html, logs.html, changelog.html, footer.html
B. API Layer (Flask App)
• app.py (Flask application factory, dependency injection)
• config.py (Singleton configuration provider)
• routes/ Blueprints:
• inventory.py
• settings.py
• logs.py
C. Service Layer (Business Logic)
• services/inventory_service.py
• services/settings_service.py
• services/backup_service.py (scheduled backups)
• services/updater_service.py (in-process updater)
D. Data Access Layer
• models/inventory.py (InventoryModel, InventoryItem)
• models/__init__.py
E. Shared Utilities
• utils/logger.py (structured logging)
• utils/file_handler.py (thread-safe file I/O)
• utils/validation.py (input schemas)
• utils/exceptions.py (custom exception hierarchy)
• utils/decorators.py (error handling, validation wrappers)
F. Data Stores
• data/inventory.json
• data/dashboard_settings.json
• db_backup/ (rotating file backups)
• version_backup/ (pre-update code snapshots)
G. Background Daemons
• updater.py (standalone updater with --daemon flag, uses schedule library)
• start_updater_daemon.bat / test_updater.bat
• backup_service triggers from within the Flask process or can be drawn as a scheduled task
H. External Dependencies
• Git (for updater)
• schedule (Python package for cron-like scheduling)
• Flask (web framework)
Relationships and Data Flow
1. User → Browser → HTTP(S) → Flask (app.py)
• Requests hit the registered Blueprints in routes/.
2. Routes → Service Layer
• routes/*.py validate input (utils/validation), call services/*.py.
3. Services → Models + Utils
• business logic in inventory_service.py calls InventoryModel for CRUD.
• backup_service and updater_service orchestrate file_handler, logger, Git operations.
4. Models → JSON Data
• InventoryModel reads/writes inventory.json via file_handler, maintains in-memory cache.
5. Background Daemons
• updater.py process invokes updater_service logic; schedules Git pull → version_backup → graceful restart of app.
• backup_service schedules file‐level backups to db_backup/ with retention policy.
6. Logging
• All components use utils/logger.py to append to app.log or updater.log.
Architectural Patterns & Principles
• Layered Architecture: Presentation → API → Services → Data Access → Storage.
• Blueprint Pattern for route modularity (routes/).
• Service Layer for business rules.
• Singleton Pattern for global config (config.py).
• Dependency Injection in app.py for swapping mocks in tests.
• Thread-safe file I/O and caching in InventoryModel.
• Scheduler library for recurring tasks (backup, updater).