Exemplo n.º 1
0
 def test_get_instance_or_create(self):
     c = get_instance_or_create(Court, name='Bundesverfassungsgericht')
     self.assertEqual(c.code, 'BVerfG')
Exemplo n.º 2
0
    def handle(self, *args, **options):

        # if options['verbose']:
        #     root_logger.setLevel(logging.DEBUG)

        # Country identical for all courts
        self.country = get_instance_or_create(Country, 'Deutschland')

        # Delete all courts
        if options['empty']:
            self.empty()

        # Court types
        # type_mapping = CourtTypes.get_name_to_code_mapping()
        previous_state_name = None
        previous_state = None
        without_type_counter = 0
        court_counter = 0
        city_counter = 0
        state_counter = 0

        if not os.path.isfile(options['input']):
            logger.error('Cannot read from: %s' % options['input'])
            exit(1)

        logger.debug('Reading from: %s' % options['input'])

        with open(options['input']) as f:
            reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
            for row in reader:
                if len(row) != 3 or reader.line_num == 1 or row[1] == '':
                    continue

                if 0 < options['limit'] <= reader.line_num:
                    logger.debug('Limit reached')
                    break

                name = row[1].replace('_', '').strip()
                code = re.sub('[^0-9a-zA-Z]+', '', row[2])
                # code = row[2].replace(' ', '')

                # Fetch state
                state_name = row[0]
                if previous_state is not None and previous_state_name == state_name:
                    state = previous_state
                else:
                    try:
                        state = State.objects.get(name=state_name)

                    except State.DoesNotExist:
                        state = State(name=state_name, country=self.country)
                        state.save()
                        state_counter += 1

                # Fetch city and court type
                city = None
                court_type = Court.extract_type_code_from_name(name)

                if court_type is None:
                    logger.debug('Court type is none: %s' % row)
                    without_type_counter += 1
                else:
                    if CourtLocationLevel.CITY in CourtTypes().get_type(
                            court_type)['levels']:
                        # Remove court type (left over is city name)
                        city_name = name.replace(court_type, '').strip()
                        city_name = city_name.replace(
                            CourtTypes().get_type(court_type)['name'],
                            '').strip()

                        try:
                            city = City.objects.get(name=city_name,
                                                    state=state)
                        except City.DoesNotExist:
                            city = City(name=city_name, state=state)
                            city.save()
                            city_counter += 1

                # Save court
                # try:
                court = Court(name=name,
                              code=code,
                              state=state,
                              city=city,
                              court_type=court_type)
                court.save()
                court_counter += 1

                logger.debug('Saved court: %s' % court)

                # except IntegrityError as e:
                #     logger.error('Cannot save court: %s' % e)

                previous_state = state
                previous_state_name = state_name

        logger.info(
            'Done. Courts: %i; Without types: %i; Cities: %i, States: %i' %
            (court_counter, without_type_counter, city_counter, state_counter))