File size: 1,467 Bytes
ff14304
6761847
5d57d55
b9521ec
5d57d55
882e90a
e1b326e
d31320d
e1b326e
 
ff14304
5d57d55
 
 
882e90a
5d57d55
 
 
ee6afd3
5d57d55
 
 
 
 
 
 
 
 
882e90a
5d57d55
e1b326e
5d57d55
e1b326e
3c7be5f
1a7c5c9
5e71dba
 
5d57d55
 
882e90a
e1b326e
5d57d55
1a7c5c9
3c7be5f
 
882e90a
f2e77b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import sqlite3
import os
from flask import g

def get_db_path():
    if os.path.exists('/data'):
        return '/data/users.db'
    elif os.path.exists('/home/user_data'):
        return '/home/user_data/users.db'
    return 'users.db'

def get_db():
    if 'db' not in g:
        db_path = get_db_path()
        os.makedirs(os.path.dirname(db_path), exist_ok=True)
        g.db = sqlite3.connect(db_path)
        g.db.row_factory = sqlite3.Row
    return g.db

def close_db(e=None):
    db = g.pop('db', None)
    if db is not None:
        db.close()

def init_db(app):
    with app.app_context():
        db = get_db()
        cursor = db.cursor()
        
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS users (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                udid TEXT NOT NULL UNIQUE,
                name TEXT DEFAULT NULL,
                premium_account BOOLEAN DEFAULT FALSE,
                usage_limit INTEGER DEFAULT 4,
                invoice TEXT DEFAULT NULL
            )
        ''')
        
        cursor.execute('SELECT COUNT(*) FROM users')
        if cursor.fetchone()[0] == 0:
            cursor.executemany('INSERT INTO users (udid, name, premium_account, usage_limit, invoice) VALUES (?, ?, ?, ?, ?)',
                             [('test_udid_1', 'Test User 1', True, 4, 'test_invoice_1'), 
                              ('test_udid_2', 'Test User 2', False, 4, None)])
        
        db.commit()