Esempio n. 1
0
def available_hours(teacher_id):
    data = flask.request.get_json()
    teacher = Teacher.get_by_id(teacher_id)
    duration = data.get("duration")
    if duration:
        duration = int(duration)
    only_approved = False
    student = None
    if current_user.teacher:
        only_approved = True
    else:
        student = current_user.student

    places = (data.get("meetup_place_id",
                       None), data.get("dropoff_place_id", None))
    return {
        "data":
        list(
            teacher.available_hours(
                datetime.strptime(data.get("date"), WORKDAY_DATE_FORMAT),
                student=student,
                duration=duration,
                only_approved=only_approved,
                places=places,
            ))
    }
Esempio n. 2
0
def cars(teacher_id):
    teacher = Teacher.get_by_id(teacher_id)
    if not teacher:
        raise RouteError("Teacher not found.")
    return {
        "data":
        [car.to_dict() for car in Car.query.filter_by(teacher=teacher).all()]
    }
Esempio n. 3
0
def test_invalid_login_to_ezcount(auth, requester):
    new_user = User.create(
        email="[email protected]", password="******", name="absolutely", area="nope"
    )
    new_teacher = Teacher.create(
        user=new_user, is_approved=True, price=100, lesson_duration=40, crn=999999999
    )
    auth.login(email=new_user.email, password="******")
    resp = requester.get("/teacher/ezcount?redirect=backoffice/expenses")
    assert "does not have an invoice account" in resp.json["message"]
Esempio n. 4
0
def teachers():
    try:
        extra_filters = {User: {"name": like_filter}}
        query = Teacher.query.filter_by(is_approved=True)
        return Teacher.filter_and_sort(
            flask.request.args,
            extra_filters=extra_filters,
            query=query,
            with_pagination=True,
        )
    except ValueError:
        raise RouteError("Wrong parameters passed.")
Esempio n. 5
0
def test_ezcount_create_user(auth, requester, teacher):
    auth.login(email=teacher.user.email)
    resp = requester.get("/teacher/ezcount_user")
    assert "already has" in resp.json["message"]
    auth.logout()

    new_user = User.create(
        email="[email protected]", password="******", name="absolutely", area="nope"
    )
    new_teacher = Teacher.create(
        user=new_user, is_approved=True, price=100, lesson_duration=40, crn=999999999
    )
    auth.login(email=new_teacher.user.email, password="******")
    resp = requester.get("/teacher/ezcount_user")
    assert "successfully" in resp.json["message"]
Esempio n. 6
0
def test_filter_and_sort(user, teacher, student, meetup, dropoff):
    """test that limit is maxed to 100, base query, custom date, non allowed filters"""
    date = datetime.utcnow() + timedelta(days=100)
    for x in range(101):
        user = User.create(email=f"{x}@test.com",
                           password="******",
                           name="teacher",
                           area="test")
        teacher = Teacher.create(user=user,
                                 price=100,
                                 lesson_duration=40,
                                 is_approved=True,
                                 crn=9)
        Appointment.create(
            teacher=teacher,
            student=student,
            creator=student.user,
            duration=40,
            date=date,
            meetup_place=meetup,
            dropoff_place=dropoff,
        )

    args = MultiDict([("teacher_id", teacher.id)])  # not allowed
    query = None
    lessons_from_db = Appointment.filter_and_sort(args, query=query)
    assert len(lessons_from_db) == 102
    args = MultiDict([("date", date.strftime(WORKDAY_DATE_FORMAT))])
    lessons_from_db = Appointment.filter_and_sort(
        args,
        query=query,
        custom_date=lambda x: datetime.strptime(x, WORKDAY_DATE_FORMAT),
    )
    assert not lessons_from_db
    query = Appointment.query.filter_by(teacher_id=3)
    args = MultiDict()
    lessons_from_db = Appointment.filter_and_sort(args, query=query)
    assert len(lessons_from_db) == 1
    query = None
    args = MultiDict([("limit", 100_000_000_000_000)])
    lessons_from_db = Appointment.filter_and_sort(args,
                                                  query=query,
                                                  with_pagination=True)
    assert len(lessons_from_db.items) == 100
Esempio n. 7
0
def make_student():
    data = flask.request.args
    user = current_user
    teacher = Teacher.get_by_id(data.get("teacher_id"))
    if current_user.teacher:
        user = User.get_by_id(data.get("user_id"))
        teacher = current_user.teacher

    if not user:
        raise RouteError("User was not found.", 401)
    if user.teacher or user.student:
        raise RouteError("User is already a student or a teacher.")

    if not teacher:
        raise RouteError("Teacher was not found.")

    try:
        price = int(data.get("price", ""))
    except ValueError:
        price = None
    student = Student.create(user=user,
                             teacher=teacher,
                             creator=current_user,
                             price=price)
    # send notification
    user_to_send_to = student.user
    body_text = gettext("%(teacher)s added you as a student!",
                        teacher=teacher.user.name)
    if student.creator == user_to_send_to:
        user_to_send_to = teacher.user
        body_text = gettext("%(student)s added you as a teacher!",
                            student=student.user.name)
    if user_to_send_to.firebase_token:
        logger.debug(f"sending fcm to {user_to_send_to}")
        try:
            FCM.notify(
                token=user_to_send_to.firebase_token,
                title=gettext("Join Request"),
                body=body_text,
            )
        except:
            pass
    return {"data": student.to_dict()}, 201
