Advertisement
Guest User

Untitled

a guest
Oct 8th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.32 KB | None | 0 0
  1. from cs50 import SQL
  2. import os
  3. import re
  4. from flask import Flask, flash, redirect, render_template, request, session, url_for
  5. from flask_session import Session
  6. import feedparser
  7. import urllib.parse
  8. from flask_jsglue import JSGlue
  9. from passlib.apps import custom_app_context as pwd_context
  10. from tempfile import mkdtemp
  11.  
  12.  
  13.  
  14. # configure application
  15. app = Flask(__name__)
  16. JSGlue(app)
  17.  
  18. # ensure responses aren't cached
  19. if app.config["DEBUG"]:
  20.     @app.after_request
  21.     def after_request(response):
  22.         response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
  23.         response.headers["Expires"] = 0
  24.         response.headers["Pragma"] = "no-cache"
  25.         return response
  26.  
  27.  
  28. # configure session to use filesystem (instead of signed cookies)
  29. app.config["SESSION_FILE_DIR"] = mkdtemp()
  30. app.config["SESSION_PERMANENT"] = False
  31. app.config["SESSION_TYPE"] = "filesystem"
  32. Session(app)
  33.  
  34. # configure CS50 Library to use SQLite database
  35. db = SQL("sqlite:///hotel.db")
  36.  
  37. @app.route("/")
  38. def index():
  39.     """welcome page."""
  40.     return render_template("index.html")
  41.  
  42.  
  43. @app.route("/signup", methods=["GET", "POST"])
  44. def signup():
  45.     """User register by creating an account."""
  46.  
  47.     # forget any user_id
  48.     session.clear()
  49.  
  50.     # if user reached route via POST (as by submitting a form via POST)
  51.     if request.method == "POST":
  52.  
  53.         # #make sure that the username choosen doesn't exist already, otherwise create the new user
  54.         # result = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)", username=request.form.get("username"), hash=pwd_context.hash(request.form.get("password")))
  55.         # if not result:
  56.         #     return apology("username already exist")
  57.  
  58.         #create a new line in database with all the infos for the new user/ type will be automatically client
  59.         db.execute("INSERT INTO users (name, last_name, email, password, phone)\
  60.                    VALUES (:name, :last_name, :email, :password, :phone)",\
  61.                     name=request.form.get("name"), last_name=request.form.get("lastname"), email=request.form.get("email"), \
  62.                     password=request.form.get("password"), phone=request.form.get("phone"))
  63.  
  64.         #get the user_id to use it in session
  65.         rows = db.execute("SELECT * FROM users WHERE email = :email", email=request.form.get("email"))
  66.  
  67.         # remember which user has logged in
  68.         session["user_id"] = rows[0]["user_id"]
  69.         session["type"] = rows[0]["type"]
  70.  
  71.         # redirect user to home page
  72.         return redirect(url_for("index"))
  73.  
  74.     # else if user reached route via GET (as by clicking a link or via redirect)
  75.     else:
  76.         return render_template("signup.html")
  77.  
  78. @app.route("/login", methods=["GET", "POST"])
  79. def login():
  80.     """Log user in."""
  81.  
  82.     # # forget any user_id
  83.     # session.clear()
  84.  
  85.     # # if user reached route via POST (as by submitting a form via POST)
  86.     # if request.method == "POST":
  87.  
  88.     #     # make sure that the username which is email and the password are correct
  89.     #     rows = db.execute("SELECT * FROM users WHERE email = :email", email=email=request.form.get("email"))
  90.  
  91.     #     # ensure username exists and password is correc
  92.     #     if len(rows) != 1 or not pwd_context.verify(request.form.get("password"), rows[0]["hash"]):
  93.     #         return alert("invalid username and/or password")
  94.  
  95.     #     # remember which user has logged in
  96.     #     session["user_id"] = rows[0]["user_id"]
  97.     #     session["type"] = rows[0]["type"]
  98.  
  99.     #     #if user already done a research, make sure to send right away the confirmation for him not to restart the search.
  100.     #     #if button confirmation is clicked
  101.     #     #return to reservation part where the confirmation template will be send to client
  102.  
  103.     #     # redirect user to home page
  104.     #     return redirect(url_for("index"))
  105.  
  106.     # # else if user reached route via GET (as by clicking a link or via redirect)
  107.     # else:
  108.     #     return render_template("login.html")
  109.  
  110. @app.route("/logout")
  111. def logout():
  112.     """Log user out."""
  113.  
  114.     # # forget any user_id
  115.     # session.clear()
  116.  
  117.     # # redirect user to the home page
  118.     # return redirect(url_for("index"))
  119.  
  120.  
  121. @app.route("/reservation", methods=["GET", "POST"])
  122. def reservation():
  123.     """Client is reserving a room by dates."""
  124.  
  125.  
  126.     # # if user reached route via POST (as by submitting a form via POST)
  127.     # if request.method == "POST":
  128.  
  129.     #     #get the date to check in database which room match for booking
  130.     #     date_start = request.form.get["date_start"]
  131.     #     date_end = request.form.get["date_end"]
  132.  
  133.     #     # check the room that are status available when reservation has no date_start adn no date_end
  134.     #     result = db.execute("SELECT * FROM reservations WHERE date_start = :date_start AND date_end = :date_end innerjoin * FROM room, status='available')
  135.     #     if not date_start < result["date_start"] || date_end > result["date_end"]
  136.     #         return alert("sorry no room is avaialbe for choosen dates")
  137.  
  138.  
  139.     #     #get the price of each room available
  140.     #     result = db.execute("SELECT * FROM rooms WHERE price = :price")
  141.  
  142.     #     #calculate for each room the total price depending of the room and the nights
  143.     #     nigths = date_end - date_start
  144.     #     total price = result["price"] * nigths
  145.  
  146.     #     # return to client the infos about each room with status available
  147.     #     return render_template(url_for("options"), #room with statut available with bottom to click reserve for each room)
  148.  
  149.     #     # #client have a button to make the reservation
  150.     #     # if session["user_id"]
  151.  
  152.     #     # #if user is not log, message to make him log or register
  153.     #     # else redirect(url'log in')
  154.  
  155.     #     #add a line in reservation to the database
  156.     #     rows = db.execute("INSERT INTO reservation (date_start, date_end, room_id, user_id, total_price, number_person, status) \
  157.     #                         VALUES (:date_start, :date_end, :room_id, :total_price, :user_id, status)",\
  158.     #                         user_id=session["user_id"], date_start=request.form.get("date_start"), date_end=request.form.get("date_end"),\
  159.     #                         room_id=room_id, total_price=total_price(room_id))
  160.  
  161.  
  162.     #     #show the reservation number confirmation with the option to cancel / do i want to send an email confirmation ?
  163.     #     #do i want to propose here the bottom to cancel
  164.     #     return render_template("confirmation.html", reservation_id=reservation_id, date start=date_start, date end= date_end)
  165.     #     #
  166.  
  167.     #     # remember which user has logged in
  168.     #     session["user_id"] = rows[0]["id"]
  169.  
  170.     #     # redirect user to home page
  171.     #     return redirect(url_for("index"))
  172.  
  173.     # # else if user reached route via GET (as by clicking a link or via redirect)
  174.     # else:
  175.     #     return render_template("reservation.html")
  176.  
  177. @app.route("/reserved", methods=["GET", "POST"])
  178. def reserved():
  179.     """client access all his reservation done with option to cancel."""
  180.  
  181.     # # forget any user_id
  182.     # session.clear()
  183.  
  184.     # # if user reached route via POST (as by submitting a form via POST)
  185.     # if request.method == "POST":
  186.  
  187.     #     # get only the reservation done with all status for only one client
  188.     #     result = db.execute("SELECT * FROM reservations WHERE user_id= :user_id", user_id=session["user_id"])
  189.  
  190.     #     #show to client all the reservation done in the past and the reservation
  191.     #     return render_template("reserved.html", reservation=reservations)
  192.  
  193.     # # # else if user reached route via GET (as by clicking a link or via redirect)
  194.     # # else:
  195.     # #     return render_template("login.html")
  196.  
  197.  
  198. @app.route("/history", methods=["GET", "POST"])
  199. def history():
  200.     """admin access history of all reservation done with all status for all clients."""
  201.  
  202.     # # forget any user_id
  203.     # session.clear()
  204.  
  205.     # # if user reached route via POST (as by submitting a form via POST)
  206.     # if request.method == "POST":
  207.  
  208.     #     # query database to get all the reservation done
  209.     #     result = db.execute("SELECT * FROM reservations WHERE reservation_id =:reservation_id", reservation_id=reservation_id)
  210.  
  211.     #     #return the info to user
  212.     #     return render_template("history.html", histories=history)
  213.  
  214.     # # else if user reached route via GET (as by clicking a link or via redirect)
  215.     # else:
  216.     #     return render_template("history.html")
  217.  
  218. @app.route("/booking", methods=["GET", "POST"])
  219. def booking():
  220.     """list of all rooms reserved with option to confirm or cancel"""
  221.  
  222.     # # forget any user_id
  223.     # session.clear()
  224.  
  225.     # # if user reached route via POST (as by submitting a form via POST)
  226.     # if request.method == "POST":
  227.  
  228.     #     # query database for checkin availability of the room
  229.     #     result = db.execute("SELECT * FROM reservation WHERE status= :status", status='reserved')
  230.  
  231.     #     return render_template("booking.html", reservation=reservations, user=users)
  232.  
  233.     # # # else if user reached route via GET (as by clicking a link or via redirect)
  234.     # # else:
  235.     # #     return render_template("login.html")
  236.  
  237. @app.route("/availability", methods=["GET", "POST"])
  238. def availability():
  239.     """all the rooms that are availabe"""
  240.  
  241.     # # forget any user_id
  242.     # session.clear()
  243.  
  244.     # # if user reached route via POST (as by submitting a form via POST)
  245.     # if request.method == "POST":
  246.  
  247.     #     # query database for checkin availability of the room
  248.     #     result = db.execute("SELECT * FROM rooms WHERE status= :status", status='available')
  249.  
  250.     #     return render_template("availability.html", room=rooms)
  251.  
  252.     # # # else if user reached route via GET (as by clicking a link or via redirect)
  253.     # # else:
  254.     # #     return render_template("login.html")
  255.  
  256. @app.route("/occupancy", methods=["GET", "POST"])
  257. def occupancy():
  258.     """all the rooms that are used"""
  259.  
  260.     # # forget any user_id
  261.     # session.clear()
  262.  
  263.     # # if user reached route via POST (as by submitting a form via POST)
  264.     # if request.method == "POST":
  265.  
  266.     #     # query database for checkin availability of the room
  267.     #     result = db.execute("SELECT * FROM rooms WHERE status= :status", status='occupied')
  268.  
  269.     # # # else if user reached route via GET (as by clicking a link or via redirect)
  270.     # # else:
  271.     # #     return render_template("login.html")
  272.  
  273.  
  274.  
  275. # @app.route("/confirm")
  276. # def confirm():
  277. #     """Admin confirm reservation when client is arrived at hotel."""
  278.  
  279. #     #get the reservation with the status reserverd
  280.  
  281. #     #buttom that switch status or reservation to reserved to confirmed
  282. #     db.execute("UPDATE reservations SET status :status WHERE reservation_id = :reservation_id", status='confirmed')
  283.  
  284. #     # #change the room status from reserved to occupied
  285. #     # db.execute("UPDATE rooms SET status :status WHERE room_id = :room_id", status='occupied', room_id=room_id)
  286.  
  287. #     #create a new line to history db
  288. #     #rows = db.execute("INSERT INTO history (id, symbol, shares, price, cost, transaction_type) VALUES (:id, :symbol,:shares, :price, :cost, :transaction_type)",\
  289. #     #                       id=session["user_id"], shares=request.form.get("number_shares"), symbol=result["symbol"], price=usd(result["price"]), cost=usd(update_cash),\
  290. #     #                       transaction_type='Sell')
  291.  
  292.  
  293.  
  294. # @app.route("/cancel")
  295. # def cancel():
  296. #     """reservation is cancelled."""
  297.  
  298. #     # #buttom that switch reservation status from reserved to available
  299. #     # db.execute("UPDATE reservations SET status :status WHERE reservation_id = :reservation_id", status='cancel', room_id=room_id)
  300.  
  301. #     # #change the room status from reserved to cancel
  302. #     # db.execute("UPDATE rooms SET status :status WHERE room_id = :room_id", status='available', room_id=room_id)
  303.  
  304. #     #add the reservation to the database
  305. #     #    rows = db.execute("INSERT INTO reservation (id, date start, date end, room id, total price, reservation time, number person, status) \
  306. #     #                        VALUES (:id, :date_start, :date_end, :total_price, :room_id, :user_id, status)",\
  307. #     #                        id=reservation_id, user_id=session["user_id"], date_start=request.form.get("date_start"), date_end=request.form.get("date_end")
  308. #     #                        , total_price=total_price(room_id), Sell')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement