예제 #1
0
def get_admin_button(trip_id, admin_user_of_this_trip):
    admin_button = None
    if admin_user_of_this_trip == app_user.get_email():
        print("this is admin user")
        admin_button = """
        <button type="button" class="btn btn-danger btn-sm pull-right" data-toggle="modal" data-target="#myModal">
            <span class="glyphicon glyphicon-plus"></span>&nbsp;&nbsp;&nbsp;&nbsp; Add New Field
        </button>
        """
        pass
    else:
        print("this is NOT admin user")
        admin_button = """
        <button type="button" class="btn btn-danger btn-sm pull-right" data-toggle="modal" data-target="#myModal" disabled>
            <span class="glyphicon glyphicon-remove"></span>&nbsp;&nbsp;&nbsp;&nbsp; No Permission to Edit
        </button>
        """
        pass
    return admin_button
예제 #2
0
def home():
    # global login_logout
    global trip_id
    if request.method == "GET":

        if session.get("logged_in") and session["logged_in"] == True:
            trip_id = get_auto_gen_trip_id(str(datetime.now()), app_user.get_email())

            trips_output = []  # set empty str
            admin_result = repo.get_trip_list(app_user.get_email(), True)  # list of admin trip_id(s) in 'user' table
            user_result = repo.get_trip_list(app_user.get_email(), False)  # list of user trip_id(s) in 'user' table

            all_users = []
            sorted_by_datetime_all = []
            if not admin_result or admin_result[0] == "None":
                pass
            else:
                ## 'admin_list' from Database Table 'trips' to table view (html)
                admin_list = []
                for each in admin_result:
                    admin_list.append(repo.get_trips_from_trips_table(each))
                # 1. trips_output.append(get_trips_in_table(admin_list, True))
                all_users.extend(admin_list)

                pass

            if not user_result or user_result[0] == "None":
                pass
            else:
                ## 'user_list' from Database Table 'trips' to table view (html)
                user_list = []
                for each in user_result:
                    user_list.append(repo.get_trips_from_trips_table(each))
                # 2. trips_output.append(get_trips_in_table(user_list, False))
                all_users.extend(user_list)

                pass

            # 3. trips_output = Markup(get_trips_in_table(sorted_by_datetime_all))
            # sort by datetime
            if all_users:
                sorted_by_datetime_all = sorted(all_users, key=lambda tup: tup[3])
                trips_output = Markup(get_trips_in_table(sorted_by_datetime_all))
            else:
                trips_output = "Create or Add your First Trip!"

            ### END..........................................

            # trips_output = Markup(" ".join(trips_output))

            return render_template(
                "index.html",
                title="Home Page",
                user_name=app_user.get_fname(),
                auto_gen_trip_id=trip_id,
                acc="'s Account" if session["log"] else "",
                log_in_out="Log Out" if session["log"] else "Log In",
                show_trip_list=trips_output,
                year=datetime.now().year,
            )
            pass
        else:
            print("failed to auth handling")
            pass

    elif request.method == "POST":

        ## Add Existing Trip_id from aother user
        if request.form["new_trip"] == "add_existing":

            # invalid input handling
            existing_trip_id = ""
            if request.form["existing_trip_id"]:
                existing_trip_id = request.form["existing_trip_id"]
                current_datetime = datetime.now().strftime("%Y-%m-%dT%H:%M")
                t = Trip(
                    existing_trip_id, "trip_name", app_user.get_email(), current_datetime, current_datetime
                )  # trip_id, trip_name, user_id(email)

                # check if the trip_id is existing ? (True or False)
                # repo.check_exist_trip(existing_trip_id) # True or False
                if repo.check_exist_trip(existing_trip_id):
                    repo.add_trip_name(t, False)
                else:
                    flash_line = "The Trip Id '" + existing_trip_id + "' is not found. Please try again."
                    flash(flash_line)
                pass
            else:
                flash("Please Enter Existing Trip Id")
                pass

            ##     *****************************    START

            trips_output = []  # set empty str
            admin_result = repo.get_trip_list(app_user.get_email(), True)  # list of admin trip_id(s) in 'user' table
            user_result = repo.get_trip_list(app_user.get_email(), False)  # list of user trip_id(s) in 'user' table

            all_users = []
            sorted_by_datetime_all = []
            if not admin_result or admin_result[0] == "None":
                pass
            else:
                ## 'admin_list' from Database Table 'trips' to table view (html)
                admin_list = []
                for each in admin_result:
                    admin_list.append(repo.get_trips_from_trips_table(each))
                # 1. trips_output.append(get_trips_in_table(admin_list, True))
                all_users.extend(admin_list)
                pass

            if not user_result or user_result[0] == "None":
                pass
            else:
                ## 'user_list' from Database Table 'trips' to table view (html)
                user_list = []
                for each in user_result:
                    user_list.append(repo.get_trips_from_trips_table(each))
                # 2. trips_output.append(get_trips_in_table(user_list, False))
                all_users.extend(user_list)
                pass

            # 3. trips_output = Markup(" ".join(trips_output))
            # sort by datetime
            if all_users:
                sorted_by_datetime_all = sorted(all_users, key=lambda tup: tup[3])
                trips_output = Markup(get_trips_in_table(sorted_by_datetime_all))
            else:
                trips_output = "Create or Add your First Trip!"

            return render_template(
                "index.html",
                title="Home Page",
                user_name=app_user.get_fname(),
                # travel_id = travel_id,
                auto_gen_trip_id=trip_id,
                show_trip_list=trips_output,
                acc="'s Account" if session["log"] else "",
                log_in_out="Log Out" if session["log"] else "Log In",
                year=datetime.now().year,
            )
            ##    *****************************    END

        elif request.form["new_trip"] == "create_new":

            # invalid input handling (TO DO)
            trip_name = ""

            if request.form["new_trip_name"]:
                trip_name = request.form["new_trip_name"]
                current_datetime = datetime.now().strftime("%Y-%m-%dT%H:%M")
                t = Trip(trip_id, trip_name, app_user.get_email(), current_datetime, current_datetime)
                repo.add_trip_name(t, True)
            else:
                flash("Please Enter Trip Name.")

            ##     *****************************    START
            trip_id = get_auto_gen_trip_id(str(datetime.now()), app_user.get_email())

            trips_output = []  # set empty str
            admin_result = repo.get_trip_list(app_user.get_email(), True)  # list of admin trip_id(s) in 'user' table
            user_result = repo.get_trip_list(app_user.get_email(), False)  # list of user trip_id(s) in 'user' table

            all_users = []
            sorted_by_datetime_all = []
            if not admin_result or admin_result[0] == "None":
                pass
            else:
                ## 'admin_list' from Database Table 'trips' to table view (html)
                admin_list = []
                for each in admin_result:
                    admin_list.append(repo.get_trips_from_trips_table(each))
                # 1. trips_output.append(get_trips_in_table(admin_list, True))
                all_users.extend(admin_list)
                pass

            if not user_result or user_result[0] == "None":
                pass
            else:
                ## 'user_list' from Database Table 'trips' to table view (html)
                user_list = []
                for each in user_result:
                    user_list.append(repo.get_trips_from_trips_table(each))
                # 2. trips_output.append(get_trips_in_table(user_list, False))
                all_users.extend(user_list)
                pass

            # 3. trips_output = Markup(" ".join(trips_output))
            # sort by datetime
            if all_users:
                sorted_by_datetime_all = sorted(all_users, key=lambda tup: tup[3])
                trips_output = Markup(get_trips_in_table(sorted_by_datetime_all))
            else:
                trips_output = "Create or Add your First Trip!"

            return render_template(
                "index.html",
                title="Home Page",
                user_name=app_user.get_fname(),
                # travel_id = travel_id,
                auto_gen_trip_id=trip_id,
                show_trip_list=trips_output,
                acc="'s Account" if session["log"] else "",
                log_in_out="Log Out" if session["log"] else "Log In",
                year=datetime.now().year,
            )
            ##    *****************************    END

        if is_bad_str(trip_name):
            msg = "WRONG INPUT: please try again to create a trip list"
            return render_template("wrongInputTrip.html", user_name=app_user.get_fname(), msg=msg)
