from alembic import op
import re
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker

from portal.models.app_text import AppText
from portal.models.organization import Organization

"""empty message

Revision ID: 9b1bedfa916b
Revises: 441185240f62
Create Date: 2017-10-26 15:24:32.623899

"""

# revision identifiers, used by Alembic.
revision = '9b1bedfa916b'
down_revision = '441185240f62'

Session = sessionmaker()


def update_org_name(old, new):
    bind = op.get_bind()
    session = Session(bind=bind)

    session.execute("UPDATE organizations SET name='{}' "
                    "WHERE name='{}'".format(new, old))
    for at in session.query(AppText).filter(AppText.name.contains(old)):
        at.name = at.name.replace(old, new)
    session.commit()


def update_eproms_refs():
    bind = op.get_bind()
    session = Session(bind=bind)

    for at in session.query(AppText).all():
        if 'truenth eproms' in at.custom_text.lower():
            at.custom_text = re.sub(r' [eE]PROMs', '', at.custom_text)
        at.custom_text = at.custom_text.replace('ePROMs', 'TrueNTH')
    session.commit()


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    update_org_name('CRV', 'TrueNTH Global Registry')
    update_eproms_refs()
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    update_org_name('TrueNTH Global Registry', 'CRV')
    # ### end Alembic commands ###

@@ -779,27 +779,137 @@
 f up
-date_eproms_refs():
+grade():%0A    # ### commands auto generated by Alembic - please adjust! ###%0A    update_org_name('CRV', 'TrueNTH Global Registry')%0A
 %0A   
@@ -1229,341 +1229,1490 @@
 ()%0A%0A
-%0Adef upgrade():%0A    # ### commands auto generated by Alembic - please adjust! ###%0A    update_org_name('CRV', 'TrueNTH Global Registry')%0A    update_eproms_refs()%0A    # ### end Alembic commands ###%0A%0A%0Adef downgrade():%0A    # ### commands auto generated by Alembic - please adjust! ###%0A    update_org_name('TrueNTH Global Registry', 'CRV'
+    new_crv_kwargs = '%7B%22cutoff_days%22: %2230,60,90%22, %22org_id%22: %2210000%22%7D'%0A    new_iron_kwargs = '%7B%22cutoff_days%22: %227,14,21,30%22, %22org_id%22: %2220000%22%7D'%0A%0A    session.execute(%22UPDATE scheduled_jobs SET name = '%7B%7D', kwargs='%7B%7D' %22%0A                    %22WHERE name = '%7B%7D'%22.format('TNGR site summary email',%0A                                               new_crv_kwargs,%0A                                               'CRV site summary email'))%0A    session.execute(%22UPDATE scheduled_jobs SET kwargs = '%7B%7D' WHERE %22%0A                    %22name = '%7B%7D'%22.format(new_iron_kwargs,%0A                                         'IRONMAN site summary email'))%0A    # ### end Alembic commands ###%0A%0A%0Adef downgrade():%0A    # ### commands auto generated by Alembic - please adjust! ###%0A    update_org_name('TrueNTH Global Registry', 'CRV')%0A%0A    bind = op.get_bind()%0A    session = Session(bind=bind)%0A%0A    new_crv_kwargs = '%7B%22cutoff_days%22: %2230,60,90%22, %22org%22: %22CRV%22%7D'%0A    new_iron_kwargs = '%7B%22cutoff_days%22: %227,14,21,30%22, %22org%22: %22IRONMAN%22%7D'%0A%0A    session.execute(%22UPDATE scheduled_jobs SET name = '%7B%7D', kwargs='%7B%7D' %22%0A                    %22WHERE name = '%7B%7D'%22.format('CRV site summary email',%0A                                               new_crv_kwargs,%0A                                               'TNGR site summary email'))%0A    session.execute(%22UPDATE scheduled_jobs SET kwargs = '%7B%7D' WHERE %22%0A                    %22name = '%7B%7D'%22.format(new_iron_kwargs,%0A                                         'IRONMAN site summary email')
 )%0A  

update site summary scheduledjob names/kwargs as part of migration