def courseman_users_post(req): ''' The "ids" list, set previously on the form, is used to ensure that only viewed signup instances are added. ''' form = req.POST # get filtered signups and update form = req.POST ids = ast.literal_eval(form.get('ids')) # conv. str repr. of lst. to lst. signups = Signup.objects.filter(id__in=ids) utils.update_signups(signups, form) cs = form['course_selector'] if re.match('\-\-\sselect', cs): req.session['message'] = 'Please select a course.' return redirect('/coursemanage/users') courses = [cs] + COURSES_MANDATORY utils.assign_courses(signups, courses) override_ldap = False if form.get('override_ldap'): override_ldap = True # perform the appropriate add-user actions for each signup req.session['message'] = 'All signups were added succesfully.' for signup in signups: utils.adduser(signup, ldap_password=req.session['ldap_password'], accept_ldap_exists=override_ldap) if signup.fail_str != '': req.session['message'] = 'Some signups reported an error. Use the override checkbox if you think the user already exists in mcweb.' return redirect('/coursemanage/users')
def userlist_au_post(req): ''' handles list form submission ''' # get filtered signups and update form = req.POST ids = ast.literal_eval(form.get('ids')) # conv. str repr. of lst. to lst. objs = Signup.objects.filter(id__in=ids) utils.update_signups(objs, form) # perform the appropriate add-user actions for each signup for signup in objs: utils.adduser(signup, ldap_password=req.session['ldap_password']) # return to the list if len(utils.get_signups()) > 0: return redirect('/userlist_au/new') elif len(utils.get_signups_limbo()) > 0: return redirect('/userlist_au/limbo') else: return redirect('/userlist_au/added')
def register(): if request.method == "GET": return render_template("register.html") else: button = request.form["button"] if button == "register": username = request.form["username"].encode("ascii", "ignore") password = request.form["password"].encode("ascii", "ignore") if not utils.adduser(username, password): print "this username already exists. please pick another." return redirect("/register") session["username"] = username return redirect("/success")
def courseman_courses_post(req): ''' create a new course from a template, enroll a user as teacher (and possibly create the user) ''' form = req.POST tmpl = form['tmpl_selector'] site = form['tbx_site'] shortname = form['field_shortname'] title = form['tbx_title'] m = re.match('\-\-\sselect\sfrom', shortname) if site != '' and shortname != '' and title != '' and not m: (backupname, courseid, status) = mu.create_course_from_template(templatename=tmpl, shortname=shortname, fullname=title) else: req.session['message'] = 'Please select a proper template and a course name.' return redirect('/coursemanage/courses') username = form['tbx_username'] firstname = form['tbx_firstname'] lastname = form['tbx_lastname'] email = form['tbx_email'] if username == '': req.session['message'] = 'Please assign a teacher for the course.' return redirect('/coursemanage/courses') if firstname != '' or email != '': if firstname == '' or email == '': req.session['message'] = 'New user creation requires a name and an email.' return redirect('/coursemanage/courses') teacher = utils.create_signup(firstname, lastname, email, username, []) utils.adduser(teacher, ldap_password=req.session['ldap_password']) # TODO: implement error handling for this case, if the user doesn't exist and no info was provided mu.enroll_user(username=username, course_sn=shortname, teacher=True) req.session['message'] = 'Course %s created with teacher %s. Restoring contents in background...' % (shortname, username) return redirect('/coursemanage/courses')
def man_courses(req, menu, post, base_context): ''' ''' if post == 'post': form = req.POST tmpl = form['tmpl_selector'] shortname = form['field_shortname'] title = form['tbx_title'] if title == '': title = "empty" username = form['tbx_username'] firstname = form['tbx_firstname'] lastname = form['tbx_lastname'] email = form['tbx_email'] # sanity check template selected if re.match('\-\-\sselect\sfrom', tmpl): req.session['message'] = 'Please select a course template.' return redirect("/manage/%s" % menu) # sanity check shortname exists if shortname == '': req.session['message'] = 'Please use a proper shortname.' return redirect("/manage/%s" % menu) # double-check that a user has been selected if username == '': req.session['message'] = 'Please assign a teacher for the course.' return redirect("/manage/%s" % menu) # search registered ldap users for 'username' users = ldaputils.listusers(uid=username) # new teacher branch if firstname != '' or lastname != '' or email != '': # sanity check for existence if len(users) == 1: t = users[0] req.session[ 'message'] = 'User %s already exists with "%s %s, %s", please clear the name and email fields.' % ( username, t.cn, t.sn, t.mail) return redirect("/manage/%s" % menu) else: # course create (before teach assignment) and schedule course restore job status = utils.create_course_from_template(templatename=tmpl, shortname=shortname, fullname=title) utils.log_coursecreated(shortname, tmpl, req.user.username) req.session[ 'message'] = 'Course "%s" creation with teacher "%s" and message "%s".' % ( shortname, username, status) # assign a teacher if firstname == '' or email == '': req.session['message'] = req.session[ 'message'] + '\n ' + 'New user creation requires a name and an email.' return redirect("/manage/%s" % menu) teacher = Signup(username=username, firstname=firstname, lastname=lastname, email=email, password=utils.get_random_passwd(), courses=[shortname]) teacher.save() utils.adduser(teacher) utils.enroluser(teacher, course_sn=shortname, teacher=True) req.session['message'] = req.session[ 'message'] + '\n ' + 'New user %s has been created.' % username # existing teacher branch elif len(users) == 1: # course create (before teacher assignment) and schedule course restore job status = utils.create_course_from_template(templatename=tmpl, shortname=shortname, fullname=title) utils.log_coursecreated(shortname, tmpl, req.user.username) req.session[ 'message'] = 'Course "%s" creation with teacher "%s" and message "%s".' % ( shortname, username, status) # assign teacher teacher = utils.get_signup(username) if not teacher: # user exists only in ldap, create a signup t_data = users[0] t = Signup(username=username, firstname=t_data.cn, lastname=t_data.sn, email=t_data.mail, is_in_ldap=True) t.save() utils.adduser(t) t.password = '' t.save() teacher = t req.session['message'] = req.session[ 'message'] + '\n ' + 'WARNING: username existed only in ldap, proceeding with teacher "%s", "%s", "%s", "%s".' % ( username, t_data.cn, t_data.sn, t_data.mail) utils.enroluser(teacher, course_sn=shortname, teacher=True) # invalid username branch else: # username does not exist, but user did not enter name etc. req.session[ 'message'] = 'Username "%s" not found. Please enter the name and email of the teacher of this course, and a new user will be created.' % username return redirect("/manage/%s" % menu) return redirect("/manage/%s" % menu) elif post: return redirect("/manage/%s" % menu) context = { 'templates': utils.get_templates(), 'next': '/manage/%s/post' % menu } context.update(base_context) return render(req, 'man_courses.html', context)
def man_limbos(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.save() # get bulk action action = form.get('bulk_actions') if action == 'add': for signup in objs: utils.adduser(signup) req.session[ 'message'] = 'Signups were attempted re-added and notified.' elif action == 'delete': for signup in objs: try: signup.delete() except Exception as e: req.session['message'] = req.session[ 'message'] + 'Error: %s.' % e.__str__() req.session[ 'message'] = req.session['message'] + 'Signups were deleted.' return redirect("/manage/%s" % menu) # bulk actions for this view bulk_actions = [] bulk_actions.append('add') bulk_actions.append('delete') # filter signups signups = [s for s in Signup.objects.all() if s.state() == 0] 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)
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)
def man_selfsignups(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_enrol': err_flag = False for signup in objs: signup.courses = COURSES_MANDATORY 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'] = 'Signups were added and enroled.' elif action == 'delete': for i in range(len(objs)): objs[0].delete() req.session['message'] = 'Selected entries were deleted.' return redirect('/manage/%s' % menu) elif post: return redirect('/manage/%s' % menu) # bulk actions for this view bulk_actions = [] bulk_actions.append('add_enrol') bulk_actions.append('delete') signups = [s for s in Signup.objects.all() if s.state() == 2] 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, 'rows_ids': rows_ids, 'ids': ids, 'bulk_actions': bulk_actions, 'demo_courses_string': ', '.join(COURSES_MANDATORY), 'entries_str': entries_str } context.update(base_context) return render(req, 'man_selfsignups.html', context)