Esempio n. 1
0
def auto_stud(studconfig_file,
              student_info_file,
              studieprogs_file,
              emne_info_file,
              drgrad_file,
              fritak_kopiavg_file,
              sysname,
              person_file,
              ou_perspective=None):
    logger.debug("Preparing AutoStud framework")
    autostud = AutoStud.AutoStud(db,
                                 logger,
                                 debug=False,
                                 cfg_file=studconfig_file,
                                 studieprogs_file=studieprogs_file,
                                 emne_info_file=emne_info_file,
                                 ou_perspective=ou_perspective)

    # Finne alle personer som skal behandles, deres gruppemedlemskap
    # og evt. fritak fra kopiavgift
    (fnr2pid, quota_victims, person_id_member, person_id_affs,
     kopiavgift_fritak, betaling_fritak) = fetch_data(drgrad_file,
                                                      fritak_kopiavg_file,
                                                      sysname, person_file,
                                                      autostud)
    logger.debug("Victims: %r", quota_victims)
    # Start call-backs via autostud modulen med vanlig
    # merged_persons.xml fil.  Vi har da mulighet til å styre kvoter
    # via de vanlige select kriteriene.  Callback metoden må sjekke
    # mot unntak.

    logger.info("Starting callbacks from %r", student_info_file)
    autostud.start_student_callbacks(
        student_info_file,
        functools.partial(recalc_quota_callback,
                          fnr2pid=fnr2pid,
                          quota_victims=quota_victims,
                          kopiavgift_fritak=kopiavgift_fritak,
                          betaling_fritak=betaling_fritak))

    # For de personer som ikke fikk autostud-callback må vi kalle
    # quota_callback funksjonen selv.
    logger.info("process persons that didn't get callback")
    for p in quota_victims.keys():
        if p not in processed_person:
            recalc_quota_callback({'person_id': p}, fnr2pid, quota_victims,
                                  kopiavgift_fritak, betaling_fritak)

    # Turn off quota for anyone that has quota and that we didn't
    # process
    logger.info("Turn off quota for those who still has quota")
    for row in pq.list(only_without_exempt=True):
        if int(row['person_id']) not in processed_pids:
            set_pq_exempt(int(row['person_id']), exempt=True)
Esempio n. 2
0
def process_move_student_requests():
    global fnr2move_student, autostud
    br = BofhdRequests(db, const)
    rows = br.get_requests(operation=const.bofh_move_student)
    if not rows:
        return
    logger.debug("Preparing autostud framework")
    autostud = AutoStud.AutoStud(db, logger, debug=False,
                                 cfg_file=studconfig_file,
                                 studieprogs_file=studieprogs_file,
                                 emne_info_file=emne_info_file,
                                 ou_perspective=ou_perspective)

    # Hent ut personens fødselsnummer + account_id
    fnr2move_student = {}
    account = Utils.Factory.get('Account')(db)
    person = Utils.Factory.get('Person')(db)
    for r in rows:
        if not is_valid_request(r['request_id']):
            continue
        account.clear()
        account.find(r['entity_id'])
        person.clear()
        person.find(account.owner_id)
        fnr = person.get_external_id(id_type=const.externalid_fodselsnr,
                                     source_system=const.system_fs)
        if not fnr:
            logger.warn("Not student fnr for: %i" % account.entity_id)
            br.delete_request(request_id=r['request_id'])
            db.commit()
            continue
        fnr = fnr[0]['external_id']
        fnr2move_student.setdefault(fnr, []).append(
            (int(account.entity_id),
             int(r['request_id']),
             int(r['requestee_id'])))
    logger.debug("Starting callbacks to find: %s" % fnr2move_student)
    autostud.start_student_callbacks(student_info_file, move_student_callback)

    # Move remaining users to pending disk
    disk = Utils.Factory.get('Disk')(db)
    disk.find_by_path(cereconf.AUTOSTUD_PENDING_DISK)
    logger.debug(str(fnr2move_student.values()))
    for tmp_stud in fnr2move_student.values():
        for account_id, request_id, requestee_id in tmp_stud:
            logger.debug("Sending %s to pending disk" % repr(account_id))
            br.delete_request(request_id=request_id)
            br.add_request(requestee_id, br.batch_time,
                           const.bofh_move_user,
                           account_id, disk.entity_id,
                           state_data=int(default_spread))
            db.commit()
def start_process_students(update_create=False):
    global autostud, accounts, persons

    logger.info("process_students started")
    autostud = AutoStud.AutoStud(db,
                                 logger,
                                 debug=debug,
                                 cfg_file=studconfig_file,
                                 studieprogs_file=studieprogs_file,
                                 emne_info_file=emne_info_file,
                                 ou_perspective=ou_perspective)
    logger.info("config processed")
    persons, accounts = get_existing_accounts()
    logger.info("got student accounts")
    if update_create:
        BuildAccounts.update_accounts_main()
    logger.info("process_students finished")
