# -*- coding: utf-8 -*- # kate: space-indent on; indent-width 4; replace-tabs on; """ * Copyright (C) 2010, Michael "Svedrin" Ziegler * * djExtDirect is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This package is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. """ try: import simplejson except ImportError: import json as simplejson import functools from django import forms from django.http import HttpResponse, Http404 from django.conf.urls.defaults import url from django.utils.safestring import mark_safe from provider import Provider # Template used for the auto-generated form classes EXT_CLASS_TEMPLATE = """ Ext.namespace('Ext.ux'); Ext.ux.%(clsname)s = function( config ){ Ext.apply( this, config ); Ext.applyIf( this, { defaults: { "anchor": "-20px" }, paramsAsHash: true, baseParams: {}, autoScroll: true, submitButtonText: "Submit" } ); Ext.applyIf( this, { buttons: [{ text: this.submitButtonText, handler: this.submit, id: '%(clsname)s_submit', scope: this }] }); var defaultconf = %(defaultconf)s; Ext.applyIf( this, defaultconf ); this.initialConfig = defaultconf; this.api = %(apiconf)s; Ext.ux.%(clsname)s.superclass.constructor.call( this ); this.form.api = this.api; this.form.paramsAsHash = true; if( typeof config.pk != "undefined" ){ this.load(); } this.form.addEvents({ 'submitSuccess': true, 'submitFailure': true }); if( typeof config.listeners != "undefined" ){ if( typeof config.listeners.submitSuccess != "undefined" ) this.form.on("submitSuccess", config.listeners.submitSuccess); if( typeof config.listeners.submitFailure != "undefined" ) this.form.on("submitFailure", config.listeners.submitFailure); } } Ext.extend( Ext.ux.%(clsname)s, Ext.form.FormPanel, { load: function(){ this.getForm().load({ params: Ext.applyIf( {pk: this.pk}, this.baseParams ) }); }, submit: function(){ this.getForm().submit({ params: Ext.applyIf( {pk: this.pk}, this.baseParams ), failure: function( form, action ){ if( action.failureType == Ext.form.Action.SERVER_INVALID && typeof action.result.errors['__all__'] != 'undefined' ){ Ext.Msg.alert( "Error", action.result.errors['__all__'] ); } form.fireEvent("submitFailure", form, action); }, success: function( form, action ){ form.fireEvent("submitSuccess", form, action); } }); }, } ); Ext.reg( '%(clslowername)s', Ext.ux.%(clsname)s ); """ # About the this.form.* lines, see # http://www.sencha.com/forum/showthread.php?96001-solved-Ext.Direct-load-data-in-extended-Form-fails-%28scope-issue%29 EXT_DYNAMICCHOICES_COMBO = """ Ext.namespace('Ext.ux'); Ext.ux.ChoicesCombo = function( config ){ Ext.apply( this, config ); Ext.applyIf( this, { displayField: this.name, valueField: this.name, hiddenName: this.name, autoSelect: false, typeAhead: true, emptyText: 'Select...', triggerAction: 'all', selectOnFocus: true, }); this.triggerAction = 'all'; this.store = new Ext.data.DirectStore({ baseParams: {'pk': this.ownerCt.pk, 'field': this.name}, directFn: this.ownerCt.api.choices, paramOrder: ['pk', 'field'], reader: new Ext.data.JsonReader({ successProperty: 'success', idProperty: this.valueField, root: 'data', fields: [this.valueField, this.displayField] }), autoLoad: true }); Ext.ux.ChoicesCombo.superclass.constructor.call( this ); }; Ext.extend( Ext.ux.ChoicesCombo, Ext.form.ComboBox, { }); Ext.reg( 'choicescombo', Ext.ux.ChoicesCombo ); """ class FormProvider(Provider): """ This class extends the provider class to handle Django forms. To export a form, register it using the ``register_form`` decorator. After registration, you will be able to retrieve an ExtJS form class definition for this form under the URL ".js". Include this script via a