Exemplo n.º 1
0
def group_to_json_full(group):
    return {
        'id':
        group.id,
        'name':
        group.name,
        'teacherId':
        group.teacher.id,
        'teacherFio':
        group.teacher.fio,
        'sectionId':
        group.section.id,
        'sectionName':
        group.section.name,
        'startMonth':
        group.start_month,
        'endMonth':
        group.end_month,
        'studentsCount':
        group.students.count(),
        'lessonsDoneCount':
        group.lessons.filter(Lesson.date <= datetime.date.today()).count(),
        'sumNotConfirmed':
        group.sum_not_confirmed or 0 if can_user_write_group(
            g.current_user_app, group) else -1,
        'schedule': [
            schedule_group_to_json_brief(schedule_group)
            for schedule_group in group.schedule_groups.join(
                ScheduleTime, ScheduleTime.id == ScheduleGroup.schedule_time_id
            ).order_by(ScheduleGroup.day_of_week,
                       coalesce(ScheduleTime.time, '25:59'), ScheduleTime.id)
        ]
    }
Exemplo n.º 2
0
 def decorated_function(*args, **kwargs):
     group_id = kwargs.get('group_id') or request.args.get(
         'group_id', 0, type=int)
     if not can_user_write_group(current_user,
                                 Group.query.get_or_404(group_id)):
         abort(403)
     return f(*args, **kwargs)
Exemplo n.º 3
0
def students_in_group(group_id):
    group = Group.query.get_or_404(group_id)
    students_in_group = group.students_in_group_by_fio.all()
    in_group_students_ids = [s.student.id for s in students_in_group]
    if 'submit' in request.form:
        if not can_user_write_group(current_user, group): abort(403)
        form_in_group = [int(i) for i in request.form.getlist('in_group')]
        for new_id in form_in_group:
            if new_id not in in_group_students_ids:
                db.session.add(
                    StudentInGroup(student_id=new_id,
                                   group_id=group_id,
                                   enter_month=group.start_month,
                                   exit_month=group.end_month))
        for old_student_in_group in students_in_group:
            if old_student_in_group.student.id not in form_in_group:
                if old_student_in_group.attendings_was.count() > 0:
                    flash(
                        'нельзя удалить {} из группы {}, так как он посещал занятия!'
                        .format(old_student_in_group.student.fio, group.name))
                elif old_student_in_group.payments_confirmed.count() > 0:
                    flash(
                        'нельзя удалить {} из группы {}, так как он вносил подтвержденную оплату!'
                        .format(old_student_in_group.student.fio, group.name))
                else:
                    old_student_in_group.payments_not_confirmed.delete()
                    for a in old_student_in_group.attendings_was_not.all():
                        db.session.delete(a)
                    old_student_in_group.notifications.delete()
                    db.session.delete(old_student_in_group)
        flash('список учеников в группе {} изменен.'.format(group.name))
        return redirect(url_for('structure.groups_list'))
    other_students = Student.query \
        .filter(Student.id.notin_(in_group_students_ids)) \
        .order_by(Student.fio) \
        .all()
    return render_template('structure/students_in_group.html',
                           group=group,
                           students_in_group=students_in_group,
                           other_students=other_students,
                           write_mode=can_user_write_group(
                               current_user, group))
Exemplo n.º 4
0
def delete_lesson(lesson_id):
    lesson = Lesson.query.get_or_404(lesson_id)
    # can't use @check_access_group_write() because no 'group_id' param.
    if not can_user_write_group(current_user, lesson.group): abort(403)
    if not is_lesson_removable(lesson): abort(409)
    if current_user.is_teacher and lesson.date < datetime.date.today():
        abort(409)
    for a in lesson.attendings_was_not:
        db.session.delete(a)
    db.session.delete(lesson)
    flash('занятие {} в {} удалено'.format(lesson.date, lesson.group.name))
    return redirect_back_or_home()
Exemplo n.º 5
0
def payments_in_group(group_id):
    group = Group.query.get_or_404(group_id)
    if not can_user_write_group(g.current_user_app, group):
        abort(403)

    create_payments_for_all_students(group)

    payments = group.payments \
        .join(Student, Student.id == StudentInGroup.student_id) \
        .order_by(Student.fio)

    return jsonify(group=group_to_json_brief(group),
                   payments=[payment_to_json(p) for p in payments])
