示例#1
0
文件: app.py 项目: markvbavel/CS50
def search():
    """ Searches student """

    # Connect to database 
    conn = connect_db(database)
    conn.row_factory = dict_factory

    if request.method == "POST":

        print("SEARCH POST")
        # Get search query
        query = str(request.form.get("search_searchbar"))
        column = request.form.get("search_class")
        
        # Query databse
        records = search_student(conn, query, column)

        # Get headers from databse
        headers = get_headers(conn)

        # Number of casts
        showtime = ("Saturday", "Sunday")

        # Close database connection
        close_connection(conn)

        return render_template("index.html",
                                session = session,
                                headers = headers, 
                                records = records,
                                showtime = showtime)
示例#2
0
文件: app.py 项目: markvbavel/CS50
def new():
    """ Adds new students to students table """

    if request.method == "POST":
        print("NEW POST")
        
        # Connect to database 
        conn = connect_db(database)
        conn.row_factory = dict_factory

        # Gather data from form and put into a tuple
        student_data = (\
            request.form.get("new_firstname"),\
            request.form.get("new_lastname"),\
            request.form.get("new_birth"),\
            request.form.get("new_class"),\
            request.form.get("new_phone_1"),\
            request.form.get("new_phone_2"),\
            request.form.get("new_email_1"),\
            request.form.get("new_email_2"),\
            request.form.get("new_showtime"),\
            request.form.get("new_role"),\
            request.form.get("new_notes"))

        # Insert into database
        insert_student(conn, student_data)

        flash("Student added!")

        return redirect(url_for("index"))

    else:
        print("NEW GET")
        return render_template("index.html")    
示例#3
0
文件: app.py 项目: markvbavel/CS50
def delete_entry(entry_id):
    """
    Deletes entry data
    """
    print("DELETE ENTRY GET")

    conn = connect_db(database)
    conn.row_factory = dict_factory

    # Function call
    del_entry(conn, entry_id)
    close_connection(conn)

    flash("Entry deleted!")

    return redirect(url_for("index"))
示例#4
0
文件: app.py 项目: markvbavel/CS50
def register():
    
    # Clear any user id
    session.clear()

    if request.method == "POST":

        print("REGISTER POST")

        # Connect to database 
        conn = connect_db(database)
        conn.row_factory = dict_factory

        # Get username and password from form
        username = request.form.get("register_username")
        password = request.form.get("register_password")

        # Error check
        if not username or not password or not request.form.get("register_confirm"):
            return apology("Please fill in all fields", 401)
        
        if password != request.form.get("register_confirm"):
            return apology("Passwords don't match", 401)

        records = search_user(conn, username)
        if len(records) != 0:
            return apology("Username already exists", 409)
        
        # Error check passed. Insert user into users database
        user_data = (username, generate_password_hash(password))
        user_id = insert_user(conn, user_data)

        # Remember user
        session["user_id"] = user_id
        session["username"] = username

        # Close database connection
        close_connection(conn)
                
        # Redirect user to home
        return redirect(url_for("index"))
            
    else:
        print("REGISTER GET")

        return render_template("index.html")
示例#5
0
文件: app.py 项目: markvbavel/CS50
def user_delete(user_id):
    """
    Deletes user account
    """

    print("DELETE USER GET")

    # Connect to database 
    conn = connect_db(database)
    conn.row_factory = dict_factory

    # Function call
    del_user(conn, user_id)
    close_connection(conn)

    flash("Profile deleted!")

    return redirect(url_for("logout"))
示例#6
0
文件: app.py 项目: markvbavel/CS50
def login():
    """Log user in"""

    # User reached route via POST
    if request.method == "POST":

        print("LOGIN POST")
        # Connect to database 
        conn = connect_db(database)
        conn.row_factory = dict_factory

        # Gather form input
        username = request.form.get("login_username")
        password = request.form.get("login_password")

        # Error checking on user input
        if not username or not password:
            return apology("Please fill in all the required fields", 403)

        # Query users table for username
        records = search_user(conn, username)

        # Ensure username exists and password is correct
        if len(records) != 1:
            return apology("username not found", 401)
        elif check_password_hash(records[0]["hash"], password) == False:
            return apology("Invalid password", 401)
        
        # Remember which user has logged in
        session["user_id"] = records[0]["id"]
        session["username"] = records[0]["username"]

        # Close database connection
        close_connection(conn)

        # Redirect user to home page
        return redirect(url_for("index"))

    # User reached route via GET
    else:
        print("LOGIN GET")

        return render_template("login.html")
