Skip to content

Developer Getting Started Guide

Welcome to CareLog development! This guide will help you set up your development environment and understand the codebase.

Development Prerequisites

Required Software

  • Python 3.8+ - Programming language
  • Git - Version control
  • Code Editor - VS Code, PyCharm, or your preferred IDE
  • pip - Package manager (included with Python)
  • pytest - For running tests
  • Black - Code formatting
  • pylint - Code linting
  • Virtual Environment - Dependency isolation

Setting Up Development Environment

1. Clone the Repository

bash
git clone https://github.com/howweishan/carelog.git
cd carelog

2. Create Virtual Environment

Windows:

bash
python -m venv venv
.\venv\Scripts\activate

macOS/Linux:

bash
python3 -m venv venv
source venv/bin/activate

3. Install Dependencies

bash
pip install -r requirements.txt

4. Install Development Dependencies

bash
pip install pytest pytest-faker black pylint

5. Run Tests

Verify everything is set up correctly:

bash
pytest tests/ -v

You should see all tests passing.

Project Structure

carelog/
├── app.py                    # Main Streamlit application entry point
├── core/                     # Core business logic
│   ├── models/              # Data models (User, Appointment, etc.)
│   │   ├── user.py
│   │   ├── appointment.py
│   │   ├── health_log.py
│   │   └── ...
│   ├── services/            # Business logic services
│   │   ├── user_service.py
│   │   ├── health_log_service.py
│   │   └── ...
│   └── utils/               # Utility functions
│       ├── auth.py          # Authentication helpers
│       └── timeformat.py    # Time formatting utilities
├── pages/                    # Streamlit page components
│   ├── Login.py             # Login page
│   ├── Patient/             # Patient role pages
│   ├── Doctor/              # Doctor role pages
│   ├── Nurse/               # Nurse role pages
│   ├── FamilyMembers/       # Family member pages
│   └── Admin/               # Admin pages
├── data/                     # Data storage
│   ├── data.json            # Main database file
│   └── backups/             # Backup storage
├── tests/                    # Test suite
│   ├── test_services.py
│   ├── test_auth.py
│   └── ...
├── docs/                     # Documentation (this!)
├── requirements.txt          # Python dependencies
└── README.md                # Project overview

Architecture Overview

CareLog follows a layered architecture:

1. Presentation Layer (Streamlit Pages)

  • Located in pages/ directory
  • Role-based UI components
  • Handles user interactions
  • Calls service layer for business logic

2. Service Layer

  • Located in core/services/
  • Contains business logic
  • Validates data
  • Coordinates between models and database
  • Transaction management

3. Model Layer

  • Located in core/models/
  • Data structures and validation
  • Business rules
  • Entity definitions

4. Data Access Layer

  • DatabaseService in core/services/database_service.py
  • Handles JSON file operations
  • Data persistence
  • Query operations

Key Concepts

Data Models

All entities inherit from or follow similar patterns:

python
@dataclass
class User:
    id: str
    name: str
    email: str
    role: str
    # ... other fields

Services

Services provide CRUD operations and business logic:

python
class UserService:
    def create(self, ...): pass
    def get_by_id(self, user_id): pass
    def update(self, user_id, **kwargs): pass
    def delete(self, user_id): pass
    def list(self): pass

Database Service

Centralized JSON-based data storage:

python
db_service = DatabaseService()
db_service.update("users", user_id, user_data)
users = db_service.get("users")

Authentication

Password hashing and verification:

python
from core.utils.auth import PasswordHash

hasher = PasswordHash()
hashed = hasher.hash("password")
is_valid = hasher.verify("password", hashed)

Development Workflow

1. Feature Development

  1. Create a feature branch

    bash
    git checkout -b feature/your-feature-name
  2. Implement your changes

    • Add/modify models in core/models/
    • Implement service logic in core/services/
    • Create UI in pages/
  3. Write tests

    bash
    # Create test file in tests/
    # tests/test_your_feature.py
  4. Run tests

    bash
    pytest tests/ -v
  5. Commit your changes

    bash
    git add .
    git commit -m "Add your feature description"