Exemplo n.º 6
0
def student_in_group_to_json(student_in_group):
    with_discount = can_user_write_group(g.current_user_app,
                                         student_in_group.group)
    return {
        'id': student_in_group.id,
        'systemUserEnabled': student_in_group.student.system_user.enabled,
        'studentId': student_in_group.student_id,
        'studentFio': student_in_group.student.fio,
        'groupId': student_in_group.group.id,
        'discount': student_in_group.discount if with_discount else -1,
        'enterMonth': student_in_group.enter_month,
        'exitMonth': student_in_group.exit_month,
        'lessonsAttendedCount': student_in_group.attendings_was.count(),
    }
Exemplo n.º 7
0
def group_to_json_brief(group):
    return {
        'id':
        group.id,
        'name':
        group.name,
        'teacherId':
        group.teacher.id,
        'teacherFio':
        group.teacher.fio,
        'startMonth':
        group.start_month,
        'endMonth':
        group.end_month,
        'sumNotConfirmed':
        group.sum_not_confirmed or 0 if can_user_write_group(
            g.current_user_app, group) else -1,
    }
Exemplo n.º 8
0
def save_attending_state():
    def int_or_400(key):
        if key not in request.json:
            abort(400)
        try:
            return int(str(request.json[key]))
        except ValueError:
            abort(400)

    attending_id = int_or_400('attendingId')
    attending_state = int_or_400('attendingState')
    if attending_state not in attending_states:
        abort(400)

    attending = Attending.query.get_or_404(attending_id)

    if not can_user_write_group(g.current_user_app, attending.lesson.group):
        abort(403)

    attending.state = attending_state
    return 'ok'
Exemplo n.º 9
0
def save_payment():
    def int_or_400(key):
        if key not in request.json:
            abort(400)
        try:
            return int(str(request.json[key]))
        except ValueError:
            abort(400)

    payment_id = int_or_400('paymentId')
    value = int_or_400('value')
    comment = request.json.get('comment')
    is_cash = str(request.json.get('cash')) == 'True'
    is_confirmed = str(request.json.get('confirmed')) == 'True'

    payment = Payment.query.get_or_404(payment_id)

    if not can_user_write_group(g.current_user_app,
                                payment.student_in_group.group):
        abort(403)

    can_confirm = g.current_user_app.is_master

    if not can_confirm and payment.confirmed:
        abort(403)

    if value < 0: value = 0
    max_value = payment.max_value
    if value > max_value: value = max_value

    if comment is None: comment = ''
    comment = comment[:32]

    payment.value = value
    payment.comment = comment
    payment.cash = is_cash

    if can_confirm: payment.confirmed = is_confirmed

    return 'ok'
Exemplo n.º 10
0
def lessons_in_month(group_id, month_number):
    group = Group.query.get_or_404(group_id)
    if month_number < group.start_month or month_number > group.end_month:
        abort(404)
    month_name = get_month_name(month_number)
    students_in_group = group.students_in_group_in_month(month_number) \
        .join(Student, Student.id == StudentInGroup.student_id) \
        .order_by(Student.fio) \
        .all()
    if 'submit' in request.form:
        if not can_user_write_group(current_user, group): abort(403)
        ls = Lesson.lessons_in_group_in_month(group_id, month_number).all()
        ps = Payment.query \
            .join(StudentInGroup, StudentInGroup.id == Payment.student_in_group_id) \
            .filter(StudentInGroup.group_id == group_id, Payment.month == month_number)
        payments = dict()
        for p in ps:
            payments[p.student_in_group_id] = p
        for student_in_group in students_in_group:
            new_value = request.form.get('p_{}'.format(student_in_group.id),
                                         0,
                                         type=int)
            if new_value < 0: new_value = 0
            max_value = group.section.price - student_in_group.discount
            if new_value > max_value: new_value = max_value
            payment = payments.get(student_in_group.id)
            is_cash = 'c_{}'.format(student_in_group.id) in request.form
            comment = request.form.get(
                'comment_{}'.format(student_in_group.id), '')
            if payment is not None:
                if not payment.confirmed:
                    payment.value = new_value
                    payment.cash = is_cash
                    payment.comment = comment
            else:
                db.session.add(
                    Payment(student_in_group=student_in_group,
                            month=month_number,
                            value=new_value,
                            cash=is_cash,
                            comment=comment))
            attendings = dict()
            for l in ls:
                attendings[l.id] = dict()
                for a in l.attendings:
                    attendings[l.id][a.student_id] = a
            for l in ls:
                attending = attendings[l.id].get(student_in_group.student_id)
                a_key = 'a_{}_{}'.format(l.id, student_in_group.student_id)
                new_state = request.form.get(a_key,
                                             attending_was_not,
                                             type=int)
                if attending is not None:
                    attending.state = new_state
                else:
                    db.session.add(
                        Attending(lesson=l,
                                  student=student_in_group.student,
                                  state=new_state))
        flash('посещения и оплата в группе {} за {} сохранены.'.format(
            group.name, month_name))
        return redirect(
            url_for('lessons.lessons_in_month',
                    group_id=group_id,
                    month_number=month_number))
    pd = payments_in_month_info(group_id, month_number)
    ll = lessons_lists(group_id, month_number)
    return render_template('lessons/lessons_in_month.html',
                           group=group,
                           month_number=month_number,
                           month_name=month_name,
                           students_in_group=students_in_group,
                           payments=pd.values,
                           confirmed=pd.confirmed,
                           cash=pd.cash,
                           comments=pd.comments,
                           lessons=ll[0],
                           attendings_states=ll[1],
                           write_mode=can_user_write_group(
                               current_user, group))
