def clean(self): super(TranslationForm, self).clean() # Check required fields required = set(('unit', 'target', 'contentsum', 'translationsum')) if not required.issubset(self.cleaned_data): return unit = self.cleaned_data['unit'] if self.cleaned_data['contentsum'] != unit.content_hash: raise ValidationError( _('Source of the message has been changed meanwhile. ' 'Please check your changes.')) if self.cleaned_data['translationsum'] != unit.get_target_hash(): raise ValidationError( _('Translation of the message has been changed meanwhile. ' 'Please check your changes.')) max_length = unit.get_max_length() for text in self.cleaned_data['target']: if len(text) > max_length: raise ValidationError(_('Translation text too long!')) if (can_review(self.user, unit.translation) and self.cleaned_data.get('review')): self.cleaned_data['state'] = int(self.cleaned_data['review']) elif self.cleaned_data['fuzzy']: self.cleaned_data['state'] = STATE_FUZZY else: self.cleaned_data['state'] = STATE_TRANSLATED
def __init__(self, user, translation, unit, *args, **kwargs): if unit is not None: kwargs['initial'] = { 'checksum': unit.checksum, 'contentsum': hash_to_checksum(unit.content_hash), 'translationsum': hash_to_checksum(unit.get_target_hash()), 'target': unit, 'fuzzy': unit.fuzzy, 'review': unit.state, } kwargs['auto_id'] = 'id_{0}_%s'.format(unit.checksum) tabindex = kwargs.pop('tabindex', 100) super(TranslationForm, self).__init__(translation, *args, **kwargs) self.user = user self.fields['target'].widget.attrs['tabindex'] = tabindex self.fields['target'].widget.profile = user.profile self.fields['review'].widget.attrs['class'] = 'review_radio' # Avoid failing validation on not translated string if args: self.fields['review'].choices.append((STATE_EMPTY, '')) self.helper = FormHelper() self.helper.form_method = 'post' self.helper.layout = Layout( Field('checksum'), Field('target'), Field('fuzzy'), Field('contentsum'), Field('translationsum'), InlineRadios('review'), ) if unit and can_review(user, unit.translation): self.fields['fuzzy'].widget = forms.HiddenInput() else: self.fields['review'].widget = forms.HiddenInput()