def test_add_demographics_rows(self, app):
        import tempfile
        with tempfile.TemporaryFile() as demo_file, tempfile.TemporaryFile() as ethn_file, tempfile.TemporaryFile() as visa_file:
            feed_files = {
                'demographics': demo_file,
                'ethnicities': ethn_file,
                'visas': visa_file,
            }
            feed_counts = {
                'demographics': 0,
                'ethnicities': 0,
                'visas': 0,
            }
            raw_feed = get_term_gpas_registration_demog('1234567890').get('demographics')
            parsed = add_demographics_rows('1234567890', raw_feed, feed_files, feed_counts)
            assert not parsed.get('filtered_ethnicities')
            assert parsed['ethnicities'] == ['Korean / Korean-American', 'White']

            feed_files['demographics'].seek(0)
            assert feed_files['demographics'].read() == b'1234567890\tFemale\tFalse\n'
            assert feed_counts['demographics'] == 1
            feed_files['ethnicities'].seek(0)
            assert feed_files['ethnicities'].read() == b'1234567890\tKorean / Korean-American\n'
            assert feed_counts['ethnicities'] == 1
            feed_files['visas'].seek(0)
            assert feed_files['visas'].read() == b'1234567890\tG\tF1\n'
            assert feed_counts['visas'] == 1
Esempio n. 2
0
    def generate_student_profile_feed(self, feed_elements, advisors,
                                      feed_files, feed_counts):
        sid = feed_elements['sid']
        uid = feed_elements['ldap_uid']
        if not uid:
            return
        sis_profile = parse_merged_sis_profile(feed_elements)
        demographics = feed_elements.get('demographics_feed') and json.loads(
            feed_elements.get('demographics_feed'))
        if demographics:
            demographics = add_demographics_rows(sid, demographics, feed_files,
                                                 feed_counts)

        advisor_feed = []
        for a in advisors:
            advisor_feed.append({
                'uid':
                a['advisor_uid'],
                'sid':
                a['advisor_sid'],
                'firstName':
                a['advisor_first_name'],
                'lastName':
                a['advisor_last_name'],
                'email': (a['advisor_campus_email'] or a['advisor_email']),
                'role':
                a['advisor_role'],
                'title':
                a['advisor_title'],
                'program':
                a['program'],
                'plan':
                a['plan'],
            })

        merged_profile = {
            'sid':
            sid,
            'uid':
            uid,
            'firstName':
            feed_elements.get('first_name'),
            'lastName':
            feed_elements.get('last_name'),
            'name':
            ' '.join([
                feed_elements.get('first_name'),
                feed_elements.get('last_name')
            ]),
            'canvasUserId':
            feed_elements.get('canvas_user_id'),
            'canvasUserName':
            feed_elements.get('canvas_user_name'),
            'sisProfile':
            sis_profile,
            'demographics':
            demographics,
            'advisors':
            advisor_feed,
        }
        feed_counts['student_profiles'] += write_to_tsv_file(
            feed_files['student_profiles'],
            [sid, json.dumps(merged_profile)])

        if sis_profile:
            first_name = merged_profile['firstName'] or ''
            last_name = merged_profile['lastName'] or ''
            level = str(sis_profile.get('level', {}).get('code') or '')
            gpa = str(sis_profile.get('cumulativeGPA') or '')
            units = str(sis_profile.get('cumulativeUnits') or '')
            transfer = str(sis_profile.get('transfer') or False)
            expected_grad_term = str(
                sis_profile.get('expectedGraduationTerm', {}).get('id') or '')
            terms_in_attendance = str(
                sis_profile.get('termsInAttendance', {}) or '')

            feed_counts['student_profile_index'] += write_to_tsv_file(
                feed_files['student_profile_index'],
                [
                    sid, uid, first_name, last_name, level, gpa, units,
                    transfer, expected_grad_term, terms_in_attendance
                ],
            )

            for plan in sis_profile.get('plans', []):
                if plan.get('status') == 'Active':
                    feed_counts['student_majors'] += write_to_tsv_file(
                        feed_files['student_majors'],
                        [
                            sid,
                            plan.get('program', None),
                            plan.get('description', None)
                        ],
                    )
            for hold in sis_profile.get('holds', []):
                feed_counts['student_holds'] += write_to_tsv_file(
                    feed_files['student_holds'], [sid, json.dumps(hold)])
            for intended_major in sis_profile.get('intendedMajors', []):
                feed_counts['intended_majors'] += write_to_tsv_file(
                    feed_files['intended_majors'],
                    [sid, intended_major.get('description', None)])
            for plan in sis_profile.get('plansMinor', []):
                if plan.get('status') == 'Active':
                    feed_counts['minors'] += write_to_tsv_file(
                        feed_files['minors'],
                        [sid, plan.get('description', None)])

        return merged_profile