"Fossies" - the Fresh Open Source Software Archive

Member "mailman-3.3.7/src/mailman/database/alembic/versions/7b254d88f122_members_and_list_moderation_action.py" (10 Nov 2022, 3101 Bytes) of package /linux/misc/mailman-3.3.7.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "7b254d88f122_members_and_list_moderation_action.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.3.6_vs_3.3.7.

    1 """Members and list moderation action.
    2 
    3 Revision ID: 7b254d88f122
    4 Revises: d4fbb4fd34ca
    5 Create Date: 2016-02-10 11:31:04.233619
    6 
    7 This is a data-only migration.  If a member has the same moderation action as
    8 the mailing list's default, then set its moderation action to None and use the
    9 fallback to the list's default.
   10 """
   11 
   12 
   13 import sqlalchemy as sa
   14 
   15 from alembic import op
   16 from mailman.database.types import Enum, SAUnicode
   17 from mailman.interfaces.action import Action
   18 from mailman.interfaces.member import MemberRole
   19 
   20 
   21 # Revision identifiers, used by Alembic.
   22 revision = '7b254d88f122'
   23 down_revision = 'd4fbb4fd34ca'
   24 
   25 
   26 mailinglist_table = sa.sql.table(
   27     'mailinglist',
   28     sa.sql.column('id', sa.Integer),
   29     sa.sql.column('list_id', SAUnicode),
   30     sa.sql.column('default_member_action', Enum(Action)),
   31     sa.sql.column('default_nonmember_action', Enum(Action)),
   32     )
   33 
   34 
   35 member_table = sa.sql.table(
   36     'member',
   37     sa.sql.column('id', sa.Integer),
   38     sa.sql.column('list_id', SAUnicode),
   39     sa.sql.column('role', Enum(MemberRole)),
   40     sa.sql.column('moderation_action', Enum(Action)),
   41     )
   42 
   43 
   44 # This migration only considers members and nonmembers.
   45 members_query = member_table.select().where(sa.or_(
   46     member_table.c.role == MemberRole.member,
   47     member_table.c.role == MemberRole.nonmember,
   48     ))
   49 
   50 
   51 # list-id -> {property-name -> action}
   52 #
   53 # where property-name will be either default_member_action or
   54 # default_nonmember_action.
   55 DEFAULT_ACTION_CACHE = {}
   56 MISSING = object()
   57 
   58 
   59 def _get_default_action(connection, member):
   60     # breakpoint()
   61     list_id = member[1]
   62     property_name = 'default_{}_action'.format(member[2].name)
   63     list_mapping = DEFAULT_ACTION_CACHE.setdefault(list_id, {})
   64     action = list_mapping.get(property_name, MISSING)
   65     if action is MISSING:
   66         action = (
   67             connection.execute(
   68                 sa.select(getattr(mailinglist_table.c, property_name))
   69                 .where(mailinglist_table.c.list_id == list_id))
   70             .fetchone()[0]
   71             )
   72         list_mapping[property_name] = action
   73     return action
   74 
   75 
   76 def upgrade():
   77     connection = op.get_bind()
   78     for member in connection.execute(members_query).fetchall():
   79         default_action = _get_default_action(connection, member)
   80         # If the (non)member's moderation action is the same as the mailing
   81         # list's default, then set it to None.  The moderation rule will
   82         # fallback to the list's default.
   83         if member[3] == default_action:
   84             connection.execute(member_table.update().where(
   85                 member_table.c.id == member[0]
   86                 ).values(moderation_action=None))
   87 
   88 
   89 def downgrade():
   90     connection = op.get_bind()
   91     for member in connection.execute(members_query.where(
   92            member_table.c.moderation_action == None)).fetchall():  # noqa: E711
   93         default_action = _get_default_action(connection, member)
   94         # Use the mailing list's default action
   95         connection.execute(member_table.update().where(
   96             member_table.c.id == member[0]
   97             ).values(moderation_action=default_action))