Skip to main content

Installable Django admin interface for bulk user creation from uploaded CSV file.

Project description

Installable Django admin interface for bulk user creation from uploaded CSV file.

Installation

First install using pip

pip install django-bulk-user-upload

Then add to your INSTALLED_PACKAGES

INSTALLED_PACKAGES = [
    . . .,
    'bulk_user_upload',
]

Then override the default User admin:

from django.contrib.admin import register
from django.contrib.auth import get_user_model

from bulk_user_upload.admin import BulkUploadUserAdmin

User = get_user_model()

@register(User)
class CustomUserAdmin(BulkUploadUserAdmin):
    pass

Setup and Customization

By default, the upload only processes username, email, permissions, and groups, e.g., you could use a CSV with the following information:

username email permissions groups
user user@example.com "auth.add_user,auth.change_user" "Example Users,Test Users"

But if you have a custom user model or need to capture more fields, you can modify the defaults by setting BULK_USER_UPLOAD in settings.py. Below are the defaults:

BULK_USER_UPLOAD = {
    'USERNAME_FIELD': 'username',  # user model username field
    'EMAIL_FIELD': 'email',  # user model email field
    'LOGIN_URL': '/',  # used in account creation notification email template
    'USER_UPLOAD_FORM': 'bulk_user_upload.forms.BulkUserUploadForm',  # django admin upload form
    'USERS_PREPROCESSOR': 'bulk_user_upload.utils.UsersPreProcessor',  # cleanup/pre-process the uploaded CSV
    'USERS_CREATOR': 'bulk_user_upload.utils.BaseUsersCreator',  # creates users from the uploaded CSV
    'USERS_VALIDATOR': 'bulk_user_upload.utils.UsersValidator',  # validates users from the uploaded CSV
    'USER_FIELD_VALIDATORS': {},  # add or override field-level validators
    'SEND_EMAILS_BY_DEFAULT': True,  # whether "send emails" is checked by default in the upload form
    'ACCOUNT_CREATION_EMAIL_SENDER_ADDRESS': None,  # email address used to notify user of account creation
    'ACCOUNT_CREATION_EMAIL_SUBJECT': 'Account Created',
    'EMAIL_SENDER': 'bulk_user_upload.utils.EmailSender',  # sends emails to created accounts
    # compute the name of the recipient, used in the account creation notification email template
    'GET_EMAIL_RECIPIENT_NAME': 'bulk_user_upload.utils.get_email_recipient_name',
}

For example, if you wanted to indicate whether your uploaded users are staff, you could modify these settings like so:

def intish(value):
    try:
        int(value)
        return True
    except:
        return False

BULK_USER_UPLOAD = dict(
    USER_FIELD_VALIDATORS=dict(
        is_staff=(
            lambda is_staff: not intish(is_staff) or int(is_staff) not in [True, False],
            lambda is_staff, *args: "is_staff must be 0 or 1.",
        )
    )
)

The sample project has an example of this and other customizations.

Demo

https://user-images.githubusercontent.com/12461302/133109664-3f2a223d-cc8c-4085-965a-c04e48065d72.mov

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-bulk-user-upload-0.1.0.tar.gz (20.0 kB view hashes)

Uploaded Source

Built Distribution

django_bulk_user_upload-0.1.0-py3-none-any.whl (27.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page