def get_checksum(self): """ Returns checksum of source string, used for quick lookup. We use MD5 as it is faster than SHA1. """ if self.checksum is None: if self.template is None: self.checksum = calculate_checksum(self.get_source(), self.get_context()) else: self.checksum = calculate_checksum(None, self.get_context()) return self.checksum
def get_checksum(self): ''' Returns checksum of source string, used for quick lookup. We use MD5 as it is faster than SHA1. ''' if self.checksum is None: if self.template is None: self.checksum = calculate_checksum(self.get_source(), self.get_context()) else: self.checksum = calculate_checksum(None, self.get_context()) return self.checksum
def update_source_units(self): """Updates source for units withing same component. This is needed when editing template translation for monolingual formats. """ # Find relevant units same_source = Unit.objects.filter( translation__subproject=self.translation.subproject, context=self.context, ) # Update source and contentsum previous_source = same_source[0].source same_source.update( source=self.target, contentsum=calculate_checksum(self.source, self.context), ) same_source.filter( translated=True ).exclude( id=self.id ).update( translated=False, fuzzy=True, previous_source=previous_source, ) # Update source index, it's enough to do it for one as we index by # checksum which is same for all update_index_unit(self, True)
def update_source_units(self): """Updates source for units withing same component. This is needed when editing template translation for monolingual formats. """ # Find relevant units same_source = Unit.objects.filter( translation__subproject=self.translation.subproject, context=self.context, ) # Update source and contentsum previous_source = same_source[0].source same_source.update( source=self.target, contentsum=calculate_checksum(self.source, self.context), ) same_source.filter(translated=True).exclude(id=self.id).update( translated=False, fuzzy=True, previous_source=previous_source, ) # Update source index for unit in same_source.iterator(): update_index_unit(unit, True)
def get_contentsum(self): ''' Returns checksum of source string and context, used for quick lookup. We use MD5 as it is faster than SHA1. ''' if self.template is None: return self.get_checksum() if self.contentsum is None: self.contentsum = calculate_checksum( self.get_source(), self.get_context() ) return self.contentsum
def update_source_units(self): """Updates source for units withing same component. This is needed when editing template translation for monolingual formats. """ # Find relevant units same_source = Unit.objects.filter( translation__subproject=self.translation.subproject, context=self.context, ) # Update source and contentsum same_source.update( source=self.target, contentsum=calculate_checksum(self.source, self.context), ) # Update source index, it's enough to do it for one as we index by # checksum which is same for all update_index_unit(self, True)
def save_backend(self, request, propagate=True, gen_change=True, change_action=None, user=None): """ Stores unit to backend. Optional user parameters defines authorship of a change. """ # For case when authorship specified, use user from request if user is None: user = request.user # Update lock timestamp if request is not None: self.translation.update_lock(request.user) else: self.translation.update_lock(user) # Store to backend try: (saved, pounit) = self.translation.update_unit(self, request, user) except FileLockException: self.log_error('failed to lock backend for %s!', self) messages.error( request, _('Failed to store message in the backend, ' 'lock timeout occurred!')) return False # Handle situation when backend did not find the message if pounit is None: self.log_error('message %s disappeared!', self) messages.error( request, _('Message not found in backend storage, ' 'it is probably corrupted.')) # Try reloading from backend self.translation.check_sync(True) return False # Get old unit from database (for notifications) oldunit = Unit.objects.get(id=self.id) # Return if there was no change # We have to explicitly check for fuzzy flag change on monolingual # files, where we handle it ourselves without storing to backend if (not saved and oldunit.fuzzy == self.fuzzy and oldunit.target == self.target): # Propagate if we should if propagate: self.propagate(request, change_action) return False # Update translated flag self.translated = pounit.is_translated() # Update comments as they might have been changed (eg, fuzzy flag # removed) self.flags = pounit.get_flags() if self.translation.is_template(): self.source = self.target self.contentsum = calculate_checksum(self.source, self.context) # Save updated unit to database self.save(backend=True) # Update translation stats old_translated = self.translation.translated self.translation.update_stats() # Notify subscribed users about new translation notify_new_translation(self, oldunit, user) # Update user stats user.profile.translated += 1 user.profile.save() # Generate Change object for this change if gen_change: self.generate_change(request, user, oldunit, change_action) # Force commiting on completing translation if (old_translated < self.translation.translated and self.translation.translated == self.translation.total): self.translation.commit_pending(request) Change.objects.create(translation=self.translation, action=Change.ACTION_COMPLETE, user=user, author=user) # Update related source strings if working on a template if self.translation.is_template(): self.update_source_units(oldunit.source) # Propagate to other projects if propagate: self.propagate(request, change_action) return True