@shared_task()<EOL>def load_user(data):
from invenio_accounts.models import User<EOL>from invenio_userprofiles.api import UserProfile<EOL>email = data['<STR_LIT:email>'].strip()<EOL>if User.query.filter_by(email=email).count() > <NUM_LIT:0>:<EOL><INDENT>raise UserEmailExistsError(<EOL>"<STR_LIT>".format(email=email))<EOL><DEDENT>last_login = None<EOL>if data['<STR_LIT>']:<EOL><INDENT>last_login = arrow.get(data['<STR_LIT>']).datetime<EOL><DEDENT>confirmed_at = None<EOL>if data['<STR_LIT>'] == '<STR_LIT:1>':<EOL><INDENT>confirmed_at = datetime.utcnow()<EOL><DEDENT>salt = data['<STR_LIT>']<EOL>checksum = data['<STR_LIT:password>']<EOL>if not checksum:<EOL><INDENT>new_password = None<EOL><DEDENT>elif checksum.startswith('<STR_LIT:$>'):<EOL><INDENT>new_password = checksum<EOL><DEDENT>else:<EOL><INDENT>new_password = str.join('<STR_LIT:$>', ['<STR_LIT>', u'<STR_LIT>', salt, checksum])<EOL><DEDENT>with db.session.begin_nested():<EOL><INDENT>obj = User(<EOL>id=data['<STR_LIT:id>'],<EOL>password=new_password,<EOL>email=email,<EOL>confirmed_at=confirmed_at,<EOL>last_login_at=last_login,<EOL>active=(data['<STR_LIT>'] != '<STR_LIT:0>'),<EOL>)<EOL>db.session.add(obj)<EOL><DEDENT>nickname = data['<STR_LIT>'].strip()<EOL>overwritten_username = ('<STR_LIT:username>' in data and '<STR_LIT>' in data)<EOL>if nickname or overwritten_username:<EOL><INDENT>p = UserProfile(user=obj)<EOL>p.full_name = data.get('<STR_LIT>', '<STR_LIT>').strip()<EOL>if overwritten_username:<EOL><INDENT>p._username = data['<STR_LIT:username>'].lower()<EOL>p._displayname = data['<STR_LIT>']<EOL><DEDENT>elif nickname:<EOL><INDENT>if UserProfile.query.filter(<EOL>UserProfile._username == nickname.lower()).count() > <NUM_LIT:0>:<EOL><INDENT>raise UserUsernameExistsError(<EOL>"<STR_LIT>".format(<EOL>username=nickname))<EOL><DEDENT>try:<EOL><INDENT>p.username = nickname<EOL><DEDENT>except ValueError:<EOL><INDENT>current_app.logger.warn(<EOL>u'<STR_LIT>'.format(<EOL>nickname, data['<STR_LIT:id>']))<EOL>p._username = nickname.lower()<EOL>p._displayname = nickname<EOL><DEDENT><DEDENT>db.session.add(p)<EOL><DEDENT>db.session.commit()<EOL>
Load user from data dump.

    NOTE: This task takes into account the possible duplication of emails and
    usernames, hence it should be called synchronously.
    In such case of collision it will raise UserEmailExistsError or
    UserUsernameExistsError, if email or username are already existing in
    the database. Caller of this task should take care to to resolve those
    collisions beforehand or after catching an exception.

    :param data: Dictionary containing user data.
    :type data: dict