forked from mozilla-it/ctms-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read Acoustic Newsletters mapping from database (ref mozilla-it#256)
- Loading branch information
Showing
8 changed files
with
249 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python3 | ||
"""Manage Acoustic newsletters mapping: add and remove from the db.""" | ||
|
||
import argparse | ||
import sys | ||
|
||
from ctms import config | ||
from ctms.database import get_db_engine | ||
from ctms.models import AcousticNewsletterMapping | ||
|
||
|
||
def main(dbsession, test_args=None) -> int: | ||
parser = argparse.ArgumentParser( | ||
description=""" | ||
Manage Acoustic Newsletter mapping | ||
""" | ||
) | ||
subparsers = parser.add_subparsers(dest="action") | ||
parser_add = subparsers.add_parser("add") | ||
parser_add.add_argument("mapping") | ||
|
||
parser_remove = subparsers.add_parser("remove") | ||
parser_remove.add_argument("mapping") | ||
|
||
parser_list = subparsers.add_parser("list") | ||
|
||
args = parser.parse_args(args=test_args) | ||
if args.action == "add": | ||
source, destination = args.mapping.split(":") | ||
# This will fail if mapping already exists. | ||
dbsession.add(AcousticNewsletterMapping(source=source, destination=destination)) | ||
dbsession.commit() | ||
print("Added.") | ||
elif args.action == "remove": | ||
row = ( | ||
dbsession.query(AcousticNewsletterMapping) | ||
.filter(AcousticNewsletterMapping.source == args.mapping) | ||
.one_or_none() | ||
) | ||
if not row: | ||
print(f"Unknown mapping '{args.mapping}'. Give up.") | ||
return 2 | ||
dbsession.delete(row) | ||
dbsession.commit() | ||
print("Removed.") | ||
else: | ||
entries = dbsession.query(AcousticNewsletterMapping).all() | ||
print("\n".join(sorted(f"- {e.source!r} → {e.destination!r}" for e in entries))) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
# Get the database | ||
config_settings = config.Settings() | ||
engine, session_factory = get_db_engine(config_settings) | ||
session = session_factory() | ||
with engine.connect() as connection: | ||
ret = main(session) # pylint:disable = invalid-name | ||
|
||
sys.exit(ret) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
migrations/versions/20221115_f8b8f6aa0e96_add_acoustic_newsletter_mapping.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Add Acoustic Newsletter Mapping | ||
Revision ID: f8b8f6aa0e96 | ||
Revises: 06f24cc01c09 | ||
Create Date: 2022-11-15 12:20:10.096170 | ||
""" | ||
# pylint: disable=no-member invalid-name | ||
# no-member is triggered by alembic.op, which has dynamically added functions | ||
# invalid-name is triggered by migration file names with a date prefix | ||
# invalid-name is triggered by top-level alembic constants like revision instead of REVISION | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "f8b8f6aa0e96" # pragma: allowlist secret | ||
down_revision = "06f24cc01c09" # pragma: allowlist secret | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
acoustic_mapping_table = op.create_table( | ||
"acoustic_newsletter_mapping", | ||
sa.Column("source", sa.String(), nullable=False), | ||
sa.Column("destination", sa.String(), nullable=False), | ||
sa.PrimaryKeyConstraint("source"), | ||
) | ||
# ### end Alembic commands ### | ||
|
||
# This mapping used to be hard-coded. We now initialize the freshly created | ||
# table with it, but the records of this table will likely be modified | ||
# eventually. | ||
MAIN_TABLE_SUBSCR_FLAGS = { | ||
# maps the Basket/CTMS newsletter name to the Acoustic name | ||
"about-mozilla": "sub_about_mozilla", | ||
"app-dev": "sub_apps_and_hacks", | ||
"common-voice": "sub_common_voice", | ||
"firefox-accounts-journey": "sub_firefox_accounts_journey", | ||
"firefox-news": "sub_firefox_news", | ||
"firefox-sweepstakes": "sub_firefox_sweepstakes", | ||
"hubs": "sub_hubs", | ||
"internet-health-report": "sub_internet_health_report", | ||
"knowledge-is-power": "sub_knowledge_is_power", | ||
"miti": "sub_miti", | ||
"mixed-reality": "sub_mixed_reality", | ||
"mozilla-and-you": "sub_firefox_news", | ||
"mozilla-fellowship-awardee-alumni": "sub_mozilla_fellowship_awardee_alumni", | ||
"mozilla-festival": "sub_mozilla_festival", | ||
"mozilla-foundation": "sub_mozilla_foundation", | ||
"mozilla-rally": "sub_rally", | ||
"mozilla-technology": "sub_mozilla_technology", | ||
"mozillians-nda": "sub_mozillians_nda", | ||
"security-privacy-news": "sub_security_privacy_news", | ||
"take-action-for-the-internet": "sub_take_action_for_the_internet", | ||
"test-pilot": "sub_test_pilot", | ||
} | ||
op.bulk_insert( | ||
acoustic_mapping_table, | ||
[{"source": k, "destination": v} for k, v in MAIN_TABLE_SUBSCR_FLAGS.items()], | ||
) | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table("acoustic_newsletter_mapping") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.