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):
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
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
)]
40 folders
= [f
for f
in folders
if f
.attachments
]
45 def get_attached_items(linked_object
, include_empty
=True, include_hidden
=True, preload_event
=False):
47 Return a structured representation of all the attachments linked
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
56 folders
= get_attached_folders(linked_object
, include_empty
=include_empty
,
57 include_hidden
=include_hidden
, preload_event
=preload_event
)
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
:
72 def get_nested_attached_items(obj
):
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.
80 from MaKaC
.conference
import Conference
, Session
, Contribution
81 attachments
= get_attached_items(obj
, include_empty
=False, include_hidden
=False)
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()
90 children
= filter(None, map(get_nested_attached_items
, nested_objects
))
92 attachments
['children'] = children
94 attachments
['object'] = obj
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
103 if isinstance(obj
, Session
) and obj
.canCoordinate(user
.as_avatar
):
105 if isinstance(obj
, Contribution
) and obj
.canUserSubmit(user
.as_avatar
):
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():