pywallter/tools/databaseinit.py

85 lines
2.1 KiB
Python
Executable File

from flask import Flask
import sqlite3
import os
from tools.utils import gen_token
from flask_bcrypt import Bcrypt
app = Flask( 'pywallter' )
app.config.from_pyfile('config.py')
bcrypt = Bcrypt(app)
DATABASE = app.config['DATABASE']
DOSSIER_PERSO = app.config['DOSSIER_APP']
DATABASE = app.config['DATABASE']
def init_db():
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,
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()
conn.close()
print ('table posts OK')
def init_dir():
if os.path.isdir('users'):
return False
else:
os.makedirs('./users/')
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")