def recalculate_score(current_user): import db db_session = db.create_db_session() # Get all users in db different from the one that request users = db_session.query(User).filter(current_user.id != User.id).all() for user in users: # Check if exists a match match = db_session.query(Matching_data).filter(and_( or_( Matching_data.user1 == current_user.id, Matching_data.user2 == current_user.id ), or_( Matching_data.user1 == user.id, Matching_data.user2 == user.id ))).one_or_none() # Calculte diff age if (current_user.birthday > user.birthday): dif_age = relativedelta(current_user.birthday, user.birthday).years else: dif_age = relativedelta(user.birthday, current_user.birthday).years if (dif_age == 0): dif_age = 1 # Calculate common_games setintersection = list(set(current_user.games) & set(user.games)) games_common = (len(setintersection)) if match is None: aux = Matching_data( user1=current_user.id, user2=user.id, common_games=games_common, age_diff=dif_age, score=games_common / dif_age ) db_session.add(aux) db_session.commit() else: if match.isAMatch == False: match.common_games = games_common, match.age_diff = dif_age, match.score = games_common / dif_age db_session.commit() db_session.close()
def execute_sql_file(sql_file): sql_folder_path = os.path.join(os.path.dirname(__file__), "sql") sql_file_path = open(os.path.join(sql_folder_path, sql_file), encoding="utf-8") sql_command = text(sql_file_path.read()) db_session.execute(sql_command) db_session.commit() sql_file_path.close() if __name__ == "__main__": settings.configure_logging() db_session = db.create_db_session() # -------------------- REMOVE AND CREATE TABLES -------------------- mylogger.info("Removing database...") SQLAlchemyBase.metadata.drop_all(db.DB_ENGINE) mylogger.info("Creating database...") SQLAlchemyBase.metadata.create_all(db.DB_ENGINE) # -------------------- CREATE USERS -------------------- mylogger.info("Creating default users...") # noinspection PyArgumentList clubs = [ "Club Tennis Manresa", "Club Padel Igualada", "Pistes Municipals Igualada", "Club Padel Odena", "Club Padel Pro" ] words = [
def process_resource(self, req, resp, resource, params): resource.db_session = db.create_db_session()
for book, chapters in tran['text'].items(): for chapter, verses in chapters.items(): for verse_number, verse in verses.items(): verse = models.Verse( book=book, chapter=chapter, verse=verse_number, text=verse, search=sqlalchemy.func.to_tsvector(verse), translation=translation.id, ) sql.DBSession.add(verse) sql.DBSession.flush() return translation if __name__ == '__main__': force = len(sys.argv) > 1 and sys.argv[1] == 'force' db.connect_db() db.create_db_session() if force: sql.Base.metadata.drop_all(sql.Base.metadata.bind) sql.Base.metadata.create_all(sql.Base.metadata.bind) insert_data(force) sql.DBSession.commit()