forms.py 735 Bytes
from django.forms import ModelForm

from webapp.models import Meaning


class EditMeaningForm(ModelForm):

    def __init__(self, *args, **kwargs):
        super(EditMeaningForm, self).__init__(*args, **kwargs)
        width = 50
        if self.instance and self.instance.comment:
            width = get_longest_line(self.instance.comment)
        if width > 100:
            width = 100
        self.fields['comment'].label = ''
        self.fields['comment'].widget.attrs['cols'] = width

    class Meta:
        model = Meaning
        fields = ('status', 'comment')


def get_longest_line(text):
    max_len = 0
    for line in text.split('\n'):
        if max_len < len(line):
            max_len = len(line)
    return max_len