Example #1
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)
Example #2
0
def mail_it(arguments, env):
    """
    Send the mail

    :param arguments: arguments gotten from the command line
    :param env: the environment of our bootstrapped command
    """
    force = arguments["-f"]
    filetype = get_value(arguments, "type")
    filepath = get_value(arguments, "file")
    filename = filepath.split("/")[-1]

    regex = REGISTER.get(filetype)
    groups = regex.match(filename)
    if groups is None:
        raise Exception("The given filepath doesn't match the %s regexp" % filetype)

    code_compta = groups.group("code_compta")
    company = get_company(code_compta)

    if company is None:
        print(u"No company found for this code_compta : %s" % code_compta)
    else:
        print("Sending the file %s" % filepath)
        print("Sending it to %s" % company.email)
        sender = SENDERS.get(filetype)
        sender(env["request"], company.email, company.id, filename, filepath, force)
Example #3
0
def mail_it(arguments, env):
    """
    Send the mail

    :param arguments: arguments gotten from the command line
    :param env: the environment of our bootstrapped command
    """
    force = arguments['-f']
    filetype = get_value(arguments, 'type')
    filepath = get_value(arguments, 'file')
    filename = filepath.split('/')[-1]

    regex = REGISTER.get(filetype)
    groups = regex.match(filename)
    if groups is None:
        raise Exception("The given filepath doesn't match the %s regexp" %
                        filetype)

    code_compta = groups.group("code_compta")
    company = get_company(code_compta)

    if company is None:
        print(u"No company found for this code_compta : %s" % code_compta)
    else:
        print('Sending the file %s' % filepath)
        print("Sending it to %s" % company.email)
        sender = SENDERS.get(filetype)
        sender(
            env['request'],
            company.email,
            company.id,
            filename,
            filepath,
            force,
        )
def export_task_totals(arguments, env):
    action = get_value(arguments, 'a')

    if action == "check":
        check_totals(arguments, env)
        return

    print(arguments)
    logger = logging.getLogger(__name__)
    filename = get_value(arguments, 'f')
    if filename:
        print("Generating a csv filename %s" % filename)
        gen_csv(filename)

    if action == 'cache':
        print(u"Caching amounts")
        session = db()
        index = 0
        for task in Task.query().filter(
            Task.type_.in_(['invoice', 'estimation', 'cancelinvoice'])
        ):
            try:
                cache_amounts(None, None, task)
                session.merge(task)
                index += 1
                if index % 50 == 0:
                    print('flushing')
                    session.flush()
            except:
                logger.exception(
                    u"Erreur avec un cache_amount : %s" % task.id
                )
Example #5
0
def export_task_totals(arguments, env):
    action = get_value(arguments, 'a')

    if action == "check":
        check_totals(arguments, env)
        return

    print(arguments)
    logger = logging.getLogger(__name__)
    filename = get_value(arguments, 'f')
    if filename:
        print("Generating a csv filename %s" % filename)
        gen_csv(filename)

    if action == 'cache':
        print(u"Caching amounts")
        session = db()
        index = 0
        for task in Task.query().filter(
                Task.type_.in_(['invoice', 'estimation', 'cancelinvoice'])):
            try:
                cache_amounts(None, None, task)
                session.merge(task)
                index += 1
                if index % 50 == 0:
                    print('flushing')
                    session.flush()
            except:
                logger.exception(u"Erreur avec un cache_amount : %s" % task.id)
Example #6
0
def _invoices_pdf_command(args, env):
    """
    export pdf of the documents matching the given parameters
    """
    request = env['request']
    from autonomie.models.task import (
        Invoice,
        CancelInvoice,
    )
    logger = logging.getLogger(__name__)
    destdir = get_value(args, "destdir")
    if not os.path.isdir(destdir):
        raise Exception(u"Le répertoire de destination n'existe pas")

    where_str = get_value(args, "where")
    if not where_str:
        raise Exception(u"L'argument de recherche where est requis")
    try:
        where = json.loads(where_str)
        if isinstance(where, dict):
            where = [where]
    except:
        logger.exception("Where should be in json format")
        where = []

    invoice_query = _get_query(Invoice, where)
    cinvoice_query = _get_query(CancelInvoice, where)
    invoice_query = invoice_query.filter_by(status='valid')
    cinvoice_query = cinvoice_query.filter_by(status='valid')
    logger.debug(u"Exporting %s invoices to pdf" % invoice_query.count())
    logger.debug(u"Exporting %s cancelinvoices to pdf" %
                 cinvoice_query.count())
    index = 0
    for invoice in invoice_query:
        _write_invoice_on_disk(request,
                               invoice[1],
                               destdir,
                               prefix=u"facture",
                               key=str(index))
        index += 1
    for cinvoice in cinvoice_query:
        _write_invoice_on_disk(request,
                               cinvoice[1],
                               destdir,
                               prefix=u"avoir",
                               key=str(index))
        index += 1
    logger.debug(u"Export has finished, check %s" % destdir)
