A set of functions related with Django
Project description
Installation
pip install django-extra-tools
Quick start
Enable django-extra-tools
INSTALLED_APPS = [
...
'django_extra_tools',
]
Install SQL functions
python manage.py migrate
StringAgg aggregate function on Django 1.4 needs monkey patch
from django_extra_tools.monkey import patch_django
patch_all()
Template filters
parse_datetime
Parse datetime from string.
{% load parse %}
{{ string_datetime|parse_datetime|date:"Y-m-d H:i" }}
parse_date
Parse date from string.
{% load parse %}
{{ string_date|parse_date|date:"Y-m-d" }}
parse_time
Parse time from string.
{% load parse %}
{{ string_time|parse_time|date:"H:i" }}
parse_duration
Parse duration (timedelta) from string.
{% load parse %}
{{ string_duration|parse_duration }}
Aggregation
First
Returns the first non-NULL item.
from django_extra_tools.db.models.aggregates import First
Table.objects.aggregate(First('col1', order_by='col2'))
Last
Returns the last non-NULL item.
from django_extra_tools.db.models.aggregates import Last
Table.objects.aggregate(Last('col1', order_by='col2'))
Median
Returns median value.
from django_extra_tools.db.models.aggregates import Median
Table.objects.aggregate(Median('col1'))
StringAgg
Combines the values as the text. Fields are separated by a “separator”.
from django_extra_tools.db.models.aggregates import StringAgg
Table.objects.aggregate(StringAgg('col1'))
Database functions
batch_qs
Returns a (start, end, total, queryset) tuple for each batch in the given queryset.
from django_extra_tools.db.models import batch_qs
qs = Table.objects.all()
start, end, total, queryset = batch_qs(qs, 10)
pg_version
Return tuple with PostgreSQL version of a specific connection.
from django_extra_tools.db.models import pg_version
version = pg_version()
HTTP Response
HttpResponseGetFile
An HTTP response class with the “download file” headers.
from django_extra_tools.http import HttpResponseGetFile
return HttpResponseGetFile(filename='file.txt', content=b'file content', content_type='file/text')
WSGI Request
get_client_ip
Get the client IP from the request.
from django_extra_tools.wsgi_request import get_client_ip
ip = get_client_ip(request)
Management
OneInstanceCommand
A management command which will be run only one instance of command with name name. No other command with name name can not be run in the same time.
from django_extra_tools.management import OneInstanceCommand
class MyCommand(OneInstanceCommand):
name = 'mycommand'
def handle_instance(self, *args, **kwargs):
# some operations
Middleware
XhrMiddleware
This middleware allows cross-domain XHR using the html5 postMessage API.
MIDDLEWARE_CLASSES = (
...
'django_extra_tools.middleware.XhrMiddleware'
)
XHR_MIDDLEWARE_ALLOWED_ORIGINS = '*'
XHR_MIDDLEWARE_ALLOWED_METHODS = ['POST', 'GET', 'OPTIONS', 'PUT', 'DELETE']
XHR_MIDDLEWARE_ALLOWED_HEADERS = ['Content-Type', 'Authorization', 'Location', '*']
XHR_MIDDLEWARE_ALLOWED_CREDENTIALS = 'true'
XHR_MIDDLEWARE_EXPOSE_HEADERS = ['Location']
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-extra-tools-0.2.0b1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf425e857cc7d15384cac400a055f26f8fd3e3d83fa0dc410736736fd483083e |
|
MD5 | c2097edb06e1510f286536467c735ecd |
|
BLAKE2b-256 | 23fd4868c3d84cee9d3d53e2395b64aa4733b934e822e30b1313630c1309b353 |
Hashes for django_extra_tools-0.2.0b1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de30ce2948572920f0eea46c7520b0dcf8a94c5cbe4bb9a22e5f0c8752bd623c |
|
MD5 | d257126b792b51ab9d7ba37f97c51227 |
|
BLAKE2b-256 | 68d727349261bd0423cf8c2f6a699440659cc4588b58e5b7bbc5e5537c91e286 |