def login(): """Log user in""" # Forget any user_id session.clear() # User reached route via POST (as by submitting a form via POST) if request.method == "POST": username = request.form.get("username") password = request.form.get("password") # Ensure username was submitted if not username: return apology("Must provide username") elif not password: return apology("Must provide password") loginSuccess, user_id = checkLogin(username, password) if loginSuccess == False: return apology("Invalid username and/or password") # Remember which user has logged in session["user_id"] = user_id return redirect("/") # User reached route via GET (as by clicking a link or via redirect) else: return render_template("login.html")
def register(): """Register user""" if request.method == "POST": username = request.form.get("username") password = request.form.get("password") confirmation = request.form.get("confirmation") # Ensure username was submitted and not already taken if not username: return apology("Must provide username") # Ensure both password fields submitted if not password or not confirmation: return apology("Please enter a password and confirm it") # Ensure both password fields match if password != confirmation: return apology("Passwords must match") if registerUser(username, password) == False: return apology("Sorry, that username is already taken") # Redirect user to index/login page return redirect("/") else: return render_template("register.html")
def book(): if request.method == 'POST': departure = request.form['departure'] destination = request.form['destination'] t_date = request.form['date'] if departure not in stations: return apology("We are Yet to start ou services at " + departure, 404) if destination not in stations: return apology("We are Yet to start ou services at " + departure, 404) if (departure == destination): return apology("Take auto dude!!!", 403) # format_str = '%Y-%m-%d' # The format # datetime_obj = datetime.datetime.strptime(t_date, format_str) # if not datetime_obj: # return apology("Yo Enter correct Date u fool", 404) # if(datetime_obj.date() < date.today()): # return apology("We don't give services for time travel", 403) # t_date = datetime_obj return redirect(f"/confirm/{departure}/{destination}/{t_date}") return render_template('book.html', stations=stations)
def confirm(departure, destination, t_date): format_str = '%Y-%m-%d' # The format datetime_obj = datetime.datetime.strptime(t_date, format_str) if not datetime_obj: return apology("Yo Enter correct Date u fool", 404) if (datetime_obj.date() < date.today()): return apology("We don't give services for time travel", 403) apiObj = Distance(departure, destination) t_fare = round(apiObj['distance'] * 1.5, 2) fare = round(t_fare + (t_fare * 0.18), 2) if (fare > current_user.wallet): return apology("Contact Developer to topup your wallet", 404) o_time = apiObj['time'] # 8 times faster t_time = o_time / 8 if (t_time >= 60): t_time = round(t_time / 60, 2) if (o_time >= 60): o_time = round(o_time / 60, 2) o_time = round(o_time - t_time, 2) if request.method == 'POST': new_wallet = round(current_user.wallet - fare, 2) num_rows_updated = UserModel.query.filter_by( id=current_user.id).update(dict(wallet=new_wallet)) travel = TravelModel(user_id=current_user.id, departure=departure, destination=destination, travel_date=datetime_obj.date(), booking_date=date.today(), distance=apiObj['distance'], fare=fare) db.session.add(travel) db.session.commit() flash('Your Ticket to ' + destination + ' is Confirmed, Happy Journey :)') return redirect('/profile') return render_template('confirm.html', departure=departure, destination=destination, travel_date=datetime_obj.date(), distance=round(apiObj['distance'], 2), fare=fare, o_time=o_time, t_time=t_time)
def cancel(): history_rows = TravelModel.query.filter_by(user_id=current_user.id).all() upcoming = [] for trip in history_rows: if (trip.travel_date >= date.today()): upcoming.append(trip) u_trips = len(upcoming) if request.method == 'POST': t_id = request.form['t_id'] trav = TravelModel.query.filter_by(travel_id=t_id).first() if (trav == None): return apology("Enter correct Travel ID!!", 404) else: new_wallet = round((current_user.wallet + (trav.fare / 2)), 2) num_rows_updated = UserModel.query.filter_by( id=current_user.id).update(dict(wallet=new_wallet)) db.session.delete(trav) db.session.commit() flash("Your Ticket is Cancelled :(") return redirect('/profile') return render_template('cancel.html', upcoming=upcoming, u_trips=u_trips)
def index(): """User is displayed with a box where they can type and submit a post. Below that are posts from users they follow""" if request.method == "POST": post = request.form.get("post") # Check to make sure user hasn't left field blank if not post: return apology("Please enter something to post") # Create post and store it in database createPost(post) # Get posts from the database from people current user is following posts = getPosts() posts.reverse() return render_template("index.html", posts=posts)
def login(): if current_user.is_authenticated: return redirect('/profile') if request.method == 'POST': email = request.form['email'] user = UserModel.query.filter_by(email=email).first() if user is not None and user.check_password(request.form['password']): login_user(user) flash('Hello ' + user.username + ', Welcome back to your Profile') return redirect('/profile') else: return apology('BRO enter correct credentials or Register again!', 505) return render_template('login.html')
def register(): if current_user.is_authenticated: return redirect('/profile') if request.method == 'POST': email = request.form['email'] username = request.form['username'] password = request.form['password'] c_password = request.form['c_password'] if UserModel.query.filter_by(email=email).first(): return apology('Email already Taken', 404) # if (password != c_password): # return apology('Your Passwords Do not Match!!!',404) user = UserModel(email=email, username=username, wallet=10000) user.set_password(password) db.session.add(user) db.session.commit() flash("Your Account is Registered!") return redirect('/login') return render_template('register.html')
def handle_404(error): # Response to wrong url return apology('Please, access correct URL!!', 404)