From b3ce1369ec9e5091facfbb6a302d624be7dd038a Mon Sep 17 00:00:00 2001 From: Yoann Pietri Date: Fri, 7 Aug 2020 22:58:21 +0200 Subject: [PATCH] Add exclude and include --- README.md | 4 +++- sync_adherents_mailman.py | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ae5eb6d..67a66b3 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,8 @@ For each section, you can have two parameters : |-------------|-------------------------------------------------------------------------------------------|---------------| | `activate` | If yes, the mailing will be synchronised. `no` is equivalent to no section at all | `no` | | `list_name` | list name (without domain) on mailman. If not given, the section name is taken by default | section name | - +| `include` | list of email addresses separated by commas to include in the mailing list | '' | +| `exclude` | list of email addresses separated by commas to exclude from the mailing list | '' | ### Example ``` [Re2o] @@ -69,6 +70,7 @@ activate = yes [rezotage] activate = yes list_name = rezo-admin +exclude = bureau@rezometz.org,autremail@rezometz.org ``` 3 mailings are generated : one which is adherents@rezometz.org with all adherents, one which is is rezo@rezometz.org with the group rezo and the last one is rezo-admin@rezometz.org with the group rezotage. diff --git a/sync_adherents_mailman.py b/sync_adherents_mailman.py index 6047f42..84f2e12 100644 --- a/sync_adherents_mailman.py +++ b/sync_adherents_mailman.py @@ -20,19 +20,24 @@ if changed: for section in config.sections(): if section not in ["Re2o", "Mailman"] and config.getboolean(section, 'activate'): list_name = config.get(section, "list_name", fallback=section) + include = config.get(section, "include", fallback="").split(",") + exclude = config.get(section, "exclude", fallback="").split(",") response1 = requests.get(roster_url.format(mailman_url=mailman_url, list_name=list_name, domain=domain), auth=(mailman_username, mailman_password)) if "entries" in response1.json(): entries = response1.json()['entries'] old_emails = [entry['email'] for entry in entries] new_emails = open(path + "/generated/ml.{}.list".format(section)).read().split("\n") emails_to_delete = [email for email in old_emails if email not in new_emails] + emails_to_delete += exclude if emails_to_delete: print("[..] Deleting non members from list {}".format(list_name)) response = requests.delete(roster_url.format(mailman_url=mailman_url, list_name=list_name, domain=domain), auth=(mailman_username,mailman_password), params={'emails': emails_to_delete}) print("[OK] Non members where deleted from list {}".format(list_name)) else: print("[INFO] No member to delete for list {}".format(list_name)) - emails_to_add = [email for email in new_emails if email not in old_emails] + emails_to_add = [email for email in new_emails if (email not in old_emails and email not in exclude)] + emails_to_add += include + print(emails_to_add) if emails_to_add: print("[..] Adding members to list {}".format(list_name)) with open(path + "/tmp", "w+") as f: