Skip to main content

A set of functions related with Django

Project description

https://travis-ci.org/tomi77/django-extra-tools.svg?branch=master https://coveralls.io/repos/github/tomi77/django-extra-tools/badge.svg Code Climate

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

Model mixins

CreatedAtMixin

Add created_at field to model.

from django.db import models
from django_extra_tools.db.models import timestampable

class MyModel(timestampable.CreatedAtMixin, models.Model):
    pass

model = MyModel()
print(model.created_at)

CreatedByMixin

Add created_by field to model.

from django.contrib.auth.models import User
from django.db import models
from django_extra_tools.db.models import timestampable

class MyModel(timestampable.CreatedByMixin, models.Model):
    pass

user = User.objects.get(username='user')
model = MyModel(created_by=user)
print(model.created_by)

UpdatedAtMixin

Add updated_at field to model.

from django.db import models
from django_extra_tools.db.models import timestampable

class MyModel(timestampable.UpdatedAtMixin, models.Model):
    operation = models.CharField(max_length=10)

model = MyModel()
print(model.updated_at)
model.operation = 'update'
model.save()
print(model.updated_at)

UpdatedByMixin

Add updated_by field to model.

from django.contrib.auth.models import User
from django.db import models
from django_extra_tools.db.models import timestampable

class MyModel(timestampable.UpdatedByMixin, models.Model):
    operation = models.CharField(max_length=10)

user = User.objects.get(username='user')
model = MyModel()
print(model.updated_by)
model.operation = 'update'
model.save_by(user)
print(model.updated_by)

DeletedAtMixin

Add deleted_at field to model.

from django.db import models
from django_extra_tools.db.models import timestampable

class MyModel(timestampable.DeletedAtMixin, models.Model):
    pass

model = MyModel()
print(model.deleted_at)
model.delete()
print(model.deleted_at)

DeletedByMixin

Add deleted_by field to model.

from django.contrib.auth.models import User
from django.db import models
from django_extra_tools.db.models import timestampable

class MyModel(timestampable.DeletedByMixin, models.Model):
    pass

user = User.objects.get(username='user')
model = MyModel()
print(model.deleted_by)
model.delete_by(user)
print(model.deleted_by)

CreatedMixin

Add created_at and created_by fields to model.

UpdatedMixin

Add updated_at and updated_by fields to model.

DeletedMixin

Add deleted_at and deleted_by fields to model.

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 Command(OneInstanceCommand):
    name = 'mycommand'

    def handle_instance(self, *args, **options):
        # some operations

NagiosCheckCommand

A management command which perform a Nagios check.

from django_extra_tools.management import NagiosCheckCommand

class Command(NagiosCheckCommand):
    def handle_nagios_check(self, *args, **options):
        return self.STATE_OK, 'OK'

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

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

django_extra_tools-0.3.0-py2.py3-none-any.whl (18.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django_extra_tools-0.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_extra_tools-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b2b1b918912d8f62a1f560b5760057189e17ebeb18410cf8620599f0330fad67
MD5 29e147c661cfb0d2f87d7d68ef0ee433
BLAKE2b-256 4b6c1052df17e91625a563d9855adfd5335eac774927a3357e21b671101d5470

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