Пример #1
0
def sudochangepassword(request, **kwargs):
	both = getme(request, both=True)
	if 'them_id' in kwargs:
		them = Users.fetch(id=kwargs['them_id'])
	else:
		them = both['sudo']
	me = both['login']
	if not me:
		return redirect('/')
	bad = restricted(request,6,them,allow_sudo=True)
	if bad:
		return bad 
	user = copy(request.POST,['password','pw_confm'])
	valid = Users.isValid(user, partial=True)
	if not valid:
		request.session['e'] = Users.errors(user, partial=True)
	correct = me.password(request.POST['current_pw'])
	if not correct:
		request.session['e']['current_pw'] = "Your password is incorrect"
	if correct and valid:
		if not them:
			return HttpResponse('User not found.', status=404)
		request.session['e'] = {}
		them.password = str(request.POST['password'])
		them.save()
	return redirect(request.META['HTTP_REFERER'])
Пример #2
0
def family_post(request, ref):
    me = getme(request)
    if me:
        me.username = request.POST['username']
        if me.owner:
            me.owner.save()
    new_family = copy(request.POST, ['last', 'phone', 'phone_type', 'email'])
    new_family['last'] = namecase(new_family['last'])
    new_user = copy(request.POST, ['username', 'password', 'pw_confm'])
    new_user['permission'] = 2
    ouu = me and str(new_user['username']) == str(me.username)
    if Families.isValid(new_family) and Users.isValid(
            new_user, override_unique_username=ouu):
        new_user.pop('pw_confm')
        if not me:
            new_family = Families.create(**new_family)
            new_user['owner'] = new_family
            me = Users.create(**new_user)
        elif me and not me.owner:
            me.username = new_user['username']
            new_family = Families.create(**new_family)
            me.owner = new_family
            me.save()
        elif me and me.owner:
            me.username = new_user['username']
            family = me.owner
            family.last = new_family['last']
            family.phone = new_family['phone']
            family.email = new_family['email']
            family.phone_type = new_family['phone_type']
            print new_family['email']
            print family.email
            family.save()
            me.save()
        request.session['e'] = {}
        request.session['meid'] = me.id
        return redirect('/register/parents')
    else:
        request.session['p'] = {
            'family': new_family,
            'user': new_user,
        }
        request.session['e'] = {
            'family': Families.errors(new_family),
            'user': Users.errors(new_user)
        }
        return redirect('/register/family')
Пример #3
0
def family_get(request, ref):
    me = getme(request)
    if me and me.owner:
        request.session['p']['family'].update(
            copyatts(me.owner, ['last', 'phone_type', 'email']))
        request.session['p']['family']['phone'] = str(me.owner.phone)
        request.session['p']['user']['username'] = me.username
        request.session['password_set'] = True
    context = copy(request.session, ['p', 'e', 'password_set'])
    return render(request, 'family.html', context)
Пример #4
0
def update(request, **kwargs):
    bad = restricted(request, 5)
    if bad:
        return bad
    policy = Policies.fetch(year=request.POST.get('year'))
    if policy:
        policy.markdown = request.POST.get('markdown')
        policy.save()
    else:
        policy = Policies.create(**copy(request.POST, ['year', 'markdown']))
    return redirect('/admin/policy/show/{}'.format(policy.year))
Пример #5
0
def changepassword_get(request, **kwargs):
    context = copy(request.session, 'pe')
    if 'them_id' in kwargs:
        context.update({
            'me': {
                'login': getme(request, both=True)['login'],
                'sudo': Users.fetch(id=kwargs['them_id']),
            }
        })
    else:
        context.update({'me': getme(request, both=True)})
    return render(request, 'main/changepassword.html', context)
Пример #6
0
def students_update(request, ref, id):
    student = Students.fetch(id=id)
    data = copy(request.POST, student_fields)
    if not Students.isValid(data):
        request.session['e'] = Students.errors(data)
        request.session['p'] = data.copy()
        return redirect('/{}/students/{}/'.format(ref, student.id))
    else:
        request.session['e'] = {}
        for field in student_fields:
            student.__setattr__(field, data[field])
        student.save()
        return redirect('/{}/students/{}/'.format(ref, request.POST['next']))
Пример #7
0
def students_create(request, ref):
    me = getme(request)
    data = copy(request.POST, student_fields)
    if not Students.isValid(data):
        if request.POST['next'] == 'new':
            request.session['e'] = Students.errors(data)
            request.session['p'] = data.copy()
            return redirect('/{}/students/new/'.format(ref))
        else:
            request.session['e'] = {}
            return redirect('/{}/students/{}/'.format(ref,
                                                      request.POST['next']))
    else:
        data['family'] = me.owner
        new_student = Students.create(**data)
        request.session['e'] = {}
        if request.POST['next'] == 'new':
            return redirect('/{}/students/{}/'.format(ref, new_student.id))
        else:
            return redirect('/{}/students/{}/'.format(ref,
                                                      request.POST['next']))
