editlive.adaptors.base
– Base Adaptor¶
-
class
editlive.adaptors.base.
BaseAdaptor
(request, field, obj, field_name, field_value='', kwargs={})[source]¶ Bases:
object
The BaseAdaptor is an abstract class which provides all the basic methods and variable necessary to interact with and render an object field.
It provides the following functionalities:
- Renders the object’s display value (rendered with filters applied)
- Renders the editlive markup
- Get a field value
- Set a field value
- Validate field value
- Save a field value
-
kwargs
¶
Note
This class is never used directly, you must subclass it.
-
format_attributes
()[source]¶ Formats the HTML attributes of the <editlive /> element.
This method takes no argument, it only use self.kwargs to build up self.attributes then convert it to HTML attributes and return it as a string.
>>> self.format_attributes() app-label="myapp" field-name="myfieldname" rendered-value="Hello World" object-id="1" data-type="charField" data-field-id="id_myfieldname" module-name="mymodule"
Most kwargs are prefixed with data- when converted to attribute except for those:
- rendered-value
- load_tags
-
get_form
()[source]¶ Creates and returns a form on the fly from the model using modelform_factory.
The returned form is instantiated with self.obj.
-
get_real_field_name
()[source]¶ Returns the reald fieldname regardless if the field is part of a formset or not.
Formsets mangles fieldnames with positional slugs. This method returns the actual field name without the position.
>>> print self.field_name "myfieldname_set-0" >>> print self.get_real_field_name() "myfieldname"
-
get_value
()[source]¶ Returns self.field_value unless it is callable. If it is callable, it calls it before returning the output.
-
render
()[source]¶ Returns the form field along with the <editlive /> tag as string
>>> self.render() <input id="id_firstname" type="text" name="firstname" maxlength="25" /> <editlive app-label="myapp" field-name="firstname" rendered-value="John" object-id="1" data-type="charField" data-field-id="id_firstname" module-name="mymodule"></editlive>
-
render_value
(value=None)[source]¶ Returns the field value as it should be rendered on the placeholder.
-
render_widget
()[source]¶ Returns the <editlive /> HTML widget as string.
This will also set the self.attributes[‘rendered-value’].
>>> self.render_widget() <editlive app-label="myapp" field-name="firstname" rendered-value="John" object-id="1" data-type="charField" data-field-id="id_firstname" module-name="mymodule"></editlive>
-
save
()[source]¶ Saves the object to database.
A form is created on the fly for validation purpose, but only the saved field is validated.
Successful save
>>> self.set_value('john@doe.com') 'john@doe.com' >>> self.save() {'rendered_value': u'john@doe.com', 'error': False}
Validation error
>>> self.set_value('Hello world') 'Hello world' >>> self.save() {'rendered_value': u'Hello world', 'error': True, 'value': u'Hello world', 'messages': [{'message': u'Enter a valid e-mail address.', 'field_name': 'email_test'}]}