app-MAIL-temp/app/phone/views/phone_reservation.py
Son 3e2c120a73 phone reservation page
- add twilio lib
- create phone listing, reservation page
- add twilio callback to receive messages
2021-12-02 17:03:13 +01:00

48 lines
1.7 KiB
Python

import arrow
from flask import render_template, flash, redirect, url_for, request
from flask_login import login_required, current_user
from app.db import Session
from app.models import PhoneReservation, PhoneMessage, User
from app.phone.base import phone_bp
current_user: User
@phone_bp.route("/reservation/<reservation_id>", methods=["GET", "POST"])
@login_required
def reservation_route(reservation_id: int):
reservation: PhoneReservation = PhoneReservation.get(reservation_id)
if not reservation or reservation.user_id != current_user.id:
flash("Unknown error, redirect back to phone page", "warning")
return redirect(url_for("phone.index"))
phone_number = reservation.number
messages = PhoneMessage.filter(
PhoneMessage.number_id == phone_number.id,
PhoneMessage.created_at > reservation.start,
PhoneMessage.created_at < reservation.end,
).all()
if request.method == "POST":
if request.form.get("form-name") == "release":
time_left = reservation.end - arrow.now()
if time_left.seconds > 0:
current_user.phone_quota += time_left.seconds // 60
flash(
f"Your phone quota is increased by {time_left.seconds // 60} minutes",
"success",
)
reservation.end = arrow.now()
Session.commit()
flash(f"{phone_number.number} is released", "success")
return redirect(url_for("phone.index"))
return render_template(
"phone/phone_reservation.html",
phone_number=phone_number,
reservation=reservation,
messages=messages,
now=arrow.now(),
)