Example #7
0
def refresh_cache(arguments, env):
    logger = logging.getLogger(__name__)
    if not arguments['refresh']:
        logger.exception(u"Unknown error")

    logger.debug(u"Refreshing cache")
    session = db()
    index = 0
    types = get_value(arguments, '--type')
    if types is None:
        types = ['invoice', 'estimation', 'cancelinvoice']

    this_year = datetime.date.today().year

    for task in Task.query().filter(
        Task.type_.in_(types)
    ).filter(extract('year', Task.date) == this_year):
        try:
            cache_amounts(None, None, task)
            session.merge(task)
            index += 1
            if index % 200 == 0:
                logger.debug('flushing')
                session.flush()
        except:
            logger.exception(u"Error while caching total : {0}".format(task.id))
Example #8
0
def refresh_cache(arguments, env):
    logger = logging.getLogger(__name__)
    if not arguments['refresh']:
        logger.exception(u"Unknown error")

    logger.debug(u"Refreshing cache")
    session = db()
    index = 0
    types = get_value(arguments, '--type')
    if types is None:
        types = ['invoice', 'estimation', 'cancelinvoice']

    this_year = datetime.date.today().year

    for task in Task.query().filter(Task.type_.in_(types)).filter(
            extract('year', Task.date) == this_year):
        try:
            cache_amounts(None, None, task)
            session.merge(task)
            index += 1
            if index % 200 == 0:
                logger.debug('flushing')
                session.flush()
        except:
            logger.exception(u"Error while caching total : {0}".format(
                task.id))
Example #9
0
def _invoices_pdf_command(args, env):
    """
    export pdf of the documents matching the given parameters
    """
    request = env['request']
    from autonomie.models.task import (
        Invoice, CancelInvoice,
    )
    logger = logging.getLogger(__name__)
    destdir = get_value(args, "destdir")
    if not os.path.isdir(destdir):
        raise Exception(u"Le répertoire de destination n'existe pas")

    where_str = get_value(args, "where")
    if not where_str:
        raise Exception(u"L'argument de recherche where est requis")
    try:
        where = json.loads(where_str)
        if isinstance(where, dict):
            where = [where]
    except:
        logger.exception("Where should be in json format")
        where = []

    invoice_query = _get_query(Invoice, where)
    cinvoice_query = _get_query(CancelInvoice, where)
    invoice_query = invoice_query.filter_by(status='valid')
    cinvoice_query = cinvoice_query.filter_by(status='valid')
    logger.debug(u"Exporting %s invoices to pdf" % invoice_query.count())
    logger.debug(u"Exporting %s cancelinvoices to pdf" % cinvoice_query.count())
    index = 0
    for invoice in invoice_query:
        _write_invoice_on_disk(
            request, invoice[1], destdir, prefix=u"facture", key=str(index)
        )
        index += 1
    for cinvoice in cinvoice_query:
        _write_invoice_on_disk(
            request, cinvoice[1], destdir, prefix=u"avoir", key=str(index)
        )
        index += 1
    logger.debug(u"Export has finished, check %s" % destdir)
Example #10
0
def add_admin(arguments, env):
    """
        Add an admin user to the database
    """
    login = get_value(arguments, 'user', 'admin.majerti')
    password = get_value(arguments, 'pwd', get_pwd())
    firstname = get_value(arguments, 'firstname', 'Admin')
    lastname = get_value(arguments, 'lastname', 'Majerti')
    email = get_value(arguments, 'email', '*****@*****.**')
    user = User(login=login,
                firstname=firstname,
                primary_group=1,  #is an admin
                lastname=lastname,
                email=email
            )
    user.set_password(password)
    db = DBSESSION()
    db.add(user)
    db.flush()
    print u"Creating account %s with password %s" % (login, unicode(password))
    return user
Example #11
0
def add_admin(arguments, env):
    """
        Add an admin user to the database
    """
    login = get_value(arguments, 'user', 'admin.majerti')
    password = get_value(arguments, 'pwd', get_pwd())
    firstname = get_value(arguments, 'firstname', 'Admin')
    lastname = get_value(arguments, 'lastname', 'Majerti')
    email = get_value(arguments, 'email', '*****@*****.**')
    user = User(
        login=login,
        firstname=firstname,
        primary_group=1,  #is an admin
        lastname=lastname,
        email=email)
    user.set_password(password)
    db = DBSESSION()
    db.add(user)
    db.flush()
    print u"Creating account %s with password %s" % (login, unicode(password))
    return user
