Exemplo n.º 1
0
def machine__login_user(request, user_token=""):
    machine = get_machine_from_session(request)
    if machine is None:
        return find_session_redirect(machine)
    if machine.current_session is not None:
        return find_session_redirect(machine)

    user_profile, error = get_profile_from_post(request)

    if user_profile is None and user_token != "":
        user_profile, error = get_profile_from_url_token(user_token)

    if user_profile is not None:
        if machine.user_is_allowed(user_profile.user):
            machinesession = models.MachineSession()
            machinesession.machine = machine
            machinesession.user = user_profile.user
            machinesession.save()
            machine.current_session = machinesession
            machine.save()
            return find_session_redirect(machine)
        else:
            error = "user_not_allowed"

    return HttpResponse(
        loader.get_template('machine/login_user.html').render(
            {
                "machine": machine,
                "last_error": error,
            }, request))
Exemplo n.º 2
0
def machine__tutor_required(request):
    machine = get_machine_from_session(request)
    if machine is None:
        return find_session_redirect(machine)

    tutor_profile, error = get_profile_from_post(request)
    if tutor_profile is not None:
        if machine.user_can_tutor(tutor_profile.user):
            machine.current_session.tutor = tutor_profile.user
            machine.current_session.save()
            return find_session_redirect(machine)
        else:
            error = "tutor_not_allowed"

    machine_usage_count = machine.count_usages(machine.current_session.user)

    return HttpResponse(
        loader.get_template('machine/tutor_required.html').render(
            {
                "machine":
                machine,
                "machine_usage_count":
                machine_usage_count,
                "tutor_required":
                machine.user_requires_tutor(machine.current_session.user),
                "tutor_required_once":
                machine.user_requires_tutor_once(machine.current_session.user),
                "last_error":
                error,
            }, request))
Exemplo n.º 3
0
def machine__payment_required(request):
    machine = get_machine_from_session(request)
    if machine is None:
        return find_session_redirect(machine)

    paying_user_profile = machine.current_session.user.profile
    if machine.current_session.user.profile.paying_user is not None:
        paying_user_profile = machine.current_session.user.profile.paying_user
    _user_can_pay, error = user_can_pay(paying_user_profile)
    if _user_can_pay is True:
        confirm_payment = request.POST.get("confirm_payment", None)
        if confirm_payment == "confirm_payment":
            error = create_payment_session(machine, paying_user_profile)
            if error is None:
                return find_session_redirect(machine)
    price_per_usage, price_per_hour = machine.get_price(paying_user_profile)

    return HttpResponse(
        loader.get_template('machine/payment_required.html').render(
            {
                "machine": machine,
                "last_error": error,
                "paying_user": paying_user_profile.user,
                "user_can_pay": _user_can_pay,
                "price_per_usage": price_per_usage,
                "price_per_hour": price_per_hour,
            }, request))
Exemplo n.º 4
0
def machine__pay_material(request, user_token=""):
    machine = get_machine_from_session(request)
    if machine is None:
        return find_session_redirect(machine)

    payment, transaction = None, None
    error = None
    value_before_payment = None

    payment_value = request.POST.get("payment_value", None)
    if payment_value is not None:
        payment_value = float(payment_value)
        if payment_value > 0.001:
            user_profile, error = get_profile_from_post(request)
            if user_profile is not None:
                if user_profile.paying_user is not None:
                    paying_user_profile = user_profile.paying_user
                else:
                    paying_user_profile = user_profile

                value_before_payment = paying_user_profile.prepaid_deposit
                payment, transaction = create_material_payment(
                    payment_value, paying_user_profile, user_profile, machine)

    template = loader.get_template('machine/pay_material.html')
    return HttpResponse(
        template.render(
            {
                "machine": machine,
                "payment": payment,
                "transaction": transaction,
                "last_error": error,
                "value_before_payment": value_before_payment
            }, request))
