@login_required
@unique_session_or_json_redirect
def newdistrict(request, planid):
    """
    Create a new district.

    The 'geolevel' parameter is required to create a new district. Geounits
    may be added to this new district by setting the 'geounits' key in the
    request.  

    Parameters:
        request - An HttpRequest, with the current user.
        planid - The plan id to which the district should be added.
    
    Returns:
        The new District's name and district_id.
    """
    note_session_activity(request)

    status = { 'success': False }
    if len(request.REQUEST.items()) >= 3:
        plan = Plan.objects.get(pk=planid, owner=request.user)

        if 'geolevel' in request.REQUEST:
            geolevel = request.REQUEST['geolevel']
        else:
            geolevel = None
        if 'geounits' in request.REQUEST:
            geounit_ids = string.split(request.REQUEST['geounits'], '|')
        else:
            geounit_ids = None

        if 'district_id' in request.REQUEST:
            district_id = int(request.REQUEST['district_id'])
        else:
            district_id = None

        if 'district_short' in request.REQUEST:
            district_short = request.REQUEST['district_short'][0:10]
        elif not district_id is None:
            district_short = plan.legislative_body.get_short_label() % {'district_id':district_id}
        else:
            district_short = None

        if 'district_long' in request.REQUEST:
            district_long = request.REQUEST['district_long'][0:256]
        elif not district_id is None:
            district_long = {'district_id':district_id} % plan.legislative_body.get_label()
        else:
            district_long = None

        if 'version' in request.REQUEST:
            version = request.REQUEST['version']
        else:
            version = plan.version

        if geolevel and geounit_ids and district_id:
            try: 
                # add the geounits selected to this district -- this will
                # create a new district w/1 version higher
                fixed = plan.add_geounits((district_id, district_short, district_long,), geounit_ids, geolevel, version)

                # if there are comments, types or multiple members, add them to the district
                district = plan.district_set.filter(district_id=district_id,short_label=district_short,long_label=district_long)[0]
                if plan.legislative_body.multi_members_allowed:
                    district.num_members = plan.legislative_body.min_multi_district_members
                    district.save()
                ct = ContentType.objects.get(app_label='redistricting',model='district')
                if 'comment' in request.POST and request.POST['comment'] != '':
                    comment = Comment(
                        object_pk=district.id, 
                        content_type=ct, 
                        site_id=Site.objects.get_current().id, 
                        user_name=request.user.username, 
                        user_email=request.user.email, 
                        comment=request.POST['comment'])
                    comment.save()

                if len(request.REQUEST.getlist('type[]')) > 0:
                    strtags = request.REQUEST.getlist('type[]')
                    for strtag in strtags:
                        if strtag == '':
                            continue
                        if strtag.count(' ') > 0:
                            strtag = '"type=%s"' % strtag
                        else:
                            strtag = 'type=%s' % strtag
                        Tag.objects.add_tag(district, strtag)

                status['success'] = True
                status['message'] = _('Created 1 new district')
                plan = Plan.objects.get(pk=planid, owner=request.user)
                status['edited'] = getutc(plan.edited).isoformat()
                status['district_id'] = district_id
                status['version'] = plan.version
            except ValidationError:
                status['message'] = _('Reached Max districts already')
            except Exception, ex:
                logger.warn('Error saving new district')
                logger.debug('Reason: %s', ex)
                status['message'] = _("Couldn't save new district.")
        else:
            status['message'] = _('Must specify name, geolevel, ' \
                'and geounit ids for new district.')
    return HttpResponse(json.dumps(status),mimetype='application/json')
dataset/ETHPy150Open PublicMapping/DistrictBuilder/django/publicmapping/redistricting/views.py/newdistrict/swapped operands of `%`