Skip to main content

A library with many features for interacting with Django

Project description

🚀 ADjango

Sometimes I use this in different projects, so I decided to put it on pypi

ADjango is a convenient library for simplifying work with Django, which offers various useful managers, services, decorators, utilities for asynchronous programming, a task scheduler for Celery, working with transactions and much more.

Installation 🛠️

pip install adjango

Settings ⚙️

  • Add the application to the project.

    INSTALLED_APPS = [
        #...
        'adjango',
    ]
    
  • In settings.py set the params

    # settings.py
    # None of the parameters are required.  
    
    # For usage @a/controller decorators
    LOGIN_URL = '/login/' 
    
    # optional
    ADJANGO_BACKENDS_APPS = BASE_DIR / 'apps' # for management commands
    ADJANGO_FRONTEND_APPS = BASE_DIR.parent / 'frontend' / 'src' / 'apps' # for management commands
    ADJANGO_APPS_PREPATH = 'apps.'  # if apps in BASE_DIR/apps/app1,app2...
    ADJANGO_UNCAUGHT_EXCEPTION_HANDLING_FUNCTION = ... # Read about @acontroller, @controller
    ADJANGO_CONTROLLERS_LOGGER_NAME = 'global' # only for usage @a/controller decorators
    ADJANGO_CONTROLLERS_LOGGING = True # only for usage @a/controller decorators
    ADJANGO_EMAIL_LOGGER_NAME = 'email' # for send_emails_task logging
    
    MIDDLEWARE = [
        ...
        # add request.ip in views if u need
        'adjango.middleware.IPAddressMiddleware',  
        ...
    ]
    

Overview

Most functions, if available in asynchronous form, are also available in synchronous form.

Manager & Services 🛎️

A simple example and everything is immediately clear...

from adjango.fields import AManyToManyField
from adjango.managers.base import AManager
from adjango.services.base import ABaseService
from adjango.managers.polymorphic import APolymorphicManager


class User(AbstractUser, ABaseService):
    objects = AManager()

class Product(Model, ABaseService):
    objects = AManager()
    # objects = APolymorphicManager() # ya u can
    name = CharField(max_length=100)

class Order(Model, ABaseService):
    objects = AManager()
    user = ForeignKey(User, CASCADE)
    products = AManyToManyField(Product)

# The following is now possible...
products = await Product.objects.aall()
products = await Product.objects.afilter(name='name')
# Returns an object or None if not found
order = await Order.objects.agetorn(id=69) # aget or none
if not order: raise

# We install products in the order
await order.products.aset(products)
# Or queryset right away...
await order.products.aset(
  Product.objects.filter(name='name') 
)
await order.products.aadd(products[0])

# We get the order again without associated objects
order: Order = await Order.objects.aget(id=69)
# Retrieve related objects asynchronously.
order.user = await order.related('user')
products = await order.products.aall()
#thk u

Utils 🔧

aall, afilter, arelated, и так далее доступны как отдельные функции

from adjango.utils.funcs import aall, agetorn, afilter, aset, aadd, arelated

Decorators 🎀

  • aforce_data

    The aforce_data decorator combines data from the GET, POST and JSON body request in request.data. This makes it easy to access all request data in one place.

  • atomic

    An asynchronous decorator that wraps function into a transactional context. If an exception occurs, all changes are rolled back.

  • acontroller/controller

    An asynchronous decorator that wraps function into a transactional context. If an exception occurs, all changes are rolled back.

    from adjango.adecorators import acontroller
    
    @acontroller(name='MyView', logger='custom_logger', log_name=True, log_time=True)
    async def my_view(request):
        pass
    
    @acontroller('OneMoreView')
    async def my_view_one_more(request):
        pass
    
    • These decorators automatically catch uncaught exceptions and log if the logger is configured ADJANGO_CONTROLLERS_LOGGER_NAME ADJANGO_CONTROLLERS_LOGGING.
    • You can also implement the interface:
      class IHandlerControllerException(ABC):
          @staticmethod
          @abstractmethod
          def handle(fn_name: str, request: WSGIRequest | ASGIRequest, e: Exception, *args, **kwargs) -> None:
              """
              An example of an exception handling function.
      
              @param fn_name: The name of the function where the exception occurred.
              @param request: The request object (WSGIRequest or ASGIRequest).
              @param e: The exception to be handled.
              @param args: Positional arguments passed to the function.
              @param kwargs: Named arguments passed to the function.
      
              @return: None
              """
              pass
      
      and use handle to get an uncaught exception:
      # settings.py
      from adjango.handlers import HCE # use my example if u need
      ADJANGO_UNCAUGHT_EXCEPTION_HANDLING_FUNCTION = HCE.handle
      

