parent
eaf690f832
commit
cd09ab9a12
Before Width: | Height: | Size: 3.6 KiB |
Binary file not shown.
@ -0,0 +1,66 @@
|
|||||||
|
import os
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
from login import User
|
||||||
|
|
||||||
|
|
||||||
|
class Database:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.TABLE_FILE = 'USERS'
|
||||||
|
self.DB_DIR = os.path.dirname(".")
|
||||||
|
self.setup_db()
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
"""Connect to an existing database instance based on the object
|
||||||
|
attributes.
|
||||||
|
"""
|
||||||
|
path = os.path.join(self.DB_DIR, "data.db")
|
||||||
|
return sqlite3.connect(path)
|
||||||
|
|
||||||
|
def setup_db(self):
|
||||||
|
"""Creates a database with tables."""
|
||||||
|
db = self.connect()
|
||||||
|
crs = db.cursor()
|
||||||
|
query = "CREATE TABLE IF NOT EXISTS " + self.TABLE_FILE + \
|
||||||
|
"(id INTEGER PRIMARY KEY AUTOINCREMENT," + \
|
||||||
|
"name CHAR(32) NOT NULL UNIQUE," + \
|
||||||
|
"password CHAR(32) NOT NULL)"
|
||||||
|
crs.execute(query)
|
||||||
|
|
||||||
|
def insert_user(self, name, password):
|
||||||
|
"""Insert a new user into the database.
|
||||||
|
"""
|
||||||
|
if self.check_name(name):
|
||||||
|
db = self.connect()
|
||||||
|
crs = db.cursor()
|
||||||
|
query = "INSERT INTO " + self.TABLE_FILE + "(`name`,`password`)" + \
|
||||||
|
"VALUES (?, ?) ON CONFLICT DO NOTHING"
|
||||||
|
crs.execute(query, (name, password))
|
||||||
|
db.commit()
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def check_name(self, name):
|
||||||
|
if self.get_by_name(name) is None:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_by_id(self, ident):
|
||||||
|
db = self.connect()
|
||||||
|
crs = db.cursor()
|
||||||
|
query = "SELECT * FROM " + self.TABLE_FILE + " WHERE id = ?"
|
||||||
|
crs.execute(query, (ident, ))
|
||||||
|
return crs.fetchone()
|
||||||
|
|
||||||
|
def get_by_name(self, name):
|
||||||
|
db = self.connect()
|
||||||
|
crs = db.cursor()
|
||||||
|
query = "SELECT * FROM " + self.TABLE_FILE + " WHERE name = ?"
|
||||||
|
crs.execute(query, (name, ))
|
||||||
|
return crs.fetchone()
|
||||||
|
|
||||||
|
def db_to_user(self, ident, name, pass_hash):
|
||||||
|
user = User(name, pass_hash)
|
||||||
|
user.set_id(ident)
|
||||||
|
return user
|
@ -0,0 +1,34 @@
|
|||||||
|
from flask_wtf import FlaskForm
|
||||||
|
from wtforms import StringField, PasswordField, SubmitField, BooleanField
|
||||||
|
from wtforms.validators import DataRequired
|
||||||
|
|
||||||
|
from werkzeug.security import generate_password_hash, check_password_hash
|
||||||
|
|
||||||
|
|
||||||
|
class User():
|
||||||
|
|
||||||
|
def __init__(self, name, pass_hash=None):
|
||||||
|
self.name = name
|
||||||
|
self.id = 0
|
||||||
|
self.is_active = True
|
||||||
|
self.is_authenticated = True
|
||||||
|
self.is_anonymous = False
|
||||||
|
self.pass_hash = pass_hash
|
||||||
|
|
||||||
|
def set_password(self, password):
|
||||||
|
self.pass_hash = generate_password_hash(password)
|
||||||
|
|
||||||
|
def set_id(self, ident):
|
||||||
|
self.id = ident
|
||||||
|
|
||||||
|
def check_password(self, password):
|
||||||
|
return check_password_hash(self.pass_hash, password)
|
||||||
|
|
||||||
|
def get_id(self):
|
||||||
|
return self.id
|
||||||
|
|
||||||
|
class LoginForm(FlaskForm):
|
||||||
|
username = StringField("Username", validators=[DataRequired()])
|
||||||
|
password = PasswordField("Password", validators=[DataRequired()])
|
||||||
|
remember_me = BooleanField("Remember Me")
|
||||||
|
submit = SubmitField("Sign In")
|
@ -1,4 +1,5 @@
|
|||||||
Flask==2.1.2
|
Flask==2.1.2
|
||||||
flask_login==0.6.2
|
Flask_Login==0.6.2
|
||||||
flask_wtf==1.0.1
|
Flask_WTF==0.14.3
|
||||||
WTForms==3.0.1
|
Werkzeug==2.1.2
|
||||||
|
WTForms==2.2.1
|
||||||
|
@ -1,18 +1,26 @@
|
|||||||
{% extends "template.html" %}
|
{% extends "template.html" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Sign In</h1>
|
|
||||||
<form action="" method="post" novalidate>
|
<div class="container">
|
||||||
{{ form.hidden_tag() }}
|
<div class="logging">
|
||||||
<p>
|
<h1>Sign In</h1>
|
||||||
{{ form.username.label }}<br>
|
<form action="" method="post" novalidate>
|
||||||
{{ form.username(size=32) }}
|
{{ form.hidden_tag() }}
|
||||||
</p>
|
<p>
|
||||||
<p>
|
{{ form.username.label }}<br>
|
||||||
{{ form.password.label }}<br>
|
{{ form.username(size=32) }}
|
||||||
{{ form.password(size=32) }}
|
</p>
|
||||||
</p>
|
<p>
|
||||||
<p>{{ form.remember_me() }} {{ form.remember_me.label }}</p>
|
{{ form.password.label }}<br>
|
||||||
<p>{{ form.submit() }}</p>
|
{{ form.password(size=32) }}
|
||||||
</form>
|
</p>
|
||||||
|
<p>{{ form.remember_me() }} {{ form.remember_me.label }}</p>
|
||||||
|
<p>{{ form.submit() }}</p>
|
||||||
|
{% for mesg in get_flashed_messages() %}
|
||||||
|
<p>{{ mesg }}</p>
|
||||||
|
{% endfor %}
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
Loading…
Reference in new issue