def upgrade():
    disable_listeners()
    op.add_column('task', sa.Column('date', sa.Date()))
    from autonomie.models.task import Task
    from autonomie_base.models.base import DBSESSION

    session = DBSESSION()
    for task in Task.query().filter(Task.type_!='manualinvoice'):
        task.date = task.taskDate
        session.merge(task)
    session.flush()

    op.execute("alter table groups modify label VARCHAR(255);")
    op.execute("alter table payment modify remittance_amount VARCHAR(255);")
    from autonomie.models.user import User, Group
    for group_id, group_name, group_label in GROUPS:
        group = session.query(Group).filter(Group.name==group_name).first()
        if group is None:
            group = Group(name=group_name, label=group_label)
            session.add(group)
            session.flush()

        users = session.query(User).filter(User.primary_group==group_id)
        for user in users:
            user._groups.append(group)
            session.merge(user)

    label = u"Peut saisir/modifier/supprimer les paiements de ses factures"
    group_name = "payment_admin"
    group = Group.query().filter(Group.name==group_name).first()
    if group is not None:
        group.label = label
        session.merge(group)
def upgrade():
    disable_listeners()
    op.add_column('task', sa.Column('date', sa.Date()))
    from autonomie.models.task import Task
    from autonomie_base.models.base import DBSESSION

    session = DBSESSION()
    for task in Task.query().filter(Task.type_ != 'manualinvoice'):
        task.date = task.taskDate
        session.merge(task)
    session.flush()

    op.execute("alter table groups modify label VARCHAR(255);")
    op.execute("alter table payment modify remittance_amount VARCHAR(255);")
    from autonomie.models.user import User, Group
    for group_id, group_name, group_label in GROUPS:
        group = session.query(Group).filter(Group.name == group_name).first()
        if group is None:
            group = Group(name=group_name, label=group_label)
            session.add(group)
            session.flush()

        users = session.query(User).filter(User.primary_group == group_id)
        for user in users:
            user._groups.append(group)
            session.merge(user)

    label = u"Peut saisir/modifier/supprimer les paiements de ses factures"
    group_name = "payment_admin"
    group = Group.query().filter(Group.name == group_name).first()
    if group is not None:
        group.label = label
        session.merge(group)
Beispiel #3
0
    def _find_one(cls, name_or_id):
        """
        Used as a creator for the initialization proxy
        """
        with DBSESSION.no_autoflush:
            res = DBSESSION.query(cls).get(name_or_id)
            if res is None:
                # We try with the id
                res = DBSESSION.query(cls).filter(cls.name == name_or_id).one()

        return res
Beispiel #4
0
    def _find_one(cls, name_or_id):
        """
        Used as a creator for the initialization proxy
        """
        with DBSESSION.no_autoflush:
            res = DBSESSION.query(cls).get(name_or_id)
            if res is None:
                # We try with the id
                res = DBSESSION.query(cls).filter(
                    cls.name == name_or_id
                ).one()

        return res
Beispiel #5
0
def upgrade():
    from autonomie.models import user
    from autonomie_base.models.base import DBSESSION

    db = DBSESSION()

    for u in db.query(user.User)\
             .filter(user.User.userdatas==None)\
             .filter(user.User.primary_group==3):
        situation = "sortie"
        if u.email:
            userdata = user.UserDatas(
                situation_situation=situation,
                coordonnees_firstname=u.firstname,
                coordonnees_lastname=u.lastname,
                coordonnees_email1=u.email,
                coordonnees_civilite=u'?',
            )
            userdata.user_id = u.id
            for company in u.companies:
                companydata = user.CompanyDatas(
                    title=company.goal,
                    name=company.name,
                )
                userdata.activity_companydatas.append(companydata)
            db.add(userdata)
            db.flush()
Beispiel #6
0
def resize_headers_command(arguments, env):
    """
    Resize headers to limit their size

    :param dict arguments: The arguments parsed from the command-line
    :param obj env: The pyramid environment
    """
    from sqlalchemy import distinct
    from autonomie.models.files import File
    from autonomie.models.company import Company
    from autonomie.forms.company import HEADER_RESIZER

    limit = get_value(arguments, 'limit', None)
    offset = get_value(arguments, "offset", None)

    session = DBSESSION()
    file_id_query = session.query(distinct(Company.header_id))
    if limit:
        file_id_query = file_id_query.limit(limit)
        if offset:
            file_id_query = file_id_query.offset(offset)

    header_ids = [i[0] for i in file_id_query]
    header_files = File.query().filter(File.id.in_(header_ids))
    for header_file in header_files:
        file_datas = header_file.data_obj
        if file_datas:
            print(
                u"Resizing header with id : {}".format(
                    header_file.id
                )
            )
            header_file.data = HEADER_RESIZER.complete(file_datas)
            session.merge(header_file)
Beispiel #7
0
    def get_next_index(cls, company, factory):
        query = DBSESSION.query(func.max(factory.company_index))
        query = query.filter(factory.company_id == company.id)
        max_num = query.first()[0]
        if max_num is None:
            max_num = 0

        return max_num + 1
Beispiel #8
0
    def get_next_index(cls, project, factory):
        query = DBSESSION.query(func.max(factory.project_index))
        query = query.filter(factory.project_id == project.id)
        max_num = query.first()[0]
        if max_num is None:
            max_num = 0

        return max_num + 1
Beispiel #9
0
    def get_next_index(cls, company, factory):
        query = DBSESSION.query(func.max(factory.company_index))
        query = query.filter(factory.company_id == company.id)
        max_num = query.first()[0]
        if max_num is None:
            max_num = 0

        return max_num + 1
def migrate_datas():
    from autonomie.models.project.types import BusinessType
    from autonomie.models.config import Config
    from autonomie.models.task.mentions import TaskMention
    from autonomie.models.project.mentions import BusinessTypeTaskMention
    from autonomie_base.models.base import DBSESSION
    from alembic.context import get_bind
    session = DBSESSION()
    conn = get_bind()

    # Collect business type ids
    business_type_ids = [b[0] for b in session.query(BusinessType.id)]

    # for each fixed config key we now use mentions
    for index, (doctype, key, label, title) in enumerate(((
            'estimation',
            'coop_estimationfooter',
            u"Informations sur l'acceptation des devis",
            u"Acceptation du devis",
    ), (
            "invoice",
            "coop_invoicepayment",
            u"Informations de paiement pour les factures",
            u"Mode de paiement",
    ), (
            "invoice",
            "coop_invoicelate",
            u"Informations sur les retards de paiement",
            u"Retard de paiement",
    ))):
        # We retrieve the configurated value
        value = Config.get_value(key, "")
        mention = TaskMention(
            order=index,
            label=label,
            title=title,
            full_text=value.replace('%IBAN%', "{IBAN}").replace(
                '%RIB%', "{RIB}").replace('%ENTREPRENEUR%', '{name}'))
        session.add(mention)
        session.flush()

        for btype_id in business_type_ids:
            rel = BusinessTypeTaskMention(
                task_mention_id=mention.id,
                business_type_id=btype_id,
                doctype=doctype,
                mandatory=True,
            )
            session.add(rel)
            session.flush()

        op.execute(
            u"INSERT INTO mandatory_task_mention_rel (task_id, mention_id) \
    SELECT task.id, {mention_id} from task join node on task.id=node.id where \
    node.type_='{type_}'".format(mention_id=mention.id, type_=doctype))
