@unique_session_or_json_redirect
def uploadfile(request):
    """
    Accept a block equivalency file, and create a plan based on that
    file.

    Parameters:
        request -- An HttpRequest, with a file upload and plan name.

    Returns:
        A plan view, with additional information about the upload status.
    """
    note_session_activity(request)

    if request.user.is_anonymous():
        # If a user is logged off from another location, they will appear
        # as an anonymous user. Redirect them to the front page. Sadly,
        # they will not get a notice that they were logged out.
        return HttpResponseRedirect('/')

    status = commonplan(request,0)
    status['upload'] = True
    status['upload_status'] = True

    index_file = request.FILES.get('indexFile', False)
    if not index_file:
        status['upload_status'] = False
        return render_to_response('viewplan.html', status)
    else:
        filename = index_file.name

    if index_file.size > settings.MAX_UPLOAD_SIZE:
        logger.error('File size exceeds allowable size.')
        status['upload_status'] = False
        return render_to_response('viewplan.html', status)

    if not filename.endswith(('.csv','.zip')):
        logger.error('Uploaded file must be ".csv" or ".zip".')
        status['upload_status'] = False
    elif request.POST['userEmail'] == '':
        logger.error('No email provided for user notification.')
        status['upload_status'] = False
    else:
        try:
            dest = tempfile.NamedTemporaryFile(mode='wb+', delete=False)
            for chunk in request.FILES['indexFile'].chunks():
                dest.write(chunk)
            dest.close()
            if request.FILES['indexFile'].name.endswith('.zip'):
                os.rename(dest.name, '%s%s' % (dest.name,'.zip'))
                filename = '%s%s' % (dest.name,'.zip')
            else:
                filename = dest.name

        except Exception as ex:
            logger.error('Could not save uploaded file')
            logger.error('Reason: %s', ex)
            status['upload_status'] = False
            return render_to_response('viewplan.html', status)

        # Put in a celery task to create the plan and email user on completion
        DistrictIndexFile.index2plan.delay(request.POST['txtNewName'], request.POST['legislativeBody'], filename, owner = request.user, template = False, purge = True, email = request.POST['userEmail'], language=translation.get_language())

    return render_to_response('viewplan.html', status)
dataset/ETHPy150Open PublicMapping/DistrictBuilder/django/publicmapping/redistricting/views.py/uploadfile/original