def save(enriched): for record in enriched: address, price, url, noisy, commute, catchment_gids, point = record realestate = RealEstate(address=address, price=price, url=url, commute=commute, noisy=noisy, geom=WKTElement(point, srid=4326)) cs = session.query(Catchments).filter( Catchments.gid.in_(catchment_gids)).all() # better way to do this? for c in cs: a = Association() a.catchments = c realestate.catchments.append(a) session.add(realestate) try: session.commit() logger.info(f'Successfully commited records to database.') return True except Exception as err: logging.error(f'Error saving record: {err}') return False
def storeAssociation(self, server_url, association): assoc = Association(server_url=server_url, handle=association.handle, secret=base64.encodestring(association.secret), issued=association.issued, lifetime=association.lifetime, assoc_type=association.assoc_type) assoc.save()
def storeAssociation(self, server_url, association): """ This method puts a C{L{Association <openid.association.Association>}} object into storage, retrievable by server URL and handle. """ assoc = Association(url=server_url, handle=association.handle, association=association.serialize()) assoc.save()
def storeAssociation(self, server_url, association): assoc = Association( server_url = server_url, handle = association.handle, secret = base64.encodestring(association.secret), issued = association.issued, lifetime = association.issued, assoc_type = association.assoc_type ) assoc.save()
def removeAssociation(self, server_url, handle): """ This method removes the matching association if it's found, and returns whether the association was removed or not. """ query = Association.gql('WHERE url = :1 AND handle = :2', server_url, handle) return self._delete_first(query)
def cleanupAssociations(self): """Remove expired associations from the store. This method is not called in the normal operation of the library. It provides a way for store admins to keep their storage from filling up with expired data. @return: the number of associations expired. @returntype: int """ query = Association.gql('WHERE created < :1', self._expiration_datetime()) return self._cleanup_batch(query)
def add_ingredient(): ingredient = request.form['ingredient'].lower() recipe = request.form['recipe'].lower() #checks if recipe exists, if not, adds to db existing_recipe = Recipe.query.filter_by(name=recipe).first() if not existing_recipe: new_recipe = Recipe(recipe) db.session.add(new_recipe) #checks if ingredient exists, if not, adds to db existing_ingredient = Ingredient.query.filter_by(name=ingredient).first() if not existing_ingredient: new_ingredient = Ingredient(ingredient) db.session.add(new_ingredient) db.session.commit() #gets quantity from form, and corresponding recipe and ingredient ids quantity = request.form['quantity'] ingredient_id = Ingredient.query.filter_by(name=ingredient).first().id recipe_id = Recipe.query.filter_by(name=recipe).first().id user_id = User.query.filter_by(username=session['username']).first().id #grabs potential preexisting association existing_association = Association.query.filter_by( ingredient_id=ingredient_id, recipe_id=recipe_id, user_id=user_id).first() #adds recipe to association table if recipe does not already exist if not existing_association: new_association = Association(ingredient_id, recipe_id, user_id, quantity) db.session.add(new_association) db.session.commit() flash("ingredient added") else: flash("recipe already contains this ingredient") #returns to pmix view--if coming from searched list, returns to same filtered list if session['search'] != []: return render_template('pmix-viewer.html', product_list=session['search'], username=session['username']) else: return render_template('pmix-viewer.html', product_list=product_list, username=session['username'])
def import_gwaskb_associations(): from models import MergedAssociation, Association, SNP, Phenotype, Paper Base.metadata.create_all(bind=engine) # read p-values directly from the tsv pvalues = {} with open('{}/tmp/associations.tsv'.format(APP_ROOT)) as f: lines = f.readlines() for line in lines: tokens = re.split(r'\t+', line.decode('utf-8')) # pvalues[(int(tokens[0]), tokens[1], tokens[2], tokens[3])] = 10 ** (float(tokens[4])) # keywords = re.split(r'\|+', tokens[2]) if (tokens[3] == '-'): tokens[3] = None snp = db2_session.query(SNP) \ .filter(SNP.rs_id == tokens[1]).first() paper = db2_session.query(Paper) \ .filter(Paper.pubmed_id == int(tokens[0])).first() if tokens[3] is None: phenotype_name = tokens[2] else: phenotype_name = "\\\\".join([tokens[2], tokens[3]]) phenotype_entry = Phenotype(name=phenotype_name, source="gwaskb") db2_session.add(phenotype_entry) db2_session.flush() # p_value = "Not found" if float(tokens[4]) < -999 else 10 ** (float(tokens[4])) p_value = 10**(float(tokens[4])) if p_value != 0: p_value = round(p_value, -int(floor(log10(abs(p_value))))) entry = Association(snp_id=snp.id, phenotype_id=phenotype_entry.id, paper_id=paper.id, pvalue=p_value, source="gwaskb") db2_session.add(entry) db2_session.commit()
def update_associations(filename, debug_print): session = db_session() association_list = list() delete_stmt = Association.__table__.delete() session.execute(delete_stmt) session.commit() with open(filename, 'rb') as csvfile: csv_reader = csv.reader(csvfile, delimiter=',') for row in csv_reader: comment_id, soint_id, text, user_account_id, user_id, username = row if "stackoverflow.com/" not in text: print "Association NOT on StackOverflow (does not contain so.com in the text): %s" % row continue soen_id = re.findall('\d+', text) if len(soen_id) > 0: soen_id = soen_id[0] else: print "Association NOT on StackOverflow: %s" % row soen_id = -1 if STACKOVERFLOW_HOSTNAME not in text: if debug_print: print "Association to another site: %s " % text continue try: comment_id = int(comment_id) soint_id = int(soint_id) user_account_id = int(user_account_id) user_id = int(user_id) except: if debug_print: print "Parse error: %s" % row continue user = session.query(User).filter_by( account_id=user_account_id).first() if user is None: user = User(user_account_id, user_id, username, 0, "", "") session.add(user) session.commit() if debug_print: print "User was added: %s" % str(user.id) association = session.query(Association).filter( and_(Association.soen_id == soen_id, Association.soint_id == soint_id, Association.user_id == user.id)).first() if association is None: association = Association(user.id, soen_id, soint_id, comment_id) session.add(association) session.commit() if debug_print: print "Association was added: %s" % str(association.id) else: if debug_print: print "Association has already existed: %s" % str( association.id) existed = False for item in association_list: if item["soen"] == soen_id or item["soint"] == soint_id: existed = True break if not existed: association_list.append({"soen": soen_id, "soint": soint_id}) session.close() sync_associations() print_association_setting(association_list)
def add_association(): access_token = session.get("access_token", None) if g.user is None or access_token is None: abort(404) soen_id = -1 soint_id = -1 try: soen_id = int(request.args.get("soen_id")) soint_id = int(request.args.get("soint_id")) except: return jsonify(**{"status": False, "msg": gettext("Wrong params")}) count = Association.query.filter_by(soen_id=soen_id).count() if count > 0: return jsonify(**{"status": False, "msg": gettext("Wrong params")}) url = STACKEXCHANGE_ADD_COMMENT_ENDPOINT.replace("{id}", str(soint_id)) association_tag = gettext(u"association") params = { "body": association_tag + u": http://stackoverflow.com/questions/" + str(soen_id) + "/", "access_token": access_token, "key": STACKEXCHANGE_CLIENT_KEY, "site": INT_STACKOVERFLOW_SITE_PARAM, "preview": "false" } r = requests.post(url, data=params) comment_id = -1 data = json.loads(r.text) if data.get("items", None) is not None: for item in data["items"]: if item.get("comment_id", None) is not None: comment_id = item["comment_id"] break if comment_id < 0: redirect_flag = False msg = r.text error_id = int(data.get("error_id", 0)) if error_id in LOGOUT_CASES: redirect_flag = True msg = LOGOUT_MSG return jsonify( **{ "status": False, "msg": msg, "logout": redirect_flag, "logout_url": url_for("logout_oauth") }) resp = { "status": True, "msg": gettext("Association was added"), "comment_id": comment_id, "full_response": r.text } association = Association(g.user.id, soen_id, soint_id, comment_id) db_session.add(association) db_session.commit() questions = Question.query.filter_by(question_id=soen_id).all() # There should be only one for question in questions: question.is_associated = True db.session.commit() return jsonify(**resp)