A comprehensive bookkeeping system designed for tracking financial transactions, generating reports, and maintaining audit trails. The application features user authentication, account management, transaction recording, and is built with a modular architecture to support future integrations, including machine learning enhancements.
- π Bookkeeping Application
Ensure you have the following installed on your system:
- Python 3.10 or above: Download and install from Python's official website.
- Virtual Environment (
venv): Comes bundled with Python 3. Ensure it's available in your Python installation. - Git (optional): For version control and cloning repositories. Download from Git's official website.
Ensure your project has the following structure:
bookkeeping/
βββ app/
β βββ __init__.py
β βββ main.py
β βββ static/
β βββ templates/
β β βββ __init__.py
β β βββ account_form.html
β β βββ account_list.html
β β βββ base.html
β β βββ index.html
β β βββ login.html
β β βββ transaction_form.html
β β βββ transaction_list.html
β βββ views/
β βββ __init__.py
β βββ accounts.py
β βββ auth.py
β βββ main.py
β βββ transactions.py
βββ modules/
β βββ __init__.py
β βββ database/
β β βββ __init__.py
β β βββ db.py
β βββ forms/
β β βββ __init__.py
β β βββ account_form.py
β β βββ login_form.py
β β βββ transaction_form.py
β βββ models/
β βββ __init__.py
β βββ account.py
β βββ transaction.py
β βββ user.py
βββ migrations/
β βββ env.py
β βββ README
β βββ versions/
β βββ [timestamp]_initial_migration.py
βββ tests/
β βββ __init__.py
β βββ conftest.py
β βββ test_models.py
β βββ test_views.py
βββ data/
β βββ app.db
βββ config.py
βββ create_user.py
βββ requirements.txt
βββ README.md
-
Clone the Repository (if using Git):
git clone https://github.com/yourusername/bookkeeping.git cd bookkeeping -
Create a Virtual Environment:
python -m venv .venv
-
Activate the Virtual Environment:
-
On Unix/Linux/macOS or Git Bash:
source .venv/bin/activate -
On Windows (Command Prompt):
.venv\Scripts\activate
-
On Windows (PowerShell):
.\.venv\Scripts\Activate.ps1
-
-
Install Required Packages:
Ensure your
requirements.txtis up-to-date. If not, create one with the necessary dependencies:pip install Flask Flask-WTF Flask-Login Flask-Migrate Flask-SQLAlchemy pytest werkzeug pip freeze > requirements.txtThen, install the dependencies:
pip install -r requirements.txt
Follow these steps to initialize and upgrade your database using Flask-Migrate.
-
Ensure the
dataDirectory ExistsSQLite requires the directory specified in the
SQLALCHEMY_DATABASE_URIto exist.mkdir data
-
Configure the Database URI
Ensure your
config.pyuses an absolute path for the database URI.# config.py import os class Config: SECRET_KEY = os.environ.get('SECRET_KEY', 'your_secret_key') # Generate absolute path for the database basedir = os.path.abspath(os.path.dirname(__file__)) SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'data', 'app.db') SQLALCHEMY_TRACK_MODIFICATIONS = False class DevelopmentConfig(Config): DEBUG = True class ProductionConfig(Config): DEBUG = False class TestingConfig(Config): TESTING = True SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:' # In-memory DB for testing WTF_CSRF_ENABLED = False # Disable CSRF for testing
-
Initialize Flask-Migrate
If you haven't already initialized migrations, run:
flask db init
-
Create the Initial Migration
Generate the migration scripts based on your models.
flask db migrate -m "Initial migration." -
Apply the Migration to the Database
Execute the migration to create the database schema.
flask db upgrade
To authenticate users in your application, create at least one user. You can use the provided create_user.py script.
-
Activate the Virtual Environment
Ensure your virtual environment is active.
-
Run the
create_user.pyScriptThis script creates a user with the specified username and password.
python create_user.py
Output:
User admin created successfully.If the user already exists, you'll see:
User admin already exists.Note: You can modify
create_user.pyto change the username and password.
Ensure your application is correctly set up by running your test suite.
-
Activate the Virtual Environment
Ensure your virtual environment is active.
-
Run Pytest
Execute the tests using
pytest.pytest
Expected Output:
============================== test session starts ============================== platform win32 -- Python 3.x.x, pytest-x.x.x, pluggy-x.x.x rootdir: /path/to/bookkeeping collected 4 items tests/test_models.py . [ 25%] tests/test_views.py ... [100%] =============================== 4 passed in XXs ===============================If tests fail, review the error messages for troubleshooting.
With the database initialized and a user created, you can now run your Flask application.
-
Activate the Virtual Environment
Ensure your virtual environment is active.
-
Set Environment Variables
Configure environment variables based on your operating system.
-
Using Git Bash or Unix Shell:
export FLASK_APP=app/main.py export FLASK_ENV=development
-
Using Windows Command Prompt:
set FLASK_APP=app/main.py set FLASK_ENV=development
-
Using Windows PowerShell:
$env:FLASK_APP = "app/main.py" $env:FLASK_ENV = "development"
-
-
Run the Flask Development Server
Start the Flask server.
flask run
Expected Output:
* Serving Flask app 'app/main.py' * Environment: development * Debug mode: on WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5000 Press CTRL+C to quit
-
Open Your Web Browser.
-
Navigate to the Login Page:
http://localhost:5000/auth/login -
Login Using the Created User:
- Username:
admin - Password:
securepassword123
- Username:
-
Test Application Functionality:
-
Create Accounts:
- Navigate to
http://localhost:5000/accounts/new. - Fill out the form and submit.
- Navigate to
-
List Accounts:
- Navigate to
http://localhost:5000/accounts/or use the link provided in the navigation bar.
- Navigate to
-
Create Transactions:
- Navigate to
http://localhost:5000/transactions/new. - Fill out the form and submit.
- Navigate to
-
List Transactions:
- Navigate to
http://localhost:5000/transactions/or use the link provided in the navigation bar.
- Navigate to
-
Logout:
- Click on the "Logout" link available in the navigation bar or navigate to
http://localhost:5000/auth/logout.
- Click on the "Logout" link available in the navigation bar or navigate to
-
Before considering the setup complete, ensure the following:
-
Directory Structure:
- All necessary directories and
__init__.pyfiles exist.
- All necessary directories and
-
Blueprints:
- Blueprints (
auth,accounts,transactions,main) are correctly defined and registered inapp/__init__.py.
- Blueprints (
-
Flask Extensions:
- All extensions (
SQLAlchemy,Flask-Migrate,Flask-Login) are initialized before registering blueprints.
- All extensions (
-
Configuration:
config.pyhas the correctSQLALCHEMY_DATABASE_URIandSECRET_KEY.- Environment variables (
FLASK_APP,FLASK_ENV) are set appropriately.
-
Database:
- Flask-Migrate is initialized.
- Initial migration has been created and applied.
- Database tables reflect your models.
-
User Authentication:
- At least one user (
admin) is created for logging in. - Authentication routes (
/auth/login,/auth/logout) are accessible and functional.
- At least one user (
-
Templates:
- All necessary HTML templates (e.g.,
login.html,account_form.html,transaction_form.html) are present inapp/templates/. - Templates correctly extend a base template (
base.html) if used.
- All necessary HTML templates (e.g.,
-
Testing:
conftest.pyis correctly set up with necessary fixtures.- Test files correctly utilize fixtures and import necessary modules.
- All tests pass when running
pytest.
-
Dependencies:
- All required packages are installed in the virtual environment (
pip install -r requirements.txt).
- All required packages are installed in the virtual environment (
-
Static Files:
- CSS styles are correctly linked and applied. Verify by checking the UI in your browser.
- Flask Documentation: https://flask.palletsprojects.com/en/2.3.x/
- Flask-Migrate Documentation: https://flask-migrate.readthedocs.io/en/latest/
- Flask-Login Documentation: https://flask-login.readthedocs.io/en/latest/
- Pytest Documentation: https://docs.pytest.org/en/latest/
- Understanding Circular Imports in Python: https://realpython.com/python-import/#circular-imports
-
Environment Variables:
- Use environment variables to manage sensitive data like
SECRET_KEY. Consider using packages likepython-dotenvto manage these variables easily.
- Use environment variables to manage sensitive data like
-
Error Handling:
- Ensure that all forms and routes handle errors gracefully, providing meaningful feedback to users.
-
User Roles & Permissions:
- Expand the
Usermodel to include roles and permissions, enhancing security and access control.
- Expand the
-
Styling & Responsiveness:
- Enhance the UI by integrating CSS frameworks like Bootstrap for a more polished and responsive design.
-
API Development:
- Consider developing RESTful APIs for your application, enabling integration with other services or frontend frameworks.
-
Continuous Integration:
- Set up CI/CD pipelines using tools like GitHub Actions or Travis CI to automate testing and deployment processes.
-
Logging & Monitoring:
- Implement logging to track application behavior and errors. Tools like Flask-Logging can be beneficial.
-
Documentation:
- Maintain comprehensive documentation for your codebase, aiding future development and collaboration.