def handle_term_update()

in pontoon/terminology/models.py [0:0]


    def handle_term_update(self):
        """
        Before updating an existing Term, update its Entity if neccessary
        """
        term = self
        old_term = Term.objects.get(pk=term.pk)

        # Ignore changes to non-localizable terms that stay non-localizable
        if not old_term.localizable and not term.localizable:
            return

        # If localizable term becomes non-localizable, obsolete its Entity
        if old_term.localizable and not term.localizable:
            old_term.obsolete_entity()

        # If non-localizable term becomes localizable, create a corresponding Entity
        elif not old_term.localizable and term.localizable:
            term.create_entity()

        # If relevant changes are made to the localizable term that stays localizable
        else:
            # If Term.text changes, a new Entity instance gets created and the previous one becomes obsolete.
            if old_term.text != term.text:
                old_term.obsolete_entity()
                term.create_entity()

            # If Term.part_of_speech, Term.definition or Term.usage change, Entity.comment gets updated.
            elif (
                old_term.part_of_speech != term.part_of_speech
                or old_term.definition != term.definition
                or old_term.usage != term.usage
            ):
                entity = term.entity

                # Ignore if term doesn't have entity assigned
                if entity is None:
                    return

                entity.comment = term.entity_comment()
                entity.save(update_fields=["comment"])

                return

        update_terminology_project_stats()