/
util.py
713 lines (613 loc) · 30.5 KB
/
util.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# This file is part of Indico.
# Copyright (C) 2002 - 2021 CERN
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see the
# LICENSE file for more details.
import json
import os
import random
import re
import warnings
from collections import defaultdict
from contextlib import contextmanager
from copy import deepcopy
from mimetypes import guess_extension
from tempfile import NamedTemporaryFile
from zipfile import ZipFile
from flask import current_app, flash, g, redirect, request, session
from sqlalchemy import inspect
from sqlalchemy.orm import load_only, noload
from werkzeug.exceptions import BadRequest, Forbidden
from werkzeug.urls import url_parse
from indico.core import signals
from indico.core.config import config
from indico.core.errors import NoReportError, UserValueError
from indico.core.permissions import FULL_ACCESS_PERMISSION, READ_ACCESS_PERMISSION
from indico.modules.api import api_settings
from indico.modules.categories.models.roles import CategoryRole
from indico.modules.events import Event
from indico.modules.events.contributions.models.contributions import Contribution
from indico.modules.events.contributions.models.subcontributions import SubContribution
from indico.modules.events.layout import theme_settings
from indico.modules.events.models.events import EventType
from indico.modules.events.models.persons import EventPerson
from indico.modules.events.models.principals import EventPrincipal
from indico.modules.events.models.roles import EventRole
from indico.modules.events.models.static_list_links import StaticListLink
from indico.modules.events.registration.models.forms import RegistrationForm
from indico.modules.events.sessions.models.sessions import Session
from indico.modules.events.timetable.models.breaks import Break
from indico.modules.events.timetable.models.entries import TimetableEntry
from indico.modules.networks import IPNetworkGroup
from indico.modules.users import User
from indico.util.caching import memoize_request
from indico.util.fs import chmod_umask, secure_filename
from indico.util.i18n import _
from indico.util.string import strip_tags
from indico.util.user import principal_from_identifier
from indico.web.flask.templating import get_template_module
from indico.web.flask.util import send_file, url_for
from indico.web.forms.colors import get_colors
def check_event_locked(rh, event, force=False):
if (not getattr(rh, 'ALLOW_LOCKED', False) or force) and event.is_locked and request.method not in ('GET', 'HEAD'):
raise NoReportError.wrap_exc(Forbidden(_('This event has been locked so no modifications are possible.')))
def get_object_from_args(args=None):
"""Retrieve an event object from request arguments.
This utility is meant to be used in cases where the same controller
can deal with objects attached to various parts of an event which
use different URLs to indicate which object to use.
:param args: The request arguments. If unspecified,
``request.view_args`` is used.
:return: An ``(object_type, event, object)`` tuple. The event is
always the :class:`Event` associated with the object.
The object may be an `Event`, `Session`, `Contribution`
or `SubContribution`. If the object does not exist,
``(object_type, None, None)`` is returned.
"""
if args is None:
args = request.view_args
object_type = args['object_type']
event = Event.query.filter_by(id=args['confId'], is_deleted=False).first()
if event is None:
obj = None
elif object_type == 'event':
obj = event
elif object_type == 'session':
obj = Session.query.with_parent(event).filter_by(id=args['session_id']).first()
elif object_type == 'contribution':
obj = Contribution.query.with_parent(event).filter_by(id=args['contrib_id']).first()
elif object_type == 'subcontribution':
obj = (SubContribution.query
.filter(SubContribution.id == args['subcontrib_id'],
~SubContribution.is_deleted,
SubContribution.contribution.has(event=event, id=args['contrib_id'], is_deleted=False))
.first())
else:
raise ValueError(f'Unexpected object type: {object_type}')
if obj is not None:
return object_type, event, obj
else:
return object_type, None, None
def get_theme(event, override_theme_id=None):
"""Get the theme ID and whether it's an override.
This is useful for places where a user may specify a different
timetable theme. If the override theme is not valid for the
event, a message is flashed and an exception redirecting the user
to the main event page is raised.
:raise BadRequest: if the override theme id is not valid
:return: a ``(theme_id, is_override)`` tuple
"""
if not override_theme_id:
return event.theme, False
elif override_theme_id == 'default':
theme = theme_settings.defaults[event.type]
return theme, theme != event.theme
elif override_theme_id in theme_settings.get_themes_for(event.type_.name):
return override_theme_id, override_theme_id != event.theme
else:
if override_theme_id in theme_settings.themes:
flash(_("The theme '{}' is not valid for this event.").format(override_theme_id))
else:
flash(_("The theme '{}' does not exist.").format(override_theme_id))
raise BadRequest(response=redirect(event.url))
def get_events_managed_by(user, dt=None):
"""Get the IDs of events where the user has management privs.
:param user: A `User`
:param dt: Only include events taking place on/after that date
:return: A set of event ids
"""
query = (user.in_event_acls
.join(Event)
.options(noload('user'), noload('local_group'), load_only('event_id'))
.filter(~Event.is_deleted, Event.ends_after(dt))
.filter(EventPrincipal.has_management_permission('ANY')))
return {principal.event_id for principal in query}
def get_events_created_by(user, dt=None):
"""Get the IDs of events created by the user.
:param user: A `User`
:param dt: Only include events taking place on/after that date
:return: A set of event ids
"""
query = (user.created_events
.filter(~Event.is_deleted, Event.ends_after(dt))
.options(load_only('id')))
return {event.id for event in query}
def get_events_with_linked_event_persons(user, dt=None):
"""
Return a dict containing the event ids and role for all events
where the user is a chairperson or (in case of a lecture) speaker.
:param user: A `User`
:param dt: Only include events taking place on/after that date
"""
query = (user.event_persons
.with_entities(EventPerson.event_id, Event._type)
.join(Event, Event.id == EventPerson.event_id)
.filter(EventPerson.event_links.any())
.filter(~Event.is_deleted, Event.ends_after(dt)))
return {event_id: ('lecture_speaker' if event_type == EventType.lecture else 'conference_chair')
for event_id, event_type in query}
def get_random_color(event):
breaks = Break.query.filter(Break.timetable_entry.has(event=event))
used_colors = {s.colors for s in event.sessions} | {b.colors for b in breaks}
unused_colors = set(get_colors()) - used_colors
return random.choice(tuple(unused_colors) or get_colors())
def serialize_event_person(person):
"""Serialize EventPerson to JSON-like object."""
return {'_type': 'EventPerson',
'id': person.id,
'email': person.email,
'name': person.display_full_name,
'firstName': person.first_name,
'familyName': person.last_name,
'title': person.title,
'affiliation': person.affiliation,
'phone': person.phone,
'address': person.address,
'user_id': person.user_id}
def serialize_person_link(person_link):
"""Serialize PersonLink to JSON-like object."""
data = {'email': person_link.person.email,
'name': person_link.display_full_name,
'fullName': person_link.display_full_name,
'firstName': person_link.first_name,
'familyName': person_link.last_name,
'title': person_link.title,
'affiliation': person_link.affiliation,
'phone': person_link.phone,
'address': person_link.address,
'displayOrder': person_link.display_order,
'userId': person_link.person.user_id}
if person_link.person.id is not None:
# In case of a newly added person we only serialize the data again
# if the form's validation failed and the field needs to be displayed
# with the same data. However, now we don't have a person ID since
# the person was never created, so if we send an ID/type in the
# serialized data it'll be sent back and cause an error as it'll appear
# as an existing person instead of a new one when submitting the form
# again.
data.update({'_type': 'PersonLink',
'id': person_link.person.id,
'personId': person_link.person.id})
return data
def update_object_principals(obj, new_principals, read_access=False, full_access=False, permission=None):
"""Update an object's ACL with a new list of principals.
Exactly one argument out of `read_access`, `full_access` and `role` must be specified.
:param obj: The object to update. Must have ``acl_entries``
:param new_principals: The set containing the new principals
:param read_access: Whether the read access ACL should be updated
:param full_access: Whether the full access ACL should be updated
:param permission: The role ACL that should be updated
"""
if read_access + full_access + bool(permission) != 1:
raise ValueError('Only one ACL property can be specified')
if full_access:
existing = {acl.principal for acl in obj.acl_entries if acl.full_access}
grant = {'full_access': True}
revoke = {'full_access': False}
elif read_access:
existing = {acl.principal for acl in obj.acl_entries if acl.read_access}
grant = {'read_access': True}
revoke = {'read_access': False}
elif permission:
existing = {acl.principal
for acl in obj.acl_entries
if acl.has_management_permission(permission, explicit=True)}
grant = {'add_permissions': {permission}}
revoke = {'del_permissions': {permission}}
new_principals = set(new_principals)
added = new_principals - existing
removed = existing - new_principals
for principal in added:
obj.update_principal(principal, **grant)
for principal in removed:
obj.update_principal(principal, **revoke)
return {'added': added, 'removed': removed}
class ListGeneratorBase:
"""Base class for classes performing actions on Indico object lists.
:param event: The associated `Event`
:param entry_parent: The parent of the entries of the list. If it's None,
the parent is assumed to be the event itself.
"""
#: The endpoint of the list management page
endpoint = None
#: Unique list identifier
list_link_type = None
#: The default list configuration dictionary
default_list_config = None
def __init__(self, event, entry_parent=None):
#: The event the list is associated with
self.event = event
#: The parent object of the list items
self.entry_parent = entry_parent or event
#: Columns that originate from the list item's properties,
#: relationships etc, but not from user defined fields (e.g.
#: registration/contribution fields)
self.static_items = {}
self.extra_filters = {}
self.static_link_used = 'config' in request.args
def _get_config_session_key(self):
"""Compose the unique configuration ID.
This ID will be used as a key to set the list's configuration to the
session.
"""
return f'{self.list_link_type}_config_{self.entry_parent.id}'
def _get_config(self):
"""Load the list's configuration from the DB and return it."""
session_key = self._get_config_session_key()
if self.static_link_used:
uuid = request.args['config']
configuration = StaticListLink.load(self.event, self.list_link_type, uuid)
if configuration and configuration['entry_parent_id'] == self.entry_parent.id:
session[session_key] = configuration['data']
return session.get(session_key, self.default_list_config)
def _split_item_ids(self, item_ids, separator_type=None):
"""Separate the dynamic item ids from the static.
:param item_ids: The list of ids to be splitted.
:param separator_type: The type of the item to base the partitioning on.
If the value is 'dynamic', the function will
return a tuple where the first element is a list
of the dynamic item IDs and the second element a
list of the rest item IDs. If the value is
'static', the first element of the returned
tuple will be a list of the static item IDs.
:return: A tuple of 2 lists as a result of the item_ids list
partitioning.
"""
if separator_type == 'dynamic':
dynamic_item_ids = [item_id for item_id in item_ids if not isinstance(item_id, str)]
return dynamic_item_ids, [item_id for item_id in item_ids if item_id not in dynamic_item_ids]
elif separator_type == 'static':
static_item_ids = [item_id for item_id in item_ids if item_id in self.static_items]
return static_item_ids, [item_id for item_id in item_ids if item_id not in static_item_ids]
return item_ids,
def _build_query(self):
"""Return the query of the list's entries.
The query should not take into account the user's filtering
configuration, for example::
return Contribution.query.with_parent(self.event)
"""
raise NotImplementedError
def _filter_list_entries(self, query, filters):
"""Apply user's filters to query and return it."""
raise NotImplementedError
def _get_filters_from_request(self):
"""Get the new filters after the filter form is submitted."""
def get_selected_options(item_id, item):
if item.get('filter_choices') or item.get('type') == 'bool':
return [x if x != 'None' else None for x in request.form.getlist(f'field_{item_id}')]
filters = deepcopy(self.default_list_config['filters'])
for item_id, item in self.static_items.items():
options = get_selected_options(item_id, item)
if options:
filters['items'][item_id] = options
for item_id, item in self.extra_filters.items():
options = get_selected_options(item_id, item)
if options:
filters['extra'][item_id] = options
return filters
def get_list_url(self, uuid=None, external=False):
"""Return the URL of the list management page."""
return url_for(self.endpoint, self.entry_parent, config=uuid, _external=external)
def generate_static_url(self):
"""Return a URL with a uuid referring to the list's configuration."""
session_key = self._get_config_session_key()
configuration = {
'entry_parent_id': self.entry_parent.id,
'data': session.get(session_key)
}
if configuration['data']:
link = StaticListLink.create(self.event, self.list_link_type, configuration)
return self.get_list_url(uuid=link.uuid, external=True)
else:
return self.get_list_url(external=True)
def store_configuration(self):
"""Load the filters from the request and store them in the session."""
filters = self._get_filters_from_request()
session_key = self._get_config_session_key()
self.list_config = session.setdefault(session_key, {})
self.list_config['filters'] = filters
if request.values.get('visible_items'):
visible_items = json.loads(request.values['visible_items'])
self.list_config['items'] = sorted(visible_items)
session.modified = True
def flash_info_message(self, obj):
raise NotImplementedError
def get_base_ical_parameters(user, detail, path, params=None):
"""Return a dict of all parameters expected by iCal template."""
from indico.web.http_api.util import generate_public_auth_request
api_mode = api_settings.get('security_mode')
persistent_allowed = api_settings.get('allow_persistent')
api_key = user.api_key if user else None
persistent_user_enabled = api_key.is_persistent_allowed if api_key else None
tpl = get_template_module('api/_messages.html')
persistent_agreement = tpl.get_ical_persistent_msg()
top_urls = generate_public_auth_request(api_key, path, params)
urls = generate_public_auth_request(api_key, path, dict(params or {}, detail=detail))
request_urls = {
'publicRequestURL': top_urls['publicRequestURL'],
'authRequestURL': top_urls['authRequestURL'],
'publicRequestDetailedURL': urls['publicRequestURL'],
'authRequestDetailedURL': urls['authRequestURL']
}
return {'api_mode': api_mode, 'api_key': api_key, 'persistent_allowed': persistent_allowed,
'persistent_user_enabled': persistent_user_enabled, 'api_active': api_key is not None,
'api_key_user_agreement': tpl.get_ical_api_key_msg(), 'api_persistent_user_agreement': persistent_agreement,
'user_logged': user is not None, 'request_urls': request_urls}
def create_event_logo_tmp_file(event, tmpdir=None):
"""Create a temporary file with the event's logo.
If `tmpdir` is specified, the logo file is created in there and
a path relative to that directory is returned.
"""
logo_meta = event.logo_metadata
logo_extension = guess_extension(logo_meta['content_type']) or os.path.splitext(logo_meta['filename'])[1]
temp_file = NamedTemporaryFile(delete=False, dir=(tmpdir or config.TEMP_DIR), suffix=logo_extension)
temp_file.write(event.logo)
temp_file.flush()
return os.path.relpath(temp_file.name, tmpdir) if tmpdir else temp_file.name
@contextmanager
def track_time_changes(auto_extend=False, user=None):
"""Track time changes of event objects.
This provides a list of changes while the context manager was
active and also triggers `times_changed` signals.
If the code running inside the ``with`` block of this context
manager raises an exception, no signals will be triggered.
:param auto_extend: Whether entry parents will get their boundaries
automatically extended or not. Passing ``'start'`` will
extend only start datetime, ``'end'`` to extend only
end datetime.
:param user: The `User` that will trigger time changes.
"""
if auto_extend:
assert user is not None
if 'old_times' in g:
raise RuntimeError('time change tracking may not be nested')
g.old_times = defaultdict(dict)
changes = defaultdict(dict)
try:
yield changes
except Exception:
del g.old_times
raise
else:
if auto_extend:
by_start = auto_extend in (True, 'start')
by_end = auto_extend in (True, 'end')
initial_changes = set(g.old_times)
# g.old_times changes during iteration
for obj in list(g.old_times):
if not isinstance(obj, Event):
obj.extend_parent(by_start=by_start, by_end=by_end)
cascade_changes = set(g.old_times) - initial_changes
for obj in cascade_changes:
if isinstance(obj, Event):
if not obj.can_manage(user):
# TODO: raise Forbidden exceptions after adding protection check in the UI
raise UserValueError(_("Your action requires modification of event boundaries, but you are "
"not authorized to manage the event."))
elif not obj.object.can_manage(user):
# TODO: raise Forbidden exceptions after adding protection check in the UI
raise UserValueError(_("Your action requires modification of session block boundaries, but you are "
"not authorized to manage the session block."))
old_times = g.pop('old_times')
for obj, info in old_times.items():
if isinstance(obj, TimetableEntry):
obj = obj.object
if obj.start_dt != info['start_dt']:
changes[obj]['start_dt'] = (info['start_dt'], obj.start_dt)
if obj.duration != info['duration']:
changes[obj]['duration'] = (info['duration'], obj.duration)
if obj.end_dt != info['end_dt']:
changes[obj]['end_dt'] = (info['end_dt'], obj.end_dt)
for obj, obj_changes in changes.items():
entry = None if isinstance(obj, Event) else obj.timetable_entry
signals.event.times_changed.send(type(obj), entry=entry, obj=obj, changes=obj_changes)
def register_time_change(entry):
"""Register a time-related change for a timetable entry.
This is an internal helper function used in the models to record
changes of the start time or duration. The changes are exposed
through the `track_time_changes` contextmanager function.
"""
# if it's a new object it's not a change so we just ignore it
if not inspect(entry).persistent:
return
try:
old_times = g.old_times
except AttributeError:
msg = f'Time change of {entry} was not tracked'
if current_app.config.get('REPL'):
warnings.warn(msg + ' (exception converted to a warning since you are using the REPL)', stacklevel=2)
return
elif current_app.config['TESTING']:
warnings.warn(msg + ' (exception converted to a warning during tests)', stacklevel=2)
return
else:
raise RuntimeError(msg)
for field in ('start_dt', 'duration', 'end_dt'):
if old_times[entry].get(field) is None:
old_times[entry][field] = getattr(entry, field)
def register_event_time_change(event):
"""Register a time-related change for an event.
This is an internal helper function used in the model to record
changes of the start time or end time. The changes are exposed
through the `track_time_changes` contextmanager function.
"""
# if it's a new object it's not a change so we just ignore it
if not inspect(event).persistent:
return
try:
old_times = g.old_times
except AttributeError:
msg = f'Time change of {event} was not tracked'
if current_app.config.get('REPL'):
warnings.warn(msg + ' (exception converted to a warning since you are using the REPL)', stacklevel=2)
return
elif current_app.config['TESTING']:
warnings.warn(msg + ' (exception converted to a warning during tests)', stacklevel=2)
return
else:
raise RuntimeError(msg)
for field in ('start_dt', 'duration', 'end_dt'):
if old_times[event].get(field) is None:
old_times[event][field] = getattr(event, field)
def serialize_event_for_ical(event, detail_level):
from indico.modules.events.contributions.util import serialize_contribution_for_ical
fossil = 'conferenceMetadataWithContribs' if detail_level == 'contributions' else 'conferenceMetadata'
data = {'id': event.id, 'title': event.title, 'description': event.description, 'startDate': event.start_dt,
'endDate': event.end_dt, 'url': event.external_url,
'location': event.venue_name, 'roomFullname': event.room_name, 'speakers': [], '_fossil': fossil,
'contributions': []}
if detail_level == 'contributions':
data['contributions'] = [serialize_contribution_for_ical(c) for c in event.contributions]
return data
def serialize_event_for_json_ld(event, full=False):
data = {
'@context': 'http://schema.org',
'@type': 'Event',
'url': event.external_url,
'name': event.title,
'startDate': event.start_dt_local.isoformat(),
'endDate': event.end_dt_local.isoformat(),
'location': {
'@type': 'Place',
'name': event.venue_name or 'No location set',
'address': event.address or 'No address set'
}
}
if full and event.description:
data['description'] = strip_tags(event.description)
if full and event.person_links:
data['performer'] = list(map(serialize_person_for_json_ld, event.person_links))
if full and event.has_logo:
data['image'] = event.external_logo_url
return data
def serialize_person_for_json_ld(person):
return {
'@type': 'Person',
'name': person.full_name,
'affiliation': {
'@type': 'Organization',
'name': person.affiliation
}
}
def get_field_values(form_data):
"""Split the form fields between custom and static."""
fields = {x: form_data[x] for x in form_data.keys() if not x.startswith('custom_')}
custom_fields = {x: form_data[x] for x in form_data.keys() if x.startswith('custom_')}
return fields, custom_fields
def set_custom_fields(obj, custom_fields_data):
changes = {}
for custom_field_name, custom_field_value in custom_fields_data.items():
custom_field_id = int(custom_field_name[7:]) # Remove the 'custom_' part
old_value = obj.set_custom_field(custom_field_id, custom_field_value)
if old_value != custom_field_value:
changes[custom_field_name] = (old_value, custom_field_value)
return changes
def check_permissions(event, field, allow_networks=False):
for principal_fossil, permissions in field.data:
principal = principal_from_identifier(principal_fossil['identifier'],
allow_groups=True,
allow_category_roles=True,
allow_event_roles=True,
allow_emails=True,
allow_registration_forms=True,
allow_networks=allow_networks,
event_id=event.id)
if isinstance(principal, IPNetworkGroup) and set(permissions) - {READ_ACCESS_PERMISSION}:
msg = _('IP networks cannot have management permissions: {}').format(principal.name)
return msg
if isinstance(principal, RegistrationForm) and set(permissions) - {READ_ACCESS_PERMISSION}:
msg = _('Registrants cannot have management permissions: {}').format(principal.name)
return msg
if FULL_ACCESS_PERMISSION in permissions and len(permissions) != 1:
# when full access permission is set, discard rest of permissions
permissions[:] = [FULL_ACCESS_PERMISSION]
def get_event_from_url(url):
data = url_parse(url)
if not all([data.scheme, data.netloc, data.path]):
raise ValueError(_('Invalid event URL'))
event_path = re.match(r'/event/(\d+)(/|$)', data.path)
if not event_path:
raise ValueError(_('Invalid event URL'))
event_id = event_path.group(1)
event = Event.get(event_id, is_deleted=False)
if not event:
raise ValueError(_('This event does not exist'))
return event
class ZipGeneratorMixin:
"""Mixin for RHs that generate zip with files."""
def _adjust_path_length(self, segments):
"""Shorten the path length to < 260 chars.
Windows' built-in ZIP tool doesn't like files whose
total path exceeds ~260 chars. Here we progressively
shorten the total until it matches that constraint.
"""
result = []
total_len = sum(len(seg) for seg in segments) + len(segments) - 1
excess = (total_len - 255) if total_len > 255 else 0
for seg in reversed(segments):
fname, ext = os.path.splitext(seg)
cut = min(excess, (len(fname) - 10) if len(fname) > 14 else 0)
if cut:
excess -= cut
fname = fname[:-cut]
result.append(fname + ext)
return reversed(result)
def _iter_items(self, files_holder):
yield from files_holder
def _generate_zip_file(self, files_holder, name_prefix='material', name_suffix=None, return_file=False):
"""Generate a zip file containing the files passed.
:param files_holder: An iterable (or an iterable containing) object that
contains the files to be added in the zip file.
:param name_prefix: The prefix to the zip file name
:param name_suffix: The suffix to the zip file name
:param return_file: Return the temp file instead of a response
"""
temp_file = NamedTemporaryFile(suffix='.zip', dir=config.TEMP_DIR, delete=False)
with ZipFile(temp_file.name, 'w', allowZip64=True) as zip_handler:
self.used_filenames = set()
for item in self._iter_items(files_holder):
name = self._prepare_folder_structure(item)
self.used_filenames.add(name)
with item.get_local_path() as filepath:
zip_handler.write(filepath, name)
zip_file_name = f'{name_prefix}-{name_suffix}.zip' if name_suffix else f'{name_prefix}.zip'
chmod_umask(temp_file.name)
if return_file:
return temp_file
return send_file(zip_file_name, temp_file.name, 'application/zip', inline=False)
def _prepare_folder_structure(self, item):
file_name = secure_filename('{}_{}'.format(str(item.id), item.filename), item.filename)
return os.path.join(*self._adjust_path_length([file_name]))
@memoize_request
def get_all_user_roles(event, user):
event_roles = set(
EventRole.query.with_parent(event)
.filter(EventRole.members.any(User.id == user.id))
)
category_roles = set(
CategoryRole.query
.join(event.category.chain_query.subquery())
.filter(CategoryRole.members.any(User.id == user.id))
)
return event_roles, category_roles