Пример #1
0
def new_demographic_row(department_id):
    department = Department.get_by_id(department_id)
    if not department:
        abort(404)

    DemographicValue.create(
        department_id=department_id,
        race=request.form["race"],
        count=int(request.form["count"]),
        department_value=request.form["department_or_city"] == "department")

    return redirect(
        url_for('department.edit_demographics', department_id=department_id))
Пример #2
0
def new_demographic_row(department_id):
    department = Department.get_by_id(department_id)
    if not department:
        abort(404)

    DemographicValue.create(
        department_id=department_id,
        race=request.form["race"],
        count=int(request.form["count"]),
        department_value=request.form["department_or_city"] == "department")

    return redirect(url_for(
        'department.edit_demographics', department_id=department_id
    ))
Пример #3
0
def delete_demographic_row(department_id, value_id):
    department = Department.get_by_id(department_id)
    value = DemographicValue.get_by_id(value_id)

    if not department or not value:
        abort(404)

    value.delete()

    return redirect(
        url_for('department.edit_demographics', department_id=department_id))
Пример #4
0
def delete_demographic_row(department_id, value_id):
    department = Department.get_by_id(department_id)
    value = DemographicValue.get_by_id(value_id)

    if not department or not value:
        abort(404)

    value.delete()

    return redirect(url_for(
        'department.edit_demographics', department_id=department_id
    ))
Пример #5
0
def test_client():
    ''' Erase the database and load in a full suite of test data
    '''
    if not prompt_bool(
            "Are you sure you want to destroy and recreate Comport's database?"
    ):
        return

    delete_everything()

    # create a fake PD and admin user
    department = Department.create(
        name="Izquierda Metropolitan Police Department",
        short_name="IMPD",
        load_defaults=True)
    user = User.create(username="******",
                       email="*****@*****.**",
                       password="******",
                       active=True,
                       is_admin=True)
    user.departments.append(department)
    user.save()

    # create some fake officer out on service data
    date_now = datetime.datetime.now()
    date_step = date_now - relativedelta(months=30)
    while date_step.year < date_now.year or date_step.month < date_now.month:
        DenominatorValue.create(
            department_id=department.id,
            month=date_step.month,
            year=date_step.year,
            officers_out_on_service=(100000 + (randint(0, 46000) - 23000)))
        date_step = date_step + relativedelta(months=1)

    # create some fake demographic data
    demo_template = [
        dict(race="Asian", city_factor=0.0194, dept_factor=0.0013),
        dict(race="Black", city_factor=0.2452, dept_factor=0.1402),
        dict(race="Hispanic", city_factor=0.0861, dept_factor=0.0253),
        dict(race="Other", city_factor=0.0699, dept_factor=0.0101),
        dict(race="White", city_factor=0.5794, dept_factor=0.8231)
    ]

    # for the city
    city_population = 100000 + round(100000 * ((randint(0, 16) / 100) - .08))
    for value in demo_template:
        DemographicValue.create(department_id=department.id,
                                race=value["race"],
                                count=round(city_population *
                                            value["city_factor"]),
                                department_value=False)

    # for the department
    dept_population = 1500 + round(1500 * ((randint(0, 16) / 100) - .08))
    for value in demo_template:
        DemographicValue.create(department_id=department.id,
                                race=value["race"],
                                count=round(dept_population *
                                            value["dept_factor"]),
                                department_value=True)

    # create a JSON test client and run it
    test_client = JSONTestClient()
    mutations = []
    # mutations.append(MissingDataMutator())
    # mutations.append(FuzzedDataMutator())
    # mutations.append(KnownBadDataMutator())
    # mutations.append(EmptyDataMutator())
    # mutations.append(CasingMutator())
    # mutations.append(CondenisngDateMutator())
    # mutations.append(GapDateMutator())

    test_client.run(department, mutations)