Other

  • AsyncAtomicContextManager🧘

    An asynchronous context manager for working with transactions, which ensures the atomicity of operations.

    from adjango.utils.base import AsyncAtomicContextManager
    
    async def some_function():
        async with AsyncAtomicContextManager():
            ...  
    
  • Tasker📋

    The Tasker class provides methods for scheduling tasks in Celery and Celery Beat.

    from adjango.utils.tasks import Tasker
    
    task_id = Tasker.put(
        task=my_celery_task,
        param1='value1',
        param2='value2',
        countdown=60 # The task will be completed in 60 seconds
    )
    
    from adjango.utils.tasks import Tasker
    from datetime import datetime
    
    # One-time task via Celery Beat
    Tasker.beat(
        task=my_celery_task,
        name='one_time_task',
        schedule_time=datetime(2024, 10, 10, 14, 30), # Start the task on October 10, 2024 at 14:30
        param1='value1',
        param2='value2'
    )
    
    # Periodic task via Celery Beat (every hour)
    Tasker.beat(
        task=my_celery_task,
        name='hourly_task',
        interval=3600, # The task runs every hour
        param1='value1',
        param2='value2'
    )
    
  • send_emails

    Allows you to send emails using templates and context rendering.

    from adjango.utils.mail import send_emails
    
    send_emails(
        subject='Welcome!',
        emails=('user1@example.com', 'user2@example.com'),
        template='emails/welcome.html',
        context={'user': 'John Doe'}
    )
    
    from adjango.tasks import send_emails_task
    from adjango.utils.tasks import Tasker
    
    send_emails_task.delay(
        subject='Hello!',
        emails=('user@example.com',),
        template='emails/hello.html',
        context={'message': 'Welcome to our service!'}
    )
    # or
    Tasker.put(
        task=send_emails_task,
        subject='Hello!',
        emails=('user@example.com',),
        template='emails/hello.html',
        context={'message': 'Welcome to our service!'},
        countdown=60 # The task will be completed in 5 seconds
    )
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

adjango-0.1.5.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

adjango-0.1.5-py3-none-any.whl (33.2 kB view details)

Uploaded Python 3

File details

Details for the file adjango-0.1.5.tar.gz.

File metadata

  • Download URL: adjango-0.1.5.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.6

File hashes

Hashes for adjango-0.1.5.tar.gz
Algorithm Hash digest
SHA256 cd6946895472e4c41d92a5e5409597ca30692bf366607278429dc290d7f0d811
MD5 8cd0940759ae4781fb065d1a70a8f602
BLAKE2b-256 522f1791ece6e302310c2374ff50464c06fc91fd300cb2463cc530204bcf3a25

See more details on using hashes here.

File details

Details for the file adjango-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: adjango-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 33.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.6

File hashes

Hashes for adjango-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 795c2f3423b5d184f42ab14126ce9e9cd702dcb17050e249b4f8db212552f93a
MD5 2d4bd01438eccb7e9ead74708b38b066
BLAKE2b-256 d8a36c01ec9c272908559ab9a8ac674c532faf6a2712960b38f3a8209b5ddb46

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page