def upgrade():
    op.execute(u"Alter table tva modify name VARCHAR(15)")
    op.execute(u"Alter table tva modify active tinyint(1)")
    op.add_column('tva', sa.Column('mention', sa.Text(), default=''))

    from autonomie.models.tva import Tva
    from autonomie_base.models.base import DBSESSION
    session = DBSESSION()
    for tva in session.query(Tva):
        if tva.value <= 0:
            tva.mention = u"TVA non applicable selon l'article 259b du CGI."
            session.merge(tva)
        else:
            tva.mention = u"TVA {0} %".format(tva.value / 100.0)
            session.merge(tva)
Beispiel #12
0
    def get_turnover(cls, company, year):
        """
        Compute the annual turnover for a given company
        """
        from autonomie.models.task import (
            Task, )
        query = DBSESSION.query(func.sum(Task.ht))
        query = query.filter(Task.company_id == company.id)
        query = query.filter(func.year(Task.date) == year)
        query = query.filter(Task.status == 'valid')

        invoice_sum = query.filter(Task.type_.in_(
            ('invoice', 'cancelinvoice'))).first()[0]
        if invoice_sum is None:
            invoice_sum = 0
        return invoice_sum
Beispiel #13
0
    def get_turnover(cls, company, year):
        """
        Compute the annual turnover for a given company
        """
        from autonomie.models.task import (
            Task,
        )
        query = DBSESSION.query(func.sum(Task.ht))
        query = query.filter(Task.company_id == company.id)
        query = query.filter(func.year(Task.date) == year)
        query = query.filter(Task.status == 'valid')

        invoice_sum = query.filter(
            Task.type_.in_(('invoice', 'cancelinvoice'))
        ).first()[0]
        if invoice_sum is None:
            invoice_sum = 0
        return invoice_sum
def migrate_datas():
    from autonomie_base.models.base import DBSESSION
    from autonomie.models.config import Config

    session = DBSESSION()

    Config.query().filter_by(app='autonomie',
                             name='invoice_number_template').delete()

    prefix = session.query(Config.value).filter_by(
        app='autonomie',
        name='invoiceprefix',
    ).scalar() or ''

    default_format = Config(app='autonomie',
                            name='invoice_number_template',
                            value=prefix + '{SEQYEAR}')
    session.add(default_format)
    session.flush()
def migrate_datas():
    from autonomie_base.models.base import DBSESSION
    from autonomie.models.config import Config

    session = DBSESSION()

    Config.query().filter_by(
        app='autonomie',
        name='invoice_number_template'
    ).delete()

    prefix = session.query(Config.value).filter_by(
        app='autonomie',
        name='invoiceprefix',
    ).scalar() or ''

    default_format = Config(
        app='autonomie',
        name='invoice_number_template',
        value=prefix + '{SEQYEAR}'
    )
    session.add(default_format)
    session.flush()
