def save(self, commit=True): gloss = super(GlossCreateForm, self).save(commit) dataset = Dataset.objects.get(id=self['dataset'].value()) for language in self.languages: glosscreate_field_name = self.gloss_create_field_prefix + language.language_code_2char annotation_idgloss_text = self[glosscreate_field_name].value() existing_annotationidglosstranslations = gloss.annotationidglosstranslation_set.filter(language=language) if existing_annotationidglosstranslations is None or len(existing_annotationidglosstranslations) == 0: annotationidglosstranslation = AnnotationIdglossTranslation(gloss=gloss, language=language, text=annotation_idgloss_text, dataset=dataset) annotationidglosstranslation.save() elif len(existing_annotationidglosstranslations) == 1: annotationidglosstranslation = existing_annotationidglosstranslations[0] annotationidglosstranslation.text = annotation_idgloss_text annotationidglosstranslation.save() else: raise Exception( "In class %s: gloss with id %s has more than one annotation idgloss translation for language %s" % (self.__class__.__name__, gloss.pk, language.name) ) gloss.creator.add(self.user) gloss.creationDate = DT.datetime.now() gloss.save() default_language = Language.objects.get(language_code_2char=DEFAULT_KEYWORDS_LANGUAGE['language_code_2char']) # create empty keywords (Keyword '' has default language) # when the newly created gloss is later edited in GlossDetailView, when the user enters new keywords, # the old keywords are removed on (via clear), so setting the initial keywords to '' here is a placeholder (kobj, created) = Keyword.objects.get_or_create(text='') trans = Translation(gloss=gloss, translation=kobj, index=0, language=default_language) trans.save() return gloss
def save(self, commit=True): gloss = super(GlossCreateForm, self).save(commit) dataset = Dataset.objects.get(id=self['dataset'].value()) for language in self.languages: glosscreate_field_name = self.gloss_create_field_prefix + language.language_code_2char annotation_idgloss_text = self[glosscreate_field_name].value() existing_annotationidglosstranslations = gloss.annotationidglosstranslation_set.filter( language=language) if existing_annotationidglosstranslations is None or len( existing_annotationidglosstranslations) == 0: annotationidglosstranslation = AnnotationIdglossTranslation( gloss=gloss, language=language, text=annotation_idgloss_text, dataset=dataset) annotationidglosstranslation.save() elif len(existing_annotationidglosstranslations) == 1: annotationidglosstranslation = existing_annotationidglosstranslations[ 0] annotationidglosstranslation.text = annotation_idgloss_text annotationidglosstranslation.save() else: raise Exception( "In class %s: gloss with id %s has more than one annotation idgloss translation for language %s" % (self.__class__.__name__, gloss.pk, language.name)) gloss.creator.add(self.user) gloss.creationDate = DT.datetime.now() gloss.save() default_language = Language.objects.get( language_code_2char=DEFAULT_KEYWORDS_LANGUAGE[ 'language_code_2char']) # create empty keywords (Keyword '' has default language) # when the newly created gloss is later edited in GlossDetailView, when the user enters new keywords, # the old keywords are removed on (via clear), so setting the initial keywords to '' here is a placeholder (kobj, created) = Keyword.objects.get_or_create(text='') trans = Translation(gloss=gloss, translation=kobj, index=0, language=default_language) trans.save() return gloss
def save(self, commit=True): gloss = super(GlossCreateForm, self).save(commit) for language in self.languages: glosscreate_field_name = self.gloss_create_field_prefix + language.language_code_2char annotation_idgloss_text = self.fields[glosscreate_field_name].value existing_annotationidglosstranslations = gloss.annotationidglosstranslation_set.filter(language=language) if existing_annotationidglosstranslations is None or len(existing_annotationidglosstranslations) == 0: annotationidglosstranslation = AnnotationIdglossTranslation(gloss=gloss, language=language, text=annotation_idgloss_text) annotationidglosstranslation.save() elif len(existing_annotationidglosstranslations) == 1: annotationidglosstranslation = existing_annotationidglosstranslations[0] annotationidglosstranslation.text = annotation_idgloss_text annotationidglosstranslation.save() else: raise Exception( "In class %s: gloss with id %s has more than one annotation idgloss translation for language %s" % (self.__class__.__name__, gloss.pk, language.name) ) gloss.creator.add(self.user) gloss.creationDate = DT.datetime.now() gloss.save() return gloss