예제 #1
0
    def run(self, csids=None):
        if not csids:
            csids = _get_advisee_sids_without_photos()
        id_mappings = get_advisee_ids(csids=csids)
        id_mappings_by_sid = {r['sid']: r['ldap_uid'] for r in id_mappings}

        app.logger.info(
            f'Starting student photo import job for {len(csids)} students...')

        successes = []
        failures = []
        photo_not_found = []
        index = 0

        for csid in csids:
            index += 1
            app.logger.info(
                f'Fetching photo for SID {csid}, ({index} of {len(csids)})')
            uid = id_mappings_by_sid.get(csid)
            if not uid:
                app.logger.error(f'No UID found for SID {csid}.')
                failures.append(csid)
                continue

            photo = get_cal1card_photo(uid)
            if photo:
                s3_photo_key = f"{app.config['LOCH_S3_CAL1CARD_PHOTOS_PATH']}/{uid}.jpg"
                if s3.upload_data(photo,
                                  s3_photo_key,
                                  bucket=app.config['LOCH_S3_PUBLIC_BUCKET']):
                    successes.append(csid)
                else:
                    app.logger.error(f'Photo upload failed for SID {csid}.')
                    failures.append(csid)
            elif photo is False:
                app.logger.info(f'No photo returned for SID {csid}.')
                photo_not_found.append(csid)
            elif photo is None:
                app.logger.error(f'Photo import failed for SID {csid}.')
                failures.append(csid)

            sleep(app.config['CAL1CARD_PHOTO_API_THROTTLE'])

        if (len(successes) == 0) and (len(photo_not_found)
                                      == 0) and (len(failures) > 0):
            raise BackgroundJobError('Failed to import student photos.')
        else:
            update_photo_import_status(successes, failures, photo_not_found)
            status = 'Student photo import completed: '
            if len(successes):
                status += f'{len(successes)} succeeded, '
            if len(photo_not_found):
                status += f'{len(photo_not_found)} had no photo available, '
            status += f'{len(failures)} failed.'
            return status
예제 #2
0
 def test_user_not_found(self, app, caplog):
     """Logs error and returns False when user not found."""
     response = cal1card_photo_api.get_cal1card_photo(9999999)
     assert '404 Client Error' in caplog.text
     assert response is False
예제 #3
0
 def test_get_photo(self, app):
     """Returns fixture data."""
     oski_response = cal1card_photo_api.get_cal1card_photo(61889)
     assert isinstance(oski_response, bytes)
     assert len(oski_response) == 3559