class TranslationProjectForm(forms.ModelForm): if template_translation_project is not None: update = forms.BooleanField(required=False, label=_("Update against templates")) #FIXME: maybe we can detect if initialize is needed to avoid # displaying it when not relevant #initialize = forms.BooleanField(required=False, label=_("Initialize")) project = forms.ModelChoiceField( queryset=Project.objects.filter(pk=current_project.pk), initial=current_project.pk, widget=forms.HiddenInput) language = LiberalModelChoiceField( label=_("Language"), queryset=Language.objects.exclude( translationproject__project=current_project), widget=forms.Select(attrs={ 'class': 'js-select2 select2-language', }), ) class Meta: prefix = "existing_language" model = TranslationProject def process_extra_fields(self): if self.instance.pk is not None: if self.cleaned_data.get('initialize', None): self.instance.initialize() if (self.cleaned_data.get('update', None) or not self.instance.stores.count()): self.instance.update_against_templates()
class TranslationProjectForm(forms.ModelForm): language = LiberalModelChoiceField( label=_("Language"), queryset=Language.objects.all(), widget=forms.Select(attrs={'class': 'js-select2 select2-language'})) project = forms.ModelChoiceField(queryset=Project.objects.all(), widget=forms.HiddenInput()) class Meta(object): prefix = "existing_language" model = TranslationProject fields = ('language', 'project') def __init__(self, *args, **kwargs): """If this form is not bound, it must be called with an initial value for Project. """ super(TranslationProjectForm, self).__init__(*args, **kwargs) if kwargs.get("instance"): project_id = kwargs["instance"].project.pk else: project_id = kwargs["initial"]["project"] self.fields["language"].queryset = ( self.fields["language"].queryset.exclude( translationproject__project_id=project_id)) self.fields["project"].queryset = self.fields[ "project"].queryset.filter(pk=project_id) def clean(self): if not self.cleaned_data.get("id"): exists_on_disk = (self.cleaned_data["language"].code in os.listdir( self.cleaned_data["project"].get_real_path())) if exists_on_disk: errordict = dict( lang=self.cleaned_data["language"].code, path=os.path.join( self.cleaned_data["project"].get_real_path(), self.cleaned_data["language"].code)) raise forms.ValidationError( _( "Cannot create translation project for language " "'%(lang)s', path '%(path)s' already exists", errordict)) def save(self, response_url, commit=True): tp = self.instance initialize_from_templates = False if tp.id is None: initialize_from_templates = tp.can_be_inited_from_templates() tp = super(TranslationProjectForm, self).save(commit) def _enqueue_job(): queue = get_queue('default') queue.enqueue(update_translation_project, tp, initialize_from_templates, response_url) connection.on_commit(_enqueue_job) return tp
class TranslationProjectForm(forms.ModelForm): language = LiberalModelChoiceField( label=_("Language"), queryset=Language.objects.all(), widget=forms.Select( attrs={ 'class': 'js-select2 select2-language'})) project = forms.ModelChoiceField( queryset=Project.objects.all(), widget=forms.HiddenInput()) class Meta(object): prefix = "existing_language" model = TranslationProject fields = ('language', 'project') def __init__(self, *args, **kwargs): """If this form is not bound, it must be called with an initial value for Project. """ super(TranslationProjectForm, self).__init__(*args, **kwargs) if kwargs.get("instance"): project_id = kwargs["instance"].project.pk else: project_id = kwargs["initial"]["project"] self.fields["language"].queryset = ( self.fields["language"].queryset.exclude( translationproject__project_id=project_id)) self.fields["project"].queryset = self.fields[ "project"].queryset.filter(pk=project_id) def save(self, response_url, commit=True): tp = self.instance initialize_from_templates = False if tp.id is None: initialize_from_templates = tp.can_be_inited_from_templates() tp = super(TranslationProjectForm, self).save(commit) def _enqueue_job(): queue = get_queue('default') queue.enqueue(update_translation_project, tp, initialize_from_templates, response_url) connection.on_commit(_enqueue_job) return tp
class TranslationProjectForm(forms.ModelForm): project = forms.ModelChoiceField( queryset=Project.objects.filter(pk=current_project.pk), initial=current_project.pk, widget=forms.HiddenInput(), ) language = LiberalModelChoiceField( label=_("Language"), queryset=Language.objects.exclude( translationproject__project=current_project), widget=forms.Select(attrs={ 'class': 'js-select2 select2-language', }), ) class Meta: prefix = "existing_language" model = TranslationProject fields = ('language', 'project')
class TranslationProjectForm(forms.ModelForm): language = LiberalModelChoiceField( label=_("Language"), queryset=Language.objects.all(), widget=forms.Select(attrs={'class': 'js-select2 select2-language'})) project = forms.ModelChoiceField(queryset=Project.objects.all(), widget=forms.HiddenInput()) fs_code = forms.CharField(label=_("Filesystem language code"), required=False) class Meta(object): prefix = "existing_language" model = TranslationProject fields = ('language', 'project') def __init__(self, *args, **kwargs): """If this form is not bound, it must be called with an initial value for Project. """ super(TranslationProjectForm, self).__init__(*args, **kwargs) if kwargs.get("instance"): project_id = kwargs["instance"].project.pk project = kwargs["instance"].project language = kwargs["instance"].language mappings = project.config.get("pootle.core.lang_mapping", {}) mappings = dict((v, k) for k, v in mappings.iteritems()) mapped = mappings.get(language.code) self.fields["fs_code"].initial = mapped else: project_id = kwargs["initial"]["project"] self.fields["language"].queryset = ( self.fields["language"].queryset.exclude( translationproject__project_id=project_id)) self.fields["project"].queryset = self.fields[ "project"].queryset.filter(pk=project_id) def clean(self): project = self.cleaned_data.get("project") language = self.cleaned_data.get("language") if project and language: mapped_code = self.cleaned_data["fs_code"] mapping = project.config.get("pootle.core.lang_mapping", {}) if mapped_code: tps = project.translationproject_set.all() lang_codes = tps.values_list("language__code", flat=True) bad_fs_code = ((mapped_code in mapping.keys() and not mapping.get(mapped_code) == language.code) or mapped_code in lang_codes) if bad_fs_code: self.errors["fs_code"] = self.error_class([ _("Unable to add mapped code '%(mapped_code)s' for " "language '%(code)s'. Mapped filesystem codes must " "be unique and cannot be in use with an existing " "Translation Project") % dict(mapped_code=mapped_code, code=language.code) ]) if language.code in mapping.keys(): self.errors["language"] = self.error_class([ _("Unable to add language '%s'. " "Another language is already mapped to this code") % language.code ]) def save(self, response_url=None, commit=True): tp = self.instance initialize_from_templates = False if tp.id is None: initialize_from_templates = tp.can_be_inited_from_templates() tp = super(TranslationProjectForm, self).save(commit) project = tp.project config = ObjectConfig(project) mappings = config.get("pootle.core.lang_mapping", {}) mappings = dict((v, k) for k, v in mappings.iteritems()) if not self.cleaned_data["fs_code"]: if tp.language.code in mappings: del mappings[tp.language.code] else: mappings[tp.language.code] = self.cleaned_data["fs_code"] config["pootle.core.lang_mapping"] = dict( (v, k) for k, v in mappings.iteritems()) if initialize_from_templates: def _enqueue_job(): queue = get_queue('default') queue.enqueue(update_translation_project, tp, response_url) connection.on_commit(_enqueue_job) return tp