Beispiel #16
0
class Anonymizer(object):
    def __init__(self, logger):
        self.us_faker = Faker()
        self.faker = Faker('fr_FR')
        self.logger = logger
        self.session = DBSESSION()

    def _zipcode(self):
        if hasattr(self.faker, 'zipcode'):
            return self.faker.zipcode()
        else:
            return self.faker.postcode()

    def _an_activity(self):
        from autonomie.models.activity import Activity, ActivityType
        for activity in self.session.query(Activity):
            for fieldname in ('point', 'objectifs', 'action', 'documents',
                              'notes'):
                setattr(activity, fieldname, self.faker.text())

        type_labels = (
            u"RV conseil",
            u"RV suivi",
            u"RV Gestion",
            u"RV Admin",
            u"RV RH",
            u"RV Compta",
            u"RV hebdo",
            u"RV Mensuel",
        )

        for index, typ in enumerate(self.session.query(ActivityType).all()):
            typ.label = type_labels[index % 7]

    def _an_commercial(self):
        from autonomie.models.commercial import TurnoverProjection
        for t in self.session.query(TurnoverProjection):
            t.comment = self.faker.text()

    def _an_company(self):
        from autonomie.models.company import Company
        for comp in self.session.query(Company):
            comp.name = self.faker.company()
            comp.goal = self.faker.bs()
            comp.comments = self.faker.catch_phrase()
            comp.phone = self.faker.phone_number()
            comp.mobile = self.faker.phone_number()
            comp.email = self.faker.ascii_safe_email()
            header = build_header(u"{0}\n {1} - {2}".format(
                comp.name, comp.phone, comp.email))
            comp.header = {'name': 'header.png', 'data': header}
        self.session.execute(u"update company set cgv=''")

    def _an_competence(self):
        from autonomie.models.competence import (
            CompetenceGridItem,
            CompetenceGridSubItem,
        )
        for item in self.session.query(CompetenceGridItem):
            item.progress = self.faker.text()
        for item in self.session.query(CompetenceGridSubItem):
            item.comments = self.faker.text()

    def _an_config(self):
        from autonomie.models.config import Config, ConfigFiles

        Config.set('cae_admin_mail', self.faker.ascii_safe_email())
        Config.set('welcome', self.faker.sentence(nb_words=15))
        ConfigFiles.set(
            'logo.png', {
                'data':
                pkg_resources.resource_stream('autonomie',
                                              'static/img/autonomie.jpg'),
                'filename':
                'logo.jpg',
            })
        Config.set('coop_cgv', self.faker.paragraph(nb_sentences=40))
        Config.set(
            'coop_pdffootertitle', u"""Une activité de ma CAE SARL SCOP à \
capital variable""")
        Config.set(
            'coop_pdffootercourse',
            u"""Organisme de formation N° de déclaration \
d'activité au titre de la FPC : xx xx xxxxx. MA CAE est exonérée de TVA pour \
les activités s'inscrivant dans le cadre de la formation professionnelle \
conformément à l'art. L920-4 du Code du travail et de l'art. 202 C de \
l'annexe II du code général des impôts""")
        footer = u"""RCS XXXX 000 000 000 00000 - SIRET 000 \
000 000 000 00 - Code naf 0000Z TVA INTRACOM : FR0000000. Siège social : 10 \
rue vieille 23200 Aubusson"""
        Config.set('coop_pdffootercontent', footer)
        Config.set('coop_pdffootertext', footer)
        Config.set(
            'coop_invoicepayment', u"""Par chèque libellé à l'ordre de : \
MA CAE/ %ENTREPRENEUR%
à envoyer à l'adresse suivante :
MA CAE/ %ENTREPRENEUR%
10 rue Vieille
23200 Aubusson

Ou par virement sur le compte de MA CAE/ %ENTREPRENEUR%
MA BANQUE
RIB : xxxxx xxxx xxxxxxxxxxxxx
IBAN : xxxx xxxx xxxx xxxx xxxx xxxx xxx
BIC : MABAFRMACAXX
Merci d'indiquer le numéro de facture sur le libellé de votre virement ou \
dos de votre chèque.
""")
        Config.set(
            "coop_invoicelate",
            u"""Tout retard de paiement entraînera à titre de \
clause pénale, conformément à la loi 92.1442 du 31 décembre 1992, une \
pénalité égale à un taux d'intérêt équivalent à une fois et demi le taux \
d'intérêt légal en vigueur à cette échéance.
Une indemnité de 40 euros forfaitaire sera demandée en sus pour chaque \
facture payée après l’échéance fixée. Celle-ci n’est pas soumise à TVA.""")

        Config.set("activity_footer", footer)

        Config.set('workshop_footer', footer)

    def _an_customer(self):
        from autonomie.models.customer import Customer
        for cust in self.session.query(Customer):
            cust.name = self.faker.company()
            cust.address = self.faker.street_address()
            cust.zipcode = self._zipcode()
            cust.city = self.faker.city()
            cust.lastname = self.faker.last_name()
            cust.firstname = self.faker.first_name()
            cust.email = self.faker.ascii_safe_email()
            cust.phone = self.faker.phone_number()
            cust.fax = self.faker.phone_number()
            cust.tva_intracomm = ""
            cust.comments = self.faker.bs()

    def _an_expense(self):
        from autonomie.models.expense.sheet import (
            BaseExpenseLine,
            ExpenseKmLine,
            Communication,
        )
        for line in self.session.query(BaseExpenseLine):
            line.description = self.faker.text()
        for line in self.session.query(ExpenseKmLine):
            line.start = self.faker.city()
            line.end = self.faker.city()

        for com in self.session.query(Communication):
            com.content = self.faker.text()

    def _an_node(self):
        from autonomie.models.node import Node
        for node in self.session.query(Node):
            node.name = self.faker.sentence(nb_words=4, variable_nb_words=True)

    def _an_payment(self):
        from autonomie.models.payments import BankAccount
        for b in self.session.query(BankAccount):
            b.label = u"Banque : {0}".format(self.faker.company())

    def _an_project(self):
        from autonomie.models.project import Project, Phase
        for p in self.session.query(Project):
            p.name = self.faker.sentence(nb_words=5)
            p.definition = self.faker.text()

        for p in self.session.query(Phase):
            if p.name != u"Phase par défaut":
                p.name = self.faker.sentence(nb_words=3)

    def _an_sale_product(self):
        from autonomie.models.sale_product import (
            SaleProductCategory,
            SaleProduct,
            SaleProductGroup,
        )
        for cat in self.session.query(SaleProductCategory):
            cat.title = self.faker.sentence(nb_words=3)
            cat.description = self.faker.text()

        for prod in self.session.query(SaleProduct):
            prod.label = self.faker.sentence(nb_words=2)
            prod.description = self.faker.text()

        for group in self.session.query(SaleProductGroup):
            group.title = self.faker.sentence(nb_words=2)
            group.description = self.faker.text()

    def _an_statistic(self):
        from autonomie.models.statistics import (StatisticSheet)
        for s in self.session.query(StatisticSheet):
            s.title = self.faker.sentence(nb_words=4)

    def _an_task(self):
        from autonomie.models.task import (
            Task,
            DiscountLine,
            TaskLine,
            TaskLineGroup,
            Estimation,
            TaskStatus,
        )
        for task in self.session.query(Task):
            if task.status_comment:
                task.status_comment = self.faker.text()
            task.description = self.faker.text()
            task.address = task.customer.full_address
            task.workplace = self.faker.address()
            task.payment_conditions = u"Par chèque ou virement à réception de "
            u"facture"
        for line in self.session.query(DiscountLine):
            line.description = self.faker.text()

        for line in self.session.query(TaskLine):
            line.description = self.faker.text()

        for group in self.session.query(TaskLineGroup):
            if group.title:
                group.title = self.faker.sentence(nb_words=4)
            if group.description:
                group.description = self.faker.text()

        for status in self.session.query(TaskStatus):
            status.status_comment = self.faker.sentence(nb_words=6)

        for estimation in self.session.query(Estimation):
            if estimation.exclusions:
                estimation.exclusions = self.faker.text()

    def _an_task_config(self):
        from autonomie.models.task import (
            PaymentConditions, )
        for i in self.session.query(PaymentConditions):
            self.session.delete(i)

        for index, label in enumerate(
            [u"30 jours fin de mois", u"À réception de facture"]):
            condition = PaymentConditions(label=label)
            if index == 0:
                condition.default = True

            self.session.add(condition)

    def _an_user(self):
        from autonomie.models.user.login import (
            Login, )
        from autonomie.models.user.user import (
            User, )
        self.session.execute("Update accounts set session_datas='{}'")
        counter = itertools.count()
        found_contractor = False
        for u in self.session.query(Login).join(User).filter(
                Login.active == True):
            index = counter.next()
            if index == 1:
                u.login = u"admin1"
                u.groups = ['admin']

            elif index == 2:
                u.login = u"manager1"
                u.groups = ["manager"]

            elif not found_contractor and "contractor" in u.groups:
                u.login = u"entrepreneur1"
                found_contractor = True
            else:
                u.login = u"user_{0}".format(index)

            u.user.lastname = self.faker.last_name()
            u.user.firstname = self.faker.first_name()
            u.user.email = self.faker.ascii_safe_email()
            u.set_password(u.login)
            if u.user.has_userdatas():
                u.user.userdatas.coordonnees_lastname = u.user.lastname
                u.user.userdatas.coordonnees_firstname = u.user.firstname
                u.user.userdatas.coordonnees_email1 = u.user.email

        for u in self.session.query(Login).join(User).filter(
                Login.active == False):
            index = counter.next()
            u.login = u"user_{0}".format(index)
            u.user.lastname = self.faker.last_name()
            u.user.firstname = self.faker.first_name()
            u.user.email = self.faker.ascii_safe_email()
            u.set_password(u.login)
            if u.user.has_userdatas():
                u.user.userdatas.coordonnees_lastname = u.user.lastname
                u.user.userdatas.coordonnees_firstname = u.user.firstname
                u.user.userdatas.coordonnees_email1 = u.user.email

    def _an_userdatas(self):
        from autonomie.models.user.userdatas import (
            UserDatas,
            CompanyDatas,
            AntenneOption,
        )
        for u in self.session.query(UserDatas):
            if u.user_id is None:
                u.coordonnees_lastname = self.faker.last_name()
                u.coordonnees_firstname = self.faker.first_name()
                u.coordonnees_email1 = self.faker.ascii_safe_email()
            u.coordonnees_ladies_lastname = self.faker.last_name_female()
            u.coordonnees_email2 = self.faker.ascii_safe_email()
            u.coordonnees_tel = self.faker.phone_number()[:14]
            u.coordonnees_mobile = self.faker.phone_number()[:14]
            u.coordonnees_address = self.faker.street_address()
            u.coordonnees_zipcode = self._zipcode()
            u.coordonnees_city = self.faker.city()
            u.coordonnees_birthplace = self.faker.city()
            u.coordonnees_birthplace_zipcode = self._zipcode()
            u.coordonnees_secu = u"0 00 00 000 000 00"
            u.coordonnees_emergency_name = self.faker.name()
            u.coordonnees_emergency_phone = self.faker.phone_number()[:14]
            u.parcours_goals = self.faker.text()
        for datas in self.session.query(CompanyDatas):
            datas.title = self.faker.company()
            datas.name = self.faker.company()
            datas.website = self.faker.url()

        for a in AntenneOption.query():
            a.label = u"Antenne : {0}".format(self.faker.city())

    def _an_files(self):
        from autonomie.models.files import File
        for file_ in self.session.query(File):
            self.session.delete(file_)

        from autonomie.models.files import Template

        sample_tmpl_path = os.path.abspath(
            pkg_resources.resource_filename('autonomie', 'sample_templates'))
        for filename in os.listdir(sample_tmpl_path):
            filepath = os.path.join(sample_tmpl_path, filename)
            with open(filepath, 'r') as fbuf:
                tmpl = Template(name=filename, description=filename)
                tmpl.data = fbuf.read()
                self.session.add(tmpl)

    def _an_celery_jobs(self):
        self.session.execute("delete from mailing_job")
        self.session.execute("delete from file_generation_job")
        self.session.execute("delete from csv_import_job")
        self.session.execute("delete from job")

    def _an_task_mentions(self):
        from autonomie.models.task.mentions import TaskMention
        for mention in TaskMention.query():
            mention.full_text = self.faker.paragraph(nb_sentences=3)
            self.session.merge(mention)

    def run(self, module_key=None):
        if module_key is not None:
            if not module_key.startswith('_an_'):
                module_key = u"_an_%s" % module_key
            keys = [module_key]
            methods = {module_key: getattr(self, module_key)}
        else:
            methods = {}
            for method_name, method in inspect.getmembers(
                    self, inspect.ismethod):
                if method_name.startswith('_an_'):
                    methods[method_name] = method
            keys = methods.keys()
            keys.sort()
        for key in keys:
            self.logger.debug(u"Step : {0}".format(key))
            methods[key]()
            transaction.commit()
            transaction.begin()