Пример #8
0
def changepassword_post(request, **kwargs):
    ref = kwargs.setdefault('ref', None)
    both = getme(request, both=True)
    me = both['login']
    if not me:
        return redirect('/')
    if 'them_id' in kwargs or both['sudo']:
        return sudochangepassword(request, **kwargs)
    user = copy(request.POST, ['password', 'pw_confm'])
    valid = Users.isValid(user, partial=True)
    if not valid:
        request.session['e'] = {}
        request.session['e'] = Users.errors(user, partial=True)
    correct = me.password(request.POST['current_pw'])
    if not correct:
        request.session['e']['current_pw'] = "Your password is incorrect"
    if correct and valid:
        request.session['e'] = {}
        me.password = request.POST['password']
        me.save()
        return redirect('/{}/'.format(ref))
    return redirect(request.META['HTTP_REFERER'])
Пример #9
0
def login_post(request, path):
    if not path:
        path = '/'
    persist = copy(request.POST, ['username', 'password'])
    me = find_user(request.POST['username'])
    if not me:
        request.session['e'] = {
            'login': {
                'username': "******"
            }
        }
        request.session['p'] = {'login': persist}
        return redirect('/login{}'.format(path))
    elif not me.password(request.POST['password']):
        request.session['e'] = {
            'login': {
                'password': "******"
            }
        }
        request.session['p'] = {'login': persist}
        return redirect('/login{}'.format(path))
    else:
        request.session['meid'] = me.id
        return redirect(path)
Пример #10
0
def login_get(request, **kwargs):
    context = copy(request.session, ['p', 'e'])
    return render(request, 'main/login.html', context)
Пример #11
0
def parents_post(request, ref):
    me = getme(request)
    # Create new Parent objects
    mom = copy(request.POST, [
        'mom_skipped', 'mom_first', 'mom_alt_last', 'mom_alt_phone',
        'mom_phone_type', 'mom_alt_email'
    ])
    dad = copy(request.POST, [
        'dad_skipped', 'dad_first', 'dad_alt_last', 'dad_alt_phone',
        'dad_phone_type', 'dad_alt_email'
    ])
    mother = copy(mom, trunc=4)
    father = copy(dad, trunc=4)
    # Convert 'skipped' booleans from JavaScript strings, to Python bools.
    mother['skipped'] = mother['skipped'] == 'true'
    father['skipped'] = father['skipped'] == 'true'
    # May it be many years before we have to change these two lines of code.
    mother['sex'] = 'F'
    father['sex'] = 'M'
    # Assign Parents to Family
    mother['family_id'] = me.owner.id
    father['family_id'] = me.owner.id
    # Return sarcastic condescending message if both parents were somehow skipped.
    if mother['skipped'] and father['skipped']:
        return HttpResponse('''
			Congratulations!  You figured out how to hack JavaScript and skip both parents!
			Seriously though, we do need at least one first name.  Sorry.
			''')
    # Validate new Parent objects
    mother_valid = mother['skipped'] or Parents.isValid(mother)
    father_valid = father['skipped'] or Parents.isValid(father)
    if mother_valid and father_valid:
        request.session['e'] = {}
        # Add parents to Database if they have not been skipped
        if not mother.pop('skipped'):
            if me.owner.mother:
                mother_proxy = me.owner.mother
                mother_proxy.first = mother['first']
                mother_proxy.alt_last = mother['alt_last']
                mother_proxy.alt_phone = mother['alt_phone']
                mother_proxy.alt_email = mother['alt_email']
                mother_proxy.save()
            else:
                family = me.owner
                family.mother = Parents.create(**mother)
                family.save()
        if not father.pop('skipped'):
            if me.owner.father:
                father_proxy = me.owner.father
                father_proxy.first = father['first']
                father_proxy.alt_last = father['alt_last']
                father_proxy.alt_phone = father['alt_phone']
                father_proxy.alt_email = father['alt_email']
                father_proxy.save()
            else:
                family = me.owner
                family.father = Parents.create(**father)
                family.save()
        return redirect('/register/students')
    else:
        request.session['p'] = {
            'mom': mom,
            'dad': dad,
        }
        request.session['e'] = {
            'mom': Parents.errors(mother),
            'dad': Parents.errors(father),
        }
        return redirect('/register/parents')
