forms.py 1.02 KB
# -*- coding: utf-8 -*-
from django.forms import Textarea, TextInput

from django.utils.translation import ugettext as _
from django import forms


def hidden_id(id):
    return forms.CharField(initial=id, widget=forms.HiddenInput())


# https://djangosnippets.org/snippets/2400/
def remove_holddown(form, fields):
    """This removes the unhelpful "Hold down the...." help texts for the
    specified fields for a form."""
    remove_message = unicode(
        _('Hold down "Control", or "Command" on a Mac, '
          'to select more than one.'))
    for field in fields:
        if field in form.base_fields:
            if form.base_fields[field].help_text:
                form.base_fields[field].help_text = \
                    form.base_fields[field].help_text.replace(
                        remove_message, '').strip()
    return form


def disable_field(field):
    if type(field.widget) in (Textarea, TextInput):
        field.widget.attrs['readonly'] = 'readonly'
    else:
        field.widget.attrs['disabled'] = 'disabled'