def get(self): """ Displays a form to upload a day's worth of incident reports """ date = self.request.get('date') incidentReports = IncidentReport.all() incidentReports.filter("time >= ", datetime.strptime('%s 12:00 AM' %(date), '%m/%d/%Y %I:%M %p')) incidentReports.filter("time <= ", datetime.strptime('%s 11:59 PM' %(date), '%m/%d/%Y %I:%M %p')) results = incidentReports.run(limit=None) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(''.join(['[', ','.join([result.asJson() for result in results]), ']']))
def post(self): """ Fetches lat/long coordinates for the given incident report type """ incidentId = int(self.request.get("id")) report = IncidentReport.get_by_id(incidentId) if None != report: params = urllib.urlencode({"address": "%s %s WI" % (report.address, report.city.name), "sensor": "true"}) url = "http://maps.googleapis.com/maps/api/geocode/json?%s" % (params) result = urlfetch.fetch(url) if 200 == result.status_code: try: llResults = json.loads(result.content)["results"][0]["geometry"]["location"] report.latLong = db.GeoPt(llResults["lat"], llResults["lng"]) report.save() except: mail.send_mail( sender="LaxCrime <*****@*****.**>", to="Ryan Brubaker <*****@*****.**>", subject="Error getting lat long", body=""" Error trying to retrieve lat/long for incident report with id: %d \n\n %s """ % (incidentId, result.content), ) else: mail.send_mail( sender="LaxCrime <*****@*****.**>", to="Ryan Brubaker <*****@*****.**>", subject="Error getting lat long", body=""" Error trying to retrieve lat/long for incident report with id: %d \n\n %s """ % (incidentId, result.content), )
def post(self): """ Parses a list of police/fire calls and fires off a task for each one """ incidentType = None incidentTypeNames = [incident.name.lower() for incident in IncidentType.all()] city = None cityNames = [city.name.lower() for city in City.all()] lines = self.request.get('report').split('\n') lineNum = 0 startDate = None endDate = None for strippedLine in [line.strip() for line in lines if line.strip() != '']: try: if 0 == lineNum: startDate = strippedLine elif 1 == lineNum: endDate = strippedLine else: if strippedLine.lower() in incidentTypeNames: incidentType = IncidentType.all().filter('name =', strippedLine.lower()).get() city = None elif strippedLine.lower() in cityNames: city = City.all().filter('name =', strippedLine.lower()).get() else: match = re.search('^[0-9]+', strippedLine) lineItemTokens = strippedLine.split(',') if None != match and 3 == len(lineItemTokens): timeToken = lineItemTokens[0] descToken = lineItemTokens[1].title() addressToken = lineItemTokens[2].title() # Check for date, anything on or after 3 pm is the start date # anything on or after 12 am is the end date dateString = endDate if int(match.group(0)) >= 3 and int(match.group(0)) < 12 and -1 != timeToken.find('p.m.'): dateString = startDate # Now make sure lines such as 3 p.m = 3:00 p.m. if (-1 == timeToken.find(':')): timeToken = timeToken.replace(' ', ':00 ') # Now replace AM or PM with a.m or p.m. dateOfIncident = datetime.strptime( '%s %s' %(dateString, timeToken.replace('a.m.', 'AM').replace('p.m.', 'PM')), '%m/%d/%Y %I:%M %p') # We've parsed out all informaton so create a record...just bail on this record # if we're in a bad state assert(None != incidentType and None != city) incidentReport = IncidentReport( incidentType = incidentType, description = descToken, time = dateOfIncident, city = city, address = addressToken) incidentReport.save() # Add background task for getting geo coordinates of the address taskqueue.add(url='/retrieve-incident-latlong', params={'id': incidentReport.key().id()}) # else: incidentType = None city = None raise Exception('Unrecognized line') # # # # except (Exception, AssertionError) as e: if isinstance(e, AssertionError): e = 'Incident type or city was null.' self.response.out.write("Error processing line: %s <br />" %(strippedLine)) self.response.out.write('<div style="margin:5px;">%s</div>' %(str(e))) self.response.out.write('<br />') # lineNum += 1