@task
def verify_preload(upload_id, language=None):
    prev_lang = None
    if not language is None:
        prev_lang = get_language()
        activate(language)

    upload = SubjectUpload.objects.get(id=upload_id)
    geolevel, nunits = LegislativeLevel.get_basest_geolevel_and_count()

    # This seizes postgres -- probably small memory limits.
    #aligned_units = upload.subjectstage_set.filter(portable_id__in=permanent_units).count()

    permanent_units = geolevel.geounit_set.all().order_by('portable_id').values_list('portable_id',flat=True)
    temp_units = upload.subjectstage_set.all().order_by('portable_id').values_list('portable_id',flat=True)

    # quick check: make sure the first and last items are aligned
    ends_match = permanent_units[0] == temp_units[0] and \
        permanent_units[permanent_units.count()-1] == temp_units[temp_units.count()-1]
    msg = _('There are a correct number of geounits in the uploaded Subject file, ')
    if not ends_match:
        msg += _('but the geounits do not have the same portable ids as those in the database.')

    # python foo here: count the number of zipped items in the 
    # permanent_units and temp_units lists that do not have the same portable_id
    # thus counting the portable_ids that are not mutually shared
    aligned_units = len(filter(lambda x:x[0] == x[1], zip(permanent_units, temp_units)))

    if ends_match and nunits != aligned_units:
        # The number of geounits in the uploaded file match, but there are some mismatches.
        mismatched = nunits - aligned_units
        msg += _n(
            'but %(count)d geounit does not match the geounits in the database.',
            'but %(count)d geounits do not match the geounits in the database.',
            mismatched) % { 'count':mismatched }

    if not ends_match or nunits != aligned_units:
        logger.debug(msg)

        upload.status = 'ER'
        upload.save()
        upload.subjectstage_set.all().delete()

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

    else:
        try:
            # The next task will load the units into the characteristic table
            task = copy_to_characteristics.delay(upload_id, language=language).task_id

            status = {'task_id':task, 'success':True, 'messages':[_('Copying records to characteristic table ...')]}

        except:
            logger.error("Couldn't copy characteristics: %s" %
                traceback.format_exc())
    # reset the language back to the default
    if not prev_lang is None:
        activate(prev_lang)

    return status
Continue the verification process by counting the number of geounits
dataset/ETHPy150Open PublicMapping/DistrictBuilder/django/publicmapping/redistricting/tasks.py/verify_preload