Add LinkMixin.link_event_log_data property
[cds-indico.git] / indico / modules / attachments / logging.py
blob4e56b3878e8c04d4212d634460d02c5aefc27eb1
1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 from __future__ import unicode_literals
19 from functools import wraps
21 from jinja2.filters import do_filesizeformat
23 from indico.core import signals
24 from indico.core.db.sqlalchemy.links import LinkType
25 from indico.modules.attachments.models.attachments import AttachmentType
26 from indico.modules.events.logs import EventLogKind, EventLogRealm
29 def connect_log_signals():
30 signals.attachments.folder_created.connect(_log_folder_created)
31 signals.attachments.folder_deleted.connect(_log_folder_deleted)
32 signals.attachments.folder_updated.connect(_log_folder_updated)
33 signals.attachments.attachment_created.connect(_log_attachment_created)
34 signals.attachments.attachment_deleted.connect(_log_attachment_deleted)
35 signals.attachments.attachment_updated.connect(_log_attachment_updated)
38 def _ignore_category(f):
39 """
40 Only calls the decorated function the attachment/folder is not
41 linked to a category.
42 """
43 @wraps(f)
44 def wrapper(sender, **kwargs):
45 folder = getattr(sender, 'folder', sender) # sender may be a folder or attachment here
46 if folder.link_type != LinkType.category:
47 f(sender, **kwargs)
49 return wrapper
52 def _get_folder_data(folder, for_attachment=False):
53 data = folder.link_event_log_data
54 if for_attachment and not folder.is_default:
55 data['Folder'] = folder.title
56 return data
59 def _get_attachment_data(attachment):
60 data = _get_folder_data(attachment.folder, True)
61 data['Type'] = unicode(attachment.type.title)
62 data['Title'] = attachment.title
63 if attachment.type == AttachmentType.link:
64 data['URL'] = attachment.link_url
65 else:
66 data.update({'File name': attachment.file.filename,
67 'File size': do_filesizeformat(attachment.file.size),
68 'File type': attachment.file.content_type})
69 return data
72 def _log(event, kind, msg, user, data):
73 event.log(EventLogRealm.management, kind, 'Materials', msg, user, data=data)
76 @_ignore_category
77 def _log_folder_created(folder, user, **kwargs):
78 if folder.link_type == LinkType.category:
79 return
80 event = folder.linked_object.getConference()
81 _log(event, EventLogKind.positive, 'Created folder "{}"'.format(folder.title), user, _get_folder_data(folder))
84 @_ignore_category
85 def _log_folder_deleted(folder, user, **kwargs):
86 if folder.link_type == LinkType.category:
87 return
88 event = folder.linked_object.getConference()
89 _log(event, EventLogKind.negative, 'Deleted folder "{}"'.format(folder.title), user, _get_folder_data(folder))
92 @_ignore_category
93 def _log_folder_updated(folder, user, **kwargs):
94 if folder.link_type == LinkType.category:
95 return
96 event = folder.linked_object.getConference()
97 _log(event, EventLogKind.change, 'Updated folder "{}"'.format(folder.title), user, _get_folder_data(folder))
100 @_ignore_category
101 def _log_attachment_created(attachment, user, **kwargs):
102 if attachment.folder.link_type == LinkType.category:
103 return
104 event = attachment.folder.linked_object.getConference()
105 _log(event, EventLogKind.positive, 'Added attachment "{}"'.format(attachment.title), user,
106 _get_attachment_data(attachment))
109 @_ignore_category
110 def _log_attachment_deleted(attachment, user, **kwargs):
111 if attachment.folder.link_type == LinkType.category:
112 return
113 event = attachment.folder.linked_object.getConference()
114 _log(event, EventLogKind.negative, 'Deleted attachment "{}"'.format(attachment.title), user,
115 _get_attachment_data(attachment))
118 @_ignore_category
119 def _log_attachment_updated(attachment, user, **kwargs):
120 if attachment.folder.link_type == LinkType.category:
121 return
122 event = attachment.folder.linked_object.getConference()
123 _log(event, EventLogKind.change, 'Updated attachment "{}"'.format(attachment.title), user,
124 _get_attachment_data(attachment))