annotate accounts/utils.py @ 1209:d8bb9c36aae1 modernize tip

Add unit test for accounts task.
author Brian Neal <bgneal@gmail.com>
date Thu, 30 Jan 2025 18:46:03 -0600
parents 02181fa5ac9d
children
rev   line source
bgneal@1206 1 import datetime
bgneal@1206 2 import logging
bgneal@1206 3
bgneal@1206 4 from django.contrib.auth.models import User
bgneal@1206 5
bgneal@1206 6
bgneal@1206 7 logger = logging.getLogger('auth')
bgneal@1206 8
bgneal@1206 9
bgneal@1206 10 def create_new_user(pending_user, ip=None, admin_activation=False):
bgneal@1206 11 """
bgneal@1206 12 This function contains the code to create a new user from a
bgneal@1206 13 pending user. The pending user is deleted and the new user
bgneal@1206 14 is saved. A log message is produced. If admin_activation is false,
bgneal@1206 15 then ip should be the user's IP they confirmed from, if available.
bgneal@1206 16
bgneal@1206 17 """
bgneal@1206 18 new_user = User()
bgneal@1206 19
bgneal@1206 20 new_user.username = pending_user.username
bgneal@1206 21 new_user.first_name = ''
bgneal@1206 22 new_user.last_name = ''
bgneal@1206 23 new_user.email = pending_user.email
bgneal@1206 24 new_user.password = pending_user.password # already been hashed
bgneal@1206 25 new_user.is_staff = False
bgneal@1206 26 new_user.is_active = True
bgneal@1206 27 new_user.is_superuser = False
bgneal@1206 28 new_user.last_login = datetime.datetime.now()
bgneal@1206 29 new_user.date_joined = new_user.last_login
bgneal@1206 30
bgneal@1206 31 new_user.save()
bgneal@1206 32 pending_user.delete()
bgneal@1206 33
bgneal@1206 34 if admin_activation:
bgneal@1206 35 msg = 'Accounts registration confirmed by ADMIN for %s' % new_user.username
bgneal@1206 36 else:
bgneal@1206 37 msg = 'Accounts registration confirmed by USER for %s from %s' % (
bgneal@1206 38 new_user.username, ip)
bgneal@1206 39
bgneal@1206 40 logger.info(msg)