Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Proof of concept, needs cleaning, and module rename to django-admin-h…

…acks of course
  • Loading branch information...
commit 487e4f24d0c233ec1114ffb029825c7270156be7 1 parent c8025ac
James Pic authored
2  .gitignore
View
@@ -0,0 +1,2 @@
+*pyc
+django_admin_tools_plus.egg-info/
38 admin_tools_plus/admin.py
View
@@ -0,0 +1,38 @@
+from django.contrib import admin
+
+from models import *
+from forms import *
+
+class FieldInline(admin.TabularInline):
+ model = Field
+ extra = 0
+
+class FormModelAdmin(admin.ModelAdmin):
+ inlines = (
+ FieldInline,
+ )
+
+ def get_formsets(self, request, obj=None):
+ if obj is not None:
+ for i in super(FormModelAdmin, self).get_formsets(request, obj):
+ yield i
+
+ def save_model(self, request, obj, form, change):
+ if not obj.pk:
+ fields = obj.model_admin().get_form(request).base_fields
+
+ # save a 'full' preset with all fields
+ if Form.objects.filter(contenttype=obj.contenttype).count() == 0:
+ form = Form(name='full', contenttype=obj.contenttype)
+ form.save()
+ for name, field in fields.items():
+ Field(form=form, name=name).save()
+
+ obj.save()
+
+ for name, field in fields.items():
+ Field(form=obj, name=name).save()
+ else:
+ obj.save()
+
+admin.site.register(Form, FormModelAdmin)
4 admin_tools_plus/forms.py
View
@@ -0,0 +1,4 @@
+from django import forms
+
+from models import *
+
40 admin_tools_plus/models.py
View
@@ -0,0 +1,40 @@
+from django.contrib import admin
+from django.db import models
+from django.utils.translation import ugettext as _
+from django.db.models import signals
+
+class Form(models.Model):
+ contenttype = models.ForeignKey('contenttypes.contenttype',
+ verbose_name=_('form'),
+ help_text=_('you are about to configure a form preset for this type'))
+ name = models.CharField(max_length=100, verbose_name=_('preset name'),
+ help_text=_('the name of the preset you are about to configure'))
+
+ def __unicode__(self):
+ return self.name
+
+ def model_admin(self, site=admin.site):
+ model = self.contenttype.model_class()
+ return site._registry[model]
+
+ class Meta:
+ unique_together = (
+ ('name', 'contenttype'),
+ )
+
+ def to_dict(self):
+ return {
+ 'name': self.name,
+ 'pk': self.pk,
+ 'field_set': [f.to_dict() for f in self.field_set.all()]
+ }
+
+class Field(models.Model):
+ form = models.ForeignKey('Form')
+ name = models.CharField(max_length=100)
+
+ def to_dict(self):
+ return {
+ 'pk': self.pk,
+ 'name': self.name,
+ }
19 admin_tools_plus/templates/admin/admin_tools_plus/form/change_form.html
View
@@ -0,0 +1,19 @@
+{% extends 'admin/change_form.html' %}
+
+{% block extrahead %}
+{{ block.super }}
+<style type="text/css">
+ input[name=_addanother], input[name=_save] {
+ display: none;
+ }
+</style>
+{% endblock %}
+
+{% block footer %}
+{{ block.super }}
+<script type="text/javascript">
+(function($) {
+ $('input[name=_continue]').addClass('default');
+})(django.jQuery)
+</script>
+{% endblock %}
55 admin_tools_plus/templates/admin_tools_plus/form_hack.js
View
@@ -0,0 +1,55 @@
+{% if change_view %}
+(function($) { $(document).ready(function() {
+ ChangeView = function(options) {
+ this.options = $.extend({
+ 'hi': 'lol',
+ }, options)
+ this.forms = {{ forms_json|safe }};
+ }
+
+ ChangeView.prototype = {
+ installSelect: function(main) {
+ main.prev().css('display', 'inline');
+ main.before(change_view.renderSelect());
+ this.select = main.prev();
+ cv = this;
+ this.select.change(function() {
+ for ( var i in cv.forms ) {
+ if ( cv.forms[i].pk == $(this).val() ) {
+ $('.form-row').each(function() {
+ var hide = true;
+ for ( var j in cv.forms[i].field_set ) {
+ var name = cv.forms[i].field_set[j].name;
+ if ( $(this).hasClass(name) ) {
+ hide = false;
+ }
+ }
+
+ if (hide) {
+ $(this).hide();
+ } else {
+ $(this).show();
+ }
+ });
+ }
+ }
+ });
+ },
+ renderSelect: function() {
+ var html = [
+ '<select id="_preset_change" style="display:inline">',
+ ];
+ for (var i in this.forms) {
+ html.push('<option value="' + this.forms[i].pk + '">');
+ html.push(this.forms[i].name);
+ html.push('</option>');
+ }
+
+ return html.join('');
+ }
+ }
+
+ var change_view = new ChangeView()
+ change_view.installSelect($('#content-main'));
+}); })(django.jQuery)
+{% endif %}
13 admin_tools_plus/urls.py
View
@@ -0,0 +1,13 @@
+from django.conf import settings
+from django.conf.urls.defaults import *
+from django.views import generic
+
+import views
+
+urlpatterns = patterns('',
+ url(
+ r'^form/hack/$',
+ views.FormHackView.as_view(),
+ name='admin_tools_plus_form_hack'
+ ),
+)
38 admin_tools_plus/views.py
View
@@ -0,0 +1,38 @@
+import re
+
+from django.db.models import get_model
+from django.views import generic
+from django.contrib.contenttypes.models import ContentType
+from django.utils import simplejson
+
+from models import *
+
+FORM_URL_REGEXP = r'/(?P<app>[a-z]+)/(?P<model>[a-z]+)/([0-9]+|(add))/$'
+
+class FormHackView(generic.TemplateView):
+ template_name = 'admin_tools_plus/form_hack.js'
+
+ def render_to_response(self, context):
+ return super(FormHackView, self).render_to_response(context,
+ content_type='text/javascript')
+
+ def get_context_data(self, **kwargs):
+ c = kwargs
+ url = self.request.META.get('HTTP_REFERER', '/admin/art/artist/3/')
+
+ # impossible to get the model name from urlresolvers.resolve
+ # so here goes some regexp that will break when django.contrib.admin
+ # change urls
+ m = re.search(FORM_URL_REGEXP, url)
+ if m is not None:
+ c['change_view'] = True
+ model = get_model(m.group('app'), m.group('model'))
+ ctype = ContentType.objects.get_for_model(model)
+ forms = c['forms'] = Form.objects.filter(
+ contenttype=ctype).select_related()
+
+ forms_dict = c['forms_dict'] = [f.to_dict() for f in forms]
+
+ c['forms_json'] = simplejson.dumps(forms_dict)
+
+ return c
Please sign in to comment.
Something went wrong with that request. Please try again.