@@ -0,0 +1,1928 @@
+import csv%0Aimport sys%0Aimport time%0A%0Afrom django.contrib.auth.models import User%0Afrom django.db.models import CharField, F%0Afrom django.db.models.functions import Cast%0A%0A%0AUSER_COLS = %5B%0A    'id',%0A    'username',%0A    'is_superuser',%0A    'is_staff',%0A    'date_joined_str',%0A    'last_login_str',%0A    'first_name',%0A    'last_name',%0A    'is_active',%0A    'email',%0A    'mfa_is_active',%0A%5D%0AMETADATA_COL = %5B'metadata'%5D%0AEXTRA_DETAILS_COLS = %5B%0A    'bio',%0A    'city',%0A    'name',%0A    'gender',%0A    'sector',%0A    'country',%0A    'twitter',%0A    'linkedin',%0A    'metadata',%0A    'instagram',%0A    'organization',%0A    'require_auth',%0A    'organization_website',%0A%5D%0A%0A%0Adef get_user_data():%0A    values = USER_COLS + METADATA_COL%0A    return (%0A        User.objects.all()%0A        .annotate(%0A            mfa_is_active=F('mfa_methods__is_active'),%0A            metadata=F('extra_details__data'),%0A            date_joined_str=Cast('date_joined', CharField()),%0A            last_login_str=Cast('last_login', CharField()),%0A        )%0A        .values(*values)%0A        .order_by('id')%0A    )%0A%0A%0Adef flatten_metadata_inplace(metadata):%0A    for k, v in metadata.items():%0A        if isinstance(v, list):%0A            v = v%5B0%5D%0A        if isinstance(v, dict) and 'label' in v:%0A            metadata%5Bk%5D = v%5B'label'%5D%0A%0A%0Adef write_csv(filename, data):%0A    with open(filename, 'w') as f:%0A        columns = USER_COLS + EXTRA_DETAILS_COLS%0A        writer = csv.DictWriter(f, fieldnames=columns)%0A        writer.writeheader()%0A        for item in data:%0A            metadata = item.pop('metadata')%0A            flatten_metadata_inplace(metadata)%0A            item.update(metadata)%0A            writer.writerow(item)%0A%0A%0Adef run(*args):%0A    if not args:%0A        print('Inlcude filename and path in %60--script-args%60')%0A        sys.exit()%0A    filename = args%5B0%5D%0A    t0 = time.time()%0A    data = get_user_data()%0A    write_csv(filename, data)%0A    t1 = time.time()%0A    print(f'Completed in %7Bt1-t0:.4f%7Ds.')%0A

add rough user list export runscript script