def students_new(request, ref): me = getme(request) if not me or not me.owner or not (me.owner.mother or me.owner.father): return redirect('/register/redirect/') students = list(me.owner.children.all()) students.append(new_student) print students context = { 'new': True, 'reg_year': getyear(), 'family': me.owner, 't_shirt_sizes': collect( Students.model.t_shirt_sizes, lambda obj: dict( collect(obj, lambda val, index: ['no' + str(index), val]))), 'students': students, 'ref': ref, 'current_student': new_student, 'e': request.session['e'], 'p': request.session['p'], } return render(request, 'students2.html', context)
def students_edit(request, ref, id): me = getme(request) if not me or not me.owner or not (me.owner.mother or me.owner.father): return redirect('/register/redirect/') student = Students.fetch(id=id) if not student: return redirect('/{}/students/new/'.format(ref)) forminit(request, 'student', student_fields, obj=student) context = { 'new': False, 'reg_year': getyear(), 'family': me.owner, 't_shirt_sizes': collect( Students.model.t_shirt_sizes, lambda obj: dict( collect(obj, lambda val, index: ['no' + str(index), val]))), 'students': me.owner.children.all(), 'ref': ref, 'current_student': student, 'e': request.session['e'], 'p': request.session['p'], } return render(request, 'students2.html', context)
def courses(request, **kwargs): me = getme(request) if not me or not me.owner or not me.owner.children_eligible_in(getyear()): return redirect('/register') if request.GET.get('action') == u'add': return add(request, **kwargs) elif request.GET.get('action') == u'defer': return defer(request, **kwargs) current_id = kwargs.setdefault('id',0) if 'id' in kwargs: current_id = kwargs['id'] current_student = me.owner.children.fetch(id=current_id) if not current_student: return redirect('/') else: current_student = me.owner.children_eligible_in(getyear())[0] reg_year = getyear() cart = me.owner.enrollments_in(reg_year).filter(course__tradition__e=True) cart = cart.filter(status__in=['aud_pass','aud_pend','invoiced','deferred','maydefer','enrolled','need_pay','pend_pub','aud_lock']) cart_pend = cart.filter(status__in=['aud_pend','invoiced','deferred']) cart_paid = cart.filter(status='enrolled') # cart_unpaid = cart.difference(cart_pend,cart_paid) # Use this line instead of next in Django 1.11+ cart_unpaid = cart.exclude(status__in=['aud_pend','invoiced','deferred','enrolled']) volunteer_total = me.owner.volunteer_total_in(reg_year) context = { 'ref':kwargs.get('ref'), 'invoiceable' : bool(cart_unpaid), # TODO: This is simple enough now to be calculated in the HTML page 'reg_year': reg_year, 'family' : me.owner, 'students': equip(me.owner.children_eligible_in(getyear()), lambda student: student.hst_age_in(reg_year), attr='age'), 'current_student' : current_student, 'menu' : current_student.course_menu(year=reg_year), 'cart' : cart, 'nCourses': { 'total' : len(cart), 'pend' : len(cart_pend), 'paid' : len(cart_paid), 'unpaid': len(cart_unpaid), }, 'tuition' : { 'total' : sum(collect(cart, lambda enr: enr.course.tuition())), 'pend' : sum(collect(cart_pend, lambda enr: enr.course.tuition())), 'paid' : sum(collect(cart_paid, lambda enr: enr.course.tuition())), 'unpaid': sum(collect(cart_unpaid, lambda enr: enr.course.tuition())), }, 'hours' : { 'total' : me.owner.volunteer_total_in(reg_year), 'pend' : me.owner.hours_signed_in(reg_year), 'paid' : me.owner.hours_worked_in(reg_year), 'unpaid': me.owner.volunteer_total_in(reg_year) - me.owner.hours_signed_in(reg_year) - me.owner.hours_worked_in(reg_year), }, } return render(request, 'courses.html', context)
def new_merge(request): if 'query' in request.GET: return merge_search(request) elif 'model' not in request.GET: return render(request, 'rest/merge_search.html') fargs = collect(request.GET, str) return redirect('/rest/merge/{model}/{old_id}/{new_id}/'.format(**fargs))
def sub_delete(request, model, old_id, new_id): fargs = collect(request.GET, str) manager = MODELS[fargs['model']] thing = manager.get(id=fargs['sub_id']) thing.delete() return redirect( '/rest/merge/{}/{}/{}/sub_move/?field={field}&reflex={reflex}'.format( model, old_id, new_id, **fargs))
def index(request, model): bad = restricted(request) if bad: return bad columns = [] for ftp in FIELDS[model]: field = ftp['field'] columns.append(field) query = collect(request.GET, str) order_by = query.get('order_by') if order_by: query.pop('order_by') limit = query.get('limit') offset = query.get('offset') limit = int(limit) if limit else None offset = int(offset) if offset else None if limit and offset: query.pop('limit') query.pop('offset') qset = MODELS[model].filter(**query)[offset:offset + limit] elif limit: query.pop('limit') qset = MODELS[model].filter(**query)[:limit] elif offset: query.pop('offset') qset = MODELS[model].filter(**query)[offset:] else: qset = MODELS[model].filter(**query) if order_by: qset = qset.order_by(order_by) display = [] for thing in qset: dthing = [ '<a href="/rest/show/{}/{}">{}</a>'.format(model, thing.id, thing.id) ] for ftp in FIELDS[model]: field = ftp['field'] value = thing.__getattribute__(field) template = ftp['template'] value = value if value else template.default dthing.append(template.static(field, value)) display.append(dthing) context = { 'columns': columns, 'display': display, 'model': model, 'Model': sub(model, { 'coursetrad': 'Course Tradition' }).title() } return render(request, 'rest/index.html', context)
def sub_transfer(request, model, old_id, new_id): fargs = collect(request.GET, str) manager = MODELS[fargs['model']] thing = manager.get(id=fargs['sub_id']) dest = fargs['dest'] if fargs['reflex'][-3:] != '_id': dest_manager = MODELS[model] dest = dest_manager.get(id=fargs['dest']) thing.__setattr__(fargs['reflex'], dest) thing.save() return redirect( '/rest/merge/{}/{}/{}/sub_move/?field={field}&reflex={reflex}'.format( model, old_id, new_id, **fargs))
def invoice_index(request, family_id): family = Families.fetch(id=family_id) bad = restricted(request, 5, family) if bad: return bad invoices = Invoices.filter(family=family) context = { 'hist': collect( gethist(0), lambda year: { 'year': year, 'invoices': Invoices.filter(year=year, family=family) }) } return render(request, 'invoice_index.html', context)
def forgot_password(request, path): if request.method == 'GET': return render(request, 'main/forgot_password.html') elif request.method == 'POST': user = find_user(request.POST['username']) context = { 'user': user, 'all_emails': collect( user.all_emails, lambda e, n: { 'email': '{}*****{}'.format(*(re.match(r'^(.{3}).*(@\w+\.\w+)$', e). groups())), 'num': n, }), } return render(request, 'main/send_password.html', context) else: return HttpResponse("Unrecognized HTTP Verb", status=405)
def volunteer_total_in(self, year): return max([0.0] + list( collect(self.enrollments_in(year), lambda enr: enr.course.vol_hours)))