예제 #1
0
def set_course_data(subject):
    terms = utils.get_courses(None)
    # Nested for loop to loop through each term, and each course, to return
    # the first valid term, course, and a random section
    for term in terms.json():
        term_id = term["id"]
        (valid_year, valid_term) = term_id.split("-")
        courses = utils.get_courses({"term_id": term_id, "id": subject})
        # looping through each course in the term.
        for course in courses.json():
            valid_course = course["id"]
            # if sections is empty, then loop again to find a course
            # with a valid section
            if course["sections"]:
                # include a random section in the response
                random_section = random.sample(course["sections"], 1)
                valid_section = random_section[0]["name"]
                global book_params
                book_params = {
                    "academicYear": valid_year,
                    "term": valid_term,
                    "subject": subject,
                    "courseNumber": valid_course,
                    "section": valid_section
                }
                # if a valid term, course and section was found,
                # exit the function with return. Otherwise loop again
                return

    # if this line was executed that means there was nothing found in the loop.
    exit("No valid courses were found for this search")
예제 #2
0
def man_templates(req, menu, post, base_context):
    '''  '''
    if post == 'post':
        form = req.POST
        shortname = form['course_selector']
        tmplname = form['field_shortname_tmpl']
        comments = form['tbx_comments']
        m = re.match('\-\-\sselect\sfrom', shortname)

        if tmplname != '' and not m:
            utils.push_files_to_subfolder_if_release(tmplname)
            ct_message = utils.create_template(shortname, tmplname)
            req.session[
                'message'] = 'Template "%s" creation from course "%s" with message "%s".' % (
                    tmplname, shortname, ct_message)
            utils.log_templatecreated(shortname, tmplname, comments,
                                      req.user.username)
        else:
            req.session[
                'message'] = 'Please select a proper course and a template name.'
        return redirect("/manage/%s" % menu)
    elif post:
        return redirect("/manage/%s" % menu)

    courses = utils.get_courses()
    templates = utils.get_templates()

    context = {
        'courses': courses,
        'templates': templates,
        'next': '/manage/%s/post' % menu
    }
    context.update(base_context)
    return render(req, 'man_templates.html', context)
예제 #3
0
def man_users(req, menu, post, base_context):
    ''' active users view '''
    if post == 'post':
        # get filtered signups depending on the hidden field "ids" as well as list selection (checkboxes following a naming convention)
        form = req.POST
        ids = ast.literal_eval(
            form.get('ids'))  # conv. str repr. of lst. to lst.
        ids = [i for i in ids if form.get('%s_cbx' % i) == 'on']
        objs = Signup.objects.filter(id__in=ids)

        # get bulk action
        action = form.get('bulk_actions')

        if action == 'disable':
            for signup in objs:
                ldaputils.rmuser(MCWEB_LDAP_DN, LDAP_PW, signup.username)
                signup.is_in_ldap = False
                signup.password = ''
                signup.save()
                req.session['message'] = 'Selected signups were disabled.'
        elif re.match('enroll_', action):
            course = re.match('enroll_(.*)', action).group(1)
            err_flag = False
            for signup in objs:
                utils.enroluser(signup, course)
                if signup.state() != 3:
                    req.session[
                        'message'] = 'Some signups reported an error, e.g. %s' % signup.fail_str
                    err_flag = True
            if not err_flag:
                req.session['message'] = 'Selected signups were enroled.'

        return redirect("/manage/%s" % menu)

    # bulk actions for this view
    bulk_actions = []
    bulk_actions.append('disable')
    courses = utils.get_courses()
    if len(courses) > 0:
        bulk_actions.append('---')
    for c in courses:
        bulk_actions.append('enroll_%s' % c)

    # filter signups
    signups = [s for s in Signup.objects.all() if s.state() == 3]
    entries_str = '(%d entries)' % len(signups)

    rows_ids = []
    ids = []
    for s in signups:
        ids.append(s.id)
        row = []

        row.append(CellInfo(s.created.strftime("%Y%m%d"), 1))
        row.append(CellInfo(s.firstname, 2))
        row.append(CellInfo(s.lastname, 3))
        row.append(CellInfo(s.email, 4))
        row.append(CellInfo(s.username, 5))
        row.append(CellInfo(s.institution_tpe, 6))
        row.append(CellInfo(s.country, 7))
        row.append(CellInfo(s.description, 8))

        rows_ids.append([row, str(s.id)])

    context = {
        'next': '/manage/%s/post' % menu,
        'uploadnext': '/manage/%s/upload' % menu,
        'ids': ids,
        'rows_ids': rows_ids,
        'bulk_actions': bulk_actions,
        'entries_str': entries_str
    }
    context.update(base_context)
    return render(req, 'man_users.html', context)
