pywallter/tools/databaseinit.py

85 lines
2.1 KiB
Python
Raw Normal View History

2022-08-06 18:22:24 +02:00
from flask import Flask
2022-07-10 15:09:03 +02:00
import sqlite3
2022-08-06 18:22:24 +02:00
import os
from tools.utils import gen_token
from flask_bcrypt import Bcrypt
2022-08-06 18:22:24 +02:00
app = Flask( 'pywallter' )
app.config.from_pyfile('config.py')
bcrypt = Bcrypt(app)
2022-08-06 18:22:24 +02:00
DATABASE = app.config['DATABASE']
DOSSIER_PERSO = app.config['DOSSIER_APP']
DATABASE = app.config['DATABASE']
2020-11-29 02:05:19 +01:00
def init_db():
2022-08-06 18:22:24 +02:00
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users(
Mail TEXT UNIQUE,
name TEXT primary KEY UNIQUE NOT NULL,
2022-08-06 18:22:24 +02:00
alias TEXT,
xmpp TEXT,
passwd TEXT,
avatar TEXT,
nom TEXT,
prenom TEXT,
age TEXT,
website TEXT,
Token CHAR(30),
invitations INTEGER DEFAULT (20),
Mail_rescue TEXT )
""")
conn.commit()
print ('table users Ok')
cursor.execute("""
CREATE TABLE IF NOT EXISTS posts(
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
title TEXT,
content TEXT,
time TEXT,
category TEXT,
author TEXT,
status TEXT
)
""")
conn.commit()
cursor.execute("""select * from users""")
accounts = cursor.fetchall()
# Si aucun account n'est crée on créé l'utilisateur
# pywallter qui permet la première inscription
if not(accounts) :
user = "pywallter"
token = gen_token()
passwd_bcrypt = bcrypt.generate_password_hash(token)
cursor.execute("""INSERT INTO users(name, passwd, token) VALUES(?, ?, ?)""", (user, passwd_bcrypt, token))
conn.commit()
2022-08-06 18:22:24 +02:00
conn.close()
print ('table posts OK')
2022-07-10 15:09:03 +02:00
2020-11-29 02:05:19 +01:00
def init_dir():
if os.path.isdir('users'):
return False
else:
os.makedirs('./users/')
2022-08-06 18:22:24 +02:00
def db_migrate():
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("""SELECT name FROM PRAGMA_TABLE_INFO('users');""")
db_columns = cursor.fetchall()
present = False
for col in db_columns:
if "invitations" == col[0]:
present = True
if not(present):
cursor.execute("""ALTER TABLE users ADD COLUMN invitations INTEGER DEFAULT (20);""")
conn.commit()
print ("Ajout du champ invitations")