Metadata-Version: 2.4
Name: mroudai-django-bookings
Version: 0.1.0
Summary: Reusable Django app that stores and manages appointment bookings.
Author-email: Kevin Oudai <kevin@mroudai.com>
License: MIT License
        
        Copyright (c) 2025
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/Kevin-Oudai/django-bookings
Project-URL: Repository, https://github.com/Kevin-Oudai/django-bookings.git
Keywords: django,bookings,appointments
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: Django
Classifier: Framework :: Django :: 4.2
Classifier: Framework :: Django :: 5.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Office/Business
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Django>=4.2
Dynamic: license-file

# mroudai-django-bookings

`django-bookings` is a reusable Django app for creating and managing appointment bookings with a simple, opinionated lifecycle. It focuses on storing bookings safely and integrating with an external slots/availability engine.

## What it does
- Booking model with lifecycle (`PENDING`, `CONFIRMED`, `CANCELLED`, `COMPLETED`, `NO_SHOW`)
- Concurrency-safe creation and rescheduling with overlap protection and optional duplicate guard window
- Optional tenancy scope and snapshots of cancellation/reschedule rules from the service
- Add-on snapshots and optional booking events for audit history
- Busy-interval selector compatible with `django-slots`

## What it does **not** do
- Payment processing
- Notifications
- Availability logic (relies on your slots engine)
- End-user UI (admin is provided)

## Installation
1. Add to `INSTALLED_APPS`:
   ```python
   INSTALLED_APPS = [
       # ...
       "bookings",
   ]
   ```
2. Configure the settings below as needed.
3. Run migrations: `python manage.py migrate`.

## Settings (defaults)
```python
BOOKINGS_TENANT_MODEL = None  # e.g. "tenants.Tenant"
BOOKINGS_SERVICE_MODEL = "services.Service"
BOOKINGS_PROVIDER_MODEL = "providers.Provider"
BOOKINGS_ADDON_MODEL = "services.ServiceAddon"
BOOKINGS_USER_MODEL = None  # defaults to settings.AUTH_USER_MODEL

# Slot validation
BOOKINGS_SLOT_VALIDATION_MODE = "ENGINE"  # or "NONE"
BOOKINGS_SLOTS_AVAILABLE_FUNC = "slots.selectors.list_available_slots"

# Optional duplicate guard (minutes)
BOOKINGS_DUPLICATE_GUARD_MINUTES = 0
```

### Tenancy
If `BOOKINGS_TENANT_MODEL` is set, bookings include a tenant FK and must match the tenant of referenced service/provider when those models expose `tenant_id`.

### Slots integration
When `BOOKINGS_SLOT_VALIDATION_MODE="ENGINE"`, `create_booking` and `reschedule_booking` call `BOOKINGS_SLOTS_AVAILABLE_FUNC(service, provider, start_dt, end_dt, tenant=None)` and require the chosen `start_at` to be present in the returned list. Set this to your slots selector.

Expose busy intervals to `django-slots` with:
```python
from bookings.selectors import get_busy_intervals
```
It returns buffered busy windows for bookings in `PENDING`/`CONFIRMED`.

## Service-layer API
```python
from bookings.services import (
    create_booking,
    cancel_booking,
    reschedule_booking,
    complete_booking,
    mark_no_show,
)
```
- `create_booking` snapshots service rules, validates availability (when enabled), prevents overlaps, and records a `CREATED` event.
- `cancel_booking` / `reschedule_booking` enforce notice windows stored on the booking.
- `complete_booking` and `mark_no_show` transition status and log events.

All business-rule failures raise `django.core.exceptions.ValidationError`.

## Concurrency and overlap protection
- Application-level overlap detection using `select_for_update()` during create/reschedule.
- Buffers (`buffer_before_minutes`/`buffer_after_minutes`) are included in busy-window checks.
- Optional `BOOKINGS_DUPLICATE_GUARD_MINUTES` rejects near-identical duplicates.

## Admin
The Django admin lists bookings with filters/search, includes inline add-ons/events, and enforces `full_clean()` on save.

## Running tests
```
python test bookings
```
The helper script defaults to `bookings.tests.settings`; pass additional labels/paths after `test` as needed.

## Publishing
Use the helper scripts to build and upload (expects PyPI credentials via `~/.pypirc` or `PYPI_USERNAME`/`PYPI_PASSWORD`):
- Bash: `bash publish.sh`
- Python: `python upload project`

SQLite is used in tests; PostgreSQL is recommended in production (consider adding an exclusion constraint for overlaps there).
