From fb54358f3e1bad4615c1e3acf4728ba9c0e86e44 Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Fri, 3 Jul 2015 18:57:58 +0200 Subject: [PATCH] Add LinkMixin.link_event_log_data property Including linked object information is useful for most linkable objects when logging something about them in the event log. --- indico/core/db/sqlalchemy/links.py | 22 ++++++++++++++++++++++ indico/modules/attachments/logging.py | 11 +---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/indico/core/db/sqlalchemy/links.py b/indico/core/db/sqlalchemy/links.py index 87a85fc22..44d132da7 100644 --- a/indico/core/db/sqlalchemy/links.py +++ b/indico/core/db/sqlalchemy/links.py @@ -23,6 +23,7 @@ from indico.core.db import db from indico.core.db.sqlalchemy import PyIntEnum from indico.util.decorators import strict_classproperty from indico.util.struct.enum import IndicoEnum +from indico.util.string import to_unicode class LinkType(int, IndicoEnum): @@ -163,10 +164,31 @@ class LinkMixin(object): @property def link_repr(self): + """A kwargs-style string suitable for the object's repr""" info = [('link_type', self.link_type.name if self.link_type is not None else 'None')] info.extend((key, getattr(self, key)) for key in _all_columns if getattr(self, key)is not None) return ', '.join('{}={}'.format(key, value) for key, value in info) + @property + def link_event_log_data(self): + """ + Returns a dict containing information about the linked object + suitable for the event log. + + It does not return any information for an object linked to a + category or the event itself. + """ + data = {} + if self.link_type == LinkType.session: + data['Session'] = to_unicode(self.linked_object.getTitle()) + elif self.link_type == LinkType.contribution: + data['Contribution'] = to_unicode(self.linked_object.getTitle()) + elif self.link_type == LinkType.subcontribution: + obj = self.linked_object + data['Contribution'] = to_unicode(obj.getContribution().getTitle()) + data['Subcontribution'] = to_unicode(obj.getTitle()) + return data + class LinkedObjectComparator(Comparator): def __init__(self, cls): diff --git a/indico/modules/attachments/logging.py b/indico/modules/attachments/logging.py index fe150737d..4e56b3878 100644 --- a/indico/modules/attachments/logging.py +++ b/indico/modules/attachments/logging.py @@ -24,7 +24,6 @@ from indico.core import signals from indico.core.db.sqlalchemy.links import LinkType from indico.modules.attachments.models.attachments import AttachmentType from indico.modules.events.logs import EventLogKind, EventLogRealm -from indico.util.string import to_unicode def connect_log_signals(): @@ -51,17 +50,9 @@ def _ignore_category(f): def _get_folder_data(folder, for_attachment=False): - data = {} + data = folder.link_event_log_data if for_attachment and not folder.is_default: data['Folder'] = folder.title - if folder.link_type == LinkType.session: - data['Session'] = to_unicode(folder.linked_object.getTitle()) - elif folder.link_type == LinkType.contribution: - data['Contribution'] = to_unicode(folder.linked_object.getTitle()) - elif folder.link_type == LinkType.subcontribution: - obj = folder.linked_object - data['Contribution'] = to_unicode(obj.getContribution().getTitle()) - data['Subcontribution'] = to_unicode(obj.getTitle()) return data -- 2.11.4.GIT