Пример #6
0
def load_test_data():
    department = Department.query.filter_by(
        name="Indianapolis Metropolitan Police Department",
        short_name="IMPD").first()
    if not department:
        department = Department.create(
            name="Indianapolis Metropolitan Police Department",
            short_name="IMPD")
    if not User.query.filter_by(username="******").first():
        User.create(username="******",
                    email="*****@*****.**",
                    password="******",
                    active=True,
                    department_id=department.id)

    for filename in glob.glob('data/testdata/complaints/complaints.csv'):
        with open(filename, 'rt') as f:
            reader = csv.DictReader(f)
            for complaint in reader:
                officer_identifier = hashlib.md5(
                    (complaint.get("OFFNUM", None) +
                     Config.SECRET_KEY).encode('UTF-8')).hexdigest()
                opaque_id = hashlib.md5(
                    (complaint.get("INCNUM", None) +
                     Config.SECRET_KEY).encode('UTF-8')).hexdigest()

                CitizenComplaint.create(
                    opaque_id=opaque_id,
                    department_id=department.id,
                    occured_date=parse_csv_date(
                        complaint.get("OCCURRED_DT", None)),
                    division=complaint.get("UDTEXT24A", None),
                    precinct=complaint.get("UDTEXT24B", None),
                    shift=complaint.get("UDTEXT24C", None),
                    beat=complaint.get("UDTEXT24D", None),
                    disposition=complaint.get("FINDING", None),
                    allegation_type=complaint.get("ALG_CLASS", None),
                    allegation=complaint.get("ALLEGATION", None),
                    census_tract=None,
                    resident_race=complaint.get("RACE", None),
                    officer_race=complaint.get("OFF_RACE", None),
                    resident_sex=complaint.get("SEX", None),
                    officer_sex=complaint.get("OFF_SEX", None),
                    officer_identifier=officer_identifier,
                    officer_years_of_service=complaint.get(
                        "OFF_YR_EMPLOY", None),
                    officer_age=complaint.get("OFF_AGE", None),
                    resident_age=complaint.get("CIT_AGE", None))

    for filename in glob.glob('data/testdata/uof/uof.csv'):
        with open(filename, 'rt') as f:
            reader = csv.DictReader(f)
            for incident in reader:
                officer_identifier = hashlib.md5(
                    (incident.get("OFFNUM", None) +
                     Config.SECRET_KEY).encode('UTF-8')).hexdigest()
                opaque_id = hashlib.md5(
                    (incident.get("INCNUM", None) +
                     Config.SECRET_KEY).encode('UTF-8')).hexdigest()

                UseOfForceIncident.create(
                    department_id=department.id,
                    opaque_id=opaque_id,
                    occured_date=parse_csv_date(
                        incident.get("OCCURRED_DT", None)),
                    division=incident.get("UDTEXT24A", None),
                    precinct=incident.get("UDTEXT24B", None),
                    shift=incident.get("UDTEXT24C", None),
                    beat=incident.get("UDTEXT24D", None),
                    disposition=incident.get("DISPOSITION", None),
                    census_tract=None,
                    officer_force_type=incident.get("UOF_FORCE_TYPE", None),
                    resident_resist_type=None,
                    officer_weapon_used=None,
                    resident_weapon_used=None,
                    service_type=incident.get("SERVICE_TYPE", None),
                    arrest_made=incident.get("CIT_ARRESTED", None),
                    arrest_charges=incident.get("CITCHARGE_TYPE", None),
                    resident_injured=incident.get("CIT_INJURED", None),
                    resident_hospitalized=incident.get("CIT_HOSPITAL", None),
                    officer_injured=incident.get("OFF_INJURED", None),
                    officer_hospitalized=incident.get("OFF_HOSPITAL", None),
                    use_of_force_reason=incident.get("UOF_REASON", None),
                    resident_race=incident.get("RACE", None),
                    officer_race=incident.get("OFF_RACE", None),
                    resident_sex=incident.get("SEX", None),
                    officer_sex=incident.get("OFF_SEX", None),
                    officer_identifier=officer_identifier,
                    officer_years_of_service=incident.get(
                        "OFF_YR_EMPLOY", None),
                    officer_age=incident.get("OFF_AGE", None),
                    resident_age=incident.get("CIT_AGE", None),
                    officer_condition=incident.get("OFF_COND_TYPE", None),
                    resident_condition=incident.get("CIT_COND_TYPE", None))

    for filename in glob.glob('data/testdata/ois/ois.csv'):
        with open(filename, 'rt') as f:
            reader = csv.DictReader(f)
            for incident in reader:
                officer_identifier = hashlib.md5(
                    (incident.get("OFFNUM", None) +
                     Config.SECRET_KEY).encode('UTF-8')).hexdigest()
                opaque_id = hashlib.md5(
                    (incident.get("INCNUM", None) +
                     Config.SECRET_KEY).encode('UTF-8')).hexdigest()

                OfficerInvolvedShooting.create(
                    department_id=department.id,
                    opaque_id=opaque_id,
                    occured_date=parse_csv_date(
                        incident.get("OCCURRED_DT", None)),
                    division=incident.get("UDTEXT24A", None),
                    precinct=incident.get("UDTEXT24B", None),
                    shift=incident.get("UDTEXT24C", None),
                    beat=incident.get("UDTEXT24D", None),
                    disposition=incident.get("DISPOSITION", None),
                    census_tract=None,
                    officer_weapon_used=incident.get("WEAPON_TYPE", None),
                    resident_weapon_used=incident.get("CIT_WEAPON_TYPE", None),
                    service_type=incident.get("SERVICE_TYPE", None),
                    resident_race=incident.get("RACE", None),
                    officer_race=incident.get("OFF_RACE", None),
                    resident_sex=incident.get("SEX", None),
                    officer_sex=incident.get("OFF_SEX", None),
                    officer_identifier=officer_identifier,
                    officer_years_of_service=incident.get(
                        "OFF_YR_EMPLOY", None),
                    officer_age=incident.get("OFF_AGE", None),
                    resident_age=incident.get("CIT_AGE", None),
                    officer_condition=incident.get("OFF_COND_TYPE", None),
                    resident_condition=incident.get("CIT_COND_TYPE", None))

    for filename in glob.glob('data/testdata/denominators/denominators.csv'):
        with open(filename, 'rt') as f:
            reader = csv.DictReader(f)
            for month in reader:
                DenominatorValue.create(department_id=department.id,
                                        month=month.get("month", None),
                                        year=month.get("year", None),
                                        officers_out_on_service=month.get(
                                            "officers out on service", None))

    for filename in glob.glob('data/testdata/demographics/demographics.csv'):
        with open(filename, 'rt') as f:
            reader = csv.DictReader(f)
            for value in reader:
                DemographicValue.create(department_id=department.id,
                                        race=value.get("race", None),
                                        count=value.get("count", None),
                                        department_value=value.get(
                                            "cityOrDepartment",
                                            None) == "department")
