#! /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 looks at django users' addresses and where there are two that differ only in case one of which is all lower case, it deletes the mixed case address and sets the lower case address primary. This is a workaround for . See the Mult_primary script for reporting issues that result from this process. Usage: ./UC_fix [database [user]] defaults are database: mailman and user: mailman """ import sys import psycopg2 def find(list): if len(list) < 2: return False for i in range(len(list)-1): for j in range(i+1, len(list)): if list[i].lower() == list[j].lower(): return (list[i], list[j]) 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, user_id from account_emailaddress;') d = {} data = c.fetchall() for email, id in data: d.setdefault(id, []).append(email) count = 0 for id, emails in d.items(): ems = find(emails) if ems: em1, em2 = ems if em1.lower() == em1: c.execute("delete from account_emailaddress where email = %s;", [em2]) c.execute("""update account_emailaddress set "primary" = 't' where email = %s;""", [em1]) elif em2.lower() == em2: c.execute("delete from account_emailaddress where email = %s;", [em1]) c.execute("""update account_emailaddress set "primary" = 't' where email = %s;""", [em2]) else: print(f'No LC email for {ems}') else: continue count += 1 print(f'Processed {count} users') c.close() db.commit() db.close()