예제 #4
0
def man_bulk_signup(req, menu, post, base_context):
    if post == 'post':
        # get filtered signups depending on the hidden field "ids" as well as list selection (checkboxes following a naming convention)
        form = req.POST
        ids = ast.literal_eval(
            form.get('ids'))  # conv. str repr. of lst. to lst.
        ids = [i for i in ids if form.get('%s_cbx' % i) == 'on']
        objs = Signup.objects.filter(id__in=ids)

        # update objs with local data (text boxes)
        for signup in objs:
            signup.firstname = form.get('%s_%d' % (str(signup.id), 2))
            signup.lastname = form.get('%s_%d' % (str(signup.id), 3))
            signup.email = form.get('%s_%d' % (str(signup.id), 4))
            signup.username = form.get('%s_%d' % (str(signup.id), 5))
            signup.institution_tpe = form.get('%s_%d' % (str(signup.id), 6))
            signup.country = form.get('%s_%d' % (str(signup.id), 7))
            signup.description = form.get('%s_%d' % (str(signup.id), 8))
            signup.save()

        # get bulk action
        action = form.get('bulk_actions')

        if action == 'add':
            err_flag = False
            for signup in objs:
                utils.adduser(signup)
                if signup.state() != 3:
                    req.session[
                        'message'] = 'Some signups reported an error, e.g. %s' % signup.fail_str
                    err_flag = True
            if not err_flag:
                req.session['message'] = 'Selected signups were added.'

        elif action == 'delete':
            for signup in objs:
                signup.delete()
            req.session['message'] = 'Selected signups were deleted.'

        elif re.match('add_enroll_', action):
            err_flag = False
            course = re.match('add_enroll_(.*)', action).group(1)
            for signup in objs:
                signup.courses = [course]
                utils.adduser(signup)
                if signup.state() != 3:
                    req.session[
                        'message'] = 'Some signups reported an error, e.g. %s' % signup.fail_str
                    err_flag = True
            if not err_flag:
                req.session[
                    'message'] = 'Selected signups were added and enroled.'

        return redirect('/manage/%s' % menu)

    elif post == 'uploadcsv_post':
        if len(req.FILES) > 0:
            try:
                f = req.FILES['up_file']
                utils.pull_csv_signups_todb(f)
            except Exception as e:
                req.session[
                    'message'] = 'Bulk add processing error: %s' % e.__str__()
        return redirect('/manage/%s' % menu)

    elif post:
        return redirect('/manage/%s' % menu)

    # bulk actions for this view
    bulk_actions = []
    bulk_actions.append('add')
    bulk_actions.append('delete')
    courses = utils.get_courses()
    if len(courses) > 0:
        bulk_actions.append('---')
    for c in courses:
        bulk_actions.append('add_enroll_%s' % c)

    signups = [s for s in Signup.objects.all() if s.state() == 1]
    entries_str = '(%d entries)' % len(signups)

    rows_ids = []
    ids = []
    if len(signups) > 0:
        for s in signups:
            row = []

            row.append(CellInfo(s.created.strftime("%Y%m%d"), 1))
            row.append(CellInfo(s.firstname, 2, txt=True))
            row.append(CellInfo(s.lastname, 3, txt=True))
            row.append(CellInfo(s.email, 4, txt=True))
            row.append(CellInfo(s.username, 5, txt=True))
            row.append(CellInfo(s.institution_tpe, 6, txt=True))
            row.append(CellInfo(s.country, 7, txt=True))
            row.append(CellInfo(s.description, 8, txt=True))

            rows_ids.append([row, str(s.id)])
            ids.append(s.id)

    context = {
        'next': '/manage/%s/post' % menu,
        'uploadnext': '/manage/%s/uploadcsv_post' % menu,
        'rows_ids': rows_ids,
        'ids': ids,
        'courses': courses,
        'bulk_actions': bulk_actions,
        'entries_str': entries_str
    }
    context.update(base_context)
    return render(req, 'man_bulk.html', context)
예제 #5
0
result_loc.mkdir(exist_ok=True)
org_search = args.org_search
druid_ip = args.Druid_hostname
elastic_search = args.elastic_search
execution_date = datetime.strptime(args.execution_date, "%d/%m/%Y")
analysis_date = execution_date - timedelta(days=1)
result_loc.joinpath(analysis_date.strftime('%Y-%m-%d')).mkdir(exist_ok=True)
result_loc.parent.joinpath('config').mkdir(exist_ok=True)
get_data_from_blob(result_loc.parent.joinpath('config', 'diksha_config.json'))
with open(result_loc.parent.joinpath('config', 'diksha_config.json'),
          'r') as f:
    config = json.loads(f.read())
get_tenant_info(result_loc_=result_loc,
                org_search_=org_search,
                date_=analysis_date)
get_courses(result_loc_=result_loc, druid_ip_=druid_ip, date_=analysis_date)
get_course_plays(result_loc_=result_loc, date_=analysis_date)
get_user_courses(result_loc_=result_loc,
                 date_=analysis_date,
                 elastic_search_=elastic_search)
get_course_batch(result_loc_=result_loc,
                 date_=analysis_date,
                 elastic_search_=elastic_search)
generate_course_usage(result_loc_=result_loc, date_=analysis_date)

end_time_sec = int(round(time.time()))
time_taken = end_time_sec - start_time_sec
metrics = [{
    "metric": "timeTakenSecs",
    "value": time_taken
}, {