Add folder name suggestions
[cds-indico.git] / indico / modules / attachments / util.py
blob73d1a1c5a6019ef378deeab6e829d07c0f78297f
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 flask import session
22 def get_attached_folders(linked_object, include_empty=True, include_hidden=True, preload_event=False):
23 """
24 Return a list of all the folders linked to an object.
26 :param linked_object: The object whose attachments are to be returned
27 :param include_empty: Whether to return empty folders as well.
28 :param include_hidden: Include folders that the user can't see
29 :param preload_event: in the process, preload all objects tied to the
30 corresponding event and keep them in cache
31 """
32 from indico.modules.attachments.models.folders import AttachmentFolder
34 folders = AttachmentFolder.get_for_linked_object(linked_object, preload_event=preload_event)
36 if not include_hidden:
37 folders = [f for f in folders if f.can_view(session.user)]
39 if not include_empty:
40 folders = [f for f in folders if f.attachments]
42 return folders
45 def get_attached_items(linked_object, include_empty=True, include_hidden=True, preload_event=False):
46 """
47 Return a structured representation of all the attachments linked
48 to an object.
50 :param linked_object: The object whose attachments are to be returned
51 :param include_empty: Whether to return empty folders as well.
52 :param include_hidden: Include folders that the user can't see
53 :param preload_event: in the process, preload all objects tied to the
54 corresponding event and keep them in cache
55 """
56 folders = get_attached_folders(linked_object, include_empty=include_empty,
57 include_hidden=include_hidden, preload_event=preload_event)
58 if not folders:
59 return {}
61 # the default folder is never shown as a folder. instead, its
62 # files are shown on the same level as other folders
63 files = folders.pop(0).attachments if folders[0].is_default else []
64 if not files and not folders:
65 return {}
66 return {
67 'folders': folders,
68 'files': files
72 def get_nested_attached_items(obj):
73 """
74 Returns a structured representation of all attachments linked to an object
75 and all its nested objects.
77 :param obj: A :class:`Conference`, :class:`Session`, :class:`Contribution`
78 or :class:`Subcontribution` object.
79 """
80 from MaKaC.conference import Conference, Session, Contribution
81 attachments = get_attached_items(obj, include_empty=False, include_hidden=False)
82 nested_objects = []
83 if isinstance(obj, Conference):
84 nested_objects = obj.getSessionList() + obj.getContributionList()
85 elif isinstance(obj, Session):
86 nested_objects = obj.getContributionList()
87 elif isinstance(obj, Contribution):
88 nested_objects = obj.getSubContributionList()
89 if nested_objects:
90 children = filter(None, map(get_nested_attached_items, nested_objects))
91 if children:
92 attachments['children'] = children
93 if attachments:
94 attachments['object'] = obj
95 return attachments
98 def can_manage_attachments(obj, user):
99 """Checks if a user can manage attachments for the object"""
100 from MaKaC.conference import Contribution, Session, SubContribution
101 if not user:
102 return False
103 if isinstance(obj, Session) and obj.canCoordinate(user.as_avatar):
104 return True
105 if isinstance(obj, Contribution) and obj.canUserSubmit(user.as_avatar):
106 return True
107 if isinstance(obj, SubContribution):
108 return can_manage_attachments(obj.getContribution(), user)
109 return obj.canModify(user.as_avatar) or obj.getAccessController().canUserSubmit(user.as_avatar)
112 def get_default_folder_names():
113 return [
114 'Agenda',
115 'Document',
116 'Drawings',
117 'List of Actions',
118 'Minutes',
119 'Notes',
120 'Paper',
121 'Pictures',
122 'Poster',
123 'Proceedings',
124 'Recording',
125 'Slides',
126 'Summary',
127 'Text',
128 'Video',
129 'Webcast',