Exemplo n.º 1
0
def smartschool_cront_task(opaque):
    update_teachers = msettings.get_configuration_setting(
        'smartschool-update-teachers')
    update_students = msettings.get_configuration_setting(
        'smartschool-update-students')
    with flask_app.app_context():
        read_from_smartschool_database(update_teachers, update_students)
Exemplo n.º 2
0
def update_scripts_on_papercut_server(skip_ssh_connect=False):
    try:
        remote_script_path = msettings.get_configuration_setting(
            'papercut-script-path').replace('\\\\', '/')
        scripts = msettings.get_configuration_setting('papercut-scripts')
        out = []
        script_name = None
        if not skip_ssh_connect:
            url = msettings.get_configuration_setting('papercut-url')
            login = msettings.get_configuration_setting('papercut-login')
            password = msettings.get_configuration_setting('papercut-password')
            ssh_client.connect(url, username=login, password=password)
        transport = ssh_client.get_transport()
        sftp = sftp_client.SFTPClient.from_transport(transport)
        for line in scripts.split('\n'):
            if 'SCRIPT-START' in line:
                script_name = line.strip().split(' ')[-1]
                out = []
            elif 'SCRIPT-STOP' in line:
                local_script_file = f'{PAPERCUT_LOCATION}/{script_name}'
                with open(local_script_file, "w") as f:
                    f.writelines(out)
                sftp.put(local_script_file, script_name)
            else:
                out.append(f'{line}\n')
        sftp.close()
        if not skip_ssh_connect:
            ssh_client.close()
    except Exception as e:
        log.error('Could not update papercut scripts: {e}')
Exemplo n.º 3
0
def get_rfids():
    try:
        url = msettings.get_configuration_setting('papercut-server-url')
        port = msettings.get_configuration_setting('papercut-server-port')
        token = msettings.get_configuration_setting('papercut-auth-token')
        nbr_updated_rfid = 0
        nbr_user_not_found = 0
        with xmlrpc.client.ServerProxy(
                f'http://{url}:{port}/rpc/api/xmlrpc') as server:
            persons = mperson.get_persons(enabled=True, active=True)
            for person in persons:
                try:
                    property = server.api.getUserProperty(
                        token, person.ad_user_name, PROPERTY_RFID)
                    if person.rfid_code != property.upper():
                        person.rfid_code = property.upper()
                        nbr_updated_rfid += 1
                except Exception as e:
                    nbr_user_not_found += 1
                    log.info(
                        f'get person rfid: person not found: {person.ad_user_name}, error: {e}'
                    )
        mperson.end_update_bulk_person()
        log.info(
            f'get rfids from papercut: nbr-persons/nbr-updated/nbr-not-found {len(persons)}/{nbr_updated_rfid}/{nbr_user_not_found}'
        )
        return {
            'status':
            True,
            'message':
            f'RFIDs from papercut\nnbr: {len(persons)}\nupdated {nbr_updated_rfid}\nnot found {nbr_user_not_found}'
        }
    except Exception as e:
        log.error('get rfids: error {e}')
        return {'status': False, 'message': f'{e}'}
Exemplo n.º 4
0
def start_job():
    try:
        cron_template = msettings.get_configuration_setting('cron-scheduler-template')
        init_job(cron_template)
        msettings.subscribe_handle_update_setting('cron-scheduler-template', update_cron_template, None)
    except Exception as e:
        log.error(f'could not start cron-scheduler: {e}')
Exemplo n.º 5
0
def start_job_now():
    try:
        init_job('now')
        time.sleep(1)
        cron_template = msettings.get_configuration_setting('cron-scheduler-template')
        init_job(cron_template)
    except Exception as e:
        log.error(f'could not start a cron job now: {e}')
