@login_required
@unique_session_or_json_redirect
def addtodistrict(request, planid, districtid):
    """
    Add geounits to a district.

    This method requires both "geolevel" and "geounits" URL parameters. 
    The geolevel must be a valid geolevel name and the geounits parameters 
    should be a pipe-separated list of geounit ids.

    Parameters:
        request -- An HttpRequest, with the current user, the geolevel, and
        the pipe-separated geounit list.
        planid -- The plan ID that contains the district.
        districtid -- The district ID to which the geounits will be added.

    Returns:
        A JSON HttpResponse that contains the number of districts modified,
        or an error message if adding fails.
    """
    note_session_activity(request)

    status = { 'success': False }

    if len(request.REQUEST.items()) >= 2:
        try:
            geolevel = request.REQUEST["geolevel"]
            geounit_ids = string.split(request.REQUEST["geounits"], "|")
            plan = Plan.objects.get(pk=planid,owner=request.user)
        except:
            status['exception'] = traceback.format_exc()
            status['message'] = _('Could not add units to district.')

        # get the version from the request or the plan
        if 'version' in request.REQUEST:
            version = request.REQUEST['version']
        else:
            version = plan.version

        try:
            fixed = plan.add_geounits(districtid, geounit_ids, geolevel, version)
            status['success'] = True;
            status['message'] = _('Updated %(num_fixed_districts)d districts') \
                % {'num_fixed_districts': fixed}
            status['updated'] = fixed
            plan = Plan.objects.get(pk=planid,owner=request.user)
            status['edited'] = getutc(plan.edited).isoformat()
            status['version'] = plan.version
        except Exception, ex: 
            status['exception'] = traceback.format_exc()
            status['message'] = _('Could not add units to district.')
            logger.warn('Could not add units to district')
            logger.debug('Reason: %s', ex)

    else:
        status['message'] = _("Geounits weren't found in a district.")

    return HttpResponse(json.dumps(status),mimetype='application/json')
dataset/ETHPy150Open PublicMapping/DistrictBuilder/django/publicmapping/redistricting/views.py/addtodistrict/original