def custom_sort(self, results, sort_on, sort_reverse): """We need to handle some sorting for special columns, which are not sortable in the catalog... """ if getattr(self, '_custom_sort_method', None) is not None: results = self._custom_sort_method(results, sort_on, sort_reverse) elif sort_on == 'sequence_number': splitter = re.compile(r'[/\., ]') def _sortable_data(brain): """Converts the "reference" into a tuple containing integers, which are converted well. Sorting "10" and "2" as strings results in wrong order.. """ value = getattr(brain, sort_on, '') if not isinstance(value, str) and not isinstance(value, unicode): return value parts = [] for part in splitter.split(value): part = part.strip() try: part = int(part) except ValueError: pass parts.append(part) return parts results = list(results) results.sort( lambda a, b: cmp(_sortable_data(a), _sortable_data(b))) if sort_reverse: results.reverse() elif sort_on == 'reference': # Get active reference formatter registry = getUtility(IRegistry) proxy = registry.forInterface(IReferenceNumberSettings) formatter = queryAdapter(IReferenceNumberFormatter, name=proxy.formatter) results = list(results) results.sort(key=formatter.sorter) if sort_reverse: results.reverse() # custom sort for sorting on the readable fullname # of the users, contacts and inboxes elif sort_on in ('responsible', 'Creator', 'checked_out', 'issuer', 'contact'): if sort_on in ('issuer', 'contact'): sort_dict = SortHelpers().get_user_contact_sort_dict() else: sort_dict = SortHelpers().get_user_sort_dict() def _sorter(a, b): return cmp( sort_dict.get(getattr(a, sort_on, ''), getattr(a, sort_on, '')), sort_dict.get(getattr(b, sort_on, ''), getattr(b, sort_on, '')), ) results = list(results) results.sort(_sorter, reverse=sort_reverse) elif sort_on == 'review_state': states = get_translated_transitions(self.context, self.request) def _state_sorter(a, b): return cmp( states.get(getattr(a, sort_on, ''), getattr(a, sort_on, '')), states.get(getattr(b, sort_on, ''), getattr(b, sort_on, '')), ) results = list(results) results.sort(_state_sorter, reverse=sort_reverse) elif sort_on == 'task_type': types = get_translated_types(self.context, self.request) def _type_sorter(a, b): return cmp( types.get(getattr(a, sort_on, ''), getattr(a, sort_on, '')), types.get(getattr(b, sort_on, ''), getattr(b, sort_on, '')), ) results = list(results) results.sort(_type_sorter, reverse=sort_reverse) elif sort_on == 'public_trial': values = translated_public_trial_terms(self.context, self.request) def _public_trial_sorter(a, b): return cmp( values.get(getattr(a, sort_on, ''), getattr(a, sort_on, '')), values.get(getattr(b, sort_on, ''), getattr(b, sort_on, '')), ) results = list(results) results.sort(_public_trial_sorter, reverse=sort_reverse) return results
def document_modified(context, event): if IAttachmentsDeletedEvent.providedBy(event): # AttachmentsDeleted is a special kind of ObjectModified event # and is handled elsewhere - don't journalize it twice. return # we need to distinguish between "metadata modified", "file modified", # "file and metadata modified" and "public_trial modified" file_changed = False metadata_changed = False public_trial_changed = False parent = aq_parent(aq_inner(context)) for desc in event.descriptions: for attr in desc.attributes: if attr in ('file', 'message', 'IDocumentMetadata.archival_file'): file_changed = True elif attr in ('IClassification.public_trial', 'public_trial'): # Attribute name is different when changed through regular # edit form vs. edit_public_trial form, so check for both public_trial_changed = True else: metadata_changed = True if context.REQUEST.get('form.widgets.file.action', u'nochange') == u'nochange': file_changed = False if not file_changed and not metadata_changed and not public_trial_changed: # the event shouldn't be fired in this case anyway.. return if file_changed and metadata_changed: title = _(u'label_document_file_and_metadata_modified', default=u'Changed file and metadata') parent_title = _(u'label_document_file_and_metadata_modified__parent', default=u'Changed file and metadata of ' 'document ${title}', mapping=dict(title=context.title_or_id())) journal_entry_factory(context, DOCUMENT_MODIIFED_ACTION, title) journal_entry_factory(parent, DOCUMENT_MODIIFED_ACTION, parent_title) elif file_changed: title = _(u'label_document_file_modified', default=u'Changed file') parent_title = _(u'label_document_file_modified__parent', default=u'Changed file of document ${title}', mapping=dict(title=context.title_or_id())) journal_entry_factory(context, DOCUMENT_MODIIFED_ACTION, title) journal_entry_factory(parent, DOCUMENT_MODIIFED_ACTION, parent_title) elif metadata_changed: title = _(u'label_document_metadata_modified', default=u'Changed metadata') parent_title = _(u'label_document_metadata_modified__parent', default=u'Changed metadata of document ${title}', mapping=dict(title=context.title_or_id())) journal_entry_factory(context, DOCUMENT_MODIIFED_ACTION, title) journal_entry_factory(parent, DOCUMENT_MODIIFED_ACTION, parent_title) # Always create a separate journal entry on document if public_trial was # changed if public_trial_changed: translated_terms = classification.translated_public_trial_terms( context, context.REQUEST) translated_public_trial = translated_terms[context.public_trial] title = _(u'label_document_public_trial_modified', default=u'Public trial changed to "${public_trial}".', mapping=dict(public_trial=translated_public_trial)) journal_entry_factory(context, PUBLIC_TRIAL_MODIFIED_ACTION, title)
def custom_sort(self, results, sort_on, sort_reverse): """We need to handle some sorting for special columns, which are not sortable in the catalog... """ if getattr(self, '_custom_sort_method', None) is not None: results = self._custom_sort_method(results, sort_on, sort_reverse) elif sort_on == 'sequence_number': splitter = re.compile('[/\., ]') def _sortable_data(brain): """ Converts the "reference" into a tuple containing integers, which are converted well. Sorting "10" and "2" as strings results in wrong order.. """ value = getattr(brain, sort_on, '') if not isinstance(value, str) and not isinstance( value, unicode): return value parts = [] for part in splitter.split(value): part = part.strip() try: part = int(part) except ValueError: pass parts.append(part) return parts results = list(results) results.sort( lambda a, b: cmp(_sortable_data(a), _sortable_data(b))) if sort_reverse: results.reverse() elif sort_on == 'reference': # Get active reference formatter registry = getUtility(IRegistry) proxy = registry.forInterface(IReferenceNumberSettings) formatter = queryAdapter(IReferenceNumberFormatter, name=proxy.formatter) results = list(results) results.sort(key=formatter.sorter) if sort_reverse: results.reverse() # custom sort for sorting on the readable fullname # of the users, contacts and inboxes elif sort_on in ( 'responsible', 'Creator', 'checked_out', 'issuer', 'contact'): if sort_on in ('issuer', 'contact'): sort_dict = SortHelpers().get_user_contact_sort_dict() else: sort_dict = SortHelpers().get_user_sort_dict() def _sorter(a, b): return cmp( sort_dict.get( getattr(a, sort_on, ''), getattr(a, sort_on, '')), sort_dict.get( getattr(b, sort_on, ''), getattr(b, sort_on, '')) ) results = list(results) results.sort(_sorter, reverse=sort_reverse) elif sort_on == 'review_state': states = get_translated_transitions(self.context, self.request) def _state_sorter(a, b): return cmp( states.get( getattr(a, sort_on, ''), getattr(a, sort_on, '')), states.get( getattr(b, sort_on, ''), getattr(b, sort_on, '')) ) results = list(results) results.sort(_state_sorter, reverse=sort_reverse) elif sort_on == 'task_type': types = get_translated_types(self.context, self.request) def _type_sorter(a, b): return cmp( types.get( getattr(a, sort_on, ''), getattr(a, sort_on, '')), types.get(getattr(b, sort_on, ''), getattr(b, sort_on, '')) ) results = list(results) results.sort(_type_sorter, reverse=sort_reverse) elif sort_on == 'public_trial': values = translated_public_trial_terms(self.context, self.request) def _public_trial_sorter(a, b): return cmp( values.get( getattr(a, sort_on, ''), getattr(a, sort_on, '')), values.get(getattr(b, sort_on, ''), getattr(b, sort_on, '')) ) results = list(results) results.sort(_public_trial_sorter, reverse=sort_reverse) return results