Exemplo n.º 6
0
def update_papercut(opaque):
    with flask_app.app_context():
        try:
            update_students = msettings.get_configuration_setting(
                'papercut-update-students')
            if update_students:
                username = msettings.get_configuration_setting(
                    'papercut-login')
                password = msettings.get_configuration_setting(
                    'papercut-password')
                url = msettings.get_configuration_setting('papercut-url')
                remote_file = msettings.get_configuration_setting(
                    'papercut-file').replace('\\\\', '/')
                filename = Path(remote_file).name
                local_file = f'{PAPERCUT_LOCATION}/{filename}'
                local_temp_file = f'{PAPERCUT_LOCATION}/temp.xlsm'
                ssh_client.connect(url, username=username, password=password)
                transport = ssh_client.get_transport()
                sftp = sftp_client.SFTPClient.from_transport(transport)
                sftp.get(remote_file, local_temp_file)
                sftp.close()
                ssh_client.close()
                try:
                    file_updated = not filecmp.cmp(local_file, local_temp_file)
                except FileNotFoundError:
                    file_updated = True
                if file_updated:
                    students = mperson.get_persons(role=mperson.ROLE.STUDENT)
                    students_cache = {s.ss_internal_nbr: s for s in students}
                    lines = pyexcel.iget_records(file_name=local_temp_file)
                    nbr_updated_students = 0
                    for line in lines:
                        if str(line[HEADING_STUDENT_ID]) in students_cache:
                            student = students_cache[str(
                                line[HEADING_STUDENT_ID])]
                            if student.rfid_code != line[HEADING_BADGE]:
                                student.rfid_code = line[HEADING_BADGE]
                                mperson.update_flag(student, True)
                                nbr_updated_students += 1
                    mperson.end_update_bulk_person()
                    log.info(
                        f'papercut students: updated: {nbr_updated_students}/')
                    shutil.copyfile(local_temp_file, local_file)
        except Exception as e:
            log.error(f'papercut job task: {e}')
Exemplo n.º 7
0
def read_from_cardpresso_database(update_students=False, force_update=False):
    try:
        if update_students:
            username = msettings.get_configuration_setting('cardpresso-login')
            password = msettings.get_configuration_setting(
                'cardpresso-password')
            url = msettings.get_configuration_setting('cardpresso-url')
            remote_file = msettings.get_configuration_setting(
                'cardpresso-file').replace('\\\\', '/')
            filename = Path(remote_file).name
            local_file = f'{CARDPRESSO_LOCATION}/{filename}'
            if force_update:
                os.remove(local_file)
            local_temp_file = f'{CARDPRESSO_LOCATION}/temp.xlsm'
            client.connect(url, username=username, password=password)
            transport = client.get_transport()
            sftp = sftp_client.SFTPClient.from_transport(transport)
            sftp.get(remote_file, local_temp_file)
            sftp.close()
            try:
                file_updated = not filecmp.cmp(local_file, local_temp_file)
            except FileNotFoundError:
                file_updated = True
            if file_updated:
                students = mperson.get_persons(role=mperson.ROLE.STUDENT)
                students_cache = {s.ss_internal_nbr: s for s in students}
                lines = pyexcel.iget_records(file_name=local_temp_file)
                nbr_updated_students = 0
                for line in lines:
                    if str(line[HEADING_STUDENT_ID]) in students_cache:
                        student = students_cache[str(line[HEADING_STUDENT_ID])]
                        if student.rfid_code != line[HEADING_BADGE].upper():
                            student.rfid_code = line[HEADING_BADGE].upper()
                            mperson.update_flag(student, True)
                            nbr_updated_students += 1
                mperson.end_update_bulk_person()
                log.info(
                    f'update from cardpresso database students: updated: {nbr_updated_students}'
                )
                shutil.copyfile(local_temp_file, local_file)
    except Exception as e:
        log.eror(f'update from cardpresso database error: {e}')
Exemplo n.º 8
0
def cardpresso_cront_task(opaque):
    update_students = msettings.get_configuration_setting(
        'cardpresso-update-students')
    with flask_app.app_context():
        read_from_cardpresso_database(update_students)