Пример #7
0
def load_test_data():
    department = Department.query.filter_by(name="Indianapolis Metropolitan Police Department", short_name="IMPD").first()
    if not department:
        department = Department.create(name="Indianapolis Metropolitan Police Department", short_name="IMPD")
    if not User.query.filter_by(username="******").first():
        User.create(username="******", email="*****@*****.**", password="******", active=True, department_id=department.id)

    for filename in glob.glob('data/testdata/complaints/complaints.csv'):
        with open(filename, 'rt') as f:
            reader = csv.DictReader(f)
            for complaint in reader:
                officer_identifier = hashlib.md5((complaint.get("OFFNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest()
                opaque_id = hashlib.md5((complaint.get("INCNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest()

                CitizenComplaint.create(
                    opaque_id=opaque_id,
                    department_id=department.id,
                    occured_date=parse_csv_date(complaint.get("OCCURRED_DT", None)),
                    division=complaint.get("UDTEXT24A", None),
                    precinct=complaint.get("UDTEXT24B", None),
                    shift=complaint.get("UDTEXT24C", None),
                    beat=complaint.get("UDTEXT24D", None),
                    disposition=complaint.get("FINDING", None),
                    allegation_type=complaint.get("ALG_CLASS", None),
                    allegation=complaint.get("ALLEGATION", None),
                    census_tract=None,
                    resident_race=complaint.get("RACE", None),
                    officer_race=complaint.get("OFF_RACE", None),
                    resident_sex=complaint.get("SEX", None),
                    officer_sex=complaint.get("OFF_SEX", None),
                    officer_identifier=officer_identifier,
                    officer_years_of_service=complaint.get("OFF_YR_EMPLOY", None),
                    officer_age=complaint.get("OFF_AGE", None),
                    resident_age=complaint.get("CIT_AGE", None)
                )

    for filename in glob.glob('data/testdata/uof/uof.csv'):
        with open(filename, 'rt') as f:
            reader = csv.DictReader(f)
            for incident in reader:
                officer_identifier = hashlib.md5((incident.get("OFFNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest()
                opaque_id = hashlib.md5((incident.get("INCNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest()

                UseOfForceIncident.create(
                    department_id=department.id,
                    opaque_id=opaque_id,
                    occured_date=parse_csv_date(incident.get("OCCURRED_DT", None)),
                    division=incident.get("UDTEXT24A", None),
                    precinct=incident.get("UDTEXT24B", None),
                    shift=incident.get("UDTEXT24C", None),
                    beat=incident.get("UDTEXT24D", None),
                    disposition=incident.get("DISPOSITION", None),
                    census_tract=None,
                    officer_force_type=incident.get("UOF_FORCE_TYPE", None),
                    resident_resist_type=None,
                    officer_weapon_used=None,
                    resident_weapon_used=None,
                    service_type=incident.get("SERVICE_TYPE", None),
                    arrest_made=incident.get("CIT_ARRESTED", None),
                    arrest_charges=incident.get("CITCHARGE_TYPE", None),
                    resident_injured=incident.get("CIT_INJURED", None),
                    resident_hospitalized=incident.get("CIT_HOSPITAL", None),
                    officer_injured=incident.get("OFF_INJURED", None),
                    officer_hospitalized=incident.get("OFF_HOSPITAL", None),
                    use_of_force_reason=incident.get("UOF_REASON", None),
                    resident_race=incident.get("RACE", None),
                    officer_race=incident.get("OFF_RACE", None),
                    resident_sex=incident.get("SEX", None),
                    officer_sex=incident.get("OFF_SEX", None),
                    officer_identifier=officer_identifier,
                    officer_years_of_service=incident.get("OFF_YR_EMPLOY", None),
                    officer_age=incident.get("OFF_AGE", None),
                    resident_age=incident.get("CIT_AGE", None),
                    officer_condition=incident.get("OFF_COND_TYPE", None),
                    resident_condition=incident.get("CIT_COND_TYPE", None)
                )

    for filename in glob.glob('data/testdata/ois/ois.csv'):
        with open(filename, 'rt') as f:
            reader = csv.DictReader(f)
            for incident in reader:
                officer_identifier = hashlib.md5((incident.get("OFFNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest()
                opaque_id = hashlib.md5((incident.get("INCNUM", None) + Config.SECRET_KEY).encode('UTF-8')).hexdigest()

                OfficerInvolvedShooting.create(
                    department_id=department.id,
                    opaque_id=opaque_id,
                    occured_date=parse_csv_date(incident.get("OCCURRED_DT", None)),
                    division=incident.get("UDTEXT24A", None),
                    precinct=incident.get("UDTEXT24B", None),
                    shift=incident.get("UDTEXT24C", None),
                    beat=incident.get("UDTEXT24D", None),
                    disposition=incident.get("DISPOSITION", None),
                    census_tract=None,
                    officer_weapon_used=incident.get("WEAPON_TYPE", None),
                    resident_weapon_used=incident.get("CIT_WEAPON_TYPE", None),
                    service_type=incident.get("SERVICE_TYPE", None),
                    resident_race=incident.get("RACE", None),
                    officer_race=incident.get("OFF_RACE", None),
                    resident_sex=incident.get("SEX", None),
                    officer_sex=incident.get("OFF_SEX", None),
                    officer_identifier=officer_identifier,
                    officer_years_of_service=incident.get("OFF_YR_EMPLOY", None),
                    officer_age=incident.get("OFF_AGE", None),
                    resident_age=incident.get("CIT_AGE", None),
                    officer_condition=incident.get("OFF_COND_TYPE", None),
                    resident_condition=incident.get("CIT_COND_TYPE", None)
                )

    for filename in glob.glob('data/testdata/denominators/denominators.csv'):
        with open(filename, 'rt') as f:
            reader = csv.DictReader(f)
            for month in reader:
                DenominatorValue.create(
                    department_id=department.id,
                    month=month.get("month", None),
                    year=month.get("year", None),
                    officers_out_on_service=month.get("officers out on service", None)
                )

    for filename in glob.glob('data/testdata/demographics/demographics.csv'):
        with open(filename, 'rt') as f:
            reader = csv.DictReader(f)
            for value in reader:
                DemographicValue.create(
                    department_id=department.id,
                    race=value.get("race", None),
                    count=value.get("count", None),
                    department_value=value.get("cityOrDepartment", None) == "department"
                )
Пример #8
0
def test_client():
    ''' Erase the database and load in a full suite of test data
    '''
    if not prompt_bool("Are you sure you want to destroy and recreate Comport's database?"):
        return

    delete_everything()

    # create a fake PD and admin user
    department = Department.create(name="Izquierda Metropolitan Police Department", short_name="IMPD", load_defaults=True)
    user = User.create(username="******", email="*****@*****.**", password="******", active=True, is_admin=True)
    user.departments.append(department)
    user.save()

    # create some fake officer out on service data
    date_now = datetime.datetime.now()
    date_step = date_now - relativedelta(months=30)
    while date_step.year < date_now.year or date_step.month < date_now.month:
        DenominatorValue.create(
            department_id=department.id,
            month=date_step.month,
            year=date_step.year,
            officers_out_on_service=(100000 + (randint(0, 46000) - 23000))
        )
        date_step = date_step + relativedelta(months=1)

    # create some fake demographic data
    demo_template = [
        dict(race="Asian", city_factor=0.0194, dept_factor=0.0013),
        dict(race="Black", city_factor=0.2452, dept_factor=0.1402),
        dict(race="Hispanic", city_factor=0.0861, dept_factor=0.0253),
        dict(race="Other", city_factor=0.0699, dept_factor=0.0101),
        dict(race="White", city_factor=0.5794, dept_factor=0.8231)
    ]

    # for the city
    city_population = 100000 + round(100000 * ((randint(0, 16) / 100) - .08))
    for value in demo_template:
        DemographicValue.create(
            department_id=department.id,
            race=value["race"],
            count=round(city_population * value["city_factor"]),
            department_value=False
        )

    # for the department
    dept_population = 1500 + round(1500 * ((randint(0, 16) / 100) - .08))
    for value in demo_template:
        DemographicValue.create(
            department_id=department.id,
            race=value["race"],
            count=round(dept_population * value["dept_factor"]),
            department_value=True
        )

    # create a JSON test client and run it
    test_client = JSONTestClient()
    mutations = []
    # mutations.append(MissingDataMutator())
    # mutations.append(FuzzedDataMutator())
    # mutations.append(KnownBadDataMutator())
    # mutations.append(EmptyDataMutator())
    # mutations.append(CasingMutator())
    # mutations.append(CondenisngDateMutator())
    # mutations.append(GapDateMutator())

    test_client.run(department, mutations)