<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def rebuild(mode=''):
 """Rebuild the site with a nice UI."""

    scan_site()  # for good measure
    if not current_user.can_rebuild_site:
        return error('You are not permitted to rebuild the site.</p>'
                     '<p class="lead">Contact an administartor for '
                     'more information.', 401)
    if db is not None:
        db.set('site:needs_rebuild', '-1')
        if not q.fetch_job('build') and not q.fetch_job('orphans'):
            b = q.enqueue_call(func=coil.tasks.build,
                               args=(app.config['REDIS_URL'],
                                     app.config['NIKOLA_ROOT'], mode),
                               job_id='build')
            q.enqueue_call(func=coil.tasks.orphans,
                           args=(app.config['REDIS_URL'],
                                 app.config['NIKOLA_ROOT']), job_id='orphans',
                           depends_on=b)
        return render('coil_rebuild.tmpl', {'title': 'Rebuild'})
    else:
        status, outputb = coil.tasks.build_single(mode)
        _, outputo = coil.tasks.orphans_single()
        site.coil_needs_rebuild = '0'
        return render('coil_rebuild_single.tmpl',
                      {'title': 'Rebuild', 'status': '1' if status else '0',
                       'outputb': outputb, 'outputo': outputo})