Example #12
0
def _export_user_datas(args, env):
    """
    Export userdatas as csv format

    Streams the output in stdout

    autonomie-export app.ini userdatas \
    --fields=coordonnees_address,coordonnees_zipcode,coordonnees_city,\
        coordonnees_sex,coordonnees_birthday,statut_social_status,\
        coordonnees_study_level,parcours_date_info_coll,parcours_prescripteur,\
        parcours_convention_cape,activity_typologie,sortie_date,sortie_motif \
    --where='[{"key":"created_at","method":"dr","type":"date",\
        "search1":"1999-01-01","search2":"2016-12-31"}]'\
    > /tmp/toto.csv

    :param dict args: The arguments coming from the command line
    :param dict env: The environment bootstraped when setting up the pyramid app
    """
    from autonomie.models.user import UserDatas

    logger = logging.getLogger(__name__)
    fields = get_value(args, "fields", "").split(',')
    where_str = get_value(args, "where", "{}")
    try:
        where = json.loads(where_str)
        if isinstance(where, dict):
            where = [where]
    except:
        logger.exception("Where should be in json format")
        where = []

    logger.debug("Fields : {0}".format(fields))
    logger.debug("Where : {0}".format(where))

    if where:
        query = _get_query(UserDatas, where)
    else:
        query = UserDatas.query()

    _stream_csv_rows(UserDatas, query, fields)
Example #13
0
def _export_userdatas_command(args, env):
    """
    Export userdatas as csv format

    Streams the output in stdout

    autonomie-export app.ini userdatas \
    --fields=coordonnees_address,coordonnees_zipcode,coordonnees_city,\
        coordonnees_sex,coordonnees_birthday,statut_social_status,\
        coordonnees_study_level,parcours_date_info_coll,parcours_prescripteur,\
        parcours_convention_cape,activity_typologie,sortie_date,sortie_motif \
    --where='[{"key":"created_at","method":"dr","type":"date",\
        "search1":"1999-01-01","search2":"2016-12-31"}]'\
    > /tmp/toto.csv

    :param dict args: The arguments coming from the command line
    :param dict env: The environment bootstraped when setting up the pyramid app
    """
    from autonomie.forms.user.user import UserDatas

    logger = logging.getLogger(__name__)
    fields = get_value(args, "fields", "").split(',')
    where_str = get_value(args, "where", "{}")
    try:
        where = json.loads(where_str)
        if isinstance(where, dict):
            where = [where]
    except:
        logger.exception("Where should be in json format")
        where = []

    logger.debug("Fields : {0}".format(fields))
    logger.debug("Where : {0}".format(where))

    if where:
        query = _get_query(UserDatas, where)
    else:
        query = UserDatas.query()

    _stream_csv_rows(UserDatas, query, fields)
Example #14
0
def test_mail(arguments, env):
    """
    Test tool for mail sending
    """
    from autonomie_base.mail import send_mail
    dest = get_value(arguments, 'to', '*****@*****.**')
    request = env['request']
    subject = u"Test d'envoi de mail"
    body = u"""Il semble que le test d'envoi de mail a réussi.
    Ce test a été réalisé depuis le script autonomie-admin

Bonne et belle journée !!!"""
    send_mail(request, [dest], body, subject)
Example #15
0
def user_add_command(arguments, env):
    """
        Add a user in the database
    """
    logger = logging.getLogger(__name__)

    login = get_value(arguments, 'user', 'admin.majerti')
    login = login.decode('utf-8')
    logger.debug(u"Adding a user {0}".format(login))

    password = get_value(arguments, 'pwd', get_pwd())
    password = password.decode('utf-8')

    login = Login(login=login)
    login.set_password(password)

    group = get_value(arguments, 'group', None)
    if group:
        try:
            login.groups.append(group)
        except NoResultFound:
            print(u"""

ERROR : group %s doesn't exist, did you launched the syncdb command :

    autonomie-admin <fichier.ini> syncdb
                """ % (group, ))
            return

    db = DBSESSION()
    db.add(login)
    db.flush()

    firstname = get_value(arguments, 'firstname', 'Admin')
    lastname = get_value(arguments, 'lastname', 'Majerti')
    email = get_value(arguments, 'email', '*****@*****.**')
    user = User(login=login,
                firstname=firstname,
                lastname=lastname,
                email=email)
    db.add(user)
    db.flush()
    print(u"""
    User Account created :
          ID        : {0.id}
          Login     : {0.login.login}
          Firstname : {0.firstname}
          Lastname  : {0.lastname}
          Email     : {0.email}
          Groups    : {0.login.groups}
          """.format(user))

    if 'pwd' not in arguments:
        print(u"""
          Password  : {0}""".format(password))

    logger.debug(u"-> Done")
    return user
Example #16
0
def test_mail(arguments, env):
    """
    Test tool for mail sending
    """
    from autonomie.mail import send_mail
    dest = get_value(arguments, 'to', '*****@*****.**')
    request = env['request']
    subject = u"Test d'envoi de mail"
    body = u"""Il semble que le test d'envoi de mail a réussi.
    Ce test a été réalisé depuis le script autonomie-admin

Bonne et belle journée !!!"""
    send_mail(
        request,
        [dest],
        body,
        subject
    )
def check_totals(arguments, env):
    print(u"Checking totals")
    f = get_value(arguments, 'f')

    lines = file(f, 'r').read().splitlines()
    query = query_tasks()
    for line in lines:
        id, ht, tva, ttc = line.split(',')
        if 'None' in (id, ht, tva, ttc):
            continue

        vals = int(id), int(ht), int(tva), int(ttc)
        task = query.filter(Task.id == vals[0]).one()
        if not task == vals:
            print(u"Unconform data migration detected")
            print(u"  + Task.id : %s" % task[0])
            print(u"    + Found %s" % str(task))
            print(u"    + Used to be %s" % str(vals))
Example #18
0
def check_totals(arguments, env):
    print(u"Checking totals")
    f = get_value(arguments, 'f')

    lines = file(f, 'r').read().splitlines()
    query = query_tasks()
    for line in lines:
        id, ht, tva, ttc = line.split(',')
        if 'None' in (id, ht, tva, ttc):
            continue

        vals = int(id), int(ht), int(tva), int(ttc)
        task = query.filter(Task.id == vals[0]).one()
        if not task == vals:
            print(u"Unconform data migration detected")
            print(u"  + Task.id : %s" % task[0])
            print(u"    + Found %s" % str(task))
            print(u"    + Used to be %s" % str(vals))
Example #19
0
def user_add(arguments, env):
    """
        Add a user in the database
    """
    logger = logging.getLogger(__name__)

    login = get_value(arguments, 'user', 'admin.majerti')
    login = login.decode('utf-8')
    logger.debug(u"Adding a user {0}".format(login))

    password = get_value(arguments, 'pwd', get_pwd())
    password = password.decode('utf-8')

    firstname = get_value(arguments, 'firstname', 'Admin')
    lastname = get_value(arguments, 'lastname', 'Majerti')
    email = get_value(arguments, 'email', '*****@*****.**')
    group = get_value(arguments, 'group', None)
    user = User(login=login,
                firstname=firstname,
                lastname=lastname,
                email=email)

    if group:
        user.groups.append(group)

    user.set_password(password)
    db = DBSESSION()
    db.add(user)
    db.flush()
    print(u"""
    Account created :
          ID        : {0.id}
          Login     : {0.login}
          Firstname : {0.firstname}
          Lastname  : {0.lastname}
          Email     : {0.email}
          Groups    : {0.groups}
          """.format(user))

    if 'pwd' not in arguments:
        print(u"""
          Password  : {0}""".format(password))

    logger.debug(u"-> Done")
    return user
Example #20
0
def user_add(arguments, env):
    """
        Add a user in the database
    """
    login = get_value(arguments, 'user', 'admin.majerti')
    login = login.decode('utf-8')

    password = get_value(arguments, 'pwd', get_pwd())
    password = password.decode('utf-8')

    firstname = get_value(arguments, 'firstname', 'Admin')
    lastname = get_value(arguments, 'lastname', 'Majerti')
    email = get_value(arguments, 'email', '*****@*****.**')
    group = get_value(arguments, 'group', None)
    user = User(
        login=login,
        firstname=firstname,
        lastname=lastname,
        email=email
    )

    if group:
        user.groups.append(group)

    user.set_password(password)
    db = DBSESSION()
    db.add(user)
    db.flush()
    print(u"""
    Account created :
          ID        : {0.id}
          Login     : {0.login}
          Firstname : {0.firstname}
          Lastname  : {0.lastname}
          Email     : {0.email}
          Groups    : {0.groups}
          """.format(user))

    if 'pwd' not in arguments:
        print(u"""
          Password  : {0}""".format(password))
    return user
Example #21
0
def test_script_utils():
    from autonomie.scripts.utils import get_value
    args = {'--test': 'toto', '--': 'titi'}
    assert get_value(args, 'test', '') == 'toto'
    assert get_value(args, 'test1', 'test') == 'test'
Example #22
0
def test_script_utils():
    from autonomie.scripts.utils import get_value
    args = {'--test': 'toto', '--': 'titi'}
    assert get_value(args, 'test', '') == 'toto'
    assert get_value(args, 'test1', 'test') == 'test'
Example #23
0
def anonymize_database(args, env):
    logger = logging.getLogger(__name__)
    method = get_value(args, "method", None)
    manager = Anonymizer(logger)
    manager.run(method)
Example #24
0
def anonymize_database(args, env):
    logger = logging.getLogger(__name__)
    method = get_value(args, "method", None)
    manager = Anonymizer(logger)
    manager.run(method)