|
@ -2,7 +2,7 @@ |
|
|
# kate: space-indent on; indent-width 4; replace-tabs on; |
|
|
# kate: space-indent on; indent-width 4; replace-tabs on; |
|
|
|
|
|
|
|
|
""" |
|
|
""" |
|
|
* Copyright (C) 200, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net> |
|
|
|
|
|
|
|
|
* Copyright (C) 2010, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net> |
|
|
* |
|
|
* |
|
|
* Mumble-Django is free software; you can redistribute it and/or modify |
|
|
* Mumble-Django is free software; you can redistribute it and/or modify |
|
|
* it under the terms of the GNU General Public License as published by |
|
|
* it under the terms of the GNU General Public License as published by |
|
@ -137,7 +137,31 @@ class Provider( object ): |
|
|
return method |
|
|
return method |
|
|
|
|
|
|
|
|
def register_form( self, formclass ): |
|
|
def register_form( self, formclass ): |
|
|
""" Register a Django Form class for handling teh stuffz, yoo know. """ |
|
|
|
|
|
|
|
|
""" Register a Django Form class. |
|
|
|
|
|
|
|
|
|
|
|
After registration, you will be able to retrieve an ExtJS form class |
|
|
|
|
|
definition for this form under the URL "<formname>.js". Include this |
|
|
|
|
|
script via a <script> tag just like the "api.js" for Ext.Direct. |
|
|
|
|
|
|
|
|
|
|
|
The form class will then be created as Ext.ux.<FormName> and will |
|
|
|
|
|
have a registered xtype of "formname". |
|
|
|
|
|
|
|
|
|
|
|
When registering a form, the Provider will automatically generate and |
|
|
|
|
|
export objects and methods for data transfer, so the form will be |
|
|
|
|
|
ready to use. |
|
|
|
|
|
|
|
|
|
|
|
To ensure that validation error messages are displayed properly, be |
|
|
|
|
|
sure to call Ext.QuickTips.init() somewhere in your code. |
|
|
|
|
|
|
|
|
|
|
|
In order to do extra validation, the Provider checks if your form class |
|
|
|
|
|
has a method called EXT_validate, and if so, calls that method with the |
|
|
|
|
|
request as parameter before calling is_valid() or save(). If EXT_validate |
|
|
|
|
|
returns False, the form will not be saved and an error will be returned |
|
|
|
|
|
instead. EXT_validate should update form.errors before returning False. |
|
|
|
|
|
""" |
|
|
|
|
|
if not issubclass( formclass, forms.ModelForm ): |
|
|
|
|
|
raise TypeError( "Ext.Direct provider can only handle ModelForms, '%s' is something else." % formclass.__name__ ) |
|
|
|
|
|
|
|
|
formname = formclass.__name__.lower() |
|
|
formname = formclass.__name__.lower() |
|
|
self.forms[formname] = formclass |
|
|
self.forms[formname] = formclass |
|
|
|
|
|
|
|
@ -356,6 +380,7 @@ class Provider( object ): |
|
|
|
|
|
|
|
|
items = [] |
|
|
items = [] |
|
|
clsname = self.forms[formname].__name__ |
|
|
clsname = self.forms[formname].__name__ |
|
|
|
|
|
hasfiles = False |
|
|
|
|
|
|
|
|
for fldname in self.forms[formname].base_fields: |
|
|
for fldname in self.forms[formname].base_fields: |
|
|
field = self.forms[formname].base_fields[fldname] |
|
|
field = self.forms[formname].base_fields[fldname] |
|
@ -386,6 +411,7 @@ class Provider( object ): |
|
|
"xtype": "numberfield", |
|
|
"xtype": "numberfield", |
|
|
}) |
|
|
}) |
|
|
elif isinstance( field, forms.FileField ) or isinstance( field, forms.ImageField ): |
|
|
elif isinstance( field, forms.FileField ) or isinstance( field, forms.ImageField ): |
|
|
|
|
|
hasfiles = True |
|
|
extfld.update({ |
|
|
extfld.update({ |
|
|
"xtype": "textfield", |
|
|
"xtype": "textfield", |
|
|
"inputType": "file" |
|
|
"inputType": "file" |
|
@ -412,11 +438,23 @@ class Provider( object ): |
|
|
clscode = EXT_CLASS_TEMPLATE % { |
|
|
clscode = EXT_CLASS_TEMPLATE % { |
|
|
'clsname': clsname, |
|
|
'clsname': clsname, |
|
|
'clslowername': formname, |
|
|
'clslowername': formname, |
|
|
'defaultconf': simplejson.dumps({ |
|
|
|
|
|
'items': items, |
|
|
|
|
|
'defaults': { "anchor": "-20px" }, |
|
|
|
|
|
'paramsAsHash': True, |
|
|
|
|
|
}, indent=4 ), |
|
|
|
|
|
|
|
|
'defaultconf': '{' |
|
|
|
|
|
'items:' + simplejson.dumps(items, indent=4) + ',' |
|
|
|
|
|
'fileUpload: ' + simplejson.dumps(hasfiles) + ',' |
|
|
|
|
|
'defaults: { "anchor": "-20px" },' |
|
|
|
|
|
'paramsAsHash: true,' |
|
|
|
|
|
"""buttons: [{ |
|
|
|
|
|
text: "Submit", |
|
|
|
|
|
handler: function(){ |
|
|
|
|
|
form = this.ownerCt.ownerCt; |
|
|
|
|
|
form.getForm().submit({ |
|
|
|
|
|
params: { |
|
|
|
|
|
uid: 34 |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}]""" |
|
|
|
|
|
'}', |
|
|
'apiconf': ('{' |
|
|
'apiconf': ('{' |
|
|
'load: ' + ("XD_%s.get" % clsname) + "," |
|
|
'load: ' + ("XD_%s.get" % clsname) + "," |
|
|
'submit:' + ("XD_%s.update" % clsname) + "," |
|
|
'submit:' + ("XD_%s.update" % clsname) + "," |
|
@ -438,8 +476,14 @@ class Provider( object ): |
|
|
pk = request.POST['pk'] |
|
|
pk = request.POST['pk'] |
|
|
formcls = self.forms[formname] |
|
|
formcls = self.forms[formname] |
|
|
instance = formcls.Meta.model.objects.get( pk=pk ) |
|
|
instance = formcls.Meta.model.objects.get( pk=pk ) |
|
|
|
|
|
if request.POST['extUpload'] == "true": |
|
|
|
|
|
forminst = formcls( request.POST, request.FILES, instance=instance ) |
|
|
|
|
|
else: |
|
|
forminst = formcls( request.POST, instance=instance ) |
|
|
forminst = formcls( request.POST, instance=instance ) |
|
|
if forminst.is_valid(): |
|
|
|
|
|
|
|
|
# save if either no usable validation method available or validation passes; and form.is_valid |
|
|
|
|
|
if ( not hasattr( forminst, "EXT_validate" ) or not callable( forminst.EXT_validate ) |
|
|
|
|
|
or forminst.EXT_validate( request ) ) \ |
|
|
|
|
|
and forminst.is_valid(): |
|
|
forminst.save() |
|
|
forminst.save() |
|
|
return { 'success': True } |
|
|
return { 'success': True } |
|
|
else: |
|
|
else: |
|
|