#! /opt/mailman/mm/venv/bin/python # Copyright (C) 2022 by the Free Software Foundation, Inc. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, # USA. """This script reports on Django users that have multiple addresses, more than one of which is designated as primary or a user address that isn't primary. This can result from the UC_fix script. Usage: ./Mult_primary [database [user]] defaults are database: mailman and user: mailman """ import sys import psycopg2 def find(list): count = 0 for email, primary in list: if primary: count += 1 if count > 1: return True return False database = 'mailman' user = 'mailman' if len(sys.argv) == 1: pass elif len(sys.argv) == 2: database = sys.argv[1] elif len(sys.argv) == 3: database = sys.argv[1] user = sys.argv[2] else: print(__doc__, file= sys.stderr) sys.exit(1) db = psycopg2.connect(user=user, database=database) c = db.cursor() c.execute('select email, "primary", user_id from account_emailaddress;') d = {} data = c.fetchall() for email, primary, id in data: d.setdefault(id, []).append((email, primary)) for id, emails in d.items(): if find(emails): print(f'Multiple primary: {emails}') c.execute('select email from auth_user where id = %s', [id]) lc_user = c.fetchone()[0].lower() found = False for email, primary in emails: if email.lower() == lc_user: found = True if not primary: print(f'User email not primary: {email}') if not found: print(f'No address for user: {lc_user}') c.close() db.close()