Python CSC Electronic Office
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB

#!/usr/bin/env python3
This is a script which adds a member term to each user who has a membership
for the current term. This should be used when MathSoc waives fees for a
Users who have been given a membership for the current term without paying
should NOT have their membership extended, so use the --exclude flag like so:
./ --exclude user1,user2
GSSAPI is used for LDAP authentication, so make sure to run `kinit` first.
Also, make sure to run this script from the top-level of the git directory
(see the sys.path hack below).
import argparse
import os
import sys
import ldap3
from ceo_common.model import Term
# modify as necessary
LDAP_URI = "ldaps://"
LDAP_MEMBERS_BASE = "ou=People,dc=csclub,dc=uwaterloo,dc=ca"
parser = argparse.ArgumentParser()
parser.add_argument('--exclude', help='comma-separated list of usernames to exclude')
parser.add_argument('--dry-run', help='dry run', action='store_true')
args = parser.parse_args()
IGNORE = args.exclude.split(',') if args.exclude is not None else []
DRY_RUN = args.dry_run
current_term = Term.current()
conn = ldap3.Connection(
LDAP_URI, authentication=ldap3.SASL, sasl_mechanism=ldap3.KERBEROS,
auto_bind=True, raise_exceptions=True), f'(term={current_term})',
attributes=['uid', 'term'])
total_records_updated = 0
for entry in conn.entries:
if entry.uid.value in IGNORE:
terms = map(Term, entry.term.values)
next_term = max(terms) + 1
changes = {'term': [(ldap3.MODIFY_ADD, [str(next_term)])]}
print('Would have modified %s' % entry.uid.value)
conn.modify(entry.entry_dn, changes)
print('Modified %s' % entry.uid.value)
total_records_updated += 1
print('Total records which would have been updated: %d' % total_records_updated)
print('Total records updated: %d' % total_records_updated)