Пример #1
0
 def _update_last_use(response):
     with db.tmp_session() as sess:
         # do not modify `token` directly, it's attached to a different session!
         sess.query(OAuthToken).filter_by(id=token_id).update(
             {OAuthToken.last_used_dt: now_utc()})
         sess.commit()
     return response
Пример #2
0
 def _update_last_use(response):
     with db.tmp_session() as sess:
         # do not modify `token` directly, it's attached to a different session!
         sess.query(token_cls).filter_by(id=token_id).update({
             token_cls.last_used_dt: now_utc(),
             token_cls.last_used_ip: flask_request.remote_addr,
             token_cls.use_count: token_cls.use_count + 1,
         })
         sess.commit()
     return response
Пример #3
0
def menu_entries_for_event(event):
    from indico.core.plugins import plugin_engine

    custom_menu_enabled = layout_settings.get(event, "use_custom_menu")
    entries = MenuEntry.get_for_event(event) if custom_menu_enabled else []
    signal_entries = get_menu_entries_from_signal()

    cache_key = unicode(event.id)
    plugin_hash = binascii.crc32(",".join(sorted(plugin_engine.get_active_plugins()))) & 0xFFFFFFFF
    cache_version = "{}:{}".format(MaKaC.__version__, plugin_hash)
    processed = entries and _cache.get(cache_key) == cache_version

    if not processed:
        # menu entries from signal
        pos_gen = count(start=(entries[-1].position + 1) if entries else 0)
        entry_names = {entry.name for entry in entries}

        # Keeping only new entries from the signal
        new_entry_names = signal_entries.viewkeys() - entry_names

        # Mapping children data to their parent
        children = defaultdict(list)
        for name, data in signal_entries.iteritems():
            if name in new_entry_names and data.parent is not None:
                children[data.parent].append(data)

        # Building the entries
        new_entries = [
            _build_menu_entry(event, custom_menu_enabled, data, next(pos_gen), children=children.get(data.name))
            for (name, data) in sorted(signal_entries.iteritems(), key=lambda (name, data): _menu_entry_key(data))
            if name in new_entry_names and data.parent is None
        ]

        if custom_menu_enabled:
            with db.tmp_session() as sess:
                sess.add_all(new_entries)
                try:
                    sess.commit()
                except IntegrityError as e:
                    # If there are two parallel requests trying to insert a new menu
                    # item one of them will fail with an error due to the unique index.
                    # If the IntegrityError involves that index, we assume it's just the
                    # race condition and ignore it.
                    sess.rollback()
                    if "ix_uq_menu_entries_event_id_name" not in unicode(e.message):
                        raise
                else:
                    _cache.set(cache_key, cache_version)
            entries = MenuEntry.get_for_event(event)
        else:
            entries = new_entries

    return entries
Пример #4
0
def _save_menu_entries(entries):
    """Save new menu entries using a separate SA session"""
    with db.tmp_session() as sess:
        sess.add_all(entries)
        try:
            sess.commit()
        except IntegrityError as e:
            # If there are two parallel requests trying to insert a new menu
            # item one of them will fail with an error due to the unique index.
            # If the IntegrityError involves that index, we assume it's just the
            # race condition and ignore it.
            sess.rollback()
            if 'ix_uq_menu_entries_event_id_name' not in unicode(e.message):
                raise
            return False
        else:
            return True
Пример #5
0
def _save_menu_entries(entries):
    """Save new menu entries using a separate SA session."""
    with db.tmp_session() as sess:
        sess.add_all(entries)
        try:
            sess.commit()
        except IntegrityError as exc:
            # If there are two parallel requests trying to insert a new menu
            # item one of them will fail with an error due to the unique index.
            # If the IntegrityError involves that index, we assume it's just the
            # race condition and ignore it.
            sess.rollback()
            if 'ix_uq_menu_entries_event_id_name' not in str(exc):
                raise
            return False
        else:
            return True