class Anonymizer(object):
    def __init__(self, logger):
        self.us_faker = Faker()
        self.faker = Faker('fr_FR')
        self.logger = logger
        self.session = DBSESSION()

    def _zipcode(self):
        if hasattr(self.faker, 'zipcode'):
            return self.faker.zipcode()
        else:
            return self.faker.postcode()

    def _an_activity(self):
        from autonomie.models.activity import Activity, ActivityType
        from autonomie.models.workshop import Workshop

        for activity in self.session.query(Activity):
            for fieldname in (
                'point',
                'objectifs',
                'action',
                'documents',
                'notes'
            ):
                setattr(activity, fieldname, self.faker.text())

        for workshop in self.session.query(Workshop).options(load_only('id')):
            workshop.description = self.faker.text()
            self.session.merge(workshop)

        type_labels = (
            u"RV conseil",
            u"RV suivi",
            u"RV Gestion",
            u"RV Admin",
            u"RV RH",
            u"RV Compta",
            u"RV hebdo",
            u"RV Mensuel",
        )

        for index, typ in enumerate(self.session.query(ActivityType).all()):
            typ.label = type_labels[index % 7]

    def _an_commercial(self):
        from autonomie.models.commercial import TurnoverProjection
        for t in self.session.query(TurnoverProjection):
            t.comment = self.faker.text()

    def _an_company(self):
        from autonomie.models.company import Company
        for comp in self.session.query(Company):
            comp.name = self.faker.company()
            comp.goal = self.faker.bs()
            comp.comments = self.faker.catch_phrase()
            comp.phone = self.faker.phone_number()
            comp.mobile = self.faker.phone_number()
            comp.email = self.faker.ascii_safe_email()
            header = build_header(
                u"{0}\n {1} - {2}".format(comp.name, comp.phone, comp.email)
            )
            comp.header = {'name': 'header.png', 'data': header}
        self.session.execute(u"update company set cgv=''")

    def _an_competence(self):
        from autonomie.models.competence import (
            CompetenceGridItem,
            CompetenceGridSubItem,
        )
        for item in self.session.query(CompetenceGridItem):
            item.progress = self.faker.text()
        for item in self.session.query(CompetenceGridSubItem):
            item.comments = self.faker.text()

    def _an_config(self):
        from autonomie.models.config import Config, ConfigFiles

        Config.set('cae_admin_mail', self.faker.ascii_safe_email())
        Config.set('welcome', self.faker.sentence(nb_words=15))
        ConfigFiles.set(
            'logo.png',
            {
                'data': pkg_resources.resource_stream(
                    'autonomie',
                    'static/img/autonomie.jpg'
                ),
                'filename': 'logo.jpg',
            }
        )
        Config.set('coop_cgv', self.faker.paragraph(nb_sentences=40))
        Config.set('coop_pdffootertitle', u"""Une activité de ma CAE SARL SCOP à \
capital variable""")
        Config.set('coop_pdffootercourse', u"""Organisme de formation N° de déclaration \
d'activité au titre de la FPC : xx xx xxxxx. MA CAE est exonérée de TVA pour \
les activités s'inscrivant dans le cadre de la formation professionnelle \
conformément à l'art. L920-4 du Code du travail et de l'art. 202 C de \
l'annexe II du code général des impôts""")
        footer = u"""RCS XXXX 000 000 000 00000 - SIRET 000 \
000 000 000 00 - Code naf 0000Z TVA INTRACOM : FR0000000. Siège social : 10 \
rue vieille 23200 Aubusson"""
        Config.set('coop_pdffootercontent', footer)
        Config.set('coop_pdffootertext', footer)
        Config.set('coop_invoicepayment', u"""Par chèque libellé à l'ordre de : \
MA CAE/ %ENTREPRENEUR%
à envoyer à l'adresse suivante :
MA CAE/ %ENTREPRENEUR%
10 rue Vieille
23200 Aubusson

Ou par virement sur le compte de MA CAE/ %ENTREPRENEUR%
MA BANQUE
RIB : xxxxx xxxx xxxxxxxxxxxxx
IBAN : xxxx xxxx xxxx xxxx xxxx xxxx xxx
BIC : MABAFRMACAXX
Merci d'indiquer le numéro de facture sur le libellé de votre virement ou \
dos de votre chèque.
""")
        Config.set("coop_invoicelate", u"""Tout retard de paiement entraînera à titre de \
clause pénale, conformément à la loi 92.1442 du 31 décembre 1992, une \
pénalité égale à un taux d'intérêt équivalent à une fois et demi le taux \
d'intérêt légal en vigueur à cette échéance.
Une indemnité de 40 euros forfaitaire sera demandée en sus pour chaque \
facture payée après l’échéance fixée. Celle-ci n’est pas soumise à TVA.""")

        Config.set("activity_footer", footer)

        Config.set('workshop_footer', footer)

    def _an_customer(self):
        from autonomie.models.customer import Customer
        for cust in self.session.query(Customer):
            cust.name = self.faker.company()
            cust.address = self.faker.street_address()
            cust.zipcode = self._zipcode()
            cust.city = self.faker.city()
            cust.lastname = self.faker.last_name()
            cust.firstname = self.faker.first_name()
            cust.email = self.faker.ascii_safe_email()
            cust.phone = self.faker.phone_number()
            cust.fax = self.faker.phone_number()
            cust.tva_intracomm = ""
            cust.comments = self.faker.bs()

    def _an_expense(self):
        from autonomie.models.expense.sheet import (
            BaseExpenseLine,
            ExpenseKmLine,
            Communication,
        )
        for line in self.session.query(BaseExpenseLine):
            line.description = self.faker.text()
        for line in self.session.query(ExpenseKmLine):
            line.start = self.faker.city()
            line.end = self.faker.city()

        for com in self.session.query(Communication):
            com.content = self.faker.text()

    def _an_node(self):
        from autonomie.models.node import Node
        for node in self.session.query(Node):
            node.name = self.faker.sentence(nb_words=4, variable_nb_words=True)

    def _an_payment(self):
        from autonomie.models.payments import BankAccount
        for b in self.session.query(BankAccount):
            b.label = u"Banque : {0}".format(self.faker.company())

    def _an_project(self):
        from autonomie.models.project import Project, Phase
        for p in self.session.query(Project):
            p.name = self.faker.sentence(nb_words=5)
            p.definition = self.faker.text()

        for p in self.session.query(Phase):
            if not p.name.is_default():
                p.name = self.faker.sentence(nb_words=3)

    def _an_sale_product(self):
        from autonomie.models.sale_product import (
            SaleProductCategory,
            SaleProduct,
            SaleProductGroup,
        )
        for cat in self.session.query(SaleProductCategory):
            cat.title = self.faker.sentence(nb_words=3)
            cat.description = self.faker.text()

        for prod in self.session.query(SaleProduct):
            prod.label = self.faker.sentence(nb_words=2)
            prod.description = self.faker.text()

        for group in self.session.query(SaleProductGroup):
            group.title = self.faker.sentence(nb_words=2)
            group.description = self.faker.text()

    def _an_statistic(self):
        from autonomie.models.statistics import (
            StatisticSheet
        )
        for s in self.session.query(StatisticSheet):
            s.title = self.faker.sentence(nb_words=4)

    def _an_task(self):
        from autonomie.models.task import (
            Task,
            DiscountLine,
            TaskLine,
            TaskLineGroup,
            Estimation,
            TaskStatus,
        )
        for task in self.session.query(Task):
            if task.status_comment:
                task.status_comment = self.faker.text()
            task.description = self.faker.text()
            task.address = task.customer.full_address
            task.workplace = self.faker.address()
            task.payment_conditions = u"Par chèque ou virement à réception de "
            u"facture"
            if task.notes:
                task.notes = self.faker.text()
        for line in self.session.query(DiscountLine):
            line.description = self.faker.text()

        for line in self.session.query(TaskLine):
            line.description = self.faker.text()

        for group in self.session.query(TaskLineGroup):
            if group.title:
                group.title = self.faker.sentence(nb_words=4)
            if group.description:
                group.description = self.faker.text()

        for status in self.session.query(TaskStatus):
            status.status_comment = self.faker.sentence(nb_words=6)

    def _an_task_config(self):
        from autonomie.models.task import (
            PaymentConditions,
        )
        for i in self.session.query(PaymentConditions):
            self.session.delete(i)

        for index, label in enumerate(
            [u"30 jours fin de mois", u"À réception de facture"]
        ):
            condition = PaymentConditions(label=label)
            if index == 0:
                condition.default = True

            self.session.add(condition)

    def _an_user(self):
        from autonomie.models.user.login import (
            Login,
        )
        from autonomie.models.user.user import (
            User,
        )
        self.session.execute("Update accounts set session_datas='{}'")
        counter = itertools.count()
        found_contractor = False
        for u in self.session.query(Login).join(User).filter(Login.active==True):
            index = counter.next()
            if index == 1:
                u.login = u"admin1"
                u.groups = ['admin']

            elif index == 2:
                u.login = u"manager1"
                u.groups = ["manager"]

            elif not found_contractor and "contractor" in u.groups:
                u.login = u"entrepreneur1"
                found_contractor = True
            else:
                u.login = u"user_{0}".format(index)

            u.user.lastname = self.faker.last_name()
            u.user.firstname = self.faker.first_name()
            u.user.email = self.faker.ascii_safe_email()
            u.set_password(u.login)
            if u.user.has_userdatas():
                u.user.userdatas.coordonnees_lastname = u.user.lastname
                u.user.userdatas.coordonnees_firstname = u.user.firstname
                u.user.userdatas.coordonnees_email1 = u.user.email

        for u in self.session.query(Login).join(User).filter(Login.active==False
                                                             ):
            index = counter.next()
            u.login = u"user_{0}".format(index)
            u.user.lastname = self.faker.last_name()
            u.user.firstname = self.faker.first_name()
            u.user.email = self.faker.ascii_safe_email()
            u.set_password(u.login)
            if u.user.has_userdatas():
                u.user.userdatas.coordonnees_lastname = u.user.lastname
                u.user.userdatas.coordonnees_firstname = u.user.firstname
                u.user.userdatas.coordonnees_email1 = u.user.email

    def _an_userdatas(self):
        from autonomie.models.user.userdatas import (
            UserDatas,
            CompanyDatas,
            AntenneOption,
        )
        for u in self.session.query(UserDatas):
            if u.user_id is None:
                u.coordonnees_lastname = self.faker.last_name()
                u.coordonnees_firstname = self.faker.first_name()
                u.coordonnees_email1 = self.faker.ascii_safe_email()
            u.coordonnees_ladies_lastname = self.faker.last_name_female()
            u.coordonnees_email2 = self.faker.ascii_safe_email()
            u.coordonnees_tel = self.faker.phone_number()[:14]
            u.coordonnees_mobile = self.faker.phone_number()[:14]
            u.coordonnees_address = self.faker.street_address()
            u.coordonnees_zipcode = self._zipcode()
            u.coordonnees_city = self.faker.city()
            u.coordonnees_birthplace = self.faker.city()
            u.coordonnees_birthplace_zipcode = self._zipcode()
            u.coordonnees_secu = u"0 00 00 000 000 00"
            u.coordonnees_emergency_name = self.faker.name()
            u.coordonnees_emergency_phone = self.faker.phone_number()[:14]
            u.parcours_goals = self.faker.text()
        for datas in self.session.query(CompanyDatas):
            datas.title = self.faker.company()
            datas.name = self.faker.company()
            datas.website = self.faker.url()

        for a in AntenneOption.query():
            a.label = u"Antenne : {0}".format(self.faker.city())

    def _an_files(self):
        from autonomie.models.files import File
        for file_ in self.session.query(File):
            self.session.delete(file_)

        from autonomie.models.files import Template

        sample_tmpl_path = os.path.abspath(
            pkg_resources.resource_filename('autonomie', 'sample_templates')
        )
        for filename in os.listdir(sample_tmpl_path):
            filepath = os.path.join(sample_tmpl_path, filename)
            with open(filepath, 'r') as fbuf:
                tmpl = Template(name=filename, description=filename)
                tmpl.data = fbuf.read()
                self.session.add(tmpl)

    def _an_celery_jobs(self):
        self.session.execute("delete from mailing_job")
        self.session.execute("delete from file_generation_job")
        self.session.execute("delete from csv_import_job")
        self.session.execute("delete from job")

    def _an_task_mentions(self):
        from autonomie.models.task.mentions import TaskMention
        for mention in TaskMention.query():
            mention.full_text = self.faker.paragraph(nb_sentences=3)
            self.session.merge(mention)

    def run(self, module_key=None):
        if module_key is not None:
            if not module_key.startswith('_an_'):
                module_key = u"_an_%s" % module_key
            keys = [module_key]
            methods = {module_key: getattr(self, module_key)}
        else:
            methods = {}
            for method_name, method in inspect.getmembers(
                self,
                inspect.ismethod
            ):
                if method_name.startswith('_an_'):
                    methods[method_name] = method
            keys = methods.keys()
            keys.sort()
        for key in keys:
            self.logger.debug(u"Step : {0}".format(key))
            methods[key]()
            transaction.commit()
            transaction.begin()
