Created
August 3, 2019 21:32
-
-
Save keddad/192046c2540e5d733dc285a67b66ac22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, request, make_response, redirect, render_template_string, config | |
import sqlite3 | |
from base64 import b64decode, b64encode | |
from hashlib import md5 | |
import os | |
app = Flask(__name__) | |
f = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'db.db') # Local file | |
def hash_string(s): | |
s = s.encode() | |
return md5(s).hexdigest() | |
def hash_id(s): | |
s = int(s) | |
if s % 2 != 0: | |
s = s * s | |
else: | |
s += 4 | |
s = str(s) | |
s = "CfjGfyu7%pXj" + s + "Rdyu7%mKEKpp" | |
return b64encode(s.encode()) | |
def get_id(s): | |
s = b64decode(s).decode() | |
s = s[12:] | |
s = s[:-12] | |
s = int(s) | |
if s % 2 == 0: | |
s -= 4 | |
else: | |
s = s / s | |
return s | |
def get_login(id): | |
if type(id) != int: | |
id = int(id) | |
conn = sqlite3.connect(f) | |
db = conn.cursor() | |
query = 'SELECT login FROM users WHERE id = ?' | |
db.execute(query, (id,)) | |
result = db.fetchone() | |
if result is None: | |
return None | |
else: | |
return result[0] | |
def get_notes(id): | |
if type(id) != str: | |
id = int(id) | |
conn = sqlite3.connect(f) | |
db = conn.cursor() | |
query = 'SELECT text FROM notes WHERE creator_id = ?' | |
result = [] | |
for row in db.execute(query, (id,)): | |
result.append(row[0]) | |
return result | |
def check_login(user): | |
conn = sqlite3.connect(f) | |
db = conn.cursor() | |
query = 'SELECT * FROM users WHERE login = ?' | |
db.execute(query, (user,)) | |
exist = db.fetchone() | |
if exist is None: | |
return False | |
else: | |
return True | |
@app.route('/user') | |
def user(): | |
id = get_id(request.cookies.get('id', None)) | |
if 'id' is None: | |
return render_template('user.html') | |
print(id) | |
login = get_login(id) | |
if not login: | |
return "Page is not longer avialable" | |
return render_template('user.html', username=login, notes=get_notes(id), id=id) | |
@app.route('/') | |
def index(): | |
return render_template('index.html') | |
@app.route('/login', methods=['GET', 'POST']) | |
def log(): | |
conn = sqlite3.connect(f) | |
db = conn.cursor() | |
if request.method == 'GET': | |
return render_template('login.html') | |
else: | |
login = request.form.get('login', '') | |
password = request.form.get('password', '') | |
if login == '' or password == '': | |
return "Login or password is missing" | |
password = hash_string(password) | |
query = 'SELECT * FROM users WHERE login = ? and password = ?' | |
db.execute(query, (login, password)) | |
result = db.fetchone() | |
if result is None: | |
return "No such user or password incorrect" | |
resp = redirect('/user') | |
resp.set_cookie('id', hash_id(str(result[0]))) | |
return resp | |
@app.route('/add_note', methods=['POST']) | |
def add_note(): | |
id = get_id(request.cookies.get('id', None)) | |
if 'id' is None: | |
return redirect('/') | |
text = request.form.get('note', '') | |
text = text.encode('utf-8', 'replace') | |
if text == '': | |
return "Empty note" | |
conn = sqlite3.connect(f) | |
db = conn.cursor() | |
query = 'INSERT INTO notes (text, creator_id) VALUES (?,?)' | |
db.execute(query, (text, id)) | |
conn.commit() | |
return redirect('/user') | |
@app.route('/register', methods=['GET', 'POST']) | |
def reg(): | |
conn = sqlite3.connect(f) | |
db = conn.cursor() | |
if request.method == 'GET': | |
return render_template('register.html') | |
else: | |
login = request.form.get('login', '') | |
password = request.form.get('password', '') | |
if login == '' or password == '': | |
return "Login or password is missing" | |
if check_login(login): | |
return "This login already exist" | |
password = hash_string(password) | |
query = 'INSERT INTO users (login,password) VALUES ("{}","{}")'.format(login, password) | |
db.execute(query) | |
conn.commit() | |
return "Success" | |
@app.errorhandler(404) | |
def page_not_found(e): | |
template = ''' | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>404</title> | |
</head> | |
<body> | |
<div class="center-content error"> | |
<h1>Oops! That page doesn't exist.</h1> | |
<h3>%s</h3> | |
</div> | |
</body> | |
</html> ''' % (request.url) | |
return render_template_string(template), 404 | |
if __name__ == '__main__': | |
conn = sqlite3.connect(f) | |
db = conn.cursor() | |
init_query = 'CREATE TABLE IF NOT EXISTS users(id integer NOT NULL PRIMARY KEY AUTOINCREMENT,login text,password text)' | |
db.execute(init_query) | |
init_query = 'CREATE TABLE IF NOT EXISTS notes(id integer NOT NULL PRIMARY KEY AUTOINCREMENT,text text,creator_id integer)' | |
db.execute(init_query) | |
conn.commit() | |
app.run(host="0.0.0.0", port=5005) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment