def handle(self, *args, **options):
        self.stdout.write("\n------------------------------------------\n")
        self.stdout.write("\n1. First row is assumed to have column names, and is skipped while loading data")
        self.stdout.write(
            "\n2. Assumed field order: State, State FIPS, County FIPS, Complete FIPS, County Name, GSE Limit, FHA Limit, VA Limit"
        )
        self.stdout.write("\n3. All current data in countylimits_(state|county|countylimit) tables will be deleted")
        self.stderr.write('\n If you read the above comments and agree, call the command with "--confirm=y" option\n')
        self.stdout.write("\n------------------------------------------\n")

        if not options.get("confirmed") or options["confirmed"].lower() != "y":
            return

        if len(args) > 0:
            try:
                with open(args[0], "rU") as csvfile:
                    csvreader = csv.reader(csvfile, delimiter=",", quotechar='"')
                    states = {}
                    counties = {}

                    CountyLimit.objects.all().delete()
                    County.objects.all().delete()
                    State.objects.all().delete()

                    i = 0
                    for row in csvreader:
                        if i == 0:
                            i += 1
                            continue
                        state, state_fips, county_fips, complete_fips, county, gse, fha, va = row
                        if state not in states:
                            s = State(state_abbr=state, state_fips=state_fips)
                            s.save()
                            states[state] = s.id

                        if complete_fips not in counties:
                            c = County(county_name=county, county_fips=county_fips, state_id=states[state])
                            c.save()
                            counties[complete_fips] = c.id

                        cl = CountyLimit(fha_limit=fha, gse_limit=gse, va_limit=va, county_id=counties[complete_fips])
                        cl.save()

                self.stdout.write("\nSuccessfully loaded data from %s\n\n" % args[0])
            except IOError as e:
                raise CommandError(e)
        else:
            raise CommandError("A path to a CSV county limits file is required.")
def county_limits(request):
    """ Return all counties with their limits per state. """
    if request.method == 'GET':
        package = {'request': {}, 'data': []}
        if 'state' in request.GET:
            state = package['request']['state'] = request.GET['state']
            package['data'] = CountyLimit.county_limits_by_state(state)
            return Response(package)
        else:
            return Response({'detail': 'Required parameter state is missing'}, status=status.HTTP_400_BAD_REQUEST)
