3
3
4
4
from app .config .settings import ALBUM_DATABASE_PATH
5
5
from app .utils .wrappers import image_exists , album_exists
6
- from app .database .images import get_id_from_path , get_path_from_id
6
+ from app .utils .path_id_mapping import get_id_from_path , get_path_from_id
7
+
7
8
8
9
def create_albums_table ():
9
10
conn = sqlite3 .connect (ALBUM_DATABASE_PATH )
10
11
cursor = conn .cursor ()
11
- cursor .execute ("""
12
+ cursor .execute (
13
+ """
12
14
CREATE TABLE IF NOT EXISTS albums (
13
15
album_name TEXT PRIMARY KEY,
14
16
image_ids TEXT,
15
17
description TEXT,
16
18
date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
17
19
)
18
- """ )
20
+ """
21
+ )
19
22
conn .commit ()
20
23
conn .close ()
21
24
25
+
22
26
def create_album (album_name , description = None ):
23
27
conn = sqlite3 .connect (ALBUM_DATABASE_PATH )
24
28
cursor = conn .cursor ()
@@ -30,11 +34,14 @@ def create_album(album_name, description=None):
30
34
conn .close ()
31
35
raise ValueError (f"Album '{ album_name } ' already exists" )
32
36
33
- cursor .execute ("INSERT INTO albums (album_name, image_ids, description) VALUES (?, ?, ?)" ,
34
- (album_name , json .dumps ([]), description ))
37
+ cursor .execute (
38
+ "INSERT INTO albums (album_name, image_ids, description) VALUES (?, ?, ?)" ,
39
+ (album_name , json .dumps ([]), description ),
40
+ )
35
41
conn .commit ()
36
42
conn .close ()
37
43
44
+
38
45
@album_exists
39
46
def delete_album (album_name ):
40
47
conn = sqlite3 .connect (ALBUM_DATABASE_PATH )
@@ -43,13 +50,14 @@ def delete_album(album_name):
43
50
conn .commit ()
44
51
conn .close ()
45
52
53
+
46
54
@album_exists
47
- @image_exists
48
55
def add_photo_to_album (album_name , image_path ):
49
56
conn = sqlite3 .connect (ALBUM_DATABASE_PATH )
50
57
cursor = conn .cursor ()
51
58
52
59
image_id = get_id_from_path (image_path )
60
+ # print("GOT IMAGE ID", image_id, flush=True)
53
61
if image_id is None :
54
62
conn .close ()
55
63
raise ValueError (f"Image '{ image_path } ' not found in the database" )
@@ -59,28 +67,13 @@ def add_photo_to_album(album_name, image_path):
59
67
image_ids = json .loads (result [0 ])
60
68
if image_id not in image_ids :
61
69
image_ids .append (image_id )
62
- cursor .execute ("UPDATE albums SET image_ids = ? WHERE album_name = ?" ,
63
- (json .dumps (image_ids ), album_name ))
70
+ cursor .execute (
71
+ "UPDATE albums SET image_ids = ? WHERE album_name = ?" ,
72
+ (json .dumps (image_ids ), album_name ),
73
+ )
64
74
conn .commit ()
65
75
conn .close ()
66
76
67
- @album_exists
68
- def add_photos_to_album (album_name , image_paths ):
69
- conn = sqlite3 .connect (ALBUM_DATABASE_PATH )
70
- cursor = conn .cursor ()
71
-
72
- cursor .execute ("SELECT image_ids FROM albums WHERE album_name = ?" , (album_name ,))
73
- result = cursor .fetchone ()
74
- if result :
75
- existing_ids = json .loads (result [0 ])
76
- new_ids = [get_id_from_path (path ) for path in image_paths if get_id_from_path (path ) is not None ]
77
- updated_ids = list (set (existing_ids + new_ids ))
78
-
79
- cursor .execute ("UPDATE albums SET image_ids = ? WHERE album_name = ?" ,
80
- (json .dumps (updated_ids ), album_name ))
81
-
82
- conn .commit ()
83
- conn .close ()
84
77
85
78
@album_exists
86
79
def get_album_photos (album_name ):
@@ -96,6 +89,7 @@ def get_album_photos(album_name):
96
89
return [get_path_from_id (image_id ) for image_id in image_ids ]
97
90
return None
98
91
92
+
99
93
@album_exists
100
94
@image_exists
101
95
def remove_photo_from_album (album_name , image_path ):
@@ -112,28 +106,60 @@ def remove_photo_from_album(album_name, image_path):
112
106
image_ids = json .loads (result [0 ])
113
107
if image_id in image_ids :
114
108
image_ids .remove (image_id )
115
- cursor .execute ("UPDATE albums SET image_ids = ? WHERE album_name = ?" ,
116
- (json .dumps (image_ids ), album_name ))
109
+ cursor .execute (
110
+ "UPDATE albums SET image_ids = ? WHERE album_name = ?" ,
111
+ (json .dumps (image_ids ), album_name ),
112
+ )
117
113
conn .commit ()
118
114
conn .close ()
119
115
116
+
120
117
def get_all_albums ():
121
118
conn = sqlite3 .connect (ALBUM_DATABASE_PATH )
122
119
cursor = conn .cursor ()
123
120
124
121
cursor .execute ("SELECT album_name, image_ids FROM albums" )
125
122
results = cursor .fetchall ()
126
- albums = [{"album_name" : name , "image_paths" : [get_path_from_id (id ) for id in json .loads (ids )]} for name , ids in results ]
123
+ albums = [
124
+ {
125
+ "album_name" : name ,
126
+ "image_paths" : [get_path_from_id (id ) for id in json .loads (ids )],
127
+ }
128
+ for name , ids in results
129
+ ]
127
130
128
131
conn .close ()
129
132
return albums
130
133
134
+
131
135
@album_exists
132
136
def edit_album_description (album_name , new_description ):
133
137
conn = sqlite3 .connect (ALBUM_DATABASE_PATH )
134
138
cursor = conn .cursor ()
135
-
136
- cursor .execute ("UPDATE albums SET description = ? WHERE album_name = ?" ,
137
- (new_description , album_name ))
139
+
140
+ cursor .execute (
141
+ "UPDATE albums SET description = ? WHERE album_name = ?" ,
142
+ (new_description , album_name ),
143
+ )
144
+ conn .commit ()
145
+ conn .close ()
146
+
147
+
148
+ def remove_image_from_all_albums (image_id ):
149
+ conn = sqlite3 .connect (ALBUM_DATABASE_PATH )
150
+ cursor = conn .cursor ()
151
+
152
+ cursor .execute ("SELECT album_name, image_ids FROM albums" )
153
+ albums = cursor .fetchall ()
154
+
155
+ for album_name , image_ids_json in albums :
156
+ image_ids = json .loads (image_ids_json )
157
+ if image_id in image_ids :
158
+ image_ids .remove (image_id )
159
+ cursor .execute (
160
+ "UPDATE albums SET image_ids = ? WHERE album_name = ?" ,
161
+ (json .dumps (image_ids ), album_name ),
162
+ )
163
+
138
164
conn .commit ()
139
165
conn .close ()
0 commit comments