Exemplo n.º 5
0
def machine__logout_user(request):
    machine = get_machine_from_session(request)
    if machine is None:
        return find_session_redirect(machine)
    if machine.current_session is None:
        return find_session_redirect(machine)

    current_session, current_payment_session = end_session(machine.current_session)
    if current_session.start is None:  # user aborted before start
        return redirect('machine__login')


    template = loader.get_template('machine/logout_user.html')
    return HttpResponse(template.render({
        "machine": machine,
        "last_machine_session": current_session,
        "last_payment_session": current_payment_session,
    }, request))
Exemplo n.º 6
0
def machine__rate_machine(request):
    machine = get_machine_from_session(request)
    if machine is None:
        return find_session_redirect(machine)

    clean_rating = request.POST.get("clean_rating", None)
    if clean_rating is not None:
        try:
            clean_rating = int(clean_rating)
            if 0 < clean_rating < 6:
                machine.current_session.rating_clean = clean_rating
                machine.current_session.save()
                return find_session_redirect(machine)
        except Exception as e:
            print("Failed to save rating:", e)

    return HttpResponse(
        loader.get_template('machine/rate_machine.html').render(
            {"machine": machine}, request))
Exemplo n.º 7
0
def machine__show_session(request):
    machine = get_machine_from_session(request)
    if machine is None:
        return find_session_redirect(machine)
    if machine.current_session is None:
        return find_session_redirect(machine)

    if machine.current_session.start is None:
        machine.current_session.start = timezone.now()
        machine.current_session.save()

    if machine.current_session.autologout_at is not None:
        if machine.current_session.autologout_timediff[0] <= 0:
            current_session, current_payment_session = end_session(
                machine.current_session)
            return find_session_redirect(machine)

    return HttpResponse(
        loader.get_template('machine/show_session.html').render(
            {"machine": machine}, request))
Exemplo n.º 8
0
def machine__login_machine(request, machine_token=""):
    error = None
    if machine_token != "" and machine_token is not None:
        machine_token = "M:" + machine_token
        try:
            machine = Token.objects.get(identifier=machine_token).machine
            request.session["machine_id"] = machine.id
        except Exception:
            error = "unknown_token"
    if error is None:
        machine = get_machine_from_session(request)
        if machine is not None:
            return find_session_redirect(machine)
        machine, error = get_machine_from_post(request)
        if machine is not None:
            return find_session_redirect(machine)

    return HttpResponse(loader.get_template('machine/login_machine.html').render({
        "last_error": error,
    }, request))
Exemplo n.º 9
0
def machine__auto_logout(request):
    machine = get_machine_from_session(request)
    if machine is None:
        return find_session_redirect(machine)

    autologout_in_minutes = request.POST.get("autologout_in_minutes", None)
    if autologout_in_minutes is not None:
        autologout_in_minutes = int(autologout_in_minutes)
        if autologout_in_minutes > 0:
            machine.current_session.autologout_at = timezone.now() + timedelta(minutes=autologout_in_minutes)
        else:
            machine.current_session.autologout_at = None
        machine.current_session.save()
        return redirect('machine__show_session')

    timediff_total_minutes = None
    if machine.current_session.autologout_at is not None:
        timediff_total_minutes = (machine.current_session.autologout_at - timezone.now()).total_seconds() / 60

    return HttpResponse(loader.get_template('machine/auto_logout.html').render({
        "machine": machine,
        "timediff_total_minutes": timediff_total_minutes,
    }, request))
Exemplo n.º 10
0
def machine__other_user_pays(request, user_token=""):
    machine = get_machine_from_session(request)
    if machine is None:
        return find_session_redirect(machine)

    payer_user_profile, error = get_profile_from_post(request)
    if payer_user_profile is not None:
        error = create_payment_session(machine, payer_user_profile)
        if error is None:
            return find_session_redirect(machine)

    if payer_user_profile is None and user_token != "":
        payer_user_profile, error = get_profile_from_url_token(user_token)
        if user_token is not None:
            error = create_payment_session(machine, payer_user_profile)
        if error is None:
            return find_session_redirect(machine)

    return HttpResponse(
        loader.get_template('machine/other_user_pays.html').render(
            {
                "machine": machine,
                "last_error": error,
            }, request))