def migrate_datas(situations_ids, stages_ids):
    """
    Migrate parcours's data from user_datas and related tables to career_path
    """
    from autonomie.models.career_path import CareerPath
    session = DBSESSION()
    cnx = op.get_bind()
    userdatas = cnx.execute("SELECT \
        id,\
        parcours_contract_type,\
        parcours_start_date,\
        parcours_end_date,\
        parcours_last_avenant,\
        parcours_taux_horaire,\
        parcours_taux_horaire_letters,\
        parcours_num_hours,\
        parcours_salary,\
        parcours_salary_letters,\
        parcours_employee_quality_id,\
        sortie_date,\
        sortie_motif_id,\
        sortie_type_id \
        FROM user_datas")
    for u in userdatas:
        # Diagnotic
        diagnotics = cnx.execute("SELECT date FROM date_diagnostic_datas WHERE date>'2000-01-01' AND userdatas_id=%s" % u.id)
        for diagnotic in diagnotics:
            session.add(CareerPath(userdatas_id=u.id, career_stage_id=stages_ids[0], start_date=diagnotic.date))
        # CAPE
        capes = cnx.execute("SELECT date, end_date FROM date_convention_cape_datas WHERE date>'2000-01-01' AND userdatas_id=%s" % u.id)
        for cape in capes:
            session.add(CareerPath(
                userdatas_id=u.id,
                career_stage_id=stages_ids[1],
                start_date=cape.date,
                end_date=cape.end_date,
                cae_situation_id=situations_ids[0],
                stage_type="entry"
            ))
        # DPAE
        dpaes = cnx.execute("SELECT date FROM date_dpae_datas WHERE date>'2000-01-01' AND userdatas_id=%s" % u.id)
        for dpae in dpaes:
            session.add(CareerPath(userdatas_id=u.id, career_stage_id=stages_ids[2], start_date=dpae.date))
        # Contrat
        if u.parcours_start_date and u.parcours_contract_type is not None:
            from autonomie.models.career_path import TypeContratOption
            cdi_type = session.query(TypeContratOption).filter(
                TypeContratOption.label==u.parcours_contract_type.upper()
            ).first()
            if cdi_type:
                cdi_type_id = cdi_type.id
            else:
                cdi_type_id = None
            session.add(CareerPath(
                userdatas_id=u.id,
                career_stage_id=stages_ids[3],
                start_date=u.parcours_start_date,
                end_date=u.parcours_end_date,
                cae_situation_id=situations_ids[1],
                stage_type="contrat",
                type_contrat_id=cdi_type_id,
                employee_quality_id=u.parcours_employee_quality_id,
                taux_horaire=u.parcours_taux_horaire,
                num_hours=u.parcours_num_hours
            ))
        # Avenant contrat
        if u.parcours_last_avenant:
            avenants = cnx.execute("SELECT date, number FROM contract_history WHERE date>'2000-01-01' AND userdatas_id=%s" % u.id)
            for avenant in avenants:
                model_avenant = CareerPath(
                    userdatas_id=u.id,
                    career_stage_id=stages_ids[4],
                    start_date=avenant.date,
                    stage_type="amendment",
                    amendment_number=avenant.number
                )
                if u.parcours_last_avenant==avenant.date:
                    model_avenant.taux_horaire=u.parcours_taux_horaire
                    model_avenant.num_hours=u.parcours_num_hours
                session.add(model_avenant)
        # Sortie
        if u.sortie_date:
            session.add(CareerPath(
                userdatas_id=u.id,
                career_stage_id=stages_ids[5],
                start_date=u.sortie_date,
                cae_situation_id=situations_ids[2],
                stage_type="exit",
                type_sortie_id=u.sortie_type_id,
                motif_sortie_id=u.sortie_motif_id
            ))
        # Historique des situations
        changes = cnx.execute("SELECT date, situation_id FROM cae_situation_change WHERE date>'2000-01-01' AND userdatas_id=%s" % u.id)
        for change in changes:
            session.add(CareerPath(userdatas_id=u.id, start_date=change.date, cae_situation_id=change.situation_id))
        # Sauvegarde du parcours de l'utilisateur
        session.flush()
