@login_required
@unique_session_or_json_redirect
@transaction.commit_manually
def add_districts_to_plan(request, planid):
    """
    This handler is used to paste existing districts from one
    plan into another plan
    
    Parameters:
        request -- An HttpRequest object including a list of districtids and
            a version
        planid -- The plan into which to paste the districts

    Returns:
        Some JSON explaining the success or failure of the paste operation
    """

    status = { 'success': False }

    # Make sure we can edit the given plan
    try:
        plan = Plan.objects.get(pk=planid)
    except:
        status['message'] = _('No plan with the given id')
        return HttpResponse(json.dumps(status),mimetype='application/json')

    if not can_edit(request.user, plan):
        status['message'] = _("User can't edit the given plan")
        return HttpResponse(json.dumps(status),mimetype='application/json')
    
    # Get the districts we want to merge
    district_list = request.POST.getlist('districts[]')
    if len(district_list) == 0:
        status['message'] = _("No districts selected to add to the given plan")
        return HttpResponse(json.dumps(status),mimetype='application/json')
    else:
        districts = District.objects.filter(id__in=district_list)
        version = int(request.POST.get('version', None))
        status['message'] = _('Going to merge %(number_of_merged_districts)d' \
            ' districts') % {'number_of_merged_districts': len(districts)}
    
    # Check to see if we have enough room to add these districts without
    # going over MAX_DISTRICTS for the legislative_body
    allowed_districts = plan.get_available_districts(version=version)
    
    if allowed_districts > len(districts):
        status['message'] = _('Tried to merge too many districts; ' \
            '%(allowed_districts)d slots left') % {'allowed_districts': allowed_districts}

    # Everything checks out, let's paste those districts
    try:
        results = plan.paste_districts(districts, version=version)
        transaction.commit()
        status['success'] = True
        status['message'] = _('Merged %(num_merged_districts)d districts') % {'num_merged_districts': len(results)}
        status['version'] = plan.version
    except Exception as ex:
        transaction.rollback()
        status['message'] = str(ex)
        status['exception'] = traceback.format_exc()

    return HttpResponse(json.dumps(status),mimetype='application/json')
dataset/ETHPy150Open PublicMapping/DistrictBuilder/django/publicmapping/redistricting/views.py/add_districts_to_plan/swapped operands of `>`