Add exclude and include

This commit is contained in:
Yoann Pietri 2020-08-07 22:58:21 +02:00 committed by root
parent 1357b67334
commit b3ce1369ec
2 changed files with 9 additions and 2 deletions

View file

@ -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` | | `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 | | `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 ### Example
``` ```
[Re2o] [Re2o]
@ -69,6 +70,7 @@ activate = yes
[rezotage] [rezotage]
activate = yes activate = yes
list_name = rezo-admin 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. 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.

View file

@ -20,19 +20,24 @@ if changed:
for section in config.sections(): for section in config.sections():
if section not in ["Re2o", "Mailman"] and config.getboolean(section, 'activate'): if section not in ["Re2o", "Mailman"] and config.getboolean(section, 'activate'):
list_name = config.get(section, "list_name", fallback=section) 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)) 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(): if "entries" in response1.json():
entries = response1.json()['entries'] entries = response1.json()['entries']
old_emails = [entry['email'] for entry in entries] old_emails = [entry['email'] for entry in entries]
new_emails = open(path + "/generated/ml.{}.list".format(section)).read().split("\n") 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 = [email for email in old_emails if email not in new_emails]
emails_to_delete += exclude
if emails_to_delete: if emails_to_delete:
print("[..] Deleting non members from list {}".format(list_name)) 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}) 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)) print("[OK] Non members where deleted from list {}".format(list_name))
else: else:
print("[INFO] No member to delete for list {}".format(list_name)) 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: if emails_to_add:
print("[..] Adding members to list {}".format(list_name)) print("[..] Adding members to list {}".format(list_name))
with open(path + "/tmp", "w+") as f: with open(path + "/tmp", "w+") as f: