Пример #1
0
    def clean(self):
        if 'center_selection_type' in self.cleaned_data:
            center_selection_type = self.cleaned_data['center_selection_type']

            if center_selection_type == 'by_constituency':
                # At least one constituency must be selected
                if not self.cleaned_data['constituencies']:
                    self.add_error('constituencies',
                                   ValidationError(_('You must select at least one constituency.')))
            elif center_selection_type == 'by_office':
                # At least one office must be selected
                if not self.cleaned_data['offices']:
                    self.add_error('offices',
                                   ValidationError(_('You must select at least one office.')))
            elif center_selection_type == 'by_center_select_list':
                # At least one center must be selected
                if not self.cleaned_data['center_select_list']:
                    self.add_error('center_select_list',
                                   ValidationError(_('You must select at least one centre.')))
            elif center_selection_type == 'by_center_text_list':
                # At least one center must be entered
                center_text = self.cleaned_data.get('center_text_list', '')
                if center_text:
                    invalid_center_ids = find_invalid_center_ids(center_text)
                    if invalid_center_ids:
                        invalid_center_ids = ', '.join(map(str, invalid_center_ids))
                        msg = _("These centre ids are invalid: {}").format(invalid_center_ids)
                        self.add_error('center_text_list', ValidationError(msg))
                else:
                    # Center text is empty or didn't validate
                    if 'center_text_list' not in self.errors:
                        self.add_error('center_text_list',
                                       ValidationError(_('You must enter at least one centre.')))
Пример #2
0
    def clean(self):
        if 'center_selection_type' in self.cleaned_data:
            center_selection_type = self.cleaned_data['center_selection_type']

            if center_selection_type == 'by_constituency':
                # At least one constituency must be selected
                if not self.cleaned_data['constituencies']:
                    self.add_error(
                        'constituencies',
                        ValidationError(
                            _('You must select at least one constituency.')))
            elif center_selection_type == 'by_office':
                # At least one office must be selected
                if not self.cleaned_data['offices']:
                    self.add_error(
                        'offices',
                        ValidationError(
                            _('You must select at least one office.')))
            elif center_selection_type == 'by_center_select_list':
                # At least one center must be selected
                if not self.cleaned_data['center_select_list']:
                    self.add_error(
                        'center_select_list',
                        ValidationError(
                            _('You must select at least one centre.')))
            elif center_selection_type == 'by_center_text_list':
                # At least one center must be entered
                center_text = self.cleaned_data.get('center_text_list', '')
                if center_text:
                    invalid_center_ids = find_invalid_center_ids(center_text)
                    if invalid_center_ids:
                        invalid_center_ids = ', '.join(
                            map(str, invalid_center_ids))
                        msg = _("These centre ids are invalid: {}").format(
                            invalid_center_ids)
                        self.add_error('center_text_list',
                                       ValidationError(msg))
                else:
                    # Center text is empty or didn't validate
                    if 'center_text_list' not in self.errors:
                        self.add_error(
                            'center_text_list',
                            ValidationError(
                                _('You must enter at least one centre.')))