Пример #12
0
def load_post(request):
	start_import = datetime.now()
	nUsers       = 0
	nFamilies    = 0
	nStudents    = 0
	nParents     = 0
	nAddresses   = 0
	nVenues      = 0
	nCourseTrads = 0
	nCourses     = 0
	nEnrollments = 0

	json_dump = request.POST['json_dump']
	request.session['json_dump'] = ''
	data = json.loads(json_dump)

	for ct in data['coursetrads']:
		if 'alias_id' not in ct:
			already = CourseTrads.fetch(id=ct['id'])
			if already:
				print 'Exists:   '+ct['title'].upper()
			else:
				print 'Importing '+ct['title'].upper()
				ct.setdefault('default','enrolled' if ct['after_tuit'] == 0 else 'need_pay')
				CourseTrads.create(**ct)
				nCourseTrads += 1

	for ct in data['coursetrads']:
		if 'alias_id' in ct:
			already = CourseTrads.fetch(id=ct['id'])
			if already:
				print 'Exists:   '+ct['title'].upper()
			else:
				print 'Importing '+ct['title'].upper()
				ct.setdefault('default','--------')
				alias = CourseTrads.get(id=ct.pop('alias_id'))
				ct['alias'] = alias
				CourseTrads.create(**ct)
				nCourseTrads += 1

	# cts = CourseTrads.filter(e=True)
	# for year in data['years']:
	# 	for ct in cts:
	# 		ct.make(year)
	# 		nCourses += 1

	for fam in data['families']:
		print fam['last']
		family = copy(fam,['last','phone','email'])
		if 'address' in fam:
			address = copy(fam['address'])
			if 'zipcode' not in address:
				address['zipcode'] = 00000
			address = Addresses.create(**address)
			nAddresses += 1
			family['address'] = address
		family = Families.create(**family)
		family.update_name_num()
		nFamilies += 1
		user_obj = fam.get('user')
		if user_obj:
			Users.create(
				owner_type = 'Family',
				owner_id   = family.id,
				username   = user_obj['username'],
				password   = user_obj['password'],
				permission = user_obj['permission'],
			)
			nUsers += 1
		if 'mother' in fam:
			mother = copy(fam['mother'])
			mother['family_id'] = family.id
			family.mother = Parents.create(**mother)
			nParents += 1
		if 'father' in fam:
			father = copy(fam['father'])
			father['family_id'] = family.id
			family.father = Parents.create(**father)
			nParents += 1
		family.save()
		for stu in fam['students']:
			if 'first' in stu:
				print '  '+stu['first']
				student = copy(stu)
				enrollments = student.pop('enrollments') if 'enrollments' in student else []
				student['family'] = family
				newStudent = Students.create(**student)
				for enrollment in enrollments:
					rolled = type(enrollment) in [object,dict]
					course_id = enrollment['course_id'] if rolled else enrollment
					print '    '+course_id
					course = Courses.fetch(id=course_id)
					if not course:
						course = Courses.create_by_id(course_id)
						nCourses += 1
					enrollment_kwargs = {
						'student': newStudent,
						'course' : course,
						'status' : enrollment.setdefault('status','enrolled') if hasattr(enrollment,'setdefault') else 'enrolled'
					}
					if rolled:
						enrollment_kwargs['role']      = enrollment['role']
						enrollment_kwargs['role_type'] = enrollment['role_type']
					Enrollments.create(**enrollment_kwargs)
					nEnrollments += 1
					# if type(enrollment) in [str,unicode]:
					# 	Courses.get(id=enrollment).sudo_enroll(newStudent)
					# else:
					# 	course = Courses.get(id=enrollment['course_id'])
					# 	Enrollments.create(course=course, student=newStudent, role=enrollment['role'], role_type=enrollment['role_type'])
				nStudents += 1

	print "- assign name_num's"
	Each(Families.all()).update_name_num()

	print '- order_coursetrads'
	order_coursetrads()

	# year = getyear()
	# print '- make {}'.format(year)
	# nCourses += make(year)

	# print '- assign developer: Wolf Elkan'
	# elkan = Families.fetch(last='Elkan')
	# if elkan:
	# 	Users.create(
	# 		username='******',
	# 		password='******',
	# 		permission=7,
	# 		owner_type='Family',
	# 		owner_id=elkan.id
	# 		)

	import_duration = datetime.now() - start_import
	
	print '\nIMPORT COMPLETED'
	print 'Users:     ' + str(nUsers).rjust(5)
	print 'Families:  ' + str(nFamilies).rjust(5)
	print 'Students:  ' + str(nStudents).rjust(5)
	print 'Parents:   ' + str(nParents).rjust(5)
	print 'Addresses: ' + str(nAddresses).rjust(5)
	print 'Venues:    ' + str(nVenues).rjust(5)
	print 'Traditions:' + str(nCourseTrads).rjust(5)
	print 'Courses:   ' + str(nCourses).rjust(5)
	print 'Enrollments:'+ str(nEnrollments).rjust(4)
	print 'Time:    '   + str(import_duration)

	return redirect('/seed/load/')