-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathapps.py
More file actions
167 lines (144 loc) · 5.27 KB
/
apps.py
File metadata and controls
167 lines (144 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import glob
import mimetypes
from django.apps import AppConfig
from mako.lookup import TemplateLookup
from framework.routing import process_rules
from framework.flask import app
from website import settings
from website.util import rubeus
def _is_image(filename):
mtype, _ = mimetypes.guess_type(filename)
return mtype and mtype.startswith("image")
NODE_SETTINGS_TEMPLATE_DEFAULT = os.path.join(
settings.TEMPLATES_PATH,
"project",
"addon",
"node_settings_default.mako",
)
USER_SETTINGS_TEMPLATE_DEFAULT = os.path.join(
settings.TEMPLATES_PATH,
"profile",
"user_settings_default.mako",
)
def generic_root_folder(addon_short_name):
def _root_folder(node_settings, auth, **kwargs):
"""Return the Rubeus/HGrid-formatted response for the root folder only."""
# Quit if node settings does not have authentication
if not node_settings.has_auth or not node_settings.folder_id:
return None
node = node_settings.owner
root = rubeus.build_addon_root(
node_settings=node_settings,
name=node_settings.fetch_folder_name(),
permissions=auth,
nodeUrl=node.url,
nodeApiUrl=node.api_url,
private_key=kwargs.get("view_only", None),
)
return [root]
_root_folder.__name__ = f"{addon_short_name}_root_folder"
return _root_folder
class BaseAddonAppConfig(AppConfig):
name = "addons.base"
label = "addons_base"
actions = tuple()
user_settings = None
node_settings = None
node_settings_template = NODE_SETTINGS_TEMPLATE_DEFAULT
user_settings_template = USER_SETTINGS_TEMPLATE_DEFAULT
views = []
added_default = []
added_mandatory = []
include_js = {} # TODO: Deprecate these elsewhere and remove
include_css = {} # TODO: Deprecate these elsewhere and remove
configs = []
has_hgrid_files = False
get_hgrid_data = None
max_file_size = None
accept_extensions = True
# NOTE: Subclasses may make routes a property to avoid import errors
routes = []
owners = []
categories = []
def __init__(self, *args, **kwargs):
ret = super().__init__(*args, **kwargs).__init__()
# Build template lookup
paths = [settings.TEMPLATES_PATH]
if self.user_settings_template:
paths.append(os.path.dirname(self.user_settings_template))
if self.node_settings_template:
paths.append(os.path.dirname(self.node_settings_template))
template_dirs = list({path for path in paths if os.path.exists(path)})
if template_dirs:
self.template_lookup = TemplateLookup(
directories=template_dirs,
default_filters=[
"unicode", # default filter; must set explicitly when overriding
"temp_ampersand_fixer",
# FIXME: Temporary workaround for data stored in wrong format in DB. Unescape it before it gets re-escaped by Markupsafe. See [#OSF-4432]
"h",
],
imports=[
"from website.util.sanitize import temp_ampersand_fixer",
# FIXME: Temporary workaround for data stored in wrong format in DB. Unescape it before it gets re-escaped by Markupsafe. See [#OSF-4432]
],
)
else:
self.template_lookup = None
return ret
@property
def full_name(self):
raise NotImplementedError
@property
def short_name(self):
raise NotImplementedError
@property
def icon(self):
try:
return self._icon
except Exception:
static_path = os.path.join("addons", self.short_name, "static")
static_files = glob.glob(os.path.join(static_path, "comicon.*"))
image_files = [
os.path.split(filename)[1]
for filename in static_files
if _is_image(filename)
]
if len(image_files) == 1:
self._icon = image_files[0]
else:
self._icon = None
return self._icon
@property
def icon_url(self):
return self._static_url(self.icon) if self.icon else None
def _static_url(self, filename):
"""Build static URL for file; use the current addon if relative path,
else the global static directory.
:param str filename: Local path to file
:return str: Static URL for file
"""
if filename.startswith("/"):
return filename
return "/static/addons/{addon}/{filename}".format(
addon=self.short_name,
filename=filename,
)
def to_json(self):
return {
"short_name": self.short_name,
"full_name": self.full_name,
"capabilities": self.short_name in settings.ADDON_CAPABILITIES,
"addon_capabilities": settings.ADDON_CAPABILITIES.get(
self.short_name
),
"icon": self.icon_url,
"has_page": "page" in self.views,
"has_widget": "widget" in self.views,
}
# Override Appconfig
def ready(self):
# Set up Flask routes
for route_group in self.routes:
process_rules(app, **route_group)