예제 #3
0
def account():
    global login_logout
    if request.method == "GET":

        return render_template(
            "account.html",
            user_name=app_user.fname,
            user_lname=app_user.lname,
            user_email=app_user.get_email(),
            acc="'s Account" if session["log"] else "",
            log_in_out="Log Out" if session["log"] else "Log In",
            year=datetime.now().year,
        )

    elif request.method == "POST":

        ### TO DO 's
        ### @1 after validation,
        ### @2 update table with new user_obj
        ### @3 set user_obj with newly retrieved information (together with @2)

        flash_list = []
        update_success = False
        # only password is required in this page...
        if request.form["login_pw"]:

            old_username = app_user.email  # temp save in case user change his/her username
            # validate password
            if app_user.check_password(request.form["login_pw"]):

                """
                if request.form['account_email']:
                    app_user.email = request.form['account_email']
                    session['session_user'] = app_user.email
                """
                update_success = True

                # not required..
                if request.form["account_fname"]:
                    app_user.fname = request.form["account_fname"]

                # not required..
                if request.form["account_lname"]:
                    app_user.lname = request.form["account_lname"]

                # not required..
                if request.form["create_new_pw"] and request.form["confirm_new_pw"]:
                    if request.form["create_new_pw"] == request.form["confirm_new_pw"]:
                        app_user.set_password(request.form["create_new_pw"])
                    else:
                        flash("New Password and Password Confirmation is not matched.")
                        update_success = False

                if not update_success:
                    flash("Your information has not been saved!")
                else:
                    repo.update_user(app_user, old_username)
                    flash("Updated Successfully!")

                pass
            else:
                flash("Current Password is not correct. Please try again.")
                pass

        else:
            flash("You MUST provide your current password to securely update your information.")
            pass

        return render_template(
            "account.html",
            user_name=app_user.fname,
            user_lname=app_user.lname,
            user_email=app_user.get_email(),
            acc="'s Account" if session["log"] else "",
            log_in_out="Log Out" if session["log"] else "Log In",
            year=datetime.now().year,
        )

    pass