Example #3
0
def county_limits(request):
    """ Return all counties with their limits per state. """
    if request.method == 'GET':
        package = {'request': {}, 'data': []}
        if 'state' in request.GET:
            state = package['request']['state'] = request.GET['state']
            package['data'] = CountyLimit.county_limits_by_state(state)
            return Response(package)
        else:
            return Response({'detail': 'Required parameter state is missing'},
                            status=status.HTTP_400_BAD_REQUEST)
 def populate_db(self):
     """ Prepopulate DB with dummy data. """
     sDC = State(state_abbr='DC', state_fips=11)
     sDC.save()
     sVA = State(state_abbr='VA', state_fips=22)
     sVA.save()
     cDC1 = County(county_name='DC County 1', county_fips=333, state_id=sDC.id)
     cDC1.save()
     cDC2 = County(county_name='DC County 2', county_fips=444, state_id=sDC.id)
     cDC2.save()
     cVA1 = County(county_name='VA County 1', county_fips=555, state_id=sVA.id)
     cVA1.save()
     cl1 = CountyLimit(county=cDC1, fha_limit=1, gse_limit=1, va_limit=1)
     cl1.save()
     cl2 = CountyLimit(county=cDC2, fha_limit=10, gse_limit=10, va_limit=10)
     cl2.save()
     cl3 = CountyLimit(county=cVA1, fha_limit=100, gse_limit=100, va_limit=100)
     cl3.save()
    def handle(self, *args, **options):
        self.stdout.write('\n------------------------------------------\n')
        self.stdout.write(
            '\nIf loading a CSV, there are 3 things to know:\n')
        self.stdout.write(
            '1. You can pass in a path, or `latest` to load the latest CSV\n'
            '   Passing `--csv=latest` is the same as passing:\n'
            '   `--csv=countylimits/data/county_limit_data_latest.csv`')
        self.stdout.write(
            "2. The CSV's first row is assumed to be the column names, "
            "and it is skipped when loading data")
        self.stdout.write(
            '3. This field order is assumed: \n'
            '    State,\n'
            '    State FIPS,\n'
            '    County FIPS,\n'
            '    Complete FIPS,\n'
            '    County Name,\n'
            '    GSE Limit,\n'
            '    FHA Limit,\n'
            '    VA Limit\n')
        self.stdout.write(
            '\nAlso Note:\n'
            '- All current data will be deleted from these '
            'tables: countylimits_(state|county|countylimit)')
        self.stdout.write(
            '- If you provide no `--csv` argument, data will be '
            'loaded from the `countylimit_data.json` fixture\n')
        self.stdout.write(
            '- If you provide both `--csv=latest` and `--dumpdata=true`,'
            'the loaded CSV data will dumped as `countylimit_data.json`\n')
        self.stderr.write(
            '\n If you read the above comments and agree, '
            'call your command again with the "--confirm=y" option\n')
        self.stdout.write('\n------------------------------------------\n')

        if not options.get('confirmed') or options['confirmed'].lower() != 'y':
            return

        if options.get('csv'):
            # Users can pass in `latest` or their own CSV path
            csv_file = DEFAULT_CSV.get(options.get('csv'), options.get('csv'))
            try:
                with open(csv_file, 'rU') as csvfile:
                    csvreader = csv.reader(
                        csvfile, delimiter=',', quotechar='"')
                    states = {}
                    counties = {}

                    CountyLimit.objects.all().delete()
                    County.objects.all().delete()
                    State.objects.all().delete()

                    i = 0
                    for row in csvreader:
                        if i == 0:
                            i += 1
                            continue
                        (state, state_fips, county_fips,
                         complete_fips, county, gse, fha, va) = row
                        if state not in states:
                            s = State(state_abbr=state, state_fips=state_fips)
                            s.save()
                            states[state] = s.id

                        if complete_fips not in counties:
                            c = County(
                                county_name=county,
                                county_fips=county_fips,
                                state_id=states[state]
                            )
                            c.save()
                            counties[complete_fips] = c.id

                        cl = CountyLimit(
                            fha_limit=fha,
                            gse_limit=gse,
                            va_limit=va,
                            county_id=counties[complete_fips]
                        )
                        cl.save()
                self.stdout.write(
                    '\nSuccessfully loaded data for {} counties from '
                    '{}\n\n'.format(CountyLimit.objects.count(), csv_file)
                )
                if options.get('dumpdata') == 'true':
                    dump_countylimit_fixture()
            except IOError as e:
                raise CommandError(e)
        else:
            CountyLimit.objects.all().delete()
            County.objects.all().delete()
            State.objects.all().delete()
            call_command(
                'loaddata',
                DEFAULT_COUNTYLIMIT_FIXTURE,
                app_label='countylimits')
            self.stdout.write(
                '\nSuccessfully loaded data for {} counties from {}'.format(
                    CountyLimit.objects.count(), DEFAULT_COUNTYLIMIT_FIXTURE)
            )
Example #6
0
 def populate_db(self):
     """ Prepopulate DB with dummy data. """
     sDC = State(state_abbr='DC', state_fips=11)
     sDC.save()
     sVA = State(state_abbr='VA', state_fips=22)
     sVA.save()
     cDC1 = County(county_name='DC County 1', county_fips=333, state_id=sDC.id)
     cDC1.save()
     cDC2 = County(county_name='DC County 2', county_fips=444, state_id=sDC.id)
     cDC2.save()
     cVA1 = County(county_name='VA County 1', county_fips=555, state_id=sVA.id)
     cVA1.save()
     cl1 = CountyLimit(county=cDC1, fha_limit=1, gse_limit=1, va_limit=1)
     cl1.save()
     cl2 = CountyLimit(county=cDC2, fha_limit=10, gse_limit=10, va_limit=10)
     cl2.save()
     cl3 = CountyLimit(county=cVA1, fha_limit=100, gse_limit=100, va_limit=100)
     cl3.save()
