def _make_shell_context(): context = {} info = [cformat('%{white!}Available objects')] add_to_context = partial(_add_to_context, context, info) add_to_context_multi = partial(_add_to_context_multi, context, info) add_to_context_smart = partial(_add_to_context_smart, context, info) # Common stdlib modules info.append(cformat('*** %{magenta!}stdlib%{reset} ***')) DATETIME_ATTRS = ('date', 'time', 'datetime', 'timedelta') ORM_ATTRS = ('joinedload', 'defaultload', 'contains_eager', 'lazyload', 'noload', 'subqueryload', 'undefer', 'undefer_group', 'load_only') add_to_context_multi([getattr(datetime, attr) for attr in DATETIME_ATTRS] + [getattr(sqlalchemy.orm, attr) for attr in ORM_ATTRS] + [itertools, re, sys, os], color='yellow') # Models info.append(cformat('*** %{magenta!}Models%{reset} ***')) models = [ cls for name, cls in sorted(db.Model._decl_class_registry.items(), key=itemgetter(0)) if hasattr(cls, '__table__') ] add_to_context_smart(models) # Tasks info.append(cformat('*** %{magenta!}Tasks%{reset} ***')) tasks = [ task for task in sorted(celery.tasks.values()) if not task.name.startswith('celery.') ] add_to_context_smart(tasks, get_name=lambda x: x.name.replace('.', '_'), color='blue!') # Plugins info.append(cformat('*** %{magenta!}Plugins%{reset} ***')) plugins = [ type(plugin) for plugin in sorted(plugin_engine.get_active_plugins().values(), key=attrgetter('name')) ] add_to_context_multi(plugins, color='yellow!') # Utils info.append(cformat('*** %{magenta!}Misc%{reset} ***')) add_to_context(celery, 'celery', doc='celery app', color='blue!') add_to_context(db, 'db', doc='sqlalchemy db interface', color='cyan!') add_to_context(now_utc, 'now_utc', doc='get current utc time', color='cyan!') add_to_context(config, 'config', doc='indico config') add_to_context(current_app, 'app', doc='flask app') add_to_context(lambda *a, **kw: server_to_utc(datetime.datetime(*a, **kw)), 'dt', doc='like datetime() but converted from localtime to utc') add_to_context(Event.get, 'EE', doc='get event by id') # Stuff from plugins signals.plugin.shell_context.send( add_to_context=add_to_context, add_to_context_multi=add_to_context_multi) return context, info
def get_matching_events(start_dt, end_dt, repeat_frequency, repeat_interval): """Get events suitable for booking linking. This finds events that overlap with an occurrence of a booking with the given dates where the user is a manager. """ occurrences = ReservationOccurrence.create_series(start_dt, end_dt, (repeat_frequency, repeat_interval)) excluded_categories = rb_settings.get('excluded_categories') return (Event.query .filter(~Event.is_deleted, ~Event.room_reservation_links.any(ReservationLink.reservation.has(Reservation.is_accepted)), db.or_(Event.happens_between(server_to_utc(occ.start_dt), server_to_utc(occ.end_dt)) for occ in occurrences), Event.timezone == config.DEFAULT_TIMEZONE, db.and_(Event.category_id != cat.id for cat in excluded_categories), Event.acl_entries.any(db.and_(EventPrincipal.type == PrincipalType.user, EventPrincipal.user_id == session.user.id, EventPrincipal.full_access))) .all())
def get_context(self): if self._context is None: self._context = context = {} self._info = [] add_to_context = partial(_add_to_context, context, self._info) add_to_context_multi = partial(_add_to_context_multi, context, self._info) add_to_context_smart = partial(_add_to_context_smart, context, self._info) # Common stdlib modules self._info.append(cformat('*** %{magenta!}stdlib%{reset} ***')) DATETIME_ATTRS = ('date', 'time', 'datetime', 'timedelta') ORM_ATTRS = ('joinedload', 'defaultload', 'contains_eager', 'lazyload', 'noload', 'subqueryload', 'undefer', 'undefer_group', 'load_only') add_to_context_multi([getattr(datetime, attr) for attr in DATETIME_ATTRS] + [getattr(sqlalchemy.orm, attr) for attr in ORM_ATTRS] + [itertools, re, sys, os], color='yellow') # Legacy Indico self._info.append(cformat('*** %{magenta!}Legacy%{reset} ***')) add_to_context_multi([Conference, ConferenceHolder], color='green') add_to_context(LocalProxy(HelperMaKaCInfo.getMaKaCInfoInstance), 'minfo', color='green') # Models self._info.append(cformat('*** %{magenta!}Models%{reset} ***')) models = [cls for name, cls in sorted(db.Model._decl_class_registry.items(), key=itemgetter(0)) if hasattr(cls, '__table__')] add_to_context_smart(models) # Tasks self._info.append(cformat('*** %{magenta!}Tasks%{reset} ***')) tasks = [task for task in sorted(celery.tasks.values()) if not task.name.startswith('celery.')] add_to_context_smart(tasks, get_name=lambda x: x.name.replace('.', '_'), color='blue!') # Plugins self._info.append(cformat('*** %{magenta!}Plugins%{reset} ***')) plugins = [type(plugin) for plugin in sorted(plugin_engine.get_active_plugins().values(), key=attrgetter('name'))] add_to_context_multi(plugins, color='yellow!') # Utils self._info.append(cformat('*** %{magenta!}Misc%{reset} ***')) add_to_context(celery, 'celery', doc='celery app', color='blue!') add_to_context(DBMgr.getInstance(), 'dbi', doc='zodb db interface', color='cyan!') add_to_context(db, 'db', doc='sqlalchemy db interface', color='cyan!') add_to_context(transaction, doc='transaction module', color='cyan!') add_to_context(now_utc, 'now_utc', doc='get current utc time', color='cyan!') add_to_context(IndicoConfigWrapper(Config.getInstance()), 'config', doc='indico config') add_to_context(current_app, 'app', doc='flask app') add_to_context(lambda *a, **kw: server_to_utc(datetime.datetime(*a, **kw)), 'dt', doc='like datetime() but converted from localtime to utc') add_to_context(lambda x: ConferenceHolder().getById(x, True), 'E', doc='get event by id (Conference)') add_to_context(Event.get, 'EE', doc='get event by id (Event)') # Stuff from plugins signals.plugin.shell_context.send(add_to_context=add_to_context, add_to_context_multi=add_to_context_multi) return self._context
def get_context(self): if self._context is None: self._context = context = {} self._info = [] add_to_context = partial(_add_to_context, context, self._info) add_to_context_multi = partial(_add_to_context_multi, context, self._info) add_to_context_smart = partial(_add_to_context_smart, context, self._info) # Common stdlib modules self._info.append(cformat('*** %{magenta!}stdlib%{reset} ***')) DATETIME_ATTRS = ('date', 'time', 'datetime', 'timedelta') ORM_ATTRS = ('joinedload', 'defaultload', 'contains_eager', 'lazyload', 'noload', 'subqueryload', 'undefer') add_to_context_multi([getattr(datetime, attr) for attr in DATETIME_ATTRS] + [getattr(sqlalchemy.orm, attr) for attr in ORM_ATTRS] + [itertools, re, sys, os], color='yellow') # Legacy Indico self._info.append(cformat('*** %{magenta!}Legacy%{reset} ***')) add_to_context_multi([Conference, ConferenceHolder, CategoryManager, Catalog, IndexesHolder], color='green') add_to_context(LocalProxy(HelperMaKaCInfo.getMaKaCInfoInstance), 'minfo', color='green') # Models self._info.append(cformat('*** %{magenta!}Models%{reset} ***')) models = [cls for name, cls in sorted(db.Model._decl_class_registry.items(), key=itemgetter(0)) if hasattr(cls, '__table__')] add_to_context_smart(models) # Tasks self._info.append(cformat('*** %{magenta!}Tasks%{reset} ***')) tasks = [task for task in sorted(celery.tasks.values()) if not task.name.startswith('celery.')] add_to_context_smart(tasks, get_name=lambda x: x.name.replace('.', '_'), color='blue!') # Plugins self._info.append(cformat('*** %{magenta!}Plugins%{reset} ***')) plugins = [type(plugin) for plugin in sorted(plugin_engine.get_active_plugins().values(), key=attrgetter('name'))] add_to_context_multi(plugins, color='yellow!') # Utils self._info.append(cformat('*** %{magenta!}Misc%{reset} ***')) add_to_context(celery, 'celery', doc='celery app', color='blue!') add_to_context(DBMgr.getInstance(), 'dbi', doc='zodb db interface', color='cyan!') add_to_context(db, 'db', doc='sqlalchemy db interface', color='cyan!') add_to_context(transaction, doc='transaction module', color='cyan!') add_to_context(now_utc, 'now_utc', doc='get current utc time', color='cyan!') add_to_context(IndicoConfigWrapper(Config.getInstance()), 'config', doc='indico config') add_to_context(current_app, 'app', doc='flask app') add_to_context(lambda *a, **kw: server_to_utc(datetime.datetime(*a, **kw)), 'dt', doc='like datetime() but converted from localtime to utc') add_to_context(lambda x: ConferenceHolder().getById(x, True), 'E', doc='get event by id (Conference)') add_to_context(Event.get, 'EE', doc='get event by id (Event)') # Stuff from plugins signals.plugin.shell_context.send(add_to_context=add_to_context, add_to_context_multi=add_to_context_multi) return self._context
def is_booking_start_within_grace_period(start_dt, user, allow_admin=False): if allow_admin and rb_is_admin(user): return True default_tz = pytz.timezone(config.DEFAULT_TIMEZONE) start_dt_localized = default_tz.localize(start_dt) grace_period = rb_settings.get('grace_period') if grace_period is None: today = server_to_utc(datetime.now()).astimezone(default_tz).date() return start_dt_localized.date() >= today start_dt_utc = start_dt_localized.astimezone(pytz.utc) grace_period = timedelta(hours=grace_period) return start_dt_utc >= now_utc() - grace_period
def is_booking_start_within_grace_period(start_dt, user, allow_admin=False): from indico.modules.rb import rb_settings if allow_admin and rb_is_admin(user): return True default_tz = pytz.timezone(config.DEFAULT_TIMEZONE) start_dt_localized = default_tz.localize(start_dt) grace_period = rb_settings.get('grace_period') if grace_period is None: today = server_to_utc(datetime.now()).astimezone(default_tz).date() return start_dt_localized.date() >= today start_dt_utc = start_dt_localized.astimezone(pytz.utc) grace_period = timedelta(hours=grace_period) return start_dt_utc >= now_utc() - grace_period
def _make_shell_context(): context = {} info = [cformat('%{white!}Available objects')] add_to_context = partial(_add_to_context, context, info) add_to_context_multi = partial(_add_to_context_multi, context, info) add_to_context_smart = partial(_add_to_context_smart, context, info) # Common stdlib modules info.append(cformat('*** %{magenta!}stdlib%{reset} ***')) DATETIME_ATTRS = ('date', 'time', 'datetime', 'timedelta') ORM_ATTRS = ('joinedload', 'defaultload', 'contains_eager', 'lazyload', 'noload', 'subqueryload', 'undefer', 'undefer_group', 'load_only') add_to_context_multi([getattr(datetime, attr) for attr in DATETIME_ATTRS] + [getattr(sqlalchemy.orm, attr) for attr in ORM_ATTRS] + [itertools, re, sys, os], color='yellow') # Models info.append(cformat('*** %{magenta!}Models%{reset} ***')) models = [cls for name, cls in sorted(db.Model._decl_class_registry.items(), key=itemgetter(0)) if hasattr(cls, '__table__')] add_to_context_smart(models) # Tasks info.append(cformat('*** %{magenta!}Tasks%{reset} ***')) tasks = [task for task in sorted(celery.tasks.values()) if not task.name.startswith('celery.')] add_to_context_smart(tasks, get_name=lambda x: x.name.replace('.', '_'), color='blue!') # Plugins info.append(cformat('*** %{magenta!}Plugins%{reset} ***')) plugins = [type(plugin) for plugin in sorted(plugin_engine.get_active_plugins().values(), key=attrgetter('name'))] add_to_context_multi(plugins, color='yellow!') # Utils info.append(cformat('*** %{magenta!}Misc%{reset} ***')) add_to_context(celery, 'celery', doc='celery app', color='blue!') add_to_context(db, 'db', doc='sqlalchemy db interface', color='cyan!') add_to_context(now_utc, 'now_utc', doc='get current utc time', color='cyan!') add_to_context(config, 'config', doc='indico config') add_to_context(current_app, 'app', doc='flask app') add_to_context(lambda *a, **kw: server_to_utc(datetime.datetime(*a, **kw)), 'dt', doc='like datetime() but converted from localtime to utc') add_to_context(Event.get, 'EE', doc='get event by id') # Stuff from plugins signals.plugin.shell_context.send(add_to_context=add_to_context, add_to_context_multi=add_to_context_multi) return context, info