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 sqlalchemy
.orm
import joinedload
21 from indico
.modules
.attachments
.models
.attachments
import AttachmentType
22 from indico
.modules
.attachments
.models
.folders
import AttachmentFolder
23 from MaKaC
.common
.contextManager
import ContextManager
26 def build_material_legacy_api_data(linked_object
):
27 # Skipping root folder and files in legacy API
28 folder_query
= (AttachmentFolder
29 .find(linked_object
=linked_object
, is_deleted
=False, is_default
=False)
30 .options(joinedload(AttachmentFolder
.legacy_mapping
), joinedload(AttachmentFolder
.attachments
)))
31 return filter(None, (_build_folder_legacy_api_data(folder
) for folder
in folder_query
))
34 def _build_folder_legacy_api_data(folder
):
35 avatar
= ContextManager
.get("currentAW").getUser()
36 user
= avatar
.user
if avatar
else None
37 if not folder
.can_access(user
):
40 resources
= [_build_file_legacy_api_data(file_
)
41 for file_
in folder
.attachments
42 if file_
.can_access(user
)]
43 if not resources
: # Skipping empty folders for legacy API
47 '_type': folder
.legacy_mapping
.material_id
.title() if folder
.legacy_mapping
is not None else 'Material',
48 '_fossil': 'materialMetadata',
49 'title': folder
.title
,
50 'id': folder
.legacy_mapping
.material_id
if folder
.legacy_mapping
is not None else str(folder
.id),
51 'resources': resources
55 def _build_file_legacy_api_data(file_
):
56 data
= {'name': file_
.title
}
58 if file_
.type == AttachmentType
.file:
59 data
['_type'] = 'LocalFile'
60 data
['_fossil'] = 'localFileMetadata'
61 data
['id'] = str(file_
.id)
62 data
['fileName'] = file_
.file.filename
63 data
['url'] = file_
.absolute_download_url
65 elif file_
.type == AttachmentType
.link
:
66 data
['_type'] = 'Link'
67 data
['_fossil'] = 'linkMetadata'
68 data
['url'] = file_
.link_url