Skip to content

Commit

Permalink
Merge pull request #16 from agiliq/16-admin
Browse files Browse the repository at this point in the history
Allow easy inclusion with Admin
  • Loading branch information
shabda committed Sep 16, 2013
2 parents f8066fb + e11523e commit 15a7b28
Show file tree
Hide file tree
Showing 12 changed files with 1,568 additions and 7 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Parsley plays well with `crispy-forms` et all.
### Installation

1. pip install `parsley` (or add to your requirements.txt)
2. Because we do not add any models, add `parsley` to your `INSTALLED_APPS` is not necessary.
2. add `parsley` to your `INSTALLED_APPS` (required for static files added by mixin)

### Usage

Expand Down Expand Up @@ -64,6 +64,16 @@ Put this form inside a

Include the parsleyjs and you are good to go.


### Admin

To add parsley validations to admin, use the `ParsleyAdminMixin` with your `ModelAdmin` like so:

class StudentAdmin(ParsleyAdminMixin, admin.ModelAdmin):
pass

Note that the above mixin adds two scripts: `parsley-standalone.min.js` and `parsley.django-admin.js` to the admin media.

### Advanced Usage

In addition to the default validators if you want to add extra client side validations
Expand Down
14 changes: 14 additions & 0 deletions example/core/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.contrib import admin

from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

from parsley.mixins import ParsleyAdminMixin


class MyUserAdmin(ParsleyAdminMixin, UserAdmin):
pass


admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
5 changes: 3 additions & 2 deletions example/example/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/django_parsley/'
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
Expand Down Expand Up @@ -118,9 +118,10 @@
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'core',
'parsley',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
Expand Down
9 changes: 6 additions & 3 deletions example/example/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.conf.urls import patterns, url
from django.contrib import admin
from django.conf.urls import patterns, url, include

from core.views import HomeView

admin.autodiscover()

urlpatterns = patterns(
'core.views',

urlpatterns = patterns('core.views',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', HomeView.as_view(), name='home'),
)
14 changes: 14 additions & 0 deletions parsley/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from parsley.decorators import parsleyfy


class ParsleyAdminMixin(object):

def get_form(self, *args, **kwargs):
form = super(ParsleyAdminMixin, self).get_form(*args, **kwargs)
return parsleyfy(form)

class Media:
js = (
"parsley/js/parsley-standalone.min.js",
"parsley/js/parsley.django-admin.js",
)
85 changes: 85 additions & 0 deletions parsley/static/parsley/js/parsley-standalone.min.js

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions parsley/static/parsley/js/parsley.django-admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Parsley.js - django admin helper
*/

!function ($) {
'use strict';

$( window ).on( 'load', function () {
$( 'form' ).each( function () {
$( this ).parsley({
animate: false,
errors: {
errorsWrapper: '<ul class="errorlist"></ul>',
container: function (element, isRadioOrCheckbox) {
return $("<div />").prependTo(element.closest(".form-row"));
}
},
listeners: {
onFieldError: function (element) {
var container = element.closest(".form-row");
if (container.not(".errors")) {
container.addClass("errors");
}
}
}
});
} );
} );
}(window.jQuery || window.Zepto);
Loading

0 comments on commit 15a7b28

Please sign in to comment.