|
| 1 | +from flask import Flask, request, session, g, redirect, url_for, abort, \ |
| 2 | + render_template, flash, send_from_directory, Response |
| 3 | +from flask_login import LoginManager, UserMixin, \ |
| 4 | + login_required, login_user, logout_user |
| 5 | +import sqlite3 |
| 6 | +import werkzeug.security |
| 7 | + |
| 8 | +app = Flask(__name__, static_url_path='') |
| 9 | +# config |
| 10 | +app.config.update( |
| 11 | + DEBUG = True, |
| 12 | + SECRET_KEY = 'GMRKMGKRMG' |
| 13 | +) |
| 14 | + |
| 15 | +def app_start(host,port): |
| 16 | + app.run(host=host, port=port) |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +# flask-login |
| 21 | +login_manager = LoginManager() |
| 22 | +login_manager.init_app(app) |
| 23 | +login_manager.login_view = "login" |
| 24 | + |
| 25 | +@login_manager.user_loader |
| 26 | +def load_user(user_id): |
| 27 | + return User.get(user_id) |
| 28 | + |
| 29 | +##A simple way to access the DB |
| 30 | +def query(sql, params=None): |
| 31 | + conn = sqlite3.connect('site.db') |
| 32 | + cursor = conn.cursor() |
| 33 | + cursor.execute(sql, params) |
| 34 | + data = cursor.fetchall() |
| 35 | + cursor.close() |
| 36 | + return data |
| 37 | + |
| 38 | +def check_email(emailx): |
| 39 | + conn = sqlite3.connect('site.db') |
| 40 | + cursor = conn.cursor() |
| 41 | + sql = "Select email from users where email=?" |
| 42 | + cursor.execute(sql, [emailx]) |
| 43 | + data = cursor.fetchone() |
| 44 | + cursor.close() |
| 45 | + if data and data[0]: |
| 46 | + return True |
| 47 | + else: |
| 48 | + return False |
| 49 | + |
| 50 | +def check_username(usernamex): |
| 51 | + data = query("Select username from users where username=?",[usernamex]) |
| 52 | + return data |
| 53 | + |
| 54 | +def check_password(emailx, passwordx): |
| 55 | + data = query("Select password from users where email=?",[emailx]) |
| 56 | + result = werkzeug.security.check_password_hash(data[0],passwordx) |
| 57 | + return result |
| 58 | + |
| 59 | +def get_id(emailx): |
| 60 | + conn = sqlite3.connect('site.db') |
| 61 | + cursor = conn.cursor() |
| 62 | + sql = "Select id from users where email=?" |
| 63 | + cursor.execute(sql, [emailx]) |
| 64 | + data = cursor.fetchone() |
| 65 | + cursor.close() |
| 66 | + return data[0] |
| 67 | + |
| 68 | +def create_account(userx,passwordx,emailx): |
| 69 | + if not check_email(emailx) and not check_username(userx): |
| 70 | + hashpw = werkzeug.security.generate_password_hash(passwordx) |
| 71 | + conn = sqlite3.connect('site.db') |
| 72 | + cursor = conn.cursor() |
| 73 | + cursor.execute("INSERT INTO users (username, password, email) VALUES(?, ?, ?)", |
| 74 | + (userx, hashpw, emailx)) |
| 75 | + conn.commit() |
| 76 | + cursor.close() |
| 77 | + return "Account created!" |
| 78 | + else: |
| 79 | + return "User or email already exists!" |
| 80 | + |
| 81 | + |
| 82 | +@app.route("/register") |
| 83 | +def show_register_form(): |
| 84 | + return app.send_static_file('register.html') |
| 85 | + |
| 86 | +@app.route("/login/") |
| 87 | +def show_login(): |
| 88 | + return app.send_static_file('login.html') |
| 89 | + |
| 90 | + |
| 91 | +@app.route("/account/register/process/", methods=["POST"]) |
| 92 | +def register_account(): |
| 93 | + username = request.form.get("username") |
| 94 | + email = request.form.get("email") |
| 95 | + password = request.form.get("password") |
| 96 | + confrim_password = request.form.get("confirm-password") |
| 97 | + if password == confrim_password: |
| 98 | + create_account(username,password,email) |
| 99 | + return "account created!" |
| 100 | + else: |
| 101 | + return "Password does not match!" |
| 102 | +@app.route("/account/login/process/", methods=["POST"]) |
| 103 | +def login_account(): |
| 104 | + email = request.form.get("email") |
| 105 | + password = request.form.get("password") |
| 106 | + if check_password(email,password) == True: |
| 107 | + username = get_username(email) |
| 108 | + return "Welcome " + username |
| 109 | + else: |
| 110 | + return "Email or password Incorrect!" |
| 111 | +@app.route('/dashboard/') |
| 112 | +@login_required |
| 113 | +def home(): |
| 114 | + return Response("Hello World!") |
| 115 | + |
| 116 | +@app.route('/check/<slug>') |
| 117 | +def test_function(slug): |
| 118 | + return get_id(slug) |
| 119 | + |
| 120 | +app_start('0.0.0.0',6060) |
0 commit comments