class FileDiffData(models.Model): """ Contains hash and base64 pairs. These pairs are used to reduce diff database storage. """ binary_hash = models.CharField(_("hash"), max_length=40, primary_key=True) binary = Base64Field(_("base64")) objects = FileDiffDataManager() extra_data = JSONField(null=True) @property def insert_count(self): return self.extra_data.get('insert_count') @insert_count.setter def insert_count(self, value): self.extra_data['insert_count'] = value @property def delete_count(self): return self.extra_data.get('delete_count') @delete_count.setter def delete_count(self, value): self.extra_data['delete_count'] = value def recalculate_line_counts(self, tool): """Recalculates the insert_count and delete_count values. This will attempt to re-parse the stored diff and fetch the line counts through the parser. """ logging.debug('Recalculating insert/delete line counts on ' 'FileDiffData %s' % self.pk) try: files = tool.get_parser(self.binary).parse() if len(files) != 1: raise DiffParserError( 'Got wrong number of files (%d)' % len(files)) except DiffParserError as e: logging.error('Failed to correctly parse stored diff data in ' 'FileDiffData ID %s when trying to get ' 'insert/delete line counts: %s', self.pk, e) else: file_info = files[0] self.insert_count = file_info.insert_count self.delete_count = file_info.delete_count if self.pk: self.save(update_fields=['extra_data'])