def migrate_datas(situations_ids, stages_ids):
    """
    Migrate parcours's data from user_datas and related tables to career_path
    """
    from autonomie.models.career_path import CareerPath
    session = DBSESSION()
    cnx = op.get_bind()
    userdatas = cnx.execute("SELECT \
        id,\
        parcours_contract_type,\
        parcours_start_date,\
        parcours_end_date,\
        parcours_last_avenant,\
        parcours_taux_horaire,\
        parcours_taux_horaire_letters,\
        parcours_num_hours,\
        parcours_salary,\
        parcours_salary_letters,\
        parcours_employee_quality_id,\
        sortie_date,\
        sortie_motif_id,\
        sortie_type_id \
        FROM user_datas")
    for u in userdatas:
        # Diagnotic
        diagnotics = cnx.execute(
            "SELECT date FROM date_diagnostic_datas WHERE date>'2000-01-01' AND userdatas_id=%s"
            % u.id)
        for diagnotic in diagnotics:
            session.add(
                CareerPath(userdatas_id=u.id,
                           career_stage_id=stages_ids[0],
                           start_date=diagnotic.date))
        # CAPE
        capes = cnx.execute(
            "SELECT date, end_date FROM date_convention_cape_datas WHERE date>'2000-01-01' AND userdatas_id=%s"
            % u.id)
        for cape in capes:
            session.add(
                CareerPath(userdatas_id=u.id,
                           career_stage_id=stages_ids[1],
                           start_date=cape.date,
                           end_date=cape.end_date,
                           cae_situation_id=situations_ids[0],
                           stage_type="entry"))
        # DPAE
        dpaes = cnx.execute(
            "SELECT date FROM date_dpae_datas WHERE date>'2000-01-01' AND userdatas_id=%s"
            % u.id)
        for dpae in dpaes:
            session.add(
                CareerPath(userdatas_id=u.id,
                           career_stage_id=stages_ids[2],
                           start_date=dpae.date))
        # Contrat
        if u.parcours_start_date and u.parcours_contract_type is not None:
            from autonomie.models.career_path import TypeContratOption
            cdi_type = session.query(TypeContratOption).filter(
                TypeContratOption.label ==
                u.parcours_contract_type.upper()).first()
            if cdi_type:
                cdi_type_id = cdi_type.id
            else:
                cdi_type_id = None
            session.add(
                CareerPath(userdatas_id=u.id,
                           career_stage_id=stages_ids[3],
                           start_date=u.parcours_start_date,
                           end_date=u.parcours_end_date,
                           cae_situation_id=situations_ids[1],
                           stage_type="contrat",
                           type_contrat_id=cdi_type_id,
                           employee_quality_id=u.parcours_employee_quality_id,
                           taux_horaire=u.parcours_taux_horaire,
                           num_hours=u.parcours_num_hours))
        # Avenant contrat
        if u.parcours_last_avenant:
            avenants = cnx.execute(
                "SELECT date, number FROM contract_history WHERE date>'2000-01-01' AND userdatas_id=%s"
                % u.id)
            for avenant in avenants:
                model_avenant = CareerPath(userdatas_id=u.id,
                                           career_stage_id=stages_ids[4],
                                           start_date=avenant.date,
                                           stage_type="amendment",
                                           amendment_number=avenant.number)
                if u.parcours_last_avenant == avenant.date:
                    model_avenant.taux_horaire = u.parcours_taux_horaire
                    model_avenant.num_hours = u.parcours_num_hours
                session.add(model_avenant)
        # Sortie
        if u.sortie_date:
            session.add(
                CareerPath(userdatas_id=u.id,
                           career_stage_id=stages_ids[5],
                           start_date=u.sortie_date,
                           cae_situation_id=situations_ids[2],
                           stage_type="exit",
                           type_sortie_id=u.sortie_type_id,
                           motif_sortie_id=u.sortie_motif_id))
        # Historique des situations
        changes = cnx.execute(
            "SELECT date, situation_id FROM cae_situation_change WHERE date>'2000-01-01' AND userdatas_id=%s"
            % u.id)
        for change in changes:
            session.add(
                CareerPath(userdatas_id=u.id,
                           start_date=change.date,
                           cae_situation_id=change.situation_id))
        # Sauvegarde du parcours de l'utilisateur
        session.flush()
