def post_location(): # print("post_location") # print(request.form) # print(request.form.get("name")) # print(request.form.get("description")) location = Location() location.create_location(name=request.form["name"], address=request.form["address"], description=request.form["description"], contact=request.form["contact"]) session.add(location) session.commit() return str(request.form)
def article_end(state): """ Called at the end of article processing """ locs = state.get("locations") if not locs: return url = state["url"] session = state["session"] # Find all placenames mentioned in article # We can use them to disambiguate addresses and street names # TODO: Perhaps do this in a more fine-grained manner, at a # sentence or paragraph level. placenames = [p.name for p in locs if p.kind == "placename"] # Get info about each location and save to database for name, kind in locs: loc = location_info(name=name, kind=kind, placename_hints=placenames) loc["article_url"] = url loc["timestamp"] = datetime.utcnow() print("Location '{0}' is a {1}".format(loc["name"], loc["kind"])) locmodel = Location(**loc) session.add(locmodel)
def create_or_update(data): q = session.query(Location).filter_by(location_id=data['location_id']) company = q.first() if not company: company = Location(**data) session.add(company) session.commit() else: q.update(data)
def article_begin(state): """ Called at the beginning of article processing """ session = state["session"] # Database session url = state["url"] # URL of the article being processed # Delete all existing locations for this article session.execute(Location.table().delete().where(Location.article_url == url)) # Set that will contain all unique locations found in the article state["locations"] = set()
def add_location(place): with session_scope() as Session: Session.add(Location(place))