示例#7
0
文件: app.py 项目: markvbavel/CS50
def user():
    """
    Displays user information. User can edit username and password
    """

    # Connect to database 
    conn = connect_db(database)
    conn.row_factory = dict_factory

    if request.method == "POST":
        # To update user data
        print("USER POST")

        # Gather data
        user_id = session["user_id"]
        username_old = session["username"]
        username_new = request.form.get("user_username")
        pw_old = request.form.get("user_old_pw")
        pw_new = request.form.get("user_new_pw")
        pw_confirm = request.form.get("user_new_confirm")
        user_data = (username_new, generate_password_hash(pw_new))

        # Compare input to existing data
        records = search_user(conn, username_old)
        if check_password_hash(records[0]["hash"], pw_old) == False:
            return apology("Invalid password", 401)
        
        if pw_new != pw_confirm:
            return apology("Passwords don't match", 401)
        
        # Update user data
        mod_user(conn, user_data, user_id)
        close_connection(conn)

        flash("Profile updated!")
        
        return redirect(url_for("index"))

    else:
        # Display user data
        print("USER GET")
        return render_template("user.html")
示例#8
0
文件: app.py 项目: markvbavel/CS50
def index():

    """Show overview of all students in a table"""
    print("INDEX GET")
            
    # Connect to database 
    conn = connect_db(database)
    conn.row_factory = dict_factory

    # Establish number of casts and group names if not yet found in database
    showtime = ("Saturday", "Sunday")
    session["student_classes"] = ["Junior", "Oranje", "Paars", "Blauw", "PG", "Demo", "Vakklas"]

    # Select all student data
    headers = get_headers(conn)
    records = student_overview(conn)
    
    # If no data is present
    if not records:              
        return render_template("index.html", 
                                session = session, 
                                showtime = showtime,
                                headers = headers)

    # Change "None" to "-" for readability
    for record in records:
        for value in record:
            if record[value] == None:
                record[value] = "-"

    #Close db connection
    close_connection(conn)
    return render_template("index.html", 
                        session = session, 
                        records = records, 
                        headers = headers,
                        showtime = showtime)
示例#9
0
文件: pmd.py 项目: dirkk0/pmd
    default="config/localhost.json")

parser.add_argument('-d',
    action="store", dest="pmdDataFolder",
    help='Name of directory to watch.',
    default="pmddata")

parser.add_argument('-s',
    action="store", dest="sleeptime", type=float,
    help='Time for polling updates.',
    default=1.0)

results = parser.parse_args()
print results

r = helpers.connect_db(results.configFileName)

if not r:
    sys.exit(1)


# helpers.download_all(r, 'cache')
if results.pmdDataFolder != "":
    folder = results.pmdDataFolder

    try:
        os.mkdir(folder)
        print "created directory: %s" % folder
        os.mkdir(folder)
    except:
        pass
示例#10
0
from models.practice_datum import PracticeData
import helpers

helpers.connect_db()

# When creating a record, all non-nullable fields need to be filled
# pd = PracticeData()
# pd.namef = 'will'
# pd.namel = 'dove'
# pd.note = 'Things and stuff'
# pd.save()

user_table_headers = PracticeData().find(1)._attributes


def return_a_detail(grabbed_detail, value):
    row = PracticeData.find(value)
    if grabbed_detail.lower() != 'all':
        try:
            detail = getattr(row, grabbed_detail.lower())
            print(f'The answer is {detail.title()}')
        except AttributeError:
            print(f'There is no "{grabbed_detail}" attribute.')
    else:
        buffer = ""
        for column, detail in user_table_headers.items():
            buffer += f'{column.upper()}: {detail} \n'
        print(buffer)


while True: