def dbsession(request, worker_id): """ Properly setup, yield and teardown an Oracle backed SQLAlchemy session Make sure this is in sync with func_scoped_dbsession """ settings = DEFAULT_TEST_SETTINGS if request.config.getoption("--use-sqlite3"): settings["adaero.use_local_sqlite3"] = True settings["adaero.worker_id"] = worker_id config = testing.setUp(settings=settings) config.include("adaero.models") settings = config.get_settings() from adaero.models import get_engine, get_session_factory, get_tm_session engine = get_engine(settings) session_factory = get_session_factory(engine) session = get_tm_session(session_factory, transaction.manager) from adaero.models.all import Base, SEQUENCES Base.metadata.drop_all(engine) for seq in SEQUENCES: seq.create(engine) Base.metadata.create_all(engine) yield session testing.tearDown() transaction.abort() Base.metadata.drop_all(engine)
def email_job(settings): log.info("Starting email job...") start_time_s = time.time() engine = get_engine(settings) session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) ldapsource = build_ldapauth_from_settings(settings) check_and_send_email(dbsession, ldapsource, settings) total_time_s = time.time() - start_time_s log.info("Finished email job, took {0:.2f} seconds".format(total_time_s))
def add_question(ctx, question, caption): engine = ctx.obj[ENGINE_KEY] session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) with transaction.manager: q = FeedbackQuestion(question_template=question, caption=caption) dbsession.add(q) with transaction.manager: result = (dbsession.query(FeedbackQuestion).filter( FeedbackQuestion.question_template == question).one()) print("Added question with id=%s" % result.id)
def nominate_everyone(ctx): engine = ctx.obj[ENGINE_KEY] session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) with transaction.manager: period = Period.get_current_period(dbsession) for user, nominee in (dbsession.query(User, Nominee).outerjoin( Nominee, Nominee.username == User.username).all()): if not nominee: dbsession.add(Nominee(user=user, period=period))
def generate_periods(ctx): engine = ctx.obj[ENGINE_KEY] session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) with transaction.manager: for i in range(1, 3): dates_dict = generate_period_dates( Period.INACTIVE_SUBPERIOD, lambda days: (datetime.utcnow() - timedelta(days=i * 30) + timedelta(days=days)), ) period = Period(name=u"Period %s" % i, template_id=1, **dates_dict) dbsession.add(period)
def reset_email_flags(ctx): engine = ctx.obj[ENGINE_KEY] session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) with transaction.manager: period = Period.get_current_period(dbsession) print("resetting email flags for period {.name}") period.enrol_email_last_sent = None period.enrol_reminder_email_last_sent = None period.entry_email_last_sent = None period.entry_reminder_email_last_sent = None period.review_email_last_sent = None period.feedback_available_mail_last_sent = None dbsession.merge(period)
def add_template(ctx, question): engine = ctx.obj[ENGINE_KEY] session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) rows = [] with transaction.manager: for i, q in enumerate(question): position = i + 1 tr = FeedbackTemplateRow(position=position, q_id=q) rows.append(tr) template = FeedbackTemplate(rows=rows) dbsession.add(template) transaction.commit() print("Added template with id=%s" % template.id)
def adjust(ctx): subperiod = SUBPERIOD_CHOICES[ctx.obj[SUBPERIOD_KEY]] engine = ctx.obj[ENGINE_KEY] session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) dates_dict = generate_period_dates( subperiod, lambda days: datetime.utcnow() + timedelta(days=days)) with transaction.manager: period = Period.get_current_period(dbsession) print("setting dates for period {.name} to {}".format( period, pformat(dates_dict))) for k, v in dates_dict.items(): setattr(period, k, v) dbsession.merge(period)
def rebuild_database(engine, settings): do_drop_database = _yes_or_no( 'Are you sure you want to drop the database "%s"?' % settings[constants.DB_NAME_KEY]) if not do_drop_database: print("Exiting without changes") sys.exit(0) Base.metadata.drop_all(engine) for seq in SEQUENCES: seq.create(engine) Base.metadata.create_all(engine) session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) return dbsession
def add_test_periods(ctx): engine = ctx.obj[ENGINE_KEY] session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) QUESTION_IDS_AND_TEMPLATES = [ (1, u"What should {display_name} CONTINUE doing?", None), (10, u"What should {display_name} STOP doing or do less of?", None), (3, u"What should {display_name} START doing or do more of?", None), ( 7, u"What is the general perception of {display_name}?", u"There are often general perceptions of a person that are not " u"always based on your direct experiences. This is can still be " u"very helpful input to provide.", ), ] template_id = add_template(dbsession, QUESTION_IDS_AND_TEMPLATES, 1) add_test_period_with_template( dbsession, Period.ENROLLMENT_SUBPERIOD, template_id, 1, u"Q1 2018", add_nominees=False, ) add_test_period_with_template( dbsession, Period.ENROLLMENT_SUBPERIOD, template_id, 2, u"Q4 2017", offset_from_utc_now_days=-30, add_nominees=False, ) add_test_period_with_template( dbsession, Period.ENROLLMENT_SUBPERIOD, template_id, 3, u"Q3 2017", offset_from_utc_now_days=-60, add_nominees=False, ) add_test_period_with_template( dbsession, Period.ENROLLMENT_SUBPERIOD, template_id, 4, u"Q2 2017", offset_from_utc_now_days=-90, add_nominees=False, ) add_test_period_with_template( dbsession, Period.ENROLLMENT_SUBPERIOD, template_id, 5, u"Q1 2017", offset_from_utc_now_days=-120, add_nominees=False, ) add_test_period_with_template( dbsession, Period.ENROLLMENT_SUBPERIOD, template_id, 6, u"Q4 2016", offset_from_utc_now_days=-150, add_nominees=False, )
def get_transaction_scoped_dbsession(engine): session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) return dbsession