Tools
It's not bad to be lazy if it brings significant benefits to your work. Therefore, we’ve created a set of tools designed to not only accelerate your workflow but also help you organize it in a clear and efficient manner.
Good practice is to create a single location for reusable components throughout the application. We recommend creating a library in the root directory, enabling you to turn it into a package in the future.
Commands
Django Commons is one of my favorite features of the framework. As such, we’ve tailored commands for easier use.
The entire solution is written asynchronously, so the async_command
will be of particular interest. It allows you to run any part of the system as a separate process. For example, you can create a dedicated container to execute background tasks. A great example of its implementation is buildapp
in the _initializer
application.
Models
Models include Django ORM structures used throughout the application. Every model in the app should inherit from the Base
class.
Other models have been created to simplify work:
CountryCode
- Adds acountry_code
field for ISO 3166-1 alpha-2 country codes.ExternalId
- Provides a uniqueexternal_id
field for models, avoiding internal ID exposure.GUID
- Replaces the default integer primary key with a UUID field.LanguageCode
- Adds alanguage_code
field for ISO 639-1 language codes.Ordered
- Provides anorder
field for custom model ordering.OwnedByAnonymous
- Links a record to an anonymous user.OwnedByOrganization
- Links a model instance to a specific organization.OwnedByUser
- Links a model instance to a specific user.
Permissions
To manage permissions, we’ve introduced helpers:
BoBPermission
- An abstract base class for implementing custom permission logic in views, designed to be used with a decorator. For example:
Permissions are validated using the validate_permissions
decorator:
Repository
We use the repository pattern to separate data management from the business layer. Each repository manages a single model type, meaning every model type requiring operations will have its own repository.
Overview
AsyncBaseRepository
is a generic base class that simplifies asynchronous repository patterns in Django. It supports:
- Async querying of models
- Eager loading (
select_related
,prefetch_related
) - Pagination
- CRUD operations
Key Features:
- Generic Design: Works with any Django model via the
model
attribute. - Asynchronous ORM: Non-blocking database operations.
- Optimized Queries: Built-in eager loading for performance.
- CRUD Operations: Async methods for saving, updating, deleting, and retrieving data.
Example Usage:
Tests
The solution includes tools to speed up testing.
AsyncClient
AsyncClientBoB
is a custom asynchronous client wrapper for testing API endpoints. It manages headers, cookies, and response validation.
Key Features:
- User Data: Manages user-related attributes like email, UID, and organization info.
- HTTP Methods: Supports async
GET
,POST
,PATCH
, andDELETE
requests with response validation.
Fixtures
Fixtures support efficient testing of user flows and components (e2e and unit tests). Features include:
- Database management
- Reusable user authentication
- Mock RabbitMQ publishers
ResponseChecker
Facilitates response validation during e2e testing, ensuring robust and detailed checks.
Exceptions
The ExceptionRegistry
centralizes custom exceptions, ensuring consistency and maintainability. Example exceptions include:
PERMISSION_DENIED
- Unauthorized access.ALREADY_EXISTS
- Duplicate resource creation.TOKEN_EXPIRED
- Expired token errors.
Logger
The CustomJsonFormatter
enhances logging with JSON formatting and fields for compatibility with Uvicorn.
Utils
A collection of utilities for generating identifiers, secure passwords, and handling cookies. Examples:
generate_external_id()
- Creates unique identifiers.set_server_side_cookie()
- Sets secure cookies in responses.to_bool()
- Converts various input types to boolean values.