In jmb.core.admin.options we add a new class that subclasses helpers.ActionForm. For now we do not add any code.
by default an ActionForm is dinamically created for your model. This is used to specify a different form to add extra action fields. To use this you need override class JumboActionForm. It’s good idea define these forms in admin_forms.py or in admin/forms.py:
from jmb.core.admin.options import JumboActionForm
class NewsletterActionForm(JumboActionForm):
mailing_list = forms.ModelChoiceField(
queryset=MailingList.objects.filter(status=1),
label=_('Mailing list'),
required=False,
# This is automatically added if not specified. Used from JS to toggle extra action fields
widget=forms.Select(attrs={'id': 'mailing_list', 'class': 'extra_action_field'})
)
A dictionary to specify action and its extra fields to complete operation:
class NewsletterAdmin(ExtendibleModelAdmin):
action_form_fields = {
'clone': [],
'make_ready_to_send': [],
'make_cancel_sending': [],
'send_newsletter': ['mailing_list'],
'send_in_test_newsletter': ['mailing_list'],
'resend_newsletter': ['mailing_list'],
'resend_all_newsletter': ['mailing_list'],
}
key is the action and value can be [] if there is not any extra fields or a list of extra fields. ‘required’ it’s used to specify that field is required. Example: ‘send_newsletter’: [‘mailing_list:required’]. When user select ‘send newsletter’ action is required specify mailing_list to whom to send newsletter.
is a method of ExtendibleModelAdmin and it used to redefine action extra fields dictionary To use these methods we rewrote changelist_view in ExtendibleModelAdmin adding get_action_form_fields to extra_context and we overrode change_list.html template. In template we have added this javascript code:
{% if action_form_fields %}
<script type="text/javascript">
// it's used to toggle action extra fields
$(document).ready(function($) {
/* load page */
var action_found = false;
{% for action, extra_fields in action_form_fields.items %}
/* find selected action */
option_value = $("select[name='action'] option:selected").val();
if (option_value == '{{ action }}'){
action_found = true;
$('select.extra_action_field').hide();
$('select.extra_action_field').parent().hide();
{% if extra_fields|length > 0 %}
/* show action extra fields */
{% for field in extra_fields %}
// elimina required se presente
cleaned_field = '{{ field }}'.split(":")[0];
$('#'+cleaned_field).parent().show();
$('#'+cleaned_field).show();
{% endfor %}
{% else %}
$('select.extra_action_field').hide();
$('select.extra_action_field').parent().hide();
{% endif %}
}
{% endfor %}
if (action_found == false){
$('select.extra_action_field').hide();
$('select.extra_action_field').parent().hide();
}
/* change action */
$('select[name=action]').change(function(e){
/* set action option selected */
var option_value = this.value;
var action_found = false;
{% for action, extra_fields in action_form_fields.items %}
if (option_value == '{{ action }}'){
action_found = true;
$('select.extra_action_field').hide();
$('select.extra_action_field').parent().hide();
{% if extra_fields|length > 0 %}
/* show action extra fields */
{% for field in extra_fields %}
// elimina required se presente
cleaned_field = '{{ field }}'.split(":")[0];
$('#'+cleaned_field).parent().show();
$('#'+cleaned_field).show();
{% endfor %}
{% else %}
$('select.extra_action_field').hide();
$('select.extra_action_field').parent().hide();
{% endif %}
}
{% endfor %}
if (action_found == false){
$('select.extra_action_field').hide();
$('select.extra_action_field').parent().hide();
}
});
});
</script>
{% endif %}
permette di duplicare gli item selezionati nella changelist_view con una nuova pk. Questa action va abilitata esplicitamente nell’ExtendibleModelAdmin.
effettuare l’import dell’action:
from jmb.core.admin.options import clone_action
ridefinire o aggiungere la action tra le actions presenti:
actions = (clone_action,)