def upgrade():
    now = time.time()
    logger = logging.getLogger('autonomie')
    logger.addHandler(logging.StreamHandler(sys.stdout))
    logger.setLevel(logging.INFO)
    from depot.fields.upload import UploadedFile
    from sqlalchemy import bindparam

    from autonomie_base.models.base import DBSESSION, METADATA

    session = DBSESSION()

    def process(thing, store):
        id, data, filename, mimetype = thing
        logger.debug("Handling file with id %s" % id)
        uploaded_file = UploadedFile({'depot_name': "local", 'files': []})
        uploaded_file._thaw()
        uploaded_file.process_content(data,
                                      filename=filename,
                                      content_type=mimetype)
        store.append({
            'nodeid': thing.id,
            'depot_datas': uploaded_file.encode(),
        })
        logger.info("Saved data for node id {}".format(id))

    window_size = 10
    window_idx = 0

    logger.info("# Starting migration of blob datas #")
    from alembic.context import get_bind
    conn = get_bind()

    # Processing the file table
    logger.debug("  + Processing files")
    files = sa.Table('file', METADATA)
    processed_files = []
    count = session.query(files.c.id).count()

    logger.debug(u"   + Moving the files on disk")
    while True:
        start = window_size * window_idx
        if start >= count:
            break
        logger.debug("Slicing from %s" % (start, ))

        req = "select distinct(file.id), data, node.name, mimetype from file join node on file.id=node.id LIMIT %s, %s" % (
            start, window_size)
        things = conn.execute(req)
        if things is None:
            break

        for thing in things:
            process(thing, processed_files)

        window_idx += 1
    logger.debug(u"-> Done")

    logger.debug(u"Migrating the 'data' column")
    op.drop_column('file', 'data')
    op.add_column('file', sa.Column('data', sa.Unicode(4096)))
    files.c.data.type = sa.Unicode(4096)

    update = files.update().where(files.c.id == bindparam('nodeid')).\
        values({files.c.data: bindparam('depot_datas')})

    def chunks(l, n):
        for i in xrange(0, len(l), n):
            yield l[i:i + n]

    for cdata in chunks(processed_files, 10):
        session.execute(update, cdata)

    logger.debug("  + Processing config files")
    logger.debug(u"   + Moving the files on disk")
    config_files = sa.Table('config_files', METADATA)
    processed_config_files = []
    req = "select id, data, name, mimetype from config_files"
    for thing in conn.execute(req):
        process(thing, processed_config_files)

    op.drop_column('config_files', 'data')
    op.add_column('config_files', sa.Column('data', sa.Unicode(4096)))
    config_files.c.data.type = sa.Unicode(4096)
    update = config_files.update().where(config_files.c.id == bindparam('nodeid')).\
        values({config_files.c.data: bindparam('depot_datas')})

    session.execute(update, processed_config_files)

    logger.debug(u"-> Done")

    from zope.sqlalchemy import mark_changed
    mark_changed(session)

    logger.info("Blob migration completed in {} seconds".format(
        int(time.time() - now)))
