A simple reusable Django app with various mixins and utility functions.
install the package using the below command
pip install django-utils-plus
or install the development version using
pip install git://github.com/jnoortheen/django-utils-plus.git@master#egg=django-utils-plus
- clear_records
- create_admin
- test_mail
- cleardata
- create_middleware
it is convenient to keep track of all external js
libraries in project using
a package.json
. It is used to keep latest version of available packages.
The following template tags can be used to serve these packages right from CDN on production and
node_modules
during development
Alternative to standard static
template tag. When you are using external static files/libraries
like bootstrap, jquery you may want to load them from CDNs instead of managing them yourself in production.
This tag helps you to do that. When settings.DEBUG
is false, this will return paths that resolved from
package.json
to versioned unpkg.com
. Otherwise it will resolve to node_modules
locally.
like `unpkg` adds support for using https://www.jsdelivr.com/
load the template tags and use unpkg
like static
tag,
{% load static utils_plus_tags %}
<link rel="stylesheet" type="text/css" href="{% unpkg 'bootstrap/dist/css/bootstrap.min.css' %}"/>
<script src="{% unpkg 'bootstrap/dist/js/bootstrap.min.js' %}"></script>
<script src="{% unpkg 'jquery/dist/jquery.min.js' %}"></script>
- the package.json should be present in the project ROOT DIR.
- When DEBUG is True the packages must be installed and should be available already inside
node_modules
.
- login_required_middleware
An elegant and DRY way to define urlpatterns. It has easier to nest many levels deeper and still have the readability. It is just a wrapper behind the standard url(), include() methods.
This is how your urls.py may look
### urls.py ###
urlpatterns = [
url(r'^studenteditordocument/(?P<doc_pk>\d+)/edit/$', EditView.as_view(), name='edit-student-doc'),
url(r'^studenteditordocument/(?P<doc_pk>\d+)/export/$', ExportView.as_view(), name='export-editore-doc'),
url(r'^docs/$', Docs.as_view(), name='student-documents'),
url(r'^publish/$', PulishOrDelete.as_view(), {'action': 'publish'}, name="publish_document"),
url(r'^delete/$', PulishOrDelete.as_view(), name='delete_document'),
]
after using Url
### urls.py ###
from utils_plus.router import url
urlpatterns = list(
url('editor')[
url.int('doc_pk')[
url('edit', DocEditorView.as_view(), 'edit-doc'),
url('export', DocExporterView.as_view(), 'export-doc'),
]
]
+ url('docs', Docs.as_view(), 'student-documents')
+ url('publish', DeleteOrPublistDocument.as_view(), 'publish_document', action='publish')
+ url('delete', DeleteOrPublistDocument.as_view(), 'delete_document')
see tests/test_router.py
for more use cases
-
CheckDeletableModelMixin
adds ais_deletable
method which then can be used to check any affected related records before actually deleting them. originally it is copied from this gist -
ChoicesEnum
Enumerator class for use with the django ORM choices field -
QueryManager
A DRYer way to set select_related, prefetch_related & filters to queryset.- this has
first_or_create
method similar to get_or_create
- this has
from django.db import models
from utils_plus.models import QueryManager
class Post(models.Model):
author = models.ForeignKey('Author')
comments = models.ManyToManyField('Comment')
published = models.BooleanField()
pub_date = models.DateField()
# custom managers
objects = QueryManager() # equivalent to models.Manager
public_posts = QueryManager(published=True).order_by('-pub_date')
rel_objects = QueryManager().selects('author').prefetches('comments')
URL_GROUP_TRAIL_SLASH
- By default all the urls generated by this class will have trailing slash
- Set this to False in settings.py to change this behaviour
- CreateUpdateView:
- combines CreateView and UpdateView
- clone the repo and run migrations after installing dependencies
- `inv test` will run all the test for the app