Esempio n. 8
0
def make_teacher():
    data = flask.request.get_json()

    if not current_user or current_user.student or current_user.teacher:
        raise RouteError("User was not found.")

    price = data.get("price")
    if not price:
        raise RouteError("Empty fields.")

    if price <= 0:
        raise RouteError("Price must be above 0.")

    teacher = Teacher.create(
        user=current_user,
        price=price,
        lesson_duration=data.get("lesson_duration"),
        crn=data.get("crn"),
    )
    return {"data": teacher.to_dict()}, 201
Esempio n. 9
0
def test_invalid_add_receipt(auth, requester, student, teacher):
    auth.login(email=teacher.user.email)
    resp = requester.get("/teacher/payments/1000/receipt")
    assert resp.status_code == 404
    auth.logout()
    new_user = User.create(
        email="[email protected]", password="******", name="absolutely", area="nope"
    )
    new_teacher = Teacher.create(
        user=new_user, is_approved=True, price=100, lesson_duration=40, crn=999999999
    )
    auth.login(email=new_user.email, password="******")
    payment = Payment.create(
        teacher=new_teacher,
        amount=new_teacher.price,
        student=student,
        payment_type=PaymentType.cash,
        details="test",
        crn=1101,
    )
    resp = requester.get(f"/teacher/payments/{payment.id}/receipt")
    assert "does not have an invoice account" in resp.json["message"]
Esempio n. 10
0
def test_teachers(auth, teacher, requester):
    new_user = User.create(
        email="[email protected]", password="******", name="absolutely", area="nope"
    )
    new_teacher = Teacher.create(
        user=new_user, is_approved=True, price=100, lesson_duration=40, crn=1
    )
    auth.login()
    resp = requester.get("/teacher/")
    first_length = len(resp.json["data"])
    assert resp.json["data"][1]["teacher_id"] == new_teacher.id
    resp = requester.get("/teacher/?order_by=created_at desc")
    assert resp.json["data"][0]["teacher_id"] == new_teacher.id
    resp = requester.get("/teacher/?name=solut")
    assert resp.json["data"][0]["teacher_id"] == new_teacher.id
    resp = requester.get("/teacher/?name=le:no way")
    assert not resp.json["data"]
    resp = requester.get("/teacher/?limit=1")
    assert len(resp.json["data"]) == 1
    new_teacher.is_approved = False
    resp = requester.get("/teacher/")
    assert len(resp.json["data"]) == first_length - 1
Esempio n. 11
0
def test_make_student(user, auth, requester):
    auth.login()
    resp = requester.get(f"/user/make_student?teacher_id=1")
    assert resp.json["data"]["my_teacher"]["teacher_id"] == 1
    teacher = Teacher.get_by_id(1)
    assert resp.json["data"]["price"] == teacher.price
Esempio n. 12
0
def approve(teacher_id):
    if not current_user.is_admin:
        raise RouteError("Not authorized.", 401)
    teacher = Teacher.get_by_id(teacher_id)
    teacher.update(is_approved=True)
    return {"data": teacher.to_dict()}
Esempio n. 13
0
def setup_db(app):
    User.create(email="*****@*****.**",
                password="******",
                name="test",
                area="test",
                phone="044444444")
    User.create(
        email="*****@*****.**",
        password="******",
        name="admin",
        area="test",
        is_admin=True,
        phone="055555555",
    )
    teacher_user = User.create(email="*****@*****.**",
                               password="******",
                               name="teacher",
                               area="test")
    teacher = Teacher.create(
        user=teacher_user,
        price=100,
        lesson_duration=40,
        is_approved=True,
        crn=999999999,
        invoice_api_key=DEMO_API_KEY,
    )
    Car.create(teacher=teacher, number=1111111111)
    student_user = User.create(email="*****@*****.**",
                               password="******",
                               name="student",
                               area="test")
    student = Student.create(user=student_user,
                             teacher=teacher,
                             creator=teacher.user,
                             is_approved=True)
    meetup = Place.create(
        description="test",
        used_as=PlaceType.meetup.value,
        student=student,
        google_id="ID1",
    )
    dropoff = Place.create(
        description="test",
        used_as=PlaceType.dropoff.value,
        student=student,
        google_id="ID2",
    )
    WorkDay.create(
        teacher=teacher,
        day=1,
        from_hour=0,
        to_hour=23,
        to_minutes=59,
        on_date=(datetime.utcnow() + timedelta(days=2)).date(),
    )  # 2 days from now
    Topic.create(title="topic test", min_lesson_number=1, max_lesson_number=5)
    Appointment.create(
        teacher=teacher,
        student=student,
        # schedule to 5 days from now to it won't bother with no test
        creator=teacher.user,
        duration=40,
        date=(datetime.utcnow() + timedelta(days=5)),
        meetup_place=meetup,
        dropoff_place=dropoff,
    )