'Rename a JIRA user. Current implementation relies on third party plugin but in the future it may use embedded JIRA functionality.
:param old_user: string with username login
:param new_user: string with username login'

def rename_user(self, old_user, new_user):

    if (self._version >= (6, 0, 0)):
        url = (self._options[u'server'] + u'/rest/api/latest/user')
        payload = {u'name': new_user}
        params = {u'username': old_user}
        logging.debug((u'renaming    %s' % self.user(old_user).emailAddress))
        r = self._session.put(url, params=params, data=json.dumps(payload))
    else:
        merge = u'true'
        try:
            self.user(new_user)
        except Exception:
            merge = u'false'
        url = (self._options[u'server'] + u'/secure/admin/groovy/CannedScriptRunner.jspa#result')
        payload = {u'cannedScript': u'com.onresolve.jira.groovy.canned.admin.RenameUser', u'cannedScriptArgs_FIELD_FROM_USER_ID': old_user, u'cannedScriptArgs_FIELD_TO_USER_ID': new_user, u'cannedScriptArgs_FIELD_MERGE': merge, u'id': u'', u'RunCanned': u'Run'}
        logging.debug((u'renaming    %s' % self.user(old_user).emailAddress))
        r = self._session.post(url, headers=self._options[u'headers'], data=payload)
        if (r.status_code == 404):
            logging.error(u'In    order    to    be    able    to    use    rename_user()    you    need    to    install    Script    Runner    plugin.    See    https://marketplace.atlassian.com/plugins/com.onresolve.jira.groovy.groovyrunner')
            return False
        if (r.status_code != 200):
            logging.error(r.status_code)
        if re.compile(u'XSRF    Security    Token    Missing').search(r.content):
            logging.fatal(u'Reconfigure    JIRA    and    disable    XSRF    in    order    to    be    able    call    this.    See    https://developer.atlassian.com/display/JIRADEV/Form+Token+Handling')
            return False
        with open((u'/tmp/jira_rename_user_%s_to%s.html' % (old_user, new_user)), u'w') as f:
            f.write(r.content)
        msg = r.status_code
        m = re.search(u'<span    class="errMsg">(.*)<\\/span>', r.content)
        if m:
            msg = m.group(1)
            logging.error(msg)
            return False
        p = re.compile(u'type="hidden"    name="cannedScriptArgs_Hidden_output"    value="(.*?)"\\/>', (re.MULTILINE | re.DOTALL))
        m = p.search(r.content)
        if m:
            h = html_parser.HTMLParser()
            msg = h.unescape(m.group(1))
            logging.info(msg)
        try:
            self.user(old_user)
        except Exception as e:
            logging.error(u'User    %s    does    not    exists.    %s', old_user, e)
            return msg
        logging.error(((msg + u'\n') + (u"User    %s    does    still    exists    after    rename,    that's    clearly    a    problem." % old_user)))
        return False