Exemplo n.º 11
0
def group_details(group_id):
    group = Group.query.get_or_404(group_id)
    students_in_group = group.students_in_group_by_fio.all()
    can_edit_discount = current_user.is_master
    if 'submit' in request.form:
        if not can_user_write_group(current_user, group): abort(403)
        for student_in_group in students_in_group:
            new_enter_month_number = request.form.get('enter_{}'.format(
                student_in_group.id),
                                                      0,
                                                      type=int)
            new_exit_month_number = request.form.get('exit_{}'.format(
                student_in_group.id),
                                                     0,
                                                     type=int)
            if new_enter_month_number > new_exit_month_number:
                new_enter_month_number = new_exit_month_number
            if new_enter_month_number is not None:
                if new_enter_month_number < group.start_month:
                    flash('ученик не может войти раньше начала группы ({})'.
                          format(student_in_group.student.fio))
                elif new_enter_month_number > group.end_month:
                    flash(
                        'ученик не может войти позже конца группы ({})'.format(
                            student_in_group.student.fio))
                elif new_enter_month_number > (
                        max_enter_month_number_student_in_group(
                            student_in_group) or 999999):
                    flash(
                        'ученик не может войти позже посещения или оплаты ({})'
                        .format(student_in_group.student.fio))
                else:
                    student_in_group.enter_month = new_enter_month_number
            if new_exit_month_number is not None:
                if new_exit_month_number < group.start_month:
                    flash('ученик не может выйти раньше начала группы ({})'.
                          format(student_in_group.student.fio))
                elif new_exit_month_number > group.end_month:
                    flash(
                        'ученик не может выйти позже конца группы ({})'.format(
                            student_in_group.student.fio))
                elif new_exit_month_number < (
                        min_exit_month_number_student_in_group(
                            student_in_group) or -1):
                    flash(
                        'ученик не может выйти раньше посещения или оплаты ({})'
                        .format(student_in_group.student.fio))
                else:
                    student_in_group.exit_month = new_exit_month_number
            delete_unconfirmed_payments_out_of_months_period_student(
                student_in_group)
            delete_attendings_was_not_out_of_months_period_student(
                student_in_group)
            if can_edit_discount:
                new_discount = request.form.get('d_{}'.format(
                    student_in_group.id),
                                                0,
                                                type=int)
                if new_discount is not None:
                    if new_discount > group.section.price:
                        flash(
                            'скидка не может быть больше стоимости({})'.format(
                                student_in_group.student.fio))
                    elif new_discount < 0:
                        flash('скидка не может быть меньше нуля! ({})'.format(
                            student_in_group.student.fio))
                    else:
                        student_in_group.discount = new_discount
        if can_edit_discount:
            flash('скидки и месяцы входа/выхода в группе {} изменены.'.format(
                group.name))
        else:
            flash('месяцы входа/выхода в группе {} изменены.'.format(
                group.name))
        return redirect(url_for('structure.groups_list'))
    return render_template('structure/group_details.html',
                           group=group,
                           students_in_group=students_in_group,
                           can_edit_discount=can_edit_discount)