Пример #3
0
    def handle(self, *args, **options):
        valid_phases = PHASES.keys()

        if len(args) != 1:
            msg = 'Please specify exactly one of the following phases: ' + ', '.join(
                valid_phases)
            raise CommandError(msg)

        phase = args[0].lower()
        if phase not in valid_phases:
            valid_phases = ', '.join(valid_phases)
            raise CommandError(
                'Phase must be one of {phases}'.format(phases=valid_phases))

        # Ensure center specification options don't conflict
        center_options = (
            'center_id_file',
            'center_ids',
            'office_id_file',
            'office_ids',
            'constituency_id_file',
            'constituency_ids',
        )
        center_options = [1 if options[name] else 0 for name in center_options]
        if sum(center_options) > 1:
            raise CommandError(
                'Please specify at most one center/office/constituency id option.'
            )

        center_ids = []
        office_ids = []
        constituency_ids = []

        # Was --center-id-list option specified?
        if options['center_ids']:
            center_ids = options['center_ids'].split(',')

        # Was --center-id-file option specified?
        if options['center_id_file']:
            center_ids = read_ids(options['center_id_file'])

        if center_ids:
            # One of the two --center-id-xxx options was specified
            try:
                validate_comma_delimited_ids(center_ids, True)
            except ValidationError:
                raise CommandError(
                    "At least one of the center ids is not valid.")

            center_ids = map(int, center_ids)
            invalid_center_ids = find_invalid_center_ids(center_ids)

            if invalid_center_ids:
                msg = "The following centers are not in the database: {}."
                raise CommandError(msg.format(invalid_center_ids))

        # Was the --office-id-list option specified?
        if options['office_ids']:
            office_ids = options['office_ids'].split(',')

        # Was the --office-id-file option specified?
        if options['office_id_file']:
            office_ids = read_ids(options['office_id_file'])

        if office_ids:
            # One of the two --office-id-xxx options was specified
            try:
                validate_comma_delimited_ids(office_ids)
            except ValidationError:
                raise CommandError(
                    "At least one of the office ids is not valid.")

            office_ids = map(int, office_ids)

            offices = Office.objects.filter(id__in=office_ids)

            valid_office_ids = [office.id for office in offices]

            invalid_office_ids = [
                id_ for id_ in office_ids if id_ not in valid_office_ids
            ]

            if invalid_office_ids:
                msg = "The following offices are not in the database: {}."
                raise CommandError(msg.format(invalid_office_ids))

        # Was the --constituency-id-list option specified?
        if options['constituency_ids']:
            constituency_ids = options['constituency_ids'].split(',')

        # Was the --constituency-id-file option specified?
        if options['constituency_id_file']:
            constituency_ids = read_ids(options['constituency_id_file'])

        if constituency_ids:
            # One of the two --constituency-id-xxx options was specified
            try:
                validate_comma_delimited_ids(constituency_ids)
            except ValidationError:
                raise CommandError(
                    "At least one of the constituency ids is not valid.")

            constituency_ids = map(int, constituency_ids)

            constituencies = Constituency.objects.filter(
                id__in=constituency_ids)

            valid_constituency_ids = [
                constituency.id for constituency in constituencies
            ]

            invalid_constituency_ids = [
                id_ for id_ in constituency_ids
                if id_ not in valid_constituency_ids
            ]

            if invalid_constituency_ids:
                msg = "The following constituencies are not in the database: {}."
                raise CommandError(msg.format(invalid_constituency_ids))

        if center_ids:
            centers = RegistrationCenter.objects.filter(
                center_id__in=center_ids)
        elif office_ids:
            centers = RegistrationCenter.objects.filter(
                office_id__in=office_ids)
        elif constituency_ids:
            centers = RegistrationCenter.objects.filter(
                constituency_id__in=constituency_ids)
        else:
            centers = RegistrationCenter.objects.all()

        centers = centers.filter(reg_open=True).prefetch_related('office')

        # Converting centers from a queryset to a list isn't necessary, but it makes testing easier
        # because I can ask mock to compare Job.__init__() call args to simple lists instead of
        # constructing querysets.
        centers = list(centers)

        if not centers:
            raise CommandError(
                "The criteria you provided match no active centers.")

        username = os.environ.get('USER', 'unknown')

        output_path = os.path.join((options['output_root'] or os.getcwd()),
                                   get_job_name(username))
        output_path = os.path.expanduser(output_path)

        input_arguments = {
            'phase': phase,
            'forgive_no_voters': options['forgive_no_voters'],
            'forgive_no_office': options['forgive_no_office'],
            'office_ids': office_ids,
            'center_ids': center_ids,
            'constituency_ids': constituency_ids,
        }

        job = Job(phase, centers, input_arguments, username, output_path)

        # Ready to roll! (ha ha, get it?)
        try:
            job.generate_rolls()
        except Exception as exception:
            handle_job_exception(exception, job.output_path)

            raise CommandError(str(exception))