Example #7
0
    def handle(self, *args, **options):
        self.stdout.write('\n------------------------------------------\n')
        self.stdout.write(
            '\n1. First row is assumed to have column names, and is skipped while loading data'
        )
        self.stdout.write(
            '\n2. Assumed field order: State, State FIPS, County FIPS, Complete FIPS, County Name, GSE Limit, FHA Limit, VA Limit'
        )
        self.stdout.write(
            '\n3. All current data in countylimits_(state|county|countylimit) tables will be deleted'
        )
        self.stderr.write(
            '\n If you read the above comments and agree, call the command with "--confirm=y" option\n'
        )
        self.stdout.write('\n------------------------------------------\n')

        if 'confirmed' not in options or options['confirmed'].lower() != 'y':
            return

        if len(args) > 0:
            try:
                with open(args[0], 'rU') as csvfile:
                    csvreader = csv.reader(csvfile,
                                           delimiter=',',
                                           quotechar='"')
                    states = {}
                    counties = {}

                    CountyLimit.objects.all().delete()
                    County.objects.all().delete()
                    State.objects.all().delete()

                    i = 0
                    for row in csvreader:
                        if i == 0:
                            i += 1
                            continue
                        state, state_fips, county_fips, complete_fips, county, gse, fha, va = row
                        if state not in states:
                            s = State(state_abbr=state, state_fips=state_fips)
                            s.save()
                            states[state] = s.id

                        if complete_fips not in counties:
                            c = County(county_name=county,
                                       county_fips=county_fips,
                                       state_id=states[state])
                            c.save()
                            counties[complete_fips] = c.id

                        cl = CountyLimit(fha_limit=fha,
                                         gse_limit=gse,
                                         va_limit=va,
                                         county_id=counties[complete_fips])
                        cl.save()

                self.stdout.write('\nSuccessfully loaded data from %s\n\n' %
                                  args[0])
            except IOError as e:
                raise CommandError(e)
        else:
            raise CommandError(
                'A path to a CSV county limits file is required.')
Example #8
0
    def handle(self, *args, **options):
        self.stdout.write('\n------------------------------------------\n')
        self.stdout.write('\nIf loading a CSV, there are 3 things to know:\n')
        self.stdout.write(
            '1. You can pass in a path, or `latest` to load the latest CSV\n'
            '   Passing `--csv=latest` is the same as passing:\n'
            '   `--csv=countylimits/data/county_limit_data_latest.csv`')
        self.stdout.write(
            "2. The CSV's first row is assumed to be the column names, "
            "and it is skipped when loading data")
        self.stdout.write('3. This field order is assumed: \n'
                          '    State,\n'
                          '    State FIPS,\n'
                          '    County FIPS,\n'
                          '    Complete FIPS,\n'
                          '    County Name,\n'
                          '    GSE Limit,\n'
                          '    FHA Limit,\n'
                          '    VA Limit\n')
        self.stdout.write('\nAlso Note:\n'
                          '- All current data will be deleted from these '
                          'tables: countylimits_(state|county|countylimit)')
        self.stdout.write('- If you provide no `--csv` argument, data will be '
                          'loaded from the `countylimit_data.json` fixture\n')
        self.stdout.write(
            '- If you provide both `--csv=latest` and `--dumpdata=true`,'
            'the loaded CSV data will dumped as `countylimit_data.json`\n')
        self.stderr.write(
            '\n If you read the above comments and agree, '
            'call your command again with the "--confirm=y" option\n')
        self.stdout.write('\n------------------------------------------\n')

        if not options.get('confirmed') or options['confirmed'].lower() != 'y':
            return

        if options.get('csv'):
            # Users can pass in `latest` or their own CSV path
            csv_file = DEFAULT_CSV.get(options.get('csv'), options.get('csv'))
            try:
                with open(csv_file, 'rU') as csvfile:
                    csvreader = csv.reader(csvfile,
                                           delimiter=',',
                                           quotechar='"')
                    states = {}
                    counties = {}

                    CountyLimit.objects.all().delete()
                    County.objects.all().delete()
                    State.objects.all().delete()

                    i = 0
                    for row in csvreader:
                        if i == 0:
                            i += 1
                            continue
                        (state, state_fips, county_fips, complete_fips, county,
                         gse, fha, va) = row
                        if state not in states:
                            s = State(state_abbr=state, state_fips=state_fips)
                            s.save()
                            states[state] = s.id

                        if complete_fips not in counties:
                            c = County(county_name=county,
                                       county_fips=county_fips,
                                       state_id=states[state])
                            c.save()
                            counties[complete_fips] = c.id

                        cl = CountyLimit(fha_limit=fha,
                                         gse_limit=gse,
                                         va_limit=va,
                                         county_id=counties[complete_fips])
                        cl.save()
                self.stdout.write(
                    '\nSuccessfully loaded data for {} counties from '
                    '{}\n\n'.format(CountyLimit.objects.count(), csv_file))
                if options.get('dumpdata') == 'true':
                    dump_countylimit_fixture()
            except IOError as e:
                raise CommandError(e)
        else:
            CountyLimit.objects.all().delete()
            County.objects.all().delete()
            State.objects.all().delete()
            call_command('loaddata',
                         DEFAULT_COUNTYLIMIT_FIXTURE,
                         app_label='countylimits')
            self.stdout.write(
                '\nSuccessfully loaded data for {} counties from {}'.format(
                    CountyLimit.objects.count(), DEFAULT_COUNTYLIMIT_FIXTURE))