def populate_default_datas():
    """
    Populate the database with missing default entries if needed
    """
    from autonomie.models.user.userdatas import CaeSituationOption
    from autonomie.models.career_stage import CareerStage
    from autonomie.models.career_path import TypeContratOption
    session = DBSESSION()
    # Populate contract types
    if session.query(TypeContratOption).filter(TypeContratOption.label=="CDD").count() == 0:
        session.add(TypeContratOption(label=u"CDD", order=0))
    if session.query(TypeContratOption).filter(TypeContratOption.label=="CDI").count() == 0:
        session.add(TypeContratOption(label=u"CDI", order=0))
    if session.query(TypeContratOption).filter(TypeContratOption.label=="CESA").count() == 0:
        session.add(TypeContratOption(label=u"CESA", order=0))
    session.flush()
    # Populate CAE situations
    situation_cand = session.query(CaeSituationOption).filter(CaeSituationOption.label=="Candidat").first()
    if situation_cand is None:
        situation_cand = CaeSituationOption(label=u"Candidat", order=0)
        session.add(situation_cand)
    situation_conv = session.query(CaeSituationOption).filter(CaeSituationOption.label=="En convention").first()
    if situation_conv is None:
        situation_conv = CaeSituationOption(label=u"En convention", is_integration=True)
        session.add(situation_conv)
    situation_es = session.query(CaeSituationOption).filter(CaeSituationOption.label=="Entrepreneur salarié").first()
    if situation_es is None:
        situation_es = CaeSituationOption(label=u"Entrepreneur salarié", is_integration=True)
        session.add(situation_es)
    situation_out = session.query(CaeSituationOption).filter(CaeSituationOption.label=="Sortie").first()
    if situation_out is None:
        situation_out = CaeSituationOption(label=u"Sortie")
        session.add(situation_out)
    session.flush()
    # Populate Career Stages
    if CareerStage.query().count() == 0:
        stage_diag = CareerStage(active=True,
                    name="Diagnostic",
                    cae_situation_id=None,
                    stage_type=None,
                )
        stage_cape = CareerStage(active=True,
                    name="Contrat CAPE",
                    cae_situation_id=situation_conv.id,
                    stage_type="entry",
                )
        stage_dpae = CareerStage(active=True,
                    name="Contrat DPAE",
                    cae_situation_id=None,
                    stage_type=None,
                )
        stage_cesa = CareerStage(active=True,
                    name="Contrat CESA",
                    cae_situation_id=situation_es.id,
                    stage_type="contract",
                )
        stage_avct = CareerStage(active=True,
                    name="Avenant contrat",
                    cae_situation_id=None,
                    stage_type="amendment",
                )
        stage_out = CareerStage(active=True,
                    name="Sortie",
                    cae_situation_id=situation_out.id,
                    stage_type="exit",
                )
        session.add(stage_diag)
        session.add(stage_cape)
        session.add(stage_dpae)
        session.add(stage_cesa)
        session.add(stage_avct)
        session.add(stage_out)
        session.flush()
    return (
        situation_conv.id,
        situation_es.id,
        situation_out.id
    ), (
        stage_diag.id,
        stage_cape.id,
        stage_dpae.id,
        stage_cesa.id,
        stage_avct.id,
        stage_out.id,
    )
def populate_default_datas():
    """
    Populate the database with missing default entries if needed
    """
    from autonomie.models.user.userdatas import CaeSituationOption
    from autonomie.models.career_stage import CareerStage
    from autonomie.models.career_path import TypeContratOption
    session = DBSESSION()
    # Populate contract types
    if session.query(TypeContratOption).filter(
            TypeContratOption.label == "CDD").count() == 0:
        session.add(TypeContratOption(label=u"CDD", order=0))
    if session.query(TypeContratOption).filter(
            TypeContratOption.label == "CDI").count() == 0:
        session.add(TypeContratOption(label=u"CDI", order=0))
    if session.query(TypeContratOption).filter(
            TypeContratOption.label == "CESA").count() == 0:
        session.add(TypeContratOption(label=u"CESA", order=0))
    session.flush()
    # Populate CAE situations
    situation_cand = session.query(CaeSituationOption).filter(
        CaeSituationOption.label == "Candidat").first()
    if situation_cand is None:
        situation_cand = CaeSituationOption(label=u"Candidat", order=0)
        session.add(situation_cand)
    situation_conv = session.query(CaeSituationOption).filter(
        CaeSituationOption.label == "En convention").first()
    if situation_conv is None:
        situation_conv = CaeSituationOption(label=u"En convention",
                                            is_integration=True)
        session.add(situation_conv)
    situation_es = session.query(CaeSituationOption).filter(
        CaeSituationOption.label == "Entrepreneur salarié").first()
    if situation_es is None:
        situation_es = CaeSituationOption(label=u"Entrepreneur salarié",
                                          is_integration=True)
        session.add(situation_es)
    situation_out = session.query(CaeSituationOption).filter(
        CaeSituationOption.label == "Sortie").first()
    if situation_out is None:
        situation_out = CaeSituationOption(label=u"Sortie")
        session.add(situation_out)
    session.flush()
    # Populate Career Stages
    if CareerStage.query().count() == 0:
        stage_diag = CareerStage(
            active=True,
            name="Diagnostic",
            cae_situation_id=None,
            stage_type=None,
        )
        stage_cape = CareerStage(
            active=True,
            name="Contrat CAPE",
            cae_situation_id=situation_conv.id,
            stage_type="entry",
        )
        stage_dpae = CareerStage(
            active=True,
            name="Contrat DPAE",
            cae_situation_id=None,
            stage_type=None,
        )
        stage_cesa = CareerStage(
            active=True,
            name="Contrat CESA",
            cae_situation_id=situation_es.id,
            stage_type="contract",
        )
        stage_avct = CareerStage(
            active=True,
            name="Avenant contrat",
            cae_situation_id=None,
            stage_type="amendment",
        )
        stage_out = CareerStage(
            active=True,
            name="Sortie",
            cae_situation_id=situation_out.id,
            stage_type="exit",
        )
        session.add(stage_diag)
        session.add(stage_cape)
        session.add(stage_dpae)
        session.add(stage_cesa)
        session.add(stage_avct)
        session.add(stage_out)
        session.flush()
    return (situation_conv.id, situation_es.id, situation_out.id), (
        stage_diag.id,
        stage_cape.id,
        stage_dpae.id,
        stage_cesa.id,
        stage_avct.id,
        stage_out.id,
    )
def migrate_datas():
    from autonomie.models.project.types import BusinessType
    from autonomie.models.config import Config
    from autonomie.models.task.mentions import TaskMention
    from autonomie.models.project.mentions import BusinessTypeTaskMention
    from autonomie_base.models.base import DBSESSION
    from alembic.context import get_bind
    session = DBSESSION()
    conn = get_bind()

    # Collect business type ids
    business_type_ids = [b[0] for b in session.query(BusinessType.id)]

    # for each fixed config key we now use mentions
    for index, (doctype, key, label, title) in enumerate((
        (
            'estimation',
            'coop_estimationfooter',
            u"Informations sur l'acceptation des devis",
            u"Acceptation du devis",
        ),
        (
            "invoice",
            "coop_invoicepayment",
            u"Informations de paiement pour les factures",
            u"Mode de paiement",
        ),
        (
            "invoice",
            "coop_invoicelate",
            u"Informations sur les retards de paiement",
            u"Retard de paiement",
        )
    )):
        # We retrieve the configurated value
        value = Config.get_value(key, "")
        mention = TaskMention(
            order=index,
            label=label,
            title=title,
            full_text=value.replace(
                '%IBAN%', "{IBAN}").replace(
                    '%RIB%', "{RIB}").replace(
                        '%ENTREPRENEUR%', '{name}')
        )
        session.add(mention)
        session.flush()

        for btype_id in business_type_ids:
            rel = BusinessTypeTaskMention(
                task_mention_id=mention.id,
                business_type_id=btype_id,
                doctype=doctype,
                mandatory=True,
            )
            session.add(rel)
            session.flush()

        op.execute(
            u"INSERT INTO mandatory_task_mention_rel (task_id, mention_id) \
    SELECT task.id, {mention_id} from task join node on task.id=node.id where \
    node.type_='{type_}'".format(mention_id=mention.id, type_=doctype)
        )