def user_submit(request): if not request.user.is_authenticated: raise Http404 try: if request.POST.get('id'): profile=UserProfile.objects.get(user_id=request.POST['id']) user=User.objects.get(id=request.POST['id']) else: profile=UserProfile() user=User() if request.POST['subscription_status']=='Registered': user.is_active=True else: user.is_active=False user.email=request.POST['email'] user.save() profile.user_id=user.id profile.school_id=request.POST['school_id'] profile.cohort_id=request.POST['cohort_id'] profile.district_id=request.POST['district_id'] profile.subscription_status=request.POST['subscription_status'] profile.save() except Exception as e: db.transaction.rollback() return HttpResponse(json.dumps({'success': False,'error':'%s' % e})) return HttpResponse(json.dumps({'success': True}))
def import_user_submit(request): message={} if request.method == 'POST': f=request.FILES['file'] try: count_success=0 # --- THIS FAILS ON SING COLUMN CVS --- # dialect = csv.Sniffer().sniff(f.read(1024), delimiters=";,") # f.seek(0) # r=csv.reader(f,dialect) r=csv.reader(f,delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL) rl = [] rl.extend(r) cohort_id=request.POST.get("cohort_id") cohort=Cohort.objects.get(id=cohort_id) if cohort.licences < UserProfile.objects.filter(~Q(subscription_status = "Inactive"),cohort_id=cohort_id).count() + len(rl): raise Exception("Licences limit exceeded") for line in rl: exist=validate_user_cvs_line(line) # if(exist): # raise Exception("An user already exists, or duplicate lines.") email=line[USER_CSV_COL_EMAIL] import random username=random_mark(20) user = User(username=username, email=email, is_active=False) user.set_password(username) user.save() registration = Registration() registration.register(user) profile=UserProfile(user=user) # profile.transaction_id=transaction_id # profile.email=email # profile.username=username profile.cohort_id=cohort_id profile.subscription_status="Imported" profile.save() cea, _ = CourseEnrollmentAllowed.objects.get_or_create(course_id='PCG_Education/PEP101.1/S2016', email=email) cea.is_active = True cea.auto_enroll = True cea.save() count_success=count_success+1 # reg = Registration.objects.get(user=user) # d = {'name': profile.name, 'key': reg.activation_key} # subject = render_to_string('emails/activation_email_subject.txt', d) # subject = ''.join(subject.splitlines()) # message = render_to_string('emails/activation_emailh.txt', d) db.transaction.commit() message={"success": True, "message":"Success! %s users imported." % (count_success), "count_success":count_success, } except Exception as e: db.transaction.rollback() message={'success': False,'error':'Import error: %s. At cvs line: %s, Nobody imported.' % (e,count_success+1)} return HttpResponse(json.dumps(message))
def import_user_submit(request): message = {} if request.method == "POST": f = request.FILES["file"] try: count_success = 0 count_exist = 0 # --- THIS FAILS ON SING COLUMN CVS --- # dialect = csv.Sniffer().sniff(f.read(1024), delimiters=";,") # f.seek(0) # r=csv.reader(f,dialect) r = csv.reader(f, delimiter="\t", quotechar="|", quoting=csv.QUOTE_MINIMAL) rl = [] rl.extend(r) cohort_id = request.POST.get("cohort_id") cohort = Cohort.objects.get(id=cohort_id) if cohort.licences < UserProfile.objects.filter(cohort_id=cohort_id).count() + len(rl): raise Exception("Licences limit exceeded") for line in rl: exist = validate_user_cvs_line(line) # if(exist): # raise Exception("An user already exists, or duplicate lines.") email = line[USER_CVS_COL_EMAIL] import random username = "".join(random.sample("abcdefg&#%^*f1234567890", 20)) user = User(username=username, email=email, is_active=True) user.set_password(username) user.save() registration = Registration() registration.register(user) profile = UserProfile(user=user) # profile.transaction_id=transaction_id # profile.email=email # profile.username=username profile.cohort_id = cohort_id profile.subscription_status = "Imported" profile.save() # reg = Registration.objects.get(user=user) # d = {'name': profile.name, 'key': reg.activation_key} # subject = render_to_string('emails/activation_email_subject.txt', d) # subject = ''.join(subject.splitlines()) # message = render_to_string('emails/activation_email.txt', d) db.transaction.commit() message = { "success": True, "message": "Success! %s users imported." % (count_success), "count_exist": count_exist, "count_success": count_success, } except Exception as e: db.transaction.rollback() message = { "success": False, "error": "Import error: %s. At cvs line: %s, Nobody imported." % (e, count_success + 1), } return HttpResponse(json.dumps(message))
def create_unknown_user(request, ms, data): '''Create the sso user who\'s not exists in pepper''' try: attribute_setting = ms.get('attributes') # Parse to mapped attribute parsed_data = {} for attr in attribute_setting: mapped_name = attr['map'] if 'map' in attr else attr['name'] if attr['name']: parsed_data[mapped_name] = data.get(attr['name']) print attribute_setting print data print parsed_data # Generate username if not provided if not parsed_data.get('username'): username = random_mark(20) else: username = parsed_data['username'] # Email must be profided email = parsed_data['email'] user = User(username=username, email=email, is_active=False) user.set_password(username) # Set password the same with username user.save() registration = Registration() registration.register(user) profile = UserProfile(user=user) profile.subscription_status = "Imported" profile.sso_type = ms.get('sso_type') profile.sso_idp = ms.get('sso_name') # Save mapped attributes for k, v in parsed_data.items(): if k == 'first_name': user.first_name = parsed_data['first_name'] elif k == 'last_name': user.last_name = parsed_data['last_name'] elif k == 'sso_user_id': profile.sso_user_id = parsed_data['sso_user_id'] elif k == 'district': profile.district = District.object.get(name=parsed_data['district']) elif k == 'school': profile.school = School.object.get(name=parsed_data['school']) elif k == 'grade_level': ids = GradeLevel.object.filter(name__in=parsed_data['grade_level'].split(',')).values_list('id', flat=True) profile.grade_level = ','.join(ids) elif k == 'major_subject_area': ids = SubjectArea.object.filter(name__in=parsed_data['major_subject_area'].split(',')).values_list('id', flat=True) profile.major_subject_area = ','.join(ids) elif k == 'years_in_education': profile.years_in_education = YearsInEducation.object.get(name=parsed_data['years_in_education']) elif k == 'percent_lunch': profile.percent_lunch = Enum.object.get(name='percent_lunch', content=parsed_data['percent_lunch']) elif k == 'percent_iep': profile.percent_iep = Enum.object.get(name='percent_iep', content=parsed_data['percent_iep']) elif k == 'percent_eng_learner': profile.percent_eng_learner = Enum.object.get(name='percent_eng_learner', content=parsed_data['percent_eng_learner']) user.save() profile.save() cea, _ = CourseEnrollmentAllowed.objects.get_or_create(course_id='PCG_Education/PEP101.1/S2016', email=email) cea.is_active = True cea.auto_enroll = True cea.save() return https_redirect(request, reverse('register_sso_user', args=[registration.activation_key])) except Exception as e: raise e db.transaction.rollback() log.error("error: failed to create SSO user: %s" % e)