def validate_config():
    if studconfig_file is None or \
            studieprogs_file is None or \
            emne_info_file is None:

        print("Missing required parameter(s). 'studconfig_file' (-C), "
              "studieprogs_file' (-S)\nand 'emne_info_file' (-e) needs "
              "to be specified when running --validate.")
        sys.exit(1)

    else:
        AutoStud.AutoStud(db,
                          logger,
                          debug=debug,
                          cfg_file=studconfig_file,
                          studieprogs_file=studieprogs_file,
                          emne_info_file=emne_info_file)
 def __init__(self,
              db,
              co,
              ou_perspective,
              emne_info_file,
              studconfig_file,
              studieprogs_file,
              default_spread=None):
     self.db = db
     self.co = co
     self.br = BofhdRequests(self.db, self.co)
     self.default_spread = default_spread
     logger.debug("Preparing autostud framework")
     self.autostud = AutoStud.AutoStud(self.db,
                                       logger.getChild('autostud'),
                                       debug=False,
                                       cfg_file=studconfig_file,
                                       studieprogs_file=studieprogs_file,
                                       emne_info_file=emne_info_file,
                                       ou_perspective=ou_perspective)
Esempio n. 6
0
def generate_report(output, source_systems, start_date):
    """Generate html formatted report of accounts in quarantine.
    sort by faculites and sort results by institutes.
    @param output: filename to write output to
    @type output: file object, returned by open or sys.stdout
    @param source_systems: not used for now,
    @type source_systems: string with comma separated systems.
    @param start_date: 
    @type start_date: ISO date, i.e YYYY-MM-DD
    """
    t0 = time.clock()
    logger = Factory.get_logger("cronjob")
    database = Factory.get('Database')()
    constants = Factory.get('Constants')(database)
    person = Factory.get('Person')(database)
    account = Factory.get('Account')(database)
    ou = Factory.get('OU')(database)
    
    logger.info('Start Quarantine Report.')
    ou2sko = dict((row['ou_id'], ("%02d%02d%02d" % (row['fakultet'],
                                                    row['institutt'],
                                                    row['avdeling'])))
                                                    for row in ou.get_stedkoder())

    sko2name = dict((ou2sko[row['entity_id']], row['name']) for row in
                    ou.search_name_with_language(name_variant=
                                                        constants.ou_name_display,
                                                        name_language=
                                                        constants.language_nb))
    perspective = constants.OUPerspective('FS')
    autostud = AutoStud.AutoStud(database, logger, debug=0,
    cfg_file='/cerebrum/etc/cerebrum/studconfig.xml',
    studieprogs_file='/cerebrum/var/cache/FS/studieprogrammer.xml',
    emne_info_file='/cerebrum/var/cache/FS/emner.xml',
    ou_perspective=int(perspective))

    t1 = time.clock()

    logger.debug('Initialization in %.2g seconds' %(t1-t0))
    
    quarantine_list = account.list_entity_quarantines(entity_types=constants.entity_account,
                                                      only_active=True)
    logger.debug('whole list length is %10d' %len(quarantine_list))
    
    quarantines_by_sko = {}
    num_quarantines = 0
    debug_counter = 0

    #diagnose if-tests
    time_if = 0
    deleted_if = 0
    owner_type_if = 0
    disk_if = 0

    t11 = time.clock()
    logger.debug('time used: %.2f' %(t11-t1))
    
    for row_qua in quarantine_list:
        
        debug_counter += 1
        if debug_counter % 10000 == 0:
            logger.debug('row nr %10d , passed %.2f seconds'
                    %(debug_counter,time.clock()-t11))
        
        if row_qua['start_date'] > start_date:
            time_if += 1
            continue # quarantine is not old enough, skip

        account.clear()
        try:
            account.find(row_qua['entity_id'])
        except NotFoundError, nfe:
            logger.error('%s' %nfe)
            continue

        
        if account.owner_type != constants.entity_person:
            owner_type_if += 1
            continue # Filter out non-personal accounts.

        if account.is_deleted() or account.is_reserved():
            deleted_if += 1
            continue # Skip deleted  and reserved accounts.

        try:
            disk_id = account.get_home(int(constants.spread_uio_nis_user))['disk_id']
        except NotFoundError:
            # no homedir exists
            continue

        if (disk_id and autostud.disk_tool.get_diskdef_by_diskid(disk_id)):
            disk_if += 1
            continue # disk_id refers to a student disk, skip.
        
        account_name = account.get_account_name()
        name = account.get_fullname()
        person.clear()
        person.find(account.owner_id)
        affiliations = person.get_affiliations()

        for row in affiliations:
            status = str(constants.PersonAffStatus(row['status']))

            quarantines_by_sko.setdefault(ou2sko[row['ou_id']], []).append({
                'name': name,
                'status': status,
                'account': account_name,
                'quarantine':row_qua
                })
        
        if not affiliations:
            quarantines_by_sko.setdefault('Uregistrert', []).append({
                'name': name,
                'status': 'ikke satt',
                'account': account_name,
                'quarantine':row_qua
                })

        num_quarantines += 1
        
        if num_quarantines % 1000 == 0:
            logger.debug( 'Found %10d quarantines after %.2f seconds' %(
                                    num_quarantines,(time.clock()-t11)))