def add_or_update_incident(cls, department, incident): ''' Add a new OIS incident or update an existing one ''' row_added = False # get a cleaner instance cleaner = Cleaners() # capitalize the location incident["bureau"] = cleaner.capitalize(incident["bureau"]) incident["division"] = cleaner.capitalize(incident["division"]) incident["assignment"] = cleaner.capitalize(incident["assignment"]) # clean weapon, race, gender incident["residentWeaponUsed"] = cleaner.resident_weapon_used(incident["residentWeaponUsed"]) incident["residentSex"] = cleaner.sex(incident["residentSex"]) incident["residentRace"] = cleaner.race(incident["residentRace"]) incident["officerSex"] = cleaner.sex(incident["officerSex"]) incident["officerRace"] = cleaner.race(incident["officerRace"]) # make sure values that might've been sent as integers are strings incident["residentAge"] = cleaner.number_to_string(incident["residentAge"]) incident["officerAge"] = cleaner.number_to_string(incident["officerAge"]) # and values that might've been sent as strings are integers incident["officerYearsOfService"] = cleaner.string_to_integer(incident["officerYearsOfService"]) found_incident = False # found_incident = cls.query.filter_by( # opaque_id=incident["opaqueId"], # department_id=department.id, # officer_identifier=incident["officerIdentifier"] # ).first() if not found_incident: found_incident = cls.create( department_id=department.id, opaque_id=incident["opaqueId"] ) row_added = True found_incident.department_id = department.id found_incident.opaque_id = incident["opaqueId"] found_incident.occured_date = parse_date(incident["occuredDate"]) found_incident.bureau = incident["bureau"] found_incident.division = incident["division"] found_incident.assignment = incident["assignment"] found_incident.disposition = incident["disposition"] found_incident.resident_weapon_used = incident["residentWeaponUsed"] found_incident.officer_weapon_used = incident["officerWeaponUsed"] found_incident.service_type = incident["serviceType"] found_incident.resident_condition = incident["residentCondition"] found_incident.officer_condition = incident["officerCondition"] found_incident.resident_identifier = incident["residentIdentifier"] found_incident.resident_race = incident["residentRace"] found_incident.resident_sex = incident["residentSex"] found_incident.resident_age = incident["residentAge"] found_incident.officer_race = incident["officerRace"] found_incident.officer_sex = incident["officerSex"] found_incident.officer_age = incident["officerAge"] found_incident.officer_years_of_service = parse_int(incident["officerYearsOfService"]) found_incident.officer_identifier = incident["officerIdentifier"] found_incident.save() return row_added
def add_or_update_incident(cls, department, incident): ''' Add a new OIS incident or update an existing one ''' # get a cleaner instance cleaner = Cleaners() # capitalize the assignment incident["assignment"] = cleaner.capitalize(incident["assignment"]) # clean weapon, race, gender incident["residentWeaponUsed"] = cleaner.resident_weapon_used(incident["residentWeaponUsed"]) incident["residentSex"] = cleaner.sex(incident["residentSex"]) incident["residentRace"] = cleaner.race(incident["residentRace"]) incident["officerSex"] = cleaner.sex(incident["officerSex"]) incident["officerRace"] = cleaner.race(incident["officerRace"]) # make sure values that might've been sent as integers are strings incident["residentAge"] = cleaner.number_to_string(incident["residentAge"]) incident["officerAge"] = cleaner.number_to_string(incident["officerAge"]) # and values that might've been sent as strings are integers incident["officerYearsOfService"] = cleaner.string_to_integer(incident["officerYearsOfService"]) # check and set the incident last updated incident_kwargs = dict(opaque_id=incident["opaqueId"], department_id=department.id, incident_type="ois") incident_updated = IncidentsUpdated.query.filter_by(**incident_kwargs).first() # this is the first time we've encountered this incident in this update if not incident_updated: # delete this incident's rows from the database cls.query.filter_by(opaque_id=incident["opaqueId"]).delete() db.session.commit() # remember it for this incident's following rows in this update IncidentsUpdated.create(**incident_kwargs) cls.create( department_id=department.id, opaque_id=incident["opaqueId"], case_number=incident["caseNumber"], occured_date=parse_date(incident["occuredDate"]), assignment=incident["assignment"], has_disposition=incident["hasDisposition"], resident_weapon_used=incident["residentWeaponUsed"], officer_weapon_used=incident["officerWeaponUsed"], service_type=incident["serviceType"], resident_condition=incident["residentCondition"], officer_condition=incident["officerCondition"], resident_identifier=incident["residentIdentifier"], resident_race=incident["residentRace"], resident_sex=incident["residentSex"], resident_age=incident["residentAge"], officer_race=incident["officerRace"], officer_sex=incident["officerSex"], officer_age=incident["officerAge"], officer_years_of_service=parse_int(incident["officerYearsOfService"]), officer_identifier=incident["officerIdentifier"] ) # TODO: re-evaluate what this return value means return True
def add_or_update_incident(cls, department, incident): ''' Add a new Citizen Complaints incident or update an existing one ''' # get a cleaner instance cleaner = Cleaners() # make sure values that might've been sent as integers are strings incident["residentAge"] = cleaner.number_to_string(incident["residentAge"]) incident["officerAge"] = cleaner.number_to_string(incident["officerAge"]) incident["officerYearsOfService"] = cleaner.number_to_string(incident["officerYearsOfService"]) # capitalize all the fields in the incident incident = cleaner.capitalize_incident(incident) # clean sex & race incident["residentSex"] = cleaner.sex(incident["residentSex"]) incident["residentRace"] = cleaner.race(incident["residentRace"]) incident["officerSex"] = cleaner.sex(incident["officerSex"]) incident["officerRace"] = cleaner.race(incident["officerRace"]) # check and set the incident last updated incident_kwargs = dict(opaque_id=incident["opaqueId"], department_id=department.id, incident_type="complaints") incident_updated = IncidentsUpdated.query.filter_by(**incident_kwargs).first() # this is the first time we've encountered this incident in this update if not incident_updated: # delete this incident's rows from the database cls.query.filter_by(opaque_id=incident["opaqueId"]).delete() db.session.commit() # remember it for this incident's following rows in this update IncidentsUpdated.create(**incident_kwargs) # create the new incident row cls.create( department_id=department.id, opaque_id=incident["opaqueId"], occured_date=parse_date(incident["occuredDate"]), bureau=incident["bureau"], division=incident["division"], assignment=incident["assignment"], service_type=incident["serviceType"], source=incident["source"], allegation=incident["allegation"], disposition=incident["disposition"], resident_identifier=incident["residentIdentifier"], resident_race=incident["residentRace"], resident_sex=incident["residentSex"], resident_age=incident["residentAge"], officer_identifier=incident["officerIdentifier"], officer_race=incident["officerRace"], officer_sex=incident["officerSex"], officer_age=incident["officerAge"], officer_years_of_service=incident["officerYearsOfService"], ) # TODO: re-evaluate what this return value means return True
def test_parse_date(self): ''' Well formatted date is parsed, other inputs return None ''' # well formatted date is parsed assert parse_date("2015-10-17 00:00:00") == datetime( 2015, 10, 17, 0, 0) # other inputs return None assert parse_date(None) is None assert parse_date("None") is None assert parse_date("2015-10-17 00:00") is None assert parse_date(123.22) is None assert parse_date(datetime(2015, 10, 17, 0, 0)) is None
def use_of_force(): username = request.authorization.username extractor = Extractor.query.filter_by(username=username).first() department_id = extractor.first_department().id j = request.json added_rows = 0 updated_rows = 0 for incident in j['data']: division = Cleaners.capitalize(incident["division"]) precinct = Cleaners.capitalize(incident["precinct"]) shift = Cleaners.capitalize(incident["shift"]) beat = Cleaners.capitalize(incident["beat"]) officer_force_type = Cleaners.officer_force_type(incident["officerForceType"]) resident_race = Cleaners.race(incident["residentRace"]) resident_sex = Cleaners.sex(incident["residentSex"]) officer_race = Cleaners.race(incident["officerRace"]) officer_sex = Cleaners.sex(incident["officerSex"]) found_incident = UseOfForceIncident.query.filter_by( opaque_id=incident["opaqueId"], department_id=department_id, officer_identifier=incident["officerIdentifier"], officer_force_type=officer_force_type ).first() occured_date = parse_date(incident["occuredDate"]) if not found_incident: found_incident = UseOfForceIncident.create( department_id=department_id, opaque_id=incident["opaqueId"], occured_date=occured_date, division=division, precinct=precinct, shift=shift, beat=beat, disposition=incident["disposition"], census_tract=None, officer_force_type=officer_force_type, use_of_force_reason=incident["useOfForceReason"], service_type=incident["serviceType"], arrest_made=incident["arrestMade"], arrest_charges=incident["arrestCharges"], resident_weapon_used=incident["residentWeaponUsed"], resident_injured=incident["residentInjured"], resident_hospitalized=incident["residentHospitalized"], officer_injured=incident["officerInjured"], officer_hospitalized=incident["officerHospitalized"], resident_race=resident_race, resident_sex=resident_sex, resident_age=incident["residentAge"], resident_condition=incident["residentCondition"], officer_identifier=incident["officerIdentifier"], officer_race=officer_race, officer_sex=officer_sex, officer_age=incident["officerAge"], officer_years_of_service=incident["officerYearsOfService"], officer_condition=incident["officerCondition"] ) added_rows += 1 continue found_incident.department_id = department_id found_incident.opaque_id = incident["opaqueId"] found_incident.occured_date = occured_date found_incident.division = division found_incident.precinct = precinct found_incident.shift = shift found_incident.beat = beat found_incident.disposition = incident["disposition"] found_incident.census_tract = None found_incident.officer_force_type = officer_force_type found_incident.use_of_force_reason = incident["useOfForceReason"] found_incident.service_type = incident["serviceType"] found_incident.arrest_made = incident["arrestMade"] found_incident.arrest_charges = incident["arrestCharges"] found_incident.resident_weapon_used = incident["residentWeaponUsed"] found_incident.resident_injured = incident["residentInjured"] found_incident.resident_hospitalized = incident["residentHospitalized"] found_incident.officer_injured = incident["officerInjured"] found_incident.officer_hospitalized = incident["officerHospitalized"] found_incident.resident_race = resident_race found_incident.resident_sex = resident_sex found_incident.resident_age = incident["residentAge"] found_incident.resident_condition = incident["residentCondition"] found_incident.officer_identifier = incident["officerIdentifier"] found_incident.officer_race = officer_race found_incident.officer_sex = officer_sex found_incident.officer_age = incident["officerAge"] found_incident.officer_years_of_service = incident["officerYearsOfService"] found_incident.officer_condition = incident["officerCondition"] found_incident.save() updated_rows += 1 extractor.next_month = None extractor.next_year = None extractor.save() return json.dumps({"added": added_rows, "updated": updated_rows})
def complaints(): username = request.authorization.username extractor = Extractor.query.filter_by(username=username).first() department_id = extractor.first_department().id j = request.json added_rows = 0 updated_rows = 0 for incident in j['data']: # capitalize all the fields in the incident incident = Cleaners.capitalize_incident(incident) # clean sex & race resident_sex = Cleaners.sex(incident["residentSex"]) resident_race = Cleaners.race(incident["residentRace"]) officer_sex = Cleaners.sex(incident["officerSex"]) officer_race = Cleaners.race(incident["officerRace"]) found_incident = CitizenComplaint.query.filter_by( opaque_id=incident["opaqueId"], allegation_type=incident["allegationType"], allegation=incident["allegation"], officer_identifier=incident["officerIdentifier"], department_id=department_id, resident_race=resident_race, resident_sex=resident_sex, resident_age=incident["residentAge"] ).first() occured_date = parse_date(incident["occuredDate"]) if not found_incident: multiple_complaintant_check = CitizenComplaint.query.filter_by( opaque_id=incident["opaqueId"], allegation_type=incident["allegationType"], allegation=incident["allegation"], officer_identifier=incident["officerIdentifier"], department_id=department_id ).first() if multiple_complaintant_check: continue found_incident = CitizenComplaint.create( department_id=department_id, opaque_id=incident["opaqueId"], service_type=incident["serviceType"], source=incident["source"], occured_date=occured_date, division=incident["division"], precinct=incident["precinct"], shift=incident["shift"], beat=incident["beat"], allegation_type=incident["allegationType"], allegation=incident["allegation"], disposition=incident["disposition"], resident_race=resident_race, resident_sex=resident_sex, resident_age=incident["residentAge"], officer_identifier=incident["officerIdentifier"], officer_race=officer_race, officer_sex=officer_sex, officer_age=incident["officerAge"], officer_years_of_service=incident["officerYearsOfService"], census_tract=None ) added_rows += 1 continue found_incident.department_id = department_id found_incident.opaque_id = incident["opaqueId"] found_incident.service_type = incident["serviceType"] found_incident.source = incident["source"] found_incident.occured_date = occured_date found_incident.division = incident["division"] found_incident.precinct = incident["precinct"] found_incident.shift = incident["shift"] found_incident.beat = incident["beat"] found_incident.allegation_type = incident["allegationType"] found_incident.allegation = incident["allegation"] found_incident.disposition = incident["disposition"] found_incident.resident_race = resident_race found_incident.resident_sex = resident_sex found_incident.resident_age = incident["residentAge"] found_incident.officer_identifier = incident["officerIdentifier"] found_incident.officer_race = officer_race found_incident.officer_sex = officer_sex found_incident.officer_age = incident["officerAge"] found_incident.officer_years_of_service = incident["officerYearsOfService"] found_incident.census_tract = None found_incident.save() updated_rows += 1 extractor.next_month = None extractor.next_year = None extractor.save() return json.dumps({"added": added_rows, "updated": updated_rows})
def add_or_update_incident(cls, department, incident): ''' Add a new UOF incident or update an existing one ''' # get a cleaner instance cleaner = Cleaners() # capitalize the location incident["bureau"] = cleaner.capitalize(incident["bureau"]) incident["division"] = cleaner.capitalize(incident["division"]) incident["unit"] = cleaner.capitalize(incident["unit"]) incident["platoon"] = cleaner.capitalize(incident["platoon"]) # clean force type, race, gender incident["officerForceType"] = cleaner.officer_force_type(incident["officerForceType"]) incident["residentRace"] = cleaner.race(incident["residentRace"]) incident["residentSex"] = cleaner.sex(incident["residentSex"]) incident["officerRace"] = cleaner.race(incident["officerRace"]) incident["officerSex"] = cleaner.sex(incident["officerSex"]) # make sure values that might've been sent as integers are strings incident["residentAge"] = cleaner.number_to_string(incident["residentAge"]) incident["officerAge"] = cleaner.number_to_string(incident["officerAge"]) incident["officerYearsOfService"] = cleaner.number_to_string(incident["officerYearsOfService"]) # check and set the incident last updated incident_kwargs = dict(opaque_id=incident["opaqueId"], department_id=department.id, incident_type="uof") incident_updated = IncidentsUpdated.query.filter_by(**incident_kwargs).first() # this is the first time we've encountered this incident in this update if not incident_updated: # delete this incident's rows from the database cls.query.filter_by(opaque_id=incident["opaqueId"]).delete() db.session.commit() # remember it for this incident's following rows in this update IncidentsUpdated.create(**incident_kwargs) cls.create( department_id=department.id, opaque_id=incident["opaqueId"], occured_date=parse_date(incident["occuredDate"]), bureau=incident["bureau"], division=incident["division"], unit=incident["unit"], platoon=incident["platoon"], disposition=incident["disposition"], use_of_force_reason=incident["useOfForceReason"], officer_force_type=incident["officerForceType"], service_type=incident["serviceType"], arrest_made=incident["arrestMade"], arrest_charges=incident["arrestCharges"], resident_injured=incident["residentInjured"], resident_hospitalized=incident["residentHospitalized"], resident_condition=incident["residentCondition"], officer_injured=incident["officerInjured"], officer_hospitalized=incident["officerHospitalized"], officer_condition=incident["officerCondition"], resident_identifier=incident["residentIdentifier"], resident_race=incident["residentRace"], resident_sex=incident["residentSex"], resident_age=incident["residentAge"], officer_race=incident["officerRace"], officer_sex=incident["officerSex"], officer_age=incident["officerAge"], officer_years_of_service=incident["officerYearsOfService"], officer_identifier=incident["officerIdentifier"] ) # TODO: re-evaluate what this return value means return True
def add_or_update_incident(cls, department, incident): ''' Add a new OIS incident or update an existing one ''' row_added = False # get a cleaner instance cleaner = Cleaners() # capitalize the location incident["division"] = cleaner.capitalize(incident["division"]) incident["precinct"] = cleaner.capitalize(incident["precinct"]) incident["shift"] = cleaner.capitalize(incident["shift"]) incident["beat"] = cleaner.capitalize(incident["beat"]) # clean weapon, race, gender incident["residentWeaponUsed"] = cleaner.resident_weapon_used(incident["residentWeaponUsed"]) incident["residentSex"] = cleaner.sex(incident["residentSex"]) incident["residentRace"] = cleaner.race(incident["residentRace"]) incident["officerSex"] = cleaner.sex(incident["officerSex"]) incident["officerRace"] = cleaner.race(incident["officerRace"]) # make sure values that might've been sent as integers are strings incident["residentAge"] = cleaner.number_to_string(incident["residentAge"]) incident["officerAge"] = cleaner.number_to_string(incident["officerAge"]) # and values that might've been sent as strings are integers incident["officerYearsOfService"] = cleaner.string_to_integer(incident["officerYearsOfService"]) found_incident = cls.query.filter_by( opaque_id=incident["opaqueId"], department_id=department.id, officer_identifier=incident["officerIdentifier"] ).first() if not found_incident: found_incident = cls.create( department_id=department.id, opaque_id=incident["opaqueId"] ) row_added = True found_incident.department_id = department.id found_incident.opaque_id = incident["opaqueId"] found_incident.service_type = incident["serviceType"] found_incident.occured_date = parse_date(incident["occuredDate"]) found_incident.division = incident["division"] found_incident.precinct = incident["precinct"] found_incident.shift = incident["shift"] found_incident.beat = incident["beat"] found_incident.disposition = incident["disposition"] found_incident.resident_race = incident["residentRace"] found_incident.resident_sex = incident["residentSex"] found_incident.resident_age = incident["residentAge"] found_incident.resident_weapon_used = incident["residentWeaponUsed"] found_incident.resident_condition = incident["residentCondition"] found_incident.officer_identifier = incident["officerIdentifier"] found_incident.officer_weapon_used = incident["officerForceType"] found_incident.officer_race = incident["officerRace"] found_incident.officer_sex = incident["officerSex"] found_incident.officer_age = incident["officerAge"] found_incident.officer_years_of_service = parse_int(incident["officerYearsOfService"]) found_incident.officer_condition = incident["officerCondition"] found_incident.census_tract = None found_incident.save() return row_added
def add_or_update_incident(cls, department, incident): ''' Add a new Citizen Complaints incident or update an existing one ''' row_added = False # get a cleaner instance cleaner = Cleaners() # make sure values that might've been sent as integers are strings incident["residentAge"] = cleaner.number_to_string(incident["residentAge"]) incident["officerAge"] = cleaner.number_to_string(incident["officerAge"]) incident["officerYearsOfService"] = cleaner.number_to_string(incident["officerYearsOfService"]) # capitalize all the fields in the incident incident = cleaner.capitalize_incident(incident) # clean sex & race incident["residentSex"] = cleaner.sex(incident["residentSex"]) incident["residentRace"] = cleaner.race(incident["residentRace"]) incident["officerSex"] = cleaner.sex(incident["officerSex"]) incident["officerRace"] = cleaner.race(incident["officerRace"]) found_incident = cls.query.filter_by( opaque_id=incident["opaqueId"], allegation_type=incident["allegationType"], allegation=incident["allegation"], officer_identifier=incident["officerIdentifier"], department_id=department.id, resident_race=incident["residentRace"], resident_sex=incident["residentSex"], resident_age=incident["residentAge"] ).first() if not found_incident: # check for multiple complainants # :TODO: validate this practice! multiple_complaintant_check = cls.query.filter_by( opaque_id=incident["opaqueId"], allegation_type=incident["allegationType"], allegation=incident["allegation"], officer_identifier=incident["officerIdentifier"], department_id=department.id ).first() if multiple_complaintant_check: return None found_incident = cls.create( department_id=department.id, opaque_id=incident["opaqueId"] ) row_added = True found_incident.department_id = department.id found_incident.opaque_id = incident["opaqueId"] found_incident.service_type = incident["serviceType"] found_incident.source = incident["source"] found_incident.occured_date = parse_date(incident["occuredDate"]) found_incident.division = incident["division"] found_incident.precinct = incident["precinct"] found_incident.shift = incident["shift"] found_incident.beat = incident["beat"] found_incident.allegation_type = incident["allegationType"] found_incident.allegation = incident["allegation"] found_incident.disposition = incident["disposition"] found_incident.resident_race = incident["residentRace"] found_incident.resident_sex = incident["residentSex"] found_incident.resident_age = incident["residentAge"] found_incident.officer_identifier = incident["officerIdentifier"] found_incident.officer_race = incident["officerRace"] found_incident.officer_sex = incident["officerSex"] found_incident.officer_age = incident["officerAge"] found_incident.officer_years_of_service = incident["officerYearsOfService"] found_incident.census_tract = None found_incident.save() return row_added
def officer_involved_shooting(): username = request.authorization.username extractor = Extractor.query.filter_by(username=username).first() j = request.json added_rows = 0 updated_rows = 0 for incident in j['data']: resident_weapon_used = Cleaners.resident_weapon_used(incident["residentWeaponUsed"]) resident_sex = Cleaners.sex(incident["residentSex"]) resident_race = Cleaners.race(incident["residentRace"]) officer_sex = Cleaners.sex(incident["officerSex"]) officer_race = Cleaners.race(incident["officerRace"]) precinct = Cleaners.capitalize(incident["precinct"]) division = Cleaners.capitalize(incident["division"]) shift = Cleaners.capitalize(incident["shift"]) beat = Cleaners.capitalize(incident["beat"]) found_incident = OfficerInvolvedShooting.query.filter_by( opaque_id=incident["opaqueId"], department_id=extractor.department_id, officer_identifier =incident["officerIdentifier"] ).first() occured_date = parse_date(incident["occuredDate"]) if not found_incident: found_incident = OfficerInvolvedShooting.create( department_id = extractor.department_id, opaque_id = incident["opaqueId"], service_type =incident["serviceType"], occured_date = occured_date, division = division, precinct =precinct, shift = shift, beat =beat, disposition =incident["disposition"], resident_sex =resident_sex, resident_race =resident_race, resident_age = incident["residentAge"], resident_weapon_used = resident_weapon_used, resident_condition =incident["residentCondition"], officer_identifier =incident["officerIdentifier"], officer_weapon_used =incident["officerWeaponUsed"], officer_race =officer_race, officer_sex =officer_sex, officer_age = incident["officerAge"], officer_years_of_service =parse_int(incident["officerYearsOfService"]), officer_condition =incident["officerCondition"], census_tract = None ) added_rows += 1 continue found_incident.department_id = extractor.department_id, found_incident.opaque_id = incident["opaqueId"], found_incident.service_type =incident["serviceType"], found_incident.occured_date = occured_date, found_incident.division = division, found_incident.precinct =precinct, found_incident.shift = shift, found_incident.beat =beat, found_incident.disposition =incident["disposition"], found_incident.resident_sex =resident_sex, found_incident.resident_race =resident_race, found_incident.resident_age = incident["residentAge"], found_incident.resident_weapon_used =resident_weapon_used, found_incident.resident_condition =incident["residentCondition"], found_incident.officer_identifier =incident["officerIdentifier"], found_incident.officer_weapon_used =incident["officerWeaponUsed"], found_incident.officer_race =officer_race, found_incident.officer_sex =officer_sex, found_incident.officer_age = incident["officerAge"], found_incident.officer_years_of_service =parse_int(incident["officerYearsOfService"]), found_incident.officer_condition =incident["officerCondition"], found_incident.census_tract = None found_incident.save() updated_rows += 1 extractor.next_month = None extractor.next_year = None extractor.save() return json.dumps({"added": added_rows, "updated": updated_rows})
def use_of_force(): username = request.authorization.username extractor = Extractor.query.filter_by(username=username).first() department_id = extractor.first_department().id j = request.json added_rows = 0 updated_rows = 0 cleaner = Cleaners() for incident in j['data']: # capitalize the location incident["division"] = cleaner.capitalize(incident["division"]) incident["precinct"] = cleaner.capitalize(incident["precinct"]) incident["shift"] = cleaner.capitalize(incident["shift"]) incident["beat"] = cleaner.capitalize(incident["beat"]) # clean force type, race, gender incident["officerForceType"] = cleaner.officer_force_type(incident["officerForceType"]) incident["residentRace"] = cleaner.race(incident["residentRace"]) incident["residentSex"] = cleaner.sex(incident["residentSex"]) incident["officerRace"] = cleaner.race(incident["officerRace"]) incident["officerSex"] = cleaner.sex(incident["officerSex"]) # make sure values that might've been sent as integers are strings incident["residentAge"] = cleaner.number_to_string(incident["residentAge"]) incident["officerAge"] = cleaner.number_to_string(incident["officerAge"]) incident["officerYearsOfService"] = cleaner.number_to_string(incident["officerYearsOfService"]) found_incident = UseOfForceIncident.query.filter_by( opaque_id=incident["opaqueId"], department_id=department_id, officer_identifier=incident["officerIdentifier"], officer_force_type=incident["officerForceType"] ).first() occured_date = parse_date(incident["occuredDate"]) if not found_incident: found_incident = UseOfForceIncident.create( department_id=department_id, opaque_id=incident["opaqueId"], occured_date=occured_date, division=incident["division"], precinct=incident["precinct"], shift=incident["shift"], beat=incident["beat"], disposition=incident["disposition"], census_tract=None, officer_force_type=incident["officerForceType"], use_of_force_reason=incident["useOfForceReason"], service_type=incident["serviceType"], arrest_made=incident["arrestMade"], arrest_charges=incident["arrestCharges"], resident_weapon_used=incident["residentWeaponUsed"], resident_injured=incident["residentInjured"], resident_hospitalized=incident["residentHospitalized"], officer_injured=incident["officerInjured"], officer_hospitalized=incident["officerHospitalized"], resident_race=incident["residentRace"], resident_sex=incident["residentSex"], resident_age=incident["residentAge"], resident_condition=incident["residentCondition"], officer_identifier=incident["officerIdentifier"], officer_race=incident["officerRace"], officer_sex=incident["officerSex"], officer_age=incident["officerAge"], officer_years_of_service=incident["officerYearsOfService"], officer_condition=incident["officerCondition"] ) added_rows += 1 continue found_incident.department_id = department_id found_incident.opaque_id = incident["opaqueId"] found_incident.occured_date = occured_date found_incident.division = incident["division"] found_incident.precinct = incident["precinct"] found_incident.shift = incident["shift"] found_incident.beat = incident["beat"] found_incident.disposition = incident["disposition"] found_incident.census_tract = None found_incident.officer_force_type = incident["officerForceType"] found_incident.use_of_force_reason = incident["useOfForceReason"] found_incident.service_type = incident["serviceType"] found_incident.arrest_made = incident["arrestMade"] found_incident.arrest_charges = incident["arrestCharges"] found_incident.resident_weapon_used = incident["residentWeaponUsed"] found_incident.resident_injured = incident["residentInjured"] found_incident.resident_hospitalized = incident["residentHospitalized"] found_incident.officer_injured = incident["officerInjured"] found_incident.officer_hospitalized = incident["officerHospitalized"] found_incident.resident_race = incident["residentRace"] found_incident.resident_sex = incident["residentSex"] found_incident.resident_age = incident["residentAge"] found_incident.resident_condition = incident["residentCondition"] found_incident.officer_identifier = incident["officerIdentifier"] found_incident.officer_race = incident["officerRace"] found_incident.officer_sex = incident["officerSex"] found_incident.officer_age = incident["officerAge"] found_incident.officer_years_of_service = incident["officerYearsOfService"] found_incident.officer_condition = incident["officerCondition"] found_incident.save() updated_rows += 1 extractor.next_month = None extractor.next_year = None extractor.save() return json.dumps({"added": added_rows, "updated": updated_rows})
def add_or_update_incident(cls, department, incident): ''' Add a new UOF incident or update an existing one ''' row_added = False # get a cleaner instance cleaner = Cleaners() # capitalize the location incident["division"] = cleaner.capitalize(incident["division"]) incident["precinct"] = cleaner.capitalize(incident["precinct"]) incident["shift"] = cleaner.capitalize(incident["shift"]) incident["beat"] = cleaner.capitalize(incident["beat"]) # clean force type, race, gender incident["officerForceType"] = cleaner.officer_force_type(incident["officerForceType"]) incident["residentRace"] = cleaner.race(incident["residentRace"]) incident["residentSex"] = cleaner.sex(incident["residentSex"]) incident["officerRace"] = cleaner.race(incident["officerRace"]) incident["officerSex"] = cleaner.sex(incident["officerSex"]) # make sure values that might've been sent as integers are strings incident["residentAge"] = cleaner.number_to_string(incident["residentAge"]) incident["officerAge"] = cleaner.number_to_string(incident["officerAge"]) incident["officerYearsOfService"] = cleaner.number_to_string(incident["officerYearsOfService"]) found_incident = cls.query.filter_by( opaque_id=incident["opaqueId"], department_id=department.id, officer_identifier=incident["officerIdentifier"], officer_force_type=incident["officerForceType"] ).first() if not found_incident: found_incident = cls.create( department_id=department.id, opaque_id=incident["opaqueId"]) row_added = True found_incident.department_id = department.id found_incident.opaque_id = incident["opaqueId"] found_incident.occured_date = parse_date(incident["occuredDate"]) found_incident.division = incident["division"] found_incident.precinct = incident["precinct"] found_incident.shift = incident["shift"] found_incident.beat = incident["beat"] found_incident.disposition = incident["disposition"] found_incident.census_tract = None found_incident.officer_force_type = incident["officerForceType"] found_incident.use_of_force_reason = incident["useOfForceReason"] found_incident.service_type = incident["serviceType"] found_incident.arrest_made = incident["arrestMade"] found_incident.arrest_charges = incident["arrestCharges"] found_incident.resident_weapon_used = incident["residentWeaponUsed"] found_incident.resident_injured = incident["residentInjured"] found_incident.resident_hospitalized = incident["residentHospitalized"] found_incident.officer_injured = incident["officerInjured"] found_incident.officer_hospitalized = incident["officerHospitalized"] found_incident.resident_race = incident["residentRace"] found_incident.resident_sex = incident["residentSex"] found_incident.resident_age = incident["residentAge"] found_incident.resident_condition = incident["residentCondition"] found_incident.officer_identifier = incident["officerIdentifier"] found_incident.officer_race = incident["officerRace"] found_incident.officer_sex = incident["officerSex"] found_incident.officer_age = incident["officerAge"] found_incident.officer_years_of_service = incident["officerYearsOfService"] found_incident.officer_condition = incident["officerCondition"] found_incident.save() return row_added
def add_or_update_incident(cls, department, incident): ''' Add a new Citizen Complaints incident or update an existing one ''' row_added = False # get a cleaner instance cleaner = Cleaners() # make sure values that might've been sent as integers are strings incident["residentAge"] = cleaner.number_to_string(incident["residentAge"]) incident["officerAge"] = cleaner.number_to_string(incident["officerAge"]) incident["officerYearsOfService"] = cleaner.number_to_string(incident["officerYearsOfService"]) # capitalize all the fields in the incident incident = cleaner.capitalize_incident(incident) # clean sex & race incident["residentSex"] = cleaner.sex(incident["residentSex"]) incident["residentRace"] = cleaner.race(incident["residentRace"]) incident["officerSex"] = cleaner.sex(incident["officerSex"]) incident["officerRace"] = cleaner.race(incident["officerRace"]) found_incident = False # found_incident = cls.query.filter_by( # opaque_id=incident["opaqueId"], # allegation=incident["allegation"], # officer_identifier=incident["officerIdentifier"], # department_id=department.id, # resident_race=incident["residentRace"], # resident_sex=incident["residentSex"], # resident_age=incident["residentAge"] # ).first() if not found_incident: # check for multiple complainants # :TODO: validate this practice! # multiple_complaintant_check = cls.query.filter_by( # opaque_id=incident["opaqueId"], # allegation=incident["allegation"], # officer_identifier=incident["officerIdentifier"], # department_id=department.id # ).first() # if multiple_complaintant_check: # return None found_incident = cls.create( department_id=department.id, opaque_id=incident["opaqueId"] ) row_added = True found_incident.department_id = department.id found_incident.opaque_id = incident["opaqueId"] found_incident.occured_date = parse_date(incident["occuredDate"]) found_incident.bureau = incident["bureau"] found_incident.division = incident["division"] found_incident.assignment = incident["assignment"] found_incident.service_type = incident["serviceType"] found_incident.source = incident["source"] found_incident.allegation = incident["allegation"] found_incident.disposition = incident["disposition"] found_incident.resident_identifier = incident["residentIdentifier"] found_incident.resident_race = incident["residentRace"] found_incident.resident_sex = incident["residentSex"] found_incident.resident_age = incident["residentAge"] found_incident.officer_identifier = incident["officerIdentifier"] found_incident.officer_race = incident["officerRace"] found_incident.officer_sex = incident["officerSex"] found_incident.officer_age = incident["officerAge"] found_incident.officer_years_of_service = incident["officerYearsOfService"] found_incident.save() return row_added