def load_incidents(): """Load incidents from test.json into incidents database.""" print('Incidents') # Delete all rows in table, so if we need to run this a second time, # we won't be trying to add duplicate users Incident.query.delete() file = open('nwbb-fxkq.json').read() # list of dictionary incidents data = json.loads(file) for incident_dict in data: date_time = incident_dict.get('incident_datetime') category = incident_dict.get('incident_category') subcategory = incident_dict.get('incident_subcategory') description = incident_dict.get('incident_description') resolution = incident_dict.get('resolution') latitude = incident_dict.get('latitude') longitude = incident_dict.get('longitude') incident = Incident(date_time=date_time, category=category, subcategory=subcategory, description=description, resolution=resolution, latitude=latitude, longitude=longitude) db.session.add(incident) db.session.commit()
def incident(): user_id = session["user_id"] upset_stomach = request.form.get("upset_stomach") stomach_cramp = request.form.get("stomach_cramp") nausea = request.form.get("nausea") vomiting = request.form.get("vomiting") diarrhea = request.form.get("diarrhea") fever = request.form.get("fever") restaurant_id = request.form.get("restaurant_id") date = request.form.get("date") new_input = Incident(user_id=user_id, upset_stomach=upset_stomach, stomach_cramp=stomach_cramp, nausea=nausea, vomiting=vomiting, diarrhea=diarrhea, fever=fever, restaurant_id=restaurant_id, date=date) db.session.add(new_input) db.session.commit() flash(f"Symptoms added to your report") return render_template("thanks.html")
def load_incidents(file): """Load ratings from u.data into database.""" print("incident") for row in open(file): row = row.rstrip() user_id, upset_stomach, stomach_cramp, nausea, vomiting, diarrhea, fever, restaurant_id, date = row.split(",") incident = Incident( upset_stomach = upset_stomach, stomach_cramp = stomach_cramp, nausea = nausea, vomiting = vomiting, diarrhea = diarrhea, fever = fever, restaurant_id = restaurant_id, user_id = user_id, date = date) db.session.add(incident) db.session.commit()
def submit_form(): """Submits the report form and saves incident to database""" #Queries dBase and gets info from form to be saves as new incident user_id = (User.query.filter( User.email == session['current_user']).first()).user_id inc_type = request.form['inc_type'] address = request.form['address'] city = request.form['city'] state = request.form['state'] lat = str(request.form['lat']) lng = str(request.form['lng']) date = request.form['date'] time = str(request.form['time']) description = request.form['description'] p_name = request.form['p_name'] badge = request.form['badge'] p_description = request.form['p_description'] sting = request.form['sting'] avoid = request.form['avoid'] other = request.form['other'] year = date[0:4] #Creates new incident and commits it to dBase new_report = Incident(year=year, user_id=user_id, police_dept_id=3, source_id=3, inc_type=inc_type, address=address, city=city, state=state, latitude=lat, longitude=lng, date=date, time=time, description=description, sting_strat=sting, avoidance=avoid, other=other) new_cop = Cop(user_id=user_id, police_dept_id=3, cop_name=p_name, cop_badge=badge, cop_desc=p_description) db.session.add(new_report) db.session.add(new_cop) db.session.commit() #Redirects to homepage sudo apt-get install build-essential libffi-dev python-dev flash('Your report has been filed and should be added to the map soon!') return redirect("/")
def add_incident_data(source_nums): """Example Basic Template for adding Incident Source API's. MUST be customized by source.""" with app.app_context(): sour = Source.query.filter_by(source_id=s_num).one() incident_info = requests.get(sour.url).json() city = 0 for row in incident_info: year = int(row["incident_datetime"][0:4]) if Incident.query.filter(Incident.police_rec_num == row["cmplnt_num"]).all() == [] and "latitude" in row: if year >= 2000: city += 1 print "City " + str(city) print row #This needs to be customized for the given source being added incident = Incident(police_dept_id=9, source_id=s_num, inc_type="API", latitude=row["latitude"], longitude=row["longitude"], address=row["boro_nm"], city="New York", state="CA", date=row["cmplnt_fr_dt"], year=year, time=(str(row["cmplnt_fr_tm"])), description=row["pd_desc"], police_rec_num=row["cmplnt_num"]) db.session.add(incident) print incident.incident_id db.session.commit()
def add_incident_data_start(source_nums): """Takes a list of source_ids to collect data from""" with app.app_context(): for s_num in source_nums: new_york = 0 sour = Source.query.filter_by(source_id=s_num).one() sf_num = 0 if s_num == 2 or s_num == 3: incident_info = requests.get(sour.url, params={ "category": "PROSTITUTION" }).json() for row in incident_info: year = int(row["date"][0:4]) if "PROST" in row["descript"].upper( ) and Incident.query.filter(Incident.police_rec_num == row["incidntnum"]).all() == []: if year >= 2000: sf_num += 1 print "sf # " + str(sf_num) print row if s_num == 3: incident = Incident( police_dept_id=2, source_id=3, inc_type="API", latitude=row["location"]["coordinates"][1], longitude=row["location"]["coordinates"] [0], address=row["address"], city="San Francisco", state="CA", date=row["date"], year=year, time=row["time"], description=row["descript"], police_rec_num=row["incidntnum"]) db.session.add(incident) elif s_num == 2: incident = Incident( police_dept_id=2, source_id=2, inc_type="API", latitude=row["location"]["latitude"], longitude=row["location"]["longitude"], address=row["address"], city="San Francisco", state="CA", date=row["date"], year=year, time=row["time"], description=row["descript"], police_rec_num=row["incidntnum"]) db.session.add(incident) print incident.latitude, incident.longitude print type(incident.latitude), type( incident.longitude) db.session.commit() elif s_num == 5: incident_info = requests.get(sour.url).json() alameda = 0 for row in incident_info: year = int(row["incident_datetime"][0:4]) if Incident.query.filter(Incident.police_rec_num == row["incident_id"]).all() == []: if year >= 2000: alameda += 1 print "Alameda" + str(alameda) print row incident = Incident( police_dept_id=4, source_id=5, inc_type="API", latitude=row["latitude"], longitude=row["longitude"], address=row["address_1"], city=row["city"], state="CA", date=row["incident_datetime"], year=year, time=(str(row["hour_of_day"]) + ":00"), description=row["incident_description"], police_rec_num=row["incident_id"]) db.session.add(incident) db.session.commit() elif s_num == 6: incident_info = requests.get(sour.url).json() santa_clara = 0 for row in incident_info: year = int(row["incident_datetime"][0:4]) if Incident.query.filter(Incident.police_rec_num == row["incident_id"]).all() == []: if year >= 2000: santa_clara += 1 print "Santa Clara " + str(santa_clara) print row incident = Incident( police_dept_id=5, source_id=6, inc_type="API", latitude=row["latitude"], longitude=row["longitude"], address=row["address_1"], city=row["city"], state="CA", date=row["incident_datetime"], year=year, time=(str(row["hour_of_day"]) + ":00"), description=row["incident_description"], police_rec_num=row["incident_id"]) db.session.add(incident) db.session.commit() elif s_num == 7: incident_info = requests.get(sour.url).json() fremont = 0 for row in incident_info: year = int(row["incident_datetime"][0:4]) if Incident.query.filter(Incident.police_rec_num == row["incident_id"]).all() == []: if year >= 2000: fremont += 1 print "Fremont " + str(fremont) print row incident = Incident( police_dept_id=6, source_id=7, inc_type="API", latitude=row["latitude"], longitude=row["longitude"], address=row["address_1"], city=row["city"], state="CA", date=row["incident_datetime"], year=year, time=(str(row["hour_of_day"]) + ":00"), description=row["incident_description"], police_rec_num=row["incident_id"]) db.session.add(incident) db.session.commit() elif s_num == 8: incident_info = requests.get(sour.url).json() san_leandro = 0 for row in incident_info: year = int(row["incident_datetime"][0:4]) if Incident.query.filter(Incident.police_rec_num == row["incident_id"]).all() == []: if year >= 2000: san_leandro += 1 print "San Leandro " + str(san_leandro) print row incident = Incident( police_dept_id=7, source_id=8, inc_type="API", latitude=row["latitude"], longitude=row["longitude"], address=row["address_1"], city=row["city"], state="CA", date=row["incident_datetime"], year=year, time=(str(row["hour_of_day"]) + ":00"), description=row["incident_description"], police_rec_num=row["incident_id"]) db.session.add(incident) db.session.commit() elif s_num == 9: incident_info = requests.get(sour.url).json() san_pablo = 0 for row in incident_info: year = int(row["incident_datetime"][0:4]) if Incident.query.filter(Incident.police_rec_num == row["incident_id"]).all() == []: if year >= 2000: san_pablo += 1 print "San Pablo " + str(san_pablo) print row incident = Incident( police_dept_id=8, source_id=9, inc_type="API", latitude=row["latitude"], longitude=row["longitude"], address=row["address_1"], city=row["city"], state="CA", date=row["incident_datetime"], year=year, time=(str(row["hour_of_day"]) + ":00"), description=row["incident_description"], police_rec_num=row["incident_id"]) db.session.add(incident) db.session.commit() elif s_num == 10 or s_num == 11: incident_info = requests.get(sour.url).json() inde = 0 for row in incident_info: new_york += 1 inde += 1 print "New York " + str(new_york) print row year = int(row["cmplnt_fr_dt"][0:4]) if Incident.query.filter( Incident.police_rec_num == row["cmplnt_num"]).all( ) == [] and "latitude" in row: if year >= 2000: new_york += 1 inde += 1 print "New York " + str(new_york) print row incident = Incident( police_dept_id=9, source_id=s_num, inc_type="API", latitude=row["latitude"], longitude=row["longitude"], address=row["boro_nm"], city="New York", state="CA", date=row["cmplnt_fr_dt"], year=year, time=(str(row["cmplnt_fr_tm"])), description=row["pd_desc"], police_rec_num=row["cmplnt_num"]) db.session.add(incident) print incident.incident_id db.session.commit() elif s_num == 4: o_num = 0 for row in open("seed_data/oaklandcoords.csv"): o_num += 1 print "Oakland # " + str(o_num) incident_row = row.split(",") # print incident[-2], incident[-1] inc = [] item_num = 0 for item in incident_row: item_num += 1 if item_num <= 10: inc += [item.translate(None, string.punctuation)] else: inc += item address = str(inc[5]) incident = Incident( police_dept_id=3, source_id=4, inc_type="API", address=address, latitude=unicode(incident_row[-2].strip(), "utf-8"), longitude=unicode(incident_row[-1].strip(), "utf-8"), city="Oakland", state="CA", date=inc[1], year=inc[1][:4], time=inc[1][11:16], description=inc[3], police_rec_num=inc[2]) db.session.add(incident) print incident.latitude, incident.longitude print type(incident.latitude) db.session.commit()