def nuke(request): # bad = restricted(request,6) # if bad: # return bad # Main request.session.clear() # Payment # PayPals.all().delete() # Invoices.all().delete() # People Addresses.all().delete() Parents.all().delete() Families.all().delete() Students.all().delete() Users.all().delete() # Program Venues.all().delete() CourseTrads.all().delete() Courses.all().delete() Enrollments.all().delete() # RAdmin Policies.all().delete() print '\n\n'+' '*34+'THE RADIANCE OF A THOUSAND SUNS'+'\n\n' return redirect ('/seed/load')
def lookup_student(obj): first = namecase(obj['first']) last = namecase(obj['last']) student = Students.fetch(first=first,family__last=last) if not student: student = Students.fetch(alt_first=first,family__last=last) return student
def register(request, **kwargs): bad = restricted(request, 5) if bad: return bad new_enrollments = {} course = Courses.get(id=str(request.POST['course_id'])) for x in request.POST: role_match = re.match(r'^role_(\d+)$', x) role_type_match = re.match(r'^role_type_(\d+)$', x) if role_match: student_id = role_match.groups()[0] if student_id not in new_enrollments: new_enrollments[student_id] = {} new_enrollments[student_id]['role'] = request.POST[x] elif role_type_match: student_id = role_type_match.groups()[0] if student_id not in new_enrollments: new_enrollments[student_id] = {} new_enrollments[student_id]['role_type'] = request.POST[x] for x in new_enrollments: student = Students.fetch(id=int(x)) Enrollments.create(student=student, course=course, role=new_enrollments[x]['role'], role_type=new_enrollments[x]['role_type'], status=request.POST['status']) return redirect('/reports/students/2016/')
def add_hids(**kwargs): for family in Families.filter(**kwargs): match = re.match(r'^([a-z])[^a-z]*([a-z])[^a-z]*([a-z])',family.last,flags=re.I) las = ''.join(match.groups()).upper() if not family.hid: # print las clash3 = Families.filter(hid__startswith=las) num = 1 for x in clash3: clashnum = int(x.hid[3:5]) if clashnum >= num: num = clashnum + 1 fhid = '{}{:0>2}'.format(las,num) if not Families.filter(hid=hid): family.hid = fhid family.save() print family.hid, family if family.father and not family.father.hid: family.father.hid = '{}{:0>2}'.format(las,'FA') if family.mother and not family.mother.hid: family.mother.hid = '{}{:0>2}'.format(las,'MO') for student in family.children.all(): if student.hid: continue num = student.birthday.year while True: # First time I've ever needed to use a do-while loop, and it's Python. Sigh. num %= 100 shid = '{}{:0>2}'.format(family.hid,num) num += 1 if not Students.filter(hid=shid): break student.hid = shid student.save() print student.hid, student
def layered_queryset(year): qset = Students.select_related('family').filter( enrollment__course__year=year, enrollment__course__tradition__m=True, enrollment__status__in=[ 'enrolled', 'invoiced', 'need_pay', 'aud_pass', 'aud_pend' ]).distinct() return qset.order_by('family__last', 'family__id', 'birthday')
def repop(self, all_students=None, **kwargs): if self.can_repop(): for enrollment in self.enrollments: enrollment.delete() for student in all_students or Students.all().order_by('family__last','family__name_num','birthday'): if check_eligex(self, student, **kwargs): Enrollments.create(course=self, student=student) self.save() return self.enrollments.count()
def refresh_summary(request, **kwargs): bad = restricted(request, 5) if bad: return bad kwargs.setdefault('year', getyear()) for course in Courses.filter(year__in=[kwargs['year'], kwargs['year'] - 1], tradition__action='trig'): for student in Students.filter(enrollment__course=course): course.trig(student) return redirect('/reports/summary/')
def update_existing_family(already, family): if not already.hid: already.hid = family.accessid if not already.phone: already.phone = family.home if not already.email: already.email = family.email if not already.address: already.address = Addresses.create( line1=family.street, line2='Apt. ' + family.apt if family.apt else '', city=family.city.title(), state=family.state.upper(), zipcode=family.zip, ) if not already.mother: already.mother = Parents.create( first=family.mfirst, # alt_first = family.mnick, alt_last='' if family.mlast == family.family else family.mlast, sex='F', alt_phone=0 if family.mcell == family.home else family.mcell, family_id=already.id, ) if not already.father: already.father = Parents.create( first=family.ffirst, # alt_first = family.fnick, alt_last='' if family.flast == family.family else family.flast, sex='M', alt_phone=0 if family.fcell == family.home else family.fcell, family_id=already.id, ) for student in OldStudents.filter(familyid=family.accessid): alreadyStudent = find_existing_student(already, student) if alreadyStudent: update_existing_student(alreadyStudent, student) else: stu = Students.create( oid=student.id, hid=student.studentid, created_at=student.creationdate, updated_at=student.moddate, family=already, first=namecase(student.first), alt_last='' if (not already) or already.last == student.last else student.last, alt_email='' if (not already) or already.email == student.email else student.email, sex=student.sex, birthday=student.dob if student.dob else '2020-12-31', grad_year=est_grad(student), tshirt=sub(student.tshirt, {'AXL': 'XL'}), needs=student.needsdescribe)
def audition_process(request, **kwargs): me = getme(request) course = Courses.fetch(id=kwargs['id']) bad = restricted(request,5,course,4) if bad: return bad for key in request.POST: if key.isdigit(): student = Students.fetch(id=int(key)) enrollment = student.enrollments.filter(course=course)[0] if request.POST[key] == u'accept': enrollment.accept(me) if request.POST[key] == u'reject': enrollment.reject(me) return redirect('/admin/auditions/')
def mass_enroll(request, **kwargs): bad = restricted(request, 5) if bad: return bad year = kwargs.setdefault('year', getyear()) courses = Courses.filter(year=kwargs['year']) students = [] for x in request.POST: if x.isdigit(): students.append(Students.fetch(id=int(x))) students.sort(key=lambda student: student.last) context = { 'students': students, 'courses': courses, 'status_choices': dict(status_choices).keys(), } return render(request, 'reports/mass_enroll.html', context)
def search_number(number, all_tables=True, **kwargs): results = set([]) if all_tables or kwargs.get('family'): results |= set( Families.filter(Q(Q(id=word) | Q(oid=word) | Q(name_num=word)))) if all_tables or kwargs.get('parent'): results |= set(Parents.filter(id=word)) if all_tables or kwargs.get('student'): results |= set( Students.filter(Q(Q(id=word) | Q(oid=word) | Q(grad_year=word)))) if all_tables or kwargs.get('invoice'): results |= set(Invoices.filter(Q(Q(id=word) | Q(amount=word)))) if all_tables or kwargs.get('course'): results |= set(Courses.filter(year=word)) if all_tables or kwargs.get('address'): results |= set(Addresses.filter(id=word)) return results
def admin(request, **kwargs): bad = restricted(request, 6) if bad: return bad course = Courses.fetch(id=kwargs['id']) results = [] if request.GET.get('enrollment'): enrollment = Enrollments.fetch(id=request.GET['enrollment']) if request.GET.get('move'): new_course = Courses.fetch(id=request.GET['move']) enrollment.course = new_course if request.GET.get('status'): enrollment.status = request.GET['status'] enrollment.save() elif request.GET.get('drop'): enrollment = Enrollments.fetch(id=request.GET['drop']) enrollment.delete() elif request.GET.get('query'): results = search_query(request.GET['query'], all_tables=False, student=True) elif request.GET.get('add'): student = Students.fetch(id=request.GET['add']) Enrollments.create(student=student, course=course) # if request.GET and not request.GET.get('query'): # return redirect('/rest/admin/{model}/{id}'.format(**kwargs)) context = { 'course': course, 'courses': Courses.filter(year=course.year, tradition__e=True, tradition__m=True).order_by('tradition__order'), 'results': results, 'status_choices': Each(status_choices).__getitem__(0), } return render(request, 'rest/admin.html', context)
def students(self): return Students.filter(enrollment__course=self,enrollment__status='enrolled').order_by('family__last','birthday').distinct()
def overview(request, **kwargs): bad = restricted(request, 5) if bad: return bad GET = request.GET.copy() year = GET.setdefault('year', getyear()) if 'repop' in GET: all_students = Students.all() course = Courses.fetch(id=GET['repop']) if course: course.repop(all_students=all_students) return redirect('/reports/overview/?year=' + year) courses = Courses.filter(year=year).order_by('tradition__order') nTickets = { 'SB': [len(Courses.fetch(tradition__id='KB', year=year).students), 0, 0], 'SC': [len(Courses.fetch(tradition__id='KC', year=year).students), 0, 0], 'SG': [ len(Courses.fetch(tradition__id='KG', year=year).students), len(Courses.fetch(tradition__id='KS', year=year).students), len(Courses.fetch(tradition__id='KW', year=year).students), ], 'SJ': [ len(Courses.fetch(tradition__id='KJ', year=year).students), len(Courses.fetch(tradition__id='KT', year=year).students), len(Courses.fetch(tradition__id='KX', year=year).students), ], 'SH': [ len(Courses.fetch(tradition__id='KH', year=year).students), len(Courses.fetch(tradition__id='KU', year=year).students), len(Courses.fetch(tradition__id='KY', year=year).students), ], 'SR': [ len(Courses.fetch(tradition__id='KR', year=year).students), len(Courses.fetch(tradition__id='KV', year=year).students), len(Courses.fetch(tradition__id='KZ', year=year).students), ], } tSlots = 0 tFilled = 0 tuitionRev = 0 for course in courses.filter(tradition__e=True, tradition__m=True): tSlots += course.tradition.nSlots tFilled += len(course.students) tuitionRev += course.revenue context = { 'date': datetime.datetime.now(), 'year': Year(year), 'ar': '{:02}'.format(int(year) % 100), 'real': courses.filter(tradition__e=True, tradition__m=True), 'auto': courses.filter(tradition__e=True, tradition__m=False), 'stat': Courses.filter(tradition__r=False, year=year).order_by('tradition__order'), 'rf': Courses.fetch(tradition__id='RF', year=year), 'tSlots': tSlots, 'tFilled': tFilled, 'tuitionRev': tuitionRev, 'total': { 'SB': nTickets['SB'][0] * 10, 'SC': nTickets['SC'][0] * 10, 'SG': nTickets['SG'][0] * 10 + nTickets['SG'][1] * 15 + nTickets['SG'][2] * 20, 'SH': nTickets['SH'][0] * 10 + nTickets['SH'][1] * 15 + nTickets['SH'][2] * 20, 'SJ': nTickets['SJ'][0] * 10 + nTickets['SJ'][1] * 15 + nTickets['SJ'][2] * 20, 'SR': nTickets['SR'][0] * 10 + nTickets['SR'][1] * 15 + nTickets['SR'][2] * 20, '10': sum(Each(nTickets.values()).__getitem__(0)), '15': sum(Each(nTickets.values()).__getitem__(1)), '20': sum(Each(nTickets.values()).__getitem__(2)), 'tt': sum(Each(nTickets.values()).__getitem__(0)) * 10 + sum(Each(nTickets.values()).__getitem__(1)) * 15 + sum(Each(nTickets.values()).__getitem__(2)) * 20, }, 'latest_enrollment': Enrollments.all().order_by('-updated_at')[0] } prepaidRev = context['prepaidRev'] = context['total']['tt'] * 10 context['totalRev'] = prepaidRev + context['tuitionRev'] + context[ 'rf'].revenue return render(request, 'reports/overview.html', context)
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/')
def courses_enroll(request, **kwargs): student_id = kwargs.setdefault('id',0) student = Students.fetch(id=student_id) course = Courses.fetch(id=request.GET['course_id']) course.cart(student) return redirect('/{}/classes/{}'.format(kwargs.get('ref'),student_id))
def students_toggle_enrollments(self): result = [] for enrollment in self.enrollments: result.append({'widget':enrollment,'static':Students.get(id=enrollment.student_id)}) return result
def check_word(trad, student, word, **kwargs): # Set defaults for omitted keyword arguments year = kwargs.setdefault('year', None) aud = kwargs.setdefault('aud', False) cur = kwargs.setdefault('cur', False) debug = kwargs.setdefault('debug', TRACE) if kwargs.get('debug'): if str(type(kwargs['debug']) ) == "<class 'django.core.handlers.wsgi.WSGIRequest'>": request = kwargs['debug'] log( request, " ".join([ 'CHECK WORD:', str(trad), str(student), str(word), kwargle(kwargs) ])) else: print 'CHECK WORD:', trad, student, word, kwargle(kwargs) # Boolean literals if '#' in word: return True elif '~' in word: return False # Sex literals elif 'm' in word: return student.sex == 'M' elif 'f' in word: return student.sex == 'F' # Check age elif 'a' in word: old_enough = student.hst_age_in(year) >= trad.min_age - word.count('y') young_enough = student.hst_age_in( year) <= trad.max_age + word.count('o') return old_enough and young_enough # Check grade elif 'g' in word: if not student.grad_year: return True old_enough = student.grade_in(year) >= trad.min_grd - word.count('y') young_enough = student.grade_in(year) <= trad.max_grd + word.count('o') return old_enough and young_enough # If you get this far, you're going to query the database else: query = { 'student': student, 'status__in': ["enrolled", "invoiced", "need_pay", "aud_pass", "maydefer"], } # Check for auditions if word == '@': if aud: return True else: query.update({ 'status': "aud_pass", 'course__tradition': trad, 'course__year': year, }) if kwargs.get('debug'): if str(type(kwargs['debug']) ) == "<class 'django.core.handlers.wsgi.WSGIRequest'>": request = kwargs['debug'] log(request, query) else: print query return bool( Enrollments.filter(**query).exclude(student=student, course__tradition=trad, course__year=year)) if 'e' in word: query['course__tradition__e'] = True if 'u' in word: query['course__tradition__m'] = True # If the word is '**', then you don't care what course it is, as long as it's this year. if word.count('*') == 2: if kwargs['cur']: return True query['course__year'] = year if kwargs.get('debug'): if str(type(kwargs['debug']) ) == "<class 'django.core.handlers.wsgi.WSGIRequest'>": request = kwargs['debug'] log(request, query) else: print query return bool( Enrollments.filter(**query).exclude(student=student, course__tradition=trad, course__year=year)) # Support * for representing any character if '*' not in word: query['course__tradition__id'] = word[0:2] elif word[0] != '*': query['course__tradition__id__startswith'] = word[0] elif word[1] != '*': query['course__tradition__id__endswith'] = word[1] if not kwargs['cur'] and '/' not in word: query['course__year'] = year if '/' in word: query['course__year__lt'] = year if '$' in word: query.pop('status__in') query['status'] = "enrolled" if '+' in word: query.pop('student') query['student__family'] = student.family query_result = Enrollments.filter(**query).exclude( student=student, course__tradition=trad, course__year=year) if kwargs.get('debug'): if str(type(kwargs['debug']) ) == "<class 'django.core.handlers.wsgi.WSGIRequest'>": request = kwargs['debug'] log(request, query) log(request, query_result) else: print query print query_result if '+' in word: return len( Students.filter( enrollment__in=query_result).distinct()) >= word.count('+') else: return bool(query_result)
def search_word(word, all_tables=True, **kwargs): results = set([]) if all_tables or kwargs.get('family'): results |= set( Families.filter( Q( Q(hid=word) | Q(last__icontains=word) | Q(phone__icontains=word) | Q(phone_type=word) | Q(email__icontains=word)))) if all_tables or kwargs.get('parent'): results |= set( Parents.filter( Q( Q(hid=word) | Q(first__icontains=word) | Q(alt_last__icontains=word) | Q(alt_phone__icontains=word) | Q(phone_type=word) | Q(alt_email__icontains=word)))) if all_tables or kwargs.get('student'): results |= set( Students.filter( Q( Q(hid=word) | Q(first__icontains=word) | Q(alt_first__icontains=word) | Q(family__last__icontains=word) | Q(alt_last__icontains=word) | Q(alt_phone__icontains=word) | Q(alt_email__icontains=word) | Q(needs__icontains=word)))) if all_tables or kwargs.get('coursetrad'): results |= set( CourseTrads.filter( Q( Q(id=word) | Q(oid=word) | Q(title__icontains=word) | Q(alias_id=word) | Q(eligex__icontains=word) | Q(default=word) | Q(action=word)))) if all_tables or kwargs.get('course'): results |= set( Courses.filter( Q( Q(id=word) | Q(tradition__id=word) | Q(tradition__oid=word) | Q(title__icontains=word) | Q(tradition__title__icontains=word) | Q(tradition__alias_id=word) | Q(tradition__eligex__icontains=word) | Q(tradition__default=word) | Q(tradition__action__icontains=word)))) if all_tables or kwargs.get('address'): results |= set( Addresses.filter( Q( Q(line1__icontains=word) | Q(line2__icontains=word) | Q(city__icontains=word) | Q(state__icontains=word) | Q(zipcode__icontains=word)))) if all_tables or kwargs.get('venue'): results |= set(Venues.filter(name__icontains=word)) if all_tables or kwargs.get('enrollment'): results |= set(Enrollments.filter(role__icontains=word)) if all_tables or kwargs.get('user'): results |= set(Users.filter(username__icontains=word)) if all_tables or kwargs.get('teacher'): results |= set( Teachers.filter( Q( Q(first__icontains=word) | Q(last__icontains=word) | Q(phone__icontains=word) | Q(email__icontains=word)))) if all_tables or kwargs.get('invoice'): results |= set( Invoices.filter( Q( Q(family__last__icontains=word) | Q(enrollment__course__title__icontains=word) | Q(paypal__txn_id__icontains=word)))) return results