yusufgundogdu commited on
Commit
a240481
·
verified ·
1 Parent(s): fa72160

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +42 -20
database.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  from flask import g
4
 
5
  def get_db_path():
 
6
  if os.path.exists('/data'):
7
  return '/data/users.db'
8
  elif os.path.exists('/home/user_data'):
@@ -10,6 +11,7 @@ def get_db_path():
10
  return 'users.db'
11
 
12
  def get_db():
 
13
  if 'db' not in g:
14
  db_path = get_db_path()
15
  os.makedirs(os.path.dirname(db_path), exist_ok=True)
@@ -18,15 +20,18 @@ def get_db():
18
  return g.db
19
 
20
  def close_db(e=None):
 
21
  db = g.pop('db', None)
22
  if db is not None:
23
  db.close()
24
 
25
  def init_db(app):
 
26
  with app.app_context():
27
  db = get_db()
28
  cursor = db.cursor()
29
 
 
30
  cursor.execute('''
31
  CREATE TABLE IF NOT EXISTS users (
32
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -40,6 +45,18 @@ def init_db(app):
40
  )
41
  ''')
42
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  cursor.execute('SELECT COUNT(*) FROM users')
44
  if cursor.fetchone()[0] == 0:
45
  cursor.executemany('''
@@ -50,48 +67,51 @@ def init_db(app):
50
  ('test_udid_2', 'IOS', False, 4, 'test_invoice_2')
51
  ])
52
 
53
- # Generate_list tablosu güncellendi (order eklendi)
54
- cursor.execute('''
55
- CREATE TABLE IF NOT EXISTS generate_list (
56
- id INTEGER PRIMARY KEY AUTOINCREMENT,
57
- title TEXT NOT NULL,
58
- image_url TEXT NOT NULL,
59
- contour INTEGER NOT NULL,
60
- item_order INTEGER NOT NULL DEFAULT 0
61
- )
62
- ''')
 
 
 
 
63
 
64
  db.commit()
65
 
66
  def get_generate_list(db):
 
67
  cursor = db.cursor()
68
  cursor.execute('''
69
  SELECT id, title, image_url, contour, item_order
70
  FROM generate_list
71
- ORDER BY item_order ASC, id ASC
72
  ''')
73
- items = cursor.fetchall()
74
- return [dict(item) for item in items]
75
 
76
  def add_to_generate_list(db, title, image_url, contour, order=None):
 
77
  cursor = db.cursor()
78
 
79
- # Order belirtilmezse en son sıraya ekle
80
  if order is None:
81
  cursor.execute('SELECT MAX(item_order) FROM generate_list')
82
- max_order = cursor.fetchone()[0]
83
- order = (max_order or 0) + 1
84
 
85
  cursor.execute('''
86
  INSERT INTO generate_list (title, image_url, contour, item_order)
87
  VALUES (?, ?, ?, ?)
88
  ''', (title, image_url, contour, order))
89
-
90
  db.commit()
91
- cursor.execute('SELECT id FROM generate_list WHERE rowid = ?', (cursor.lastrowid,))
92
- return cursor.fetchone()[0]
93
 
94
  def update_item_order(db, item_id, new_order):
 
95
  cursor = db.cursor()
96
  cursor.execute('''
97
  UPDATE generate_list
@@ -102,6 +122,7 @@ def update_item_order(db, item_id, new_order):
102
  return cursor.rowcount > 0
103
 
104
  def create_user(udid, platform):
 
105
  try:
106
  if platform not in ['AND', 'IOS']:
107
  raise ValueError("Platform must be either 'AND' or 'IOS'")
@@ -112,7 +133,8 @@ def create_user(udid, platform):
112
  db.commit()
113
  cursor.execute('SELECT * FROM users WHERE udid = ?', (udid,))
114
  new_user = cursor.fetchone()
115
- return dict(new_user)
 
116
  except sqlite3.IntegrityError:
117
  raise ValueError("User with this UDID already exists")
118
  except Exception as e:
 
3
  from flask import g
4
 
5
  def get_db_path():
6
+ """Veritabanı dosya yolunu belirler"""
7
  if os.path.exists('/data'):
8
  return '/data/users.db'
9
  elif os.path.exists('/home/user_data'):
 
11
  return 'users.db'
12
 
13
  def get_db():
14
+ """Veritabanı bağlantısını getirir veya oluşturur"""
15
  if 'db' not in g:
16
  db_path = get_db_path()
17
  os.makedirs(os.path.dirname(db_path), exist_ok=True)
 
20
  return g.db
21
 
22
  def close_db(e=None):
23
+ """Veritabanı bağlantısını kapatır"""
24
  db = g.pop('db', None)
25
  if db is not None:
26
  db.close()
27
 
28
  def init_db(app):
29
+ """Veritabanı tablolarını oluşturur ve örnek verileri ekler"""
30
  with app.app_context():
31
  db = get_db()
32
  cursor = db.cursor()
33
 
34
+ # Kullanıcılar tablosu
35
  cursor.execute('''
36
  CREATE TABLE IF NOT EXISTS users (
37
  id INTEGER PRIMARY KEY AUTOINCREMENT,
 
45
  )
46
  ''')
47
 
48
+ # Generate list tablosu
49
+ cursor.execute('''
50
+ CREATE TABLE IF NOT EXISTS generate_list (
51
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
52
+ title TEXT NOT NULL,
53
+ image_url TEXT NOT NULL,
54
+ contour INTEGER NOT NULL,
55
+ item_order INTEGER NOT NULL DEFAULT 0
56
+ )
57
+ ''')
58
+
59
+ # Örnek kullanıcı verileri
60
  cursor.execute('SELECT COUNT(*) FROM users')
61
  if cursor.fetchone()[0] == 0:
62
  cursor.executemany('''
 
67
  ('test_udid_2', 'IOS', False, 4, 'test_invoice_2')
68
  ])
69
 
70
+ # Örnek model verileri
71
+ cursor.execute('SELECT COUNT(*) FROM generate_list')
72
+ if cursor.fetchone()[0] == 0:
73
+ default_models = [
74
+ ("REALISTIC", "assets/images/0.jpg", 0, 0),
75
+ ("DRAWING", "assets/images/1.jpg", 10, 1),
76
+ ("TOY FIGURE", "assets/images/2.jpg", 10, 2),
77
+ ("3D", "assets/images/3.jpg", 40, 3),
78
+ ("ANIMATION", "assets/images/4.jpg", 50, 4)
79
+ ]
80
+ cursor.executemany('''
81
+ INSERT INTO generate_list (title, image_url, contour, item_order)
82
+ VALUES (?, ?, ?, ?)
83
+ ''', default_models)
84
 
85
  db.commit()
86
 
87
  def get_generate_list(db):
88
+ """Generate listesindeki tüm öğeleri sıralı şekilde getirir"""
89
  cursor = db.cursor()
90
  cursor.execute('''
91
  SELECT id, title, image_url, contour, item_order
92
  FROM generate_list
93
+ ORDER BY item_order ASC
94
  ''')
95
+ return cursor.fetchall()
 
96
 
97
  def add_to_generate_list(db, title, image_url, contour, order=None):
98
+ """Generate listesine yeni öğe ekler"""
99
  cursor = db.cursor()
100
 
 
101
  if order is None:
102
  cursor.execute('SELECT MAX(item_order) FROM generate_list')
103
+ max_order = cursor.fetchone()[0] or 0
104
+ order = max_order + 1
105
 
106
  cursor.execute('''
107
  INSERT INTO generate_list (title, image_url, contour, item_order)
108
  VALUES (?, ?, ?, ?)
109
  ''', (title, image_url, contour, order))
 
110
  db.commit()
111
+ return cursor.lastrowid
 
112
 
113
  def update_item_order(db, item_id, new_order):
114
+ """Öğenin sıralama pozisyonunu günceller"""
115
  cursor = db.cursor()
116
  cursor.execute('''
117
  UPDATE generate_list
 
122
  return cursor.rowcount > 0
123
 
124
  def create_user(udid, platform):
125
+ """Yeni kullanıcı oluşturur"""
126
  try:
127
  if platform not in ['AND', 'IOS']:
128
  raise ValueError("Platform must be either 'AND' or 'IOS'")
 
133
  db.commit()
134
  cursor.execute('SELECT * FROM users WHERE udid = ?', (udid,))
135
  new_user = cursor.fetchone()
136
+ return dict(new_user) if new_user else None
137
+
138
  except sqlite3.IntegrityError:
139
  raise ValueError("User with this UDID already exists")
140
  except Exception as e: