sageteamorg / django-sage-tools

Reusable, generic mixins for Django
MIT License
6 stars 5 forks source link
django django-https django-mixin django-packages sageteam views

django-sage-tools

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.

Features

Some of the key features include:

Installation

Using pip

  1. Create a Virtual Environment:
    python -m venv .venv
  2. Activate the Virtual Environment:
    • On Windows:
      .venv\Scripts\activate
    • On macOS and Linux:
      source .venv/bin/activate
  3. Install the Package:
    pip install django-sage-tools

Using Poetry

  1. Install Poetry: Follow the official installation instructions at the Poetry website.
  2. Create a New Project (Optional):
    poetry new myproject
    cd myproject
  3. Add the Package as a Dependency:
    poetry add django-sage-tools
  4. Activate the Virtual Environment:
    poetry shell

Usage

Here are some examples of the capabilities provided by django-sage-tools:

1. Mixins for Views

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"

2. Model Mixins

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)

3. Validators

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)])

4. Admin Tools

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

5. Utilities

Example:

from sage_tools.utils.converters import UnitConvertor

bytes_value = 1024
megabytes = UnitConvertor.convert_byte_to_megabyte(bytes_value)

Configuration

Some features rely on Django settings:

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

Contribution Guidelines

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

License

django-sage-tools is licensed under the MIT License. See the LICENSE file for details.