Пример #6
0
def increment_and_get(col, filter_):
    """Increments and returns a numeric column.

    This is committed to the database immediately in a separate
    transaction to avoid possible conflicts.

    The main purpose of this utility is to generate "scoped" IDs
    (which cannot be represented using database-level sequences as you
    would need one sequence per scope) without risking collisions when
    inserting the objects those IDs are eventually assigned to.

    :param col: The column to update, e.g. ``SomeModel.last_num``
    :param filter_: A filter expression such as ``SomeModel.id == n``
                    to restrict which columns to update.
    """
    from indico.core.db import db
    with db.tmp_session() as s:
        rv = s.execute(update(col.class_).where(filter_).values({col: col + 1}).returning(col)).fetchone()[0]
        s.commit()
    return rv
Пример #7
0
def increment_and_get(col, filter_, n=1):
    """Increment and returns a numeric column.

    This is committed to the database immediately in a separate
    transaction to avoid possible conflicts.

    The main purpose of this utility is to generate "scoped" IDs
    (which cannot be represented using database-level sequences as you
    would need one sequence per scope) without risking collisions when
    inserting the objects those IDs are eventually assigned to.

    :param col: The column to update, e.g. ``SomeModel.last_num``
    :param filter_: A filter expression such as ``SomeModel.id == n``
                    to restrict which columns to update.
    :param n: The number of units to increment the ID of.
    """
    from indico.core.db import db
    with db.tmp_session() as s:
        rv = s.execute(update(col.class_).where(filter_).values({col: col + n}).returning(col)).fetchone()[0]
        s.commit()
    return rv
Пример #8
0
 def _update_last_use(response):
     with db.tmp_session() as sess:
         # do not modify `token` directly, it's attached to a different session!
         sess.query(OAuthToken).filter_by(id=token_id).update({OAuthToken.last_used_dt: now_utc()})
         sess.commit()
     return response
Пример #9
0
def menu_entries_for_event(event):
    from indico.core.plugins import plugin_engine

    custom_menu_enabled = layout_settings.get(event, 'use_custom_menu')
    entries = MenuEntry.get_for_event(event) if custom_menu_enabled else []
    signal_entries = get_menu_entries_from_signal()

    cache_key = unicode(event.id)
    plugin_hash = crc32(','.join(sorted(plugin_engine.get_active_plugins())))
    cache_version = '{}:{}'.format(MaKaC.__version__, plugin_hash)
    processed = entries and _cache.get(cache_key) == cache_version

    if not processed:
        # menu entries from signal
        pos_gen = count(start=(entries[-1].position + 1) if entries else 0)
        entry_names = {entry.name for entry in entries}

        # Keeping only new entries from the signal
        new_entry_names = signal_entries.viewkeys() - entry_names

        # Mapping children data to their parent
        children = defaultdict(list)
        for name, data in signal_entries.iteritems():
            if name in new_entry_names and data.parent is not None:
                children[data.parent].append(data)

        # Building the entries
        new_entries = [
            _build_menu_entry(event,
                              custom_menu_enabled,
                              data,
                              next(pos_gen),
                              children=children.get(data.name))
            for (
                name,
                data) in sorted(signal_entries.iteritems(),
                                key=lambda (name, data): _menu_entry_key(data))
            if name in new_entry_names and data.parent is None
        ]

        if custom_menu_enabled:
            with db.tmp_session() as sess:
                sess.add_all(new_entries)
                try:
                    sess.commit()
                except IntegrityError as e:
                    # If there are two parallel requests trying to insert a new menu
                    # item one of them will fail with an error due to the unique index.
                    # If the IntegrityError involves that index, we assume it's just the
                    # race condition and ignore it.
                    sess.rollback()
                    if 'ix_uq_menu_entries_event_id_name' not in unicode(
                            e.message):
                        raise
                else:
                    _cache.set(cache_key, cache_version)
            entries = MenuEntry.get_for_event(event)
        else:
            entries = new_entries

    return entries