forked from inveniosoftware/invenio-communities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunities.py
458 lines (391 loc) · 14.2 KB
/
communities.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016-2021 CERN.
# Copyright (C) 2023 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""Routes for community-related pages provided by Invenio-Communities."""
from copy import deepcopy
from flask import current_app, g, render_template
from flask.templating import _render
from flask_login import login_required
from invenio_i18n import lazy_gettext as _
from invenio_records_resources.services.errors import PermissionDeniedError
from invenio_vocabularies.proxies import current_service as vocabulary_service
from jinja2 import TemplateError
from invenio_communities.proxies import current_communities
from ..communities.resources.ui_schema import TypesSchema
from .decorators import pass_community
from .template_loader import CommunityThemeChoiceJinjaLoader
VISIBILITY_FIELDS = [
{
"text": "Public",
"value": "public",
"icon": "group",
"helpText": _(
"Your community is publicly accessible" " and shows up in search results."
),
},
{
"text": "Restricted",
"value": "restricted",
"icon": "lock",
"helpText": _("Your community is restricted to users" " with access."),
},
]
REVIEW_POLICY_FIELDS = [
{
"text": "Review all submissions",
"value": "closed",
"icon": "lock",
"helpText": _("All submissions to the community must be reviewed."),
},
{
"text": "Allow curators, managers and owners to publish without review",
"value": "open",
"icon": "group",
"helpText": _(
"Submissions to the community by default requires review, but curators, managers and owners can publish directly without review."
),
},
]
def render_community_theme_template(template_name_or_list, theme_brand=None, **context):
"""Render community theme."""
if theme_brand:
if isinstance(template_name_or_list, str):
loader = CommunityThemeChoiceJinjaLoader(theme_brand)
community_theme_view_env = current_app.jinja_env.overlay(loader=loader)
template = community_theme_view_env.get_or_select_template(
template_name_or_list
)
app = current_app._get_current_object()
# not ideal using the private flask function
return _render(app, template, context)
else:
raise TemplateError("Themed template path should be of type str.")
else:
templates = template_name_or_list
return render_template(templates, **context)
def _filter_roles(action, member_types, community_id, identity=None):
"""Compute current identity roles for action, member type and community."""
identity = identity or g.identity
service_config = current_communities.service.config
roles = []
for role in current_app.config["COMMUNITIES_ROLES"]:
permission = service_config.permission_policy_cls(
action,
community_id=community_id,
role=role["name"],
member_types=member_types,
)
if permission.allows(identity):
roles.append(role)
return roles
def _get_roles_can_update(community_id):
"""Get the full list of roles that current identity can update."""
return _filter_roles("members_update", {"user", "group"}, community_id)
def _get_roles_can_invite(community_id):
"""Get the full list of roles that current identity can invite."""
return dict(
user=_filter_roles("members_invite", {"user"}, community_id),
group=_filter_roles("members_add", {"group"}, community_id),
)
def communities_frontpage():
"""Communities index page."""
can_create = current_communities.service.check_permission(g.identity, "create")
return render_template(
"invenio_communities/frontpage.html",
permissions=dict(can_create=can_create),
)
def communities_search():
"""Communities search page."""
can_create = current_communities.service.check_permission(g.identity, "create")
return render_template(
"invenio_communities/search.html",
permissions=dict(can_create=can_create),
)
# Custom fields config loading
def load_custom_fields(dump_only_required=False):
"""Load custom fields configuration for communities.
@param boolean dump_only_required: keep only required fields in UI configuration
"""
conf = current_app.config
conf_ui = deepcopy(conf.get("COMMUNITIES_CUSTOM_FIELDS_UI", []))
conf_backend = {cf.name: cf for cf in conf.get("COMMUNITIES_CUSTOM_FIELDS", [])}
_vocabulary_fields = []
for section_cfg in conf_ui:
_fields = []
fields = section_cfg["fields"]
for field in fields:
field_instance = conf_backend.get(field["field"])
if getattr(field_instance, "relation_cls", None):
# add vocabulary options to field's properties
field["props"]["options"] = field_instance.options(g.identity)
_vocabulary_fields.append(field["field"])
if dump_only_required:
is_field_required = getattr(field_instance, "_field_args", {}).get(
"required"
)
if is_field_required:
_fields.append(field)
else:
_fields.append(field)
if not _fields: # do not display section if there are no fields
conf_ui.remove(section_cfg)
else:
section_cfg["fields"] = _fields
return {
"ui": conf_ui,
"vocabularies": _vocabulary_fields,
}
@login_required
def communities_new():
"""Communities creation page."""
can_create = current_communities.service.check_permission(g.identity, "create")
if not can_create:
raise PermissionDeniedError()
can_create_restricted = current_communities.service.check_permission(
g.identity, "create_restricted"
)
return render_template(
"invenio_communities/new.html",
form_config=dict(
access=dict(visibility=VISIBILITY_FIELDS),
SITE_UI_URL=current_app.config["SITE_UI_URL"],
),
custom_fields=load_custom_fields(
dump_only_required=True,
),
can_create_restricted=can_create_restricted,
)
@pass_community(serialize=True)
def communities_settings(pid_value, community, community_ui):
"""Community settings/profile page."""
permissions = community.has_permissions_to(
[
"update",
"read",
"search_requests",
"search_invites",
"manage_access",
"rename",
"delete",
"moderate",
]
)
if not permissions["can_update"]:
raise PermissionDeniedError()
_types = vocabulary_service.read_all(
g.identity,
fields=["id", "title"],
type="communitytypes",
max_records=10,
)
types_json = {
"types": [{"id": i["id"], "title": i["title"]} for i in list(_types.hits)]
}
types_serialized = TypesSchema().dump(types_json)
try:
current_communities.service.read_logo(g.identity, pid_value)
logo = True
except FileNotFoundError:
logo = False
logo_size_limit = 10**6
max_size = current_app.config["COMMUNITIES_LOGO_MAX_FILE_SIZE"]
if type(max_size) is int and max_size > 0:
logo_size_limit = max_size
return render_community_theme_template(
"invenio_communities/details/settings/profile.html",
theme_brand=community_ui.get("theme", {}).get("brand"),
community=community,
community_ui=community_ui,
has_logo=True if logo else False,
logo_quota=logo_size_limit,
types=types_serialized["types"],
permissions=permissions, # hide/show UI components
active_menu_tab="settings",
custom_fields=load_custom_fields(dump_only_required=False),
)
@pass_community(serialize=True)
def communities_requests(pid_value, community, community_ui):
"""Community requests page."""
permissions = community.has_permissions_to(
["update", "read", "search_requests", "search_invites", "moderate"]
)
if not permissions["can_search_requests"]:
raise PermissionDeniedError()
return render_community_theme_template(
"invenio_communities/details/requests/index.html",
theme_brand=community_ui.get("theme", {}).get("brand"),
community=community_ui,
permissions=permissions,
)
@pass_community(serialize=True)
def communities_settings_privileges(pid_value, community, community_ui):
"""Community settings/privileges page."""
permissions = community.has_permissions_to(
[
"update",
"read",
"search_requests",
"search_invites",
"manage_access",
"moderate",
]
)
if not permissions["can_manage_access"]:
raise PermissionDeniedError()
return render_community_theme_template(
"invenio_communities/details/settings/privileges.html",
theme_brand=community_ui.get("theme", {}).get("brand"),
community=community_ui,
form_config=dict(
access=dict(visibility=VISIBILITY_FIELDS),
),
permissions=permissions,
)
@pass_community(serialize=True)
def communities_settings_curation_policy(pid_value, community, community_ui):
"""Community settings/curation-policy page."""
permissions = community.has_permissions_to(
["update", "read", "search_requests", "manage_access", "moderate"]
)
if not permissions["can_update"]:
raise PermissionDeniedError()
return render_community_theme_template(
"invenio_communities/details/settings/curation_policy.html",
theme_brand=community_ui.get("theme", {}).get("brand"),
community=community_ui,
permissions=permissions,
form_config=dict(
access=dict(review_policy=REVIEW_POLICY_FIELDS),
),
)
@pass_community(serialize=True)
def communities_settings_pages(pid_value, community, community_ui):
"""Community settings/curation-policy page."""
permissions = community.has_permissions_to(
[
"update",
"read",
"search_requests",
"search_invites",
"manage_access",
"moderate",
]
)
if not permissions["can_update"]:
raise PermissionDeniedError()
return render_community_theme_template(
"invenio_communities/details/settings/pages.html",
theme_brand=community_ui.get("theme", {}).get("brand"),
community=community_ui,
permissions=permissions,
form_config=dict(
access=dict(review_policy=REVIEW_POLICY_FIELDS),
),
)
@pass_community(serialize=True)
def members(pid_value, community, community_ui):
"""Community members page."""
permissions = community.has_permissions_to(
[
"update",
"members_search",
"members_search_public",
"members_manage",
"read",
"search_requests",
"search_invites",
"invite_owners",
"moderate",
]
)
if not permissions["can_read"]:
raise PermissionDeniedError()
return render_community_theme_template(
"invenio_communities/details/members/members.html",
theme_brand=community_ui.get("theme", {}).get("brand"),
community=community_ui,
permissions=permissions,
roles_can_update=_get_roles_can_update(community.id),
roles_can_invite=_get_roles_can_invite(community.id),
)
@pass_community(serialize=True)
def invitations(pid_value, community, community_ui):
"""Community invitations page."""
permissions = community.has_permissions_to(
[
"update",
"read",
"search_requests",
"search_invites",
"invite_owners",
"moderate",
]
)
if not permissions["can_search_invites"]:
raise PermissionDeniedError()
return render_community_theme_template(
"invenio_communities/details/members/invitations.html",
theme_brand=community_ui.get("theme", {}).get("brand"),
community=community_ui,
roles_can_invite=_get_roles_can_invite(community.id),
permissions=permissions,
)
@pass_community(serialize=True)
def communities_about(pid_value, community, community_ui):
"""Community about page."""
permissions = community.has_permissions_to(
[
"update",
"read",
"search_requests",
"moderate",
]
)
if not permissions["can_read"]:
raise PermissionDeniedError()
return render_community_theme_template(
"invenio_communities/details/about/index.html",
theme_brand=community_ui.get("theme", {}).get("brand"),
community=community_ui,
permissions=permissions,
custom_fields_ui=load_custom_fields(dump_only_required=False)["ui"],
)
@pass_community(serialize=True)
def communities_curation_policy(pid_value, community, community_ui):
"""Community curation policy page."""
permissions = community.has_permissions_to(
[
"update",
"read",
"search_requests",
"moderate",
]
)
if not permissions["can_read"]:
raise PermissionDeniedError()
return render_community_theme_template(
"invenio_communities/details/curation_policy/index.html",
theme_brand=community_ui.get("theme", {}).get("brand"),
community=community_ui,
permissions=permissions,
)
@pass_community(serialize=False)
def community_theme_css_config(pid_value, revision, community):
"""Community brand theme view to serve css config."""
theme_config = community.data.get("theme", {}).get("config")
if theme_config is None:
template = ""
else:
template = render_template(
"invenio_communities/community_theme_template.css", theme=theme_config
)
return (
template,
200,
{"Cache-control": "max-age 1 year", "Content-Type": "text/css; charset=utf-8"},
)