def write_valid_row(csv_file_obj_key, csv_row_obj_key): csv_file_obj = CSVFile.get(csv_file_obj_key) csv_row_obj = CSVRow.get(csv_row_obj_key) row_dict = pickle.loads(csv_row_obj.row_dict) validation = pickle.loads(csv_row_obj.validation) geocoded_address = pickle.loads(csv_row_obj.geocoded_address) if validation['validates']: for gc_field_name, gc_field_value in geocoded_address.items(): if not row_dict.get(gc_field_name, None): row_dict[gc_field_name] = gc_field_value for org_field_name in ['reported_by', 'claimed_by']: if row_dict.get(org_field_name, None): row_dict[org_field_name] = ( organization.Organization.get_by_id( row_dict[org_field_name])) # create site object new_site = site_db.Site(**row_dict) new_site.save() event_db.AddSiteToEvent(new_site, csv_file_obj.event.key().id(), can_overwrite=True) # update counters csv_row_obj.saved = True csv_row_obj.save() inc_counter_and_check(csv_file_obj.key(), 'saved_count', 'valid_row_count', 'saving', flag_value=False)
def add_site(data, event): site = site_db.Site(name=data["name"], request_date=datetime.strptime(data["request_date"], '%Y-%m-%d %H:%M:%S'), address=data["address"], phone1=data["phone1"], work_type=data["work_type"]) for key in data: if key not in [ "name", "request_date", "address", "phone1", "work_type" ]: # special cases, claimed_by, reported_by, modified_by, prepared_by, latitude, latitude_blur, request_date if key in [ "latitude", "longitude", "latitude_blur", "longitude_blur" ]: setattr(site, key, float(data[key])) elif key == "request_date": setattr(site, key, datetime.strptime(data[key], '%Y-%m-%d %H:%M:%S')) elif key in ["reported_by", "claimed_by"]: d = str(data[key]) if d: c = float(str(d)) b = int(c) setattr(site, key, organization.Organization.get_by_id(b)) elif key == "modified_by": setattr(site, key, None) else: setattr(site, key, data[key]) success = event_db.AddSiteToEvent(site, event.key().id()) add_audit(site, "create") return success
def GetOrganizationForm(post_data): e = event_db.Event(name = "Test Incident", case_label = "B", counties = ["Kings"]) query_string = "SELECT * FROM Organization WHERE is_active = True ORDER BY name" organizations = db.GqlQuery(query_string) events = event_db.GetAllCached() events = db.GqlQuery("SELECT * From Event ORDER BY created_date DESC") event_key = None if events.count() == 0: logging.warning("Initialize called") e = event_db.Event(name = "North Central Victorian Floods", case_label = "A", short_name = "ncv_floods") e.put() event_key = e.key() # TODO(Jeremy): This could be dangerous if we reset events. for s in site_db.Site.all().run(batch_size = 1000): event_db.AddSiteToEvent(s, e.key().id(), force = True) events = [e] if organizations.count() == 0: # init: populate the database with Admin user admin_org = organization.Organization( name="Admin", password="******", org_verified=True, is_active=True, is_admin=True, incidents=[event_key] ) admin_org.put() admin_contact = primary_contact_db.Contact( first_name="Admin", last_name="Admin", title="Admin", phone="1234", email="*****@*****.**", organization=admin_org, is_primary=True ) admin_contact.put() organizations = db.GqlQuery("SELECT * FROM Organization WHERE is_active = True ORDER BY name") class OrganizationForm(wtforms.form.Form): event = wtforms.fields.SelectField( 'Work Event', choices = [(e.name, e.name) for e in events], validators = [wtforms.validators.required()]) password = wtforms.fields.PasswordField( 'Password', validators = [ wtforms.validators.required() ]) form = OrganizationForm(post_data) return form
def AuthenticatedPost(self, org, event): if org.permissions == "Situational Awareness": self.redirect("/sit_aware_redirect") return my_string = "" for k, v in self.request.POST.iteritems(): if v == "": v = "stub" my_string += k + " = '" + v + "', " data = site_db.StandardSiteForm(self.request.POST) post_dict = dict(self.request.POST) post_json = json.dumps(post_dict) #single_site_template = jinja_environment.get_template('single_site.html') #if event.short_name in [HATTIESBURG_SHORT_NAME, GEORGIA_SHORT_NAME]: #single_site_template = jinja_environment.get_template('single_site_derechos.html') claim_for_org = self.request.get("claim_for_org") == "y" #data = None #if event.short_name in [HATTIESBURG_SHORT_NAME, GEORGIA_SHORT_NAME]: #data = site_db.DerechosSiteForm(self.request.POST) #else: #data = site_db.SiteForm(self.request.POST) # un-escaping data caused by base.py = self.request.POST[i] = cgi.escape(self.request.POST[i]) data.name.data = site_util.unescape(data.name.data) data.priority.data = int(data.priority.data) data.name.validators = data.name.validators + [ wtforms.validators.Length( min=1, max=100, message="Name must be between 1 and 100 characters") ] data.phone1.validators = data.phone1.validators + [ wtforms.validators.Length( min=1, max=100, message="Please enter a primary phone number") ] data.city.validators = data.city.validators + [ wtforms.validators.Length( min=1, max=100, message="Please enter a city name") ] data.state.validators = data.state.validators + [ wtforms.validators.Length( min=1, max=100, message="Please enter a state name") ] data.work_type.validators = data.work_type.validators + [ wtforms.validators.Length( min=1, max=100, message="Please set a primary work type") ] if data.validate(): lookup = site_db.Site.gql( "WHERE name = :name and address = :address LIMIT 1", name=data.name.data, address=data.address.data) site = None for l in lookup: # See if this same site is for a different event. # If so, we'll make a new one. if l.event and l.event.name == event.name: site = l break if not site: # Save the data, and redirect to the view page site = site_db.Site(address=data.address.data, name=data.name.data, priority=int(data.priority.data)) for k, v in self.request.POST.iteritems(): if k not in site_db.STANDARD_SITE_PROPERTIES_LIST: if k == "request_date": date_saved = False try: date_object = datetime.strptime( v, '%Y-%m-%d %H:%M:%S') setattr(site, k, date_object) date_saved = True except: date_saved = False pass if date_saved is False: try: v = v.replace("/", "-") date_object = datetime.strptime(v, '%Y-%m-%d') setattr(site, k, date_object) date_saved = True except: date_saved = False pass if date_saved is False: try: v = v.replace("/", "-") date_object = datetime.strptime(v, '%m-%d-%Y') setattr(site, k, date_object) date_saved = True except: date_saved = False pass else: setattr(site, k, v) try: data.populate_obj(site) except: raise Exception("populate") site.reported_by = org if claim_for_org: site.claimed_by = org # clear assigned_to if status is unassigned if data.status.data == 'Open, unassigned': site.assigned_to = '' # attempt to save site similar_site = None if site.similar(event) and not self.request.get( 'ignore_similar', None): similar_site = site.similar(event) message = None elif site.event or event_db.AddSiteToEvent(site, event.key().id()): site_db.PutAndCache(site) try: audit_db.create(site, "create", org) except: logging.error("Audit exception") #dict_dict_site = site_db.SiteToDict(site) #raise Exception(dict_dict_site) self.redirect("/?message=" + "Successfully added " + urllib2.quote(site.name)) return else: message = "Failed to add site to event: " + event.name else: message = "Failed to validate" similar_site = None q = db.Query(form_db.IncidentForm) q.filter("incident =", event.key()) query = q.get() # set it as form_stub # send to single site inc_form = None if query: inc_form = query.form_html single_site = single_site_template.render({ "form": data, "org": org, "incident_form_block": inc_form, }) self.response.out.write( template.render({ "message": message, "similar_site": similar_site, "version": os.environ['CURRENT_VERSION_ID'], "errors": data.errors, "menubox": menubox_template.render({ "org": org, "event": event }), "single_site": single_site, "form": data, "id": None, "page": "/", "post_json": post_json, "event_name": event.name }))
def AuthenticatedGet(self, org, event): f = open('sheet1.csv') fileReader = csv.reader(f) firstRow = None all_sites = [] for row in fileReader: if not firstRow: firstRow = row else: m = [(firstRow[i], row[i]) for i in range(len(row))] all_sites.append(dict(m)) for s in all_sites: if s["Contact Name"] == "": s["Contact Name"] = "Unknown" self.response.write(s["Contact Name"] + "<br />") lookup = site_db.Site.gql( "WHERE name = :name and address = :address and zip_code = :zip_code LIMIT 1", name=s["Contact Name"], address=s["Street Address"], zip_code=s["Zip Code"]) site = None for l in lookup: site = l def IfSet(c): if s[c] != "": return " " + c + ": " + s[c] return "" if not site: # Save the data, and redirect to the view page logging.critical(s["Contact Name"]) site = site_db.Site( status="Open, unassigned", address=str(s["Street Address"]), name=s["Contact Name"], phone1=str(s["Phone Number"]), county=s["County"], city=s["City"], state=s["State"], latitude=float(s["Latitude"]), longitude=float(s["Longitude"]), zip_code=str(s["Zip Code"]), notes=IfSet("Other Needs") + IfSet("Tools or Skills Needed") + IfSet("Other Information") + IfSet("Crew Size Needed") + IfSet("Estimated Length of Job") + IfSet("Damaged Appliance/Furniture/Flooring?") + IfSet("Functioning Utilities") + IfSet("Primary Home?") + IfSet("Age of Other Residents") + IfSet("Structural Damage to Home?") + IfSet("Mold Growing?") + IfSet("Contacted Insurance?") + IfSet("Water Level in Home") + IfSet("Contacted Landlord?") + IfSet("Part of House Flooded") + IfSet("Text") + IfSet("Waiting for Insurance Company?") + IfSet("Email") + IfSet("Source Name"), other_hazards=IfSet("Hazards to Look Out For") + IfSet("Hazardous Materials on Property?"), electrical_lines="yes" in s["Power Lines Down on Property?"].lower(), prepared_by=s["Person Reporting"], disabled="yes" in s["Disabled?"].lower(), older_than_60="yes" in s["Elderly?"].lower(), work_requested=s["Description"] + " Damage: " + s["Damage Incurred"], standing_water="yes" in s["Currently Water in Home?"].lower(), electricity="no" in s["Gas/Electricity Turned Off?"].lower(), insurance=s["Have Insurance?"]) try: if len(s["Priority"]) > 0: site.priority = int(s["Priority"][0]) except ValueError: pass if 'yes' in s["Homeowner?"].lower(): site.rent_or_own = "Own" site.claimed_by = org event_db.AddSiteToEvent(site, event.key().id()) #logging.critical('No lat/lng: ' + site.name + " " + s["Lat, Long"]) site_db.PutAndCache(site)