@task
@transaction.commit_manually
def verify_count(upload_id, localstore, language):
    """
    Initialize the verification process by counting the number of geounits
    in the uploaded file. After this step completes, the verify_preload
    method is called.

    Parameters:
        upload_id - The id of the SubjectUpload record.
        localstore - a new subject file that remains when the task is complete
        language - Optional. If provided, translate the status messages
            into the specified language (if message files are complete).
    """
    reader = csv.DictReader(open(localstore,'r'))

    if len(reader.fieldnames) < 2:
        msg = _('There are missing columns in the uploaded Subject file')

        return {'task_id':None, 'success':False, 'messages':[msg]}
        

    upload = SubjectUpload.objects.get(id=upload_id)
    upload.subject_name = reader.fieldnames[1][0:50]
    upload.save()
    transaction.commit()

    logger.debug('Created new SubjectUpload transaction record for "%s".', upload.subject_name)

    # do this in bulk!
    # insert upload_id, portable_id, number
    sql = 'INSERT INTO "%s" ("%s","%s","%s") VALUES (%%(upload_id)s, %%(geoid)s, %%(number)s)' % (SubjectStage._meta.db_table, SubjectStage._meta.fields[1].attname, SubjectStage._meta.fields[2].attname, SubjectStage._meta.fields[3].attname)
    args = []

    try:
        for row in reader:
            args.append( {'upload_id':upload.id, 'geoid':row[reader.fieldnames[0]].strip(), 'number':row[reader.fieldnames[1]].strip()} )
            # django ORM takes about 320s for 280K geounits
            #SubjectStage(upload=upload, portable_id=row[reader.fieldnames[0]],number=row[reader.fieldnames[1]]).save()

        # direct access to db-api takes about 60s for 280K geounits
        cursor = connection.cursor()
        cursor.executemany(sql, tuple(args))

        logger.debug('Bulk loaded CSV records into the staging area.')
    except __HOLE__, aex:
        msg = _('There are an incorrect number of columns in the uploaded '
            'Subject file')

        transaction.rollback()
        return {'task_id':None, 'success':False, 'messages':[msg]}
    except Exception, ex:
        msg = _('Invalid data detected in the uploaded Subject file')

        transaction.rollback()
        return {'task_id':None, 'success':False, 'messages':[msg]}

    nlines = upload.subjectstage_set.all().count()
    geolevel, nunits = LegislativeLevel.get_basest_geolevel_and_count()

    prev_lang = None
    if not language is None:
        prev_lang = get_language()
        activate(language)

    # Validation #1: if the number of geounits in the uploaded file
    # don't match the geounits in the database, the content is not valid
    if nlines != nunits:
        # The number of geounits in the uploaded file do not match the base geolevel geounits
        msg = _('There are an incorrect number of geounits in the uploaded Subject file. ')
        if nlines < nunits:
            missing = nunits - nlines
            msg += _n(
                'There is %(count)d geounit missing.', 
                'There are %(count)d geounits missing.',
                missing) % { 'count':missing }
        else:
            extra = nlines - nunits
            msg += _n(
                'There is %(count)d extra geounit.',
                'There are %(count)d extra geounits.',
                extra) % { 'count':extra }

        # since the transaction was never committed after all the inserts, this nullifies
        # all the insert statements, so there should be no quarantine to clean up
        transaction.rollback()

        logger.debug(msg)

        upload.status = 'ER'
        upload.save()

        status = {'task_id':None, 'success':False, 'messages':[msg]}

    else:
        # The next task will preload the units into the quarintine table
        task = verify_preload.delay(upload_id, language=language).task_id

        status = {'task_id':task, 'success':True, 'messages':[_('Verifying consistency of uploaded geounits ...')]}

    transaction.commit()

    # reset language to default
    if not prev_lang is None:
        activate(prev_lang)

    return status
dataset/ETHPy150Open PublicMapping/DistrictBuilder/django/publicmapping/redistricting/tasks.py/verify_count