2. Testing

Running All Tests

bash
pytest tests/ -v

Running Specific Test File

bash
pytest tests/test_services.py -v

Running Specific Test

bash
pytest tests/test_services.py::test_health_log_service -v

Running with Coverage

bash
pytest tests/ --cov=core --cov-report=html

3. Code Quality

Format Code

bash
black .

Lint Code

bash
pylint core/ pages/

Common Development Tasks

Adding a New Model

  1. Create model file in core/models/
  2. Define dataclass with fields
  3. Add validation if needed
  4. Export from core/models/__init__.py

Example:

python
# core/models/your_model.py
from dataclasses import dataclass

@dataclass
class YourModel:
    id: str
    name: str
    # ... other fields

Adding a New Service

  1. Create service file in core/services/
  2. Implement CRUD operations
  3. Add business logic methods
  4. Export from core/services/__init__.py

Example:

python
# core/services/your_service.py
from core.services.database_service import DatabaseService

class YourService:
    def __init__(self, db_service: DatabaseService):
        self.db = db_service
        self.collection = "your_collection"
    
    def create(self, ...):
        # Implementation
        pass

Adding a New Page

  1. Create page file in appropriate pages/ subfolder
  2. Import required services
  3. Implement Streamlit UI
  4. Handle user interactions

Example:

python
# pages/Patient/your_page.py
import streamlit as st
from core.services.your_service import YourService

st.set_page_config(page_title="Your Page")

# Check authentication
if "user" not in st.session_state:
    st.switch_page("pages/Login.py")

# Your page logic
st.title("Your Page")
# ... rest of implementation

Writing Tests

  1. Create test file in tests/
  2. Use pytest fixtures for setup
  3. Write test functions
  4. Use assertions to verify behavior

Example:

python
# tests/test_your_feature.py
import pytest
from core.services.your_service import YourService

@pytest.fixture
def temp_db():
    # Setup
    yield db, path
    # Teardown

def test_your_feature(temp_db):
    db, path = temp_db
    service = YourService(db_service=db)
    
    # Test implementation
    result = service.your_method()
    assert result is not None

Debugging

Streamlit Debugging

  1. Use st.write() for debug output
  2. Check browser console for JavaScript errors
  3. Use st.session_state to inspect state

Python Debugging

  1. Use Python debugger:

    python
    import pdb; pdb.set_trace()
  2. Add logging:

    python
    import logging
    logging.debug("Debug message")

Common Issues

Import Errors

  • Ensure virtual environment is activated
  • Verify all dependencies are installed
  • Check Python path

Database Errors

  • Verify data/data.json exists
  • Check file permissions
  • Validate JSON structure

Streamlit Page Navigation

  • Ensure authentication check is present
  • Verify role-based access control
  • Check session state

Best Practices

Code Style

  1. Follow PEP 8 guidelines
  2. Use type hints
  3. Write docstrings for functions/classes
  4. Keep functions small and focused

Testing

  1. Write tests for all new features
  2. Aim for high code coverage
  3. Test edge cases
  4. Use meaningful test names

Git Workflow

  1. Create feature branches
  2. Write clear commit messages
  3. Keep commits atomic
  4. Rebase before merging

Security

  1. Never commit sensitive data
  2. Use environment variables for secrets
  3. Validate all user input
  4. Follow principle of least privilege

API Reference

See API Reference for detailed API documentation.

Contributing

Pull Request Process

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Write/update tests
  5. Update documentation
  6. Submit pull request

Code Review Checklist

  • [ ] Tests pass
  • [ ] Code follows style guidelines
  • [ ] Documentation updated
  • [ ] No security vulnerabilities
  • [ ] Backward compatible (if applicable)

Resources

Next Steps