Exemplo n.º 9
0
def read_from_smartschool_database(update_teachers, update_students):
    try:
        api_key = msettings.get_configuration_setting('smartschool-api-key')
        api_url = msettings.get_configuration_setting('smartschool-api-url')
        soap = Client(api_url)

        if update_teachers:
            current_teachers = mperson.get_persons(role=mperson.ROLE.TEACHER)
            current_teacher_user_names = {}
            nbr_active_teachers_before = mperson.get_person_count(
                active=True, role=mperson.ROLE.TEACHER)
            for teacher in current_teachers:
                current_teacher_user_names[teacher.ss_user_name] = teacher
                mperson.activate_person(teacher, False)
            ret = soap.service.getAllAccountsExtended(api_key, TEACHERS_GROUP,
                                                      1)
            if not isinstance(ret, int):
                new_teachers = json.loads(ret)
                nbr_new_teachers = 0
                for new_teacher in new_teachers:
                    ss_user_name = new_teacher['gebruikersnaam']
                    if ss_user_name in current_teacher_user_names:
                        current_teacher = current_teacher_user_names[
                            ss_user_name]
                        full_name = f'{new_teacher["voornaam"]} {new_teacher["naam"]}'
                        if current_teacher.full_name != full_name:
                            current_teacher.full_name = full_name
                            mperson.update_flag(current_teacher, True)
                        if current_teacher.ss_internal_nbr != new_teacher[
                                'internnummer']:
                            current_teacher.ss_internal_nbr = new_teacher[
                                'internnummer']
                            mperson.update_flag(current_teacher, True)
                        mperson.activate_person(current_teacher, True)
                    else:
                        mperson.add_bulk_person(
                            full_name=
                            f'{new_teacher["voornaam"]} {new_teacher["naam"]}',
                            ss_user_name=ss_user_name,
                            ss_internal_nbr=new_teacher['internnummer'],
                            ad_user_name=ss_user_name,
                            role=mperson.ROLE.TEACHER)
                        nbr_new_teachers += 1
                mperson.end_add_bulk_person()
                nbr_active_teachers_after = mperson.get_person_count(
                    active=True, role=mperson.ROLE.TEACHER)
                nbr_updated_teachers = mperson.get_person_count(
                    updated=True, role=mperson.ROLE.TEACHER)
                log.info(f'update from smartschool teachers: new/updated/active-before/active-after: {nbr_new_teachers}/' \
                        f'{nbr_updated_teachers}/{nbr_active_teachers_before}/{nbr_active_teachers_after}')
            else:
                error_codes = soap.service.returnJsonErrorCodes()
                error = error_codes[str(ret)]
                log.eror(
                    f'updating from smartschool teachers: soap returned: {error}'
                )

        if update_students:
            current_students = mperson.get_persons(role=mperson.ROLE.STUDENT)
            current_student_user_names = {}
            nbr_active_students_before = mperson.get_person_count(
                active=True, role=mperson.ROLE.STUDENT)
            for student in current_students:
                current_student_user_names[student.ss_user_name] = student
                mperson.activate_person(student, False)
            ret = soap.service.getAllAccountsExtended(api_key, STUDENTS_GROUP,
                                                      1)
            if not isinstance(ret, int):
                new_students = json.loads(ret)
                nbr_new_students = 0
                for new_student in new_students:
                    if new_student['basisrol'] != '1': continue
                    ss_user_name = new_student['gebruikersnaam']
                    if ss_user_name in current_student_user_names:
                        current_student = current_student_user_names[
                            ss_user_name]
                        full_name = f'{new_student["voornaam"]} {new_student["naam"]}'
                        if current_student.full_name != full_name:
                            current_student.full_name = full_name
                            mperson.update_flag(current_student, True)
                        if current_student.ss_internal_nbr != new_student[
                                'internnummer']:
                            current_student.ss_internal_nbr = new_student[
                                'internnummer']
                            mperson.update_flag(current_student, True)
                        mperson.activate_person(current_student, True)
                    else:
                        ad_user_name = ss_user_name
                        ad_user_name = re.sub("[ùÜü]", "u", ad_user_name)
                        ad_user_name = re.sub("[ ]", "", ad_user_name)
                        ad_user_name = re.sub("[Öö]", "o", ad_user_name)
                        ad_user_name = re.sub("[ç]", "c", ad_user_name)
                        ad_user_name = re.sub("[ï]", "i", ad_user_name)
                        ad_user_name = re.sub("[.]", "", ad_user_name)
                        ad_user_name = re.sub("[ãÃ]", "a", ad_user_name)
                        ad_user_name = re.sub("[Ğğ]", "g", ad_user_name)
                        ad_user_name = re.sub("[ËÉÈ_Êëéèê]", "e", ad_user_name)
                        ad_user_name = re.sub("[şŞ]", "s", ad_user_name)
                        mperson.add_bulk_person(
                            full_name=
                            f'{new_student["voornaam"]} {new_student["naam"]}',
                            ss_user_name=ss_user_name,
                            ss_internal_nbr=new_student['internnummer'],
                            ad_user_name=ad_user_name,
                            role=mperson.ROLE.STUDENT)
                        nbr_new_students += 1
                mperson.end_add_bulk_person()
                nbr_active_students_after = mperson.get_person_count(
                    active=True, role=mperson.ROLE.STUDENT)
                nbr_updated_students = mperson.get_person_count(
                    updated=True, role=mperson.ROLE.STUDENT)
                log.info(f'update from smartschool students: new/updated/active-before/active-after: {nbr_new_students}/' \
                        f'{nbr_updated_students}/{nbr_active_students_before}/{nbr_active_students_after}')
            else:
                error_codes = soap.service.returnJsonErrorCodes()
                error = error_codes[str(ret)]
                log.eror(
                    f'updating from smartschool students: soap returned: {error}'
                )
    except Exception as e:
        log.eror(f'update from smartschool error: {e}')