django-sage-tools
is a Django package providing an extensive suite of tools, mixins, utilities, and validators tailored for class-based views, model handling, form processing, and various other enhancements. This library simplifies common development patterns in Django, offering robust solutions for request handling, data validation, model management, caching, and more.
Some of the key features include:
python -m venv .venv
.venv\Scripts\activate
source .venv/bin/activate
pip install django-sage-tools
poetry new myproject
cd myproject
poetry add django-sage-tools
poetry shell
Here are some examples of the capabilities provided by django-sage-tools
:
AccessMixin
: Base class for view access control, handling unauthenticated redirects and permission handling.LoginRequiredMixin
, AnonymousRequiredMixin
: Require or restrict user authentication for accessing views.CacheControlMixin
and NeverCacheMixin
: Control caching behavior with cache-control headers or prevent caching entirely.FormMessagesMixin
: Adds success and failure messages on form validation.Example:
from sage_tools.mixins.views.access import LoginRequiredMixin
from django.views.generic import ListView
class MyListView(LoginRequiredMixin, ListView):
model = MyModel
template_name = "my_template.html"
TimeStampMixin
: Automatically manages created_at
and modified_at
fields.UUIDBaseModel
: Adds a UUID primary key to the model.BaseTitleSlugMixin
: Provides a title and auto-generated unique slug field.Example:
from django.db import models
from sage_tools.mixins.models.base import TimeStampMixin, UUIDBaseModel
class Product(TimeStampMixin, UUIDBaseModel):
name = models.CharField(max_length=255)
FileSizeValidator
: Validates the file size against a specified maximum.HalfPointIncrementValidator
: Ensures a rating is in half-point increments between 1 and 5.NameValidator
: Validates names to contain only letters, spaces, hyphens, and apostrophes.Example:
from django.db import models
from sage_tools.validators.file import FileSizeValidator
class Document(models.Model):
file = models.FileField(upload_to='documents/', validators=[FileSizeValidator(max_size=5 * 1024 * 1024)])
LimitOneInstanceAdminMixin
: Enforces a singleton pattern in the Django admin.ReadOnlyAdmin
: Makes a model read-only in the admin.Example:
from django.contrib import admin
from sage_tools.mixins.admins.limits import LimitOneInstanceAdminMixin
from .models import SingletonModel
@admin.register(SingletonModel)
class SingletonModelAdmin(LimitOneInstanceAdminMixin, admin.ModelAdmin):
pass
UnitConvertor
class for converting bytes to megabytes, days to seconds, etc.BaseDataGenerator
for creating placeholder images, random text, colors, prices, and more.Example:
from sage_tools.utils.converters import UnitConvertor
bytes_value = 1024
megabytes = UnitConvertor.convert_byte_to_megabyte(bytes_value)
Some features rely on Django settings:
FERNET_SECRET_KEY
: Required for session encryption with FernetEncryptor
.CLEANUP_DELETE_FILES
: Enables/disables automatic file deletion on model instance updates.AUTO_SLUGIFY_ENABLED
: Controls whether slugs are automatically generated in SlugService
.Add these in your Django settings.py
file as needed:
FERNET_SECRET_KEY = "your_fernet_key_here"
CLEANUP_DELETE_FILES = True
AUTO_SLUGIFY_ENABLED = True
Thank you for your interest in contributing to our package! This document outlines the tools and steps to follow to ensure a smooth and consistent workflow. CODE OF CONDUCT
django-sage-tools
is licensed under the MIT License. See the LICENSE file for details.