Fix day filter
[cds-indico.git] / indico / util / mimetypes.py
blob66e996f1f9a613c44c794e482f7a106f94cd1460
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, absolute_import
19 import mimetypes
20 import re
23 _exact_mapping = {
24 'application/json': 'icon-file-css',
25 'text/css': 'icon-file-css',
26 'text/calendar': 'icon-calendar',
27 # Word
28 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'icon-file-word',
29 'application/msword': 'icon-file-word',
30 # PDF
31 'application/pdf': 'icon-file-pdf',
32 # Excel
33 'application/vnd.ms-excel': 'icon-file-excel',
34 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'icon-file-excel',
35 # Powerpoint
36 'application/vnd.ms-powerpoint': 'icon-file-presentation',
37 'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'icon-file-presentation',
38 # Archive
39 'application/x-7z-compressed': 'icon-file-zip',
40 'application/x-ace-compressed': 'icon-file-zip',
41 'application/x-rar-compressed': 'icon-file-zip',
42 'application/x-tar': 'icon-file-zip',
43 'application/zip': 'icon-file-zip',
44 # Markup Languages
45 'application/xml': 'icon-file-xml',
46 'text/xml': 'icon-file-xml',
47 'text/n3': 'icon-file-xml',
48 'text/html': 'icon-file-xml',
49 'text/sgml': 'icon-file-xml',
50 # X-separated-values
51 'text/csv': 'icon-file-spreadsheet',
52 'text/tab-separated-values': 'icon-file-spreadsheet',
55 _regex_mapping = [
56 # Archive
57 ('^application/x-bzip', 'icon-file-zip'), # matches bzip and bzip2
58 # Audio
59 ('^audio/', 'icon-file-music'),
60 # Images
61 ('^image/', 'icon-file-image'),
62 # Text
63 ('^text/', 'icon-file-text'),
64 # Video
65 ('^video/', 'icon-file-video'),
66 # OpenOffice
67 ('application/vnd\.oasis\.opendocument\.', 'icon-file-openoffice'),
68 # XML
69 ('.+/.+\+xml$', 'icon-file-xml'),
70 # JSON
71 ('.+/.+\+json$', 'icon-file-css')
73 _regex_mapping = [(re.compile(regex), icon) for regex, icon in _regex_mapping]
76 def icon_from_mimetype(mimetype, default_icon='icon-file-filled'):
77 """Gets the most suitable icon for a MIME type."""
78 mimetype = mimetype.lower()
79 try:
80 return _exact_mapping[mimetype]
81 except KeyError:
82 for pattern, icon in _regex_mapping:
83 if pattern.search(mimetype):
84 return icon
85 return default_icon
88 def register_custom_mimetypes():
89 """Registers additional extension/mimetype mappings.
91 This is used for mimetypes/extensions that are not in the official
92 mapping but useful, e.g. because indico has special handling for
93 files with that type.
94 """
95 mimetypes.add_type(b'text/markdown', b'.md')