Пример #4
0
    def handle(self, *args, **options):
        valid_phases = PHASES.keys()

        if len(args) != 1:
            msg = 'Please specify exactly one of the following phases: ' + ', '.join(valid_phases)
            raise CommandError(msg)

        phase = args[0].lower()
        if phase not in valid_phases:
            valid_phases = ', '.join(valid_phases)
            raise CommandError('Phase must be one of {phases}'.format(phases=valid_phases))

        # Ensure center specification options don't conflict
        center_options = ('center_id_file', 'center_ids', 'office_id_file', 'office_ids',
                          'constituency_id_file', 'constituency_ids', )
        center_options = [1 if options[name] else 0 for name in center_options]
        if sum(center_options) > 1:
            raise CommandError('Please specify at most one center/office/constituency id option.')

        center_ids = []
        office_ids = []
        constituency_ids = []

        # Was --center-id-list option specified?
        if options['center_ids']:
            center_ids = options['center_ids'].split(',')

        # Was --center-id-file option specified?
        if options['center_id_file']:
            center_ids = read_ids(options['center_id_file'])

        if center_ids:
            # One of the two --center-id-xxx options was specified
            try:
                validate_comma_delimited_ids(center_ids, True)
            except ValidationError:
                raise CommandError("At least one of the center ids is not valid.")

            center_ids = map(int, center_ids)
            invalid_center_ids = find_invalid_center_ids(center_ids)

            if invalid_center_ids:
                msg = "The following centers are not in the database: {}."
                raise CommandError(msg.format(invalid_center_ids))

        # Was the --office-id-list option specified?
        if options['office_ids']:
            office_ids = options['office_ids'].split(',')

        # Was the --office-id-file option specified?
        if options['office_id_file']:
            office_ids = read_ids(options['office_id_file'])

        if office_ids:
            # One of the two --office-id-xxx options was specified
            try:
                validate_comma_delimited_ids(office_ids)
            except ValidationError:
                raise CommandError("At least one of the office ids is not valid.")

            office_ids = map(int, office_ids)

            offices = Office.objects.filter(id__in=office_ids)

            valid_office_ids = [office.id for office in offices]

            invalid_office_ids = [id_ for id_ in office_ids if id_ not in valid_office_ids]

            if invalid_office_ids:
                msg = "The following offices are not in the database: {}."
                raise CommandError(msg.format(invalid_office_ids))

        # Was the --constituency-id-list option specified?
        if options['constituency_ids']:
            constituency_ids = options['constituency_ids'].split(',')

        # Was the --constituency-id-file option specified?
        if options['constituency_id_file']:
            constituency_ids = read_ids(options['constituency_id_file'])

        if constituency_ids:
            # One of the two --constituency-id-xxx options was specified
            try:
                validate_comma_delimited_ids(constituency_ids)
            except ValidationError:
                raise CommandError("At least one of the constituency ids is not valid.")

            constituency_ids = map(int, constituency_ids)

            constituencies = Constituency.objects.filter(id__in=constituency_ids)

            valid_constituency_ids = [constituency.id for constituency in constituencies]

            invalid_constituency_ids = [id_ for id_ in constituency_ids if id_ not in
                                        valid_constituency_ids]

            if invalid_constituency_ids:
                msg = "The following constituencies are not in the database: {}."
                raise CommandError(msg.format(invalid_constituency_ids))

        if center_ids:
            centers = RegistrationCenter.objects.filter(center_id__in=center_ids)
        elif office_ids:
            centers = RegistrationCenter.objects.filter(office_id__in=office_ids)
        elif constituency_ids:
            centers = RegistrationCenter.objects.filter(constituency_id__in=constituency_ids)
        else:
            centers = RegistrationCenter.objects.all()

        centers = centers.filter(reg_open=True).prefetch_related('office')

        # Converting centers from a queryset to a list isn't necessary, but it makes testing easier
        # because I can ask mock to compare Job.__init__() call args to simple lists instead of
        # constructing querysets.
        centers = list(centers)

        if not centers:
            raise CommandError("The criteria you provided match no active centers.")

        username = os.environ.get('USER', 'unknown')

        output_path = os.path.join((options['output_root'] or os.getcwd()), get_job_name(username))
        output_path = os.path.expanduser(output_path)

        input_arguments = {'phase': phase,
                           'forgive_no_voters': options['forgive_no_voters'],
                           'forgive_no_office': options['forgive_no_office'],
                           'office_ids': office_ids,
                           'center_ids': center_ids,
                           'constituency_ids': constituency_ids,
                           }

        job = Job(phase, centers, input_arguments, username, output_path)

        # Ready to roll! (ha ha, get it?)
        try:
            job.generate_rolls()
        except Exception as exception:
            handle_job_exception(exception, job.output_path)

            raise CommandError(str(exception))