Update db.py
Browse files
db.py
CHANGED
@@ -3,6 +3,7 @@ import sqlite3
|
|
3 |
|
4 |
from peewee import *
|
5 |
|
|
|
6 |
class MemoryConnection(sqlite3.Connection):
|
7 |
def __init__(self, dbname, *args, **kwargs):
|
8 |
load_conn = sqlite3.connect(dbname)
|
@@ -34,14 +35,17 @@ class SqliteMemDatabase(SqliteDatabase):
|
|
34 |
finally:
|
35 |
save_conn.close()
|
36 |
|
|
|
37 |
db: SqliteDatabase = None
|
38 |
tag_cache_map = {}
|
39 |
|
|
|
40 |
def get_tag_by_id(tag_id):
|
41 |
if tag_id not in tag_cache_map:
|
42 |
tag_cache_map[tag_id] = Tag.get_by_id(tag_id)
|
43 |
return tag_cache_map[tag_id]
|
44 |
|
|
|
45 |
class EnumField(IntegerField):
|
46 |
def __init__(self, enum_list, *args, **kwargs):
|
47 |
super().__init__(*args, **kwargs)
|
@@ -82,9 +86,9 @@ class Tag(BaseModel):
|
|
82 |
return f"<Tag '{self.name}'>"
|
83 |
|
84 |
def __repr__(self):
|
85 |
-
#from objprint import objstr
|
86 |
return f"<Tag|#{self.id}|{self.name}|{self.type[:2]}>"
|
87 |
|
|
|
88 |
class Post(BaseModel):
|
89 |
id = IntegerField(primary_key=True)
|
90 |
created_at = CharField()
|
@@ -115,12 +119,15 @@ class Post(BaseModel):
|
|
115 |
file_url = CharField(null=True)
|
116 |
large_file_url = CharField(null=True)
|
117 |
preview_file_url = CharField(null=True)
|
118 |
-
|
119 |
-
updated_at = CharField() # "updated_at": "2024-05-19T01:30:25.096-04:00"
|
120 |
|
121 |
-
|
|
|
|
|
122 |
_tags_cache = None
|
123 |
|
|
|
|
|
|
|
124 |
@property
|
125 |
def tag_count(self):
|
126 |
return len(self.tag_list) if self.tag_list else 0
|
@@ -171,6 +178,7 @@ class Post(BaseModel):
|
|
171 |
def tag_list_meta(self):
|
172 |
return [tag for tag in self.tag_list if tag.type == "meta"]
|
173 |
|
|
|
174 |
# create table LocalPost Table that can contain "filepath", "latentpath"
|
175 |
class LocalPost(BaseModel):
|
176 |
# Usage : LocalPost.create(filepath="path/to/file", latentpath="path/to/latent", post=post)
|
@@ -185,6 +193,7 @@ class LocalPost(BaseModel):
|
|
185 |
def __repr__(self):
|
186 |
return f"<LocalPost|#{self.id}|{self.filepath}|{self.latentpath}|{self.post}>"
|
187 |
|
|
|
188 |
class PostTagRelation(BaseModel):
|
189 |
post = ForeignKeyField(Post, backref="post_tags")
|
190 |
tag = ForeignKeyField(Tag, backref="tag_posts")
|
@@ -212,20 +221,39 @@ def load_db(db_file: str):
|
|
212 |
assert db is not None, "Database is not loaded"
|
213 |
return db
|
214 |
|
|
|
215 |
def print_post_info(post):
|
216 |
"""
|
217 |
Debugging function to print out a post's tags
|
218 |
"""
|
219 |
print(f"Post id : {post.id}")
|
220 |
-
print("General Tags: ", post.tag_count_general, len(post.tag_list_general), post.tag_list_general)
|
221 |
-
print("Artist Tags: ", post.tag_count_artist, len(post.tag_list_artist), post.tag_list_artist)
|
222 |
print(
|
223 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
)
|
225 |
print(
|
226 |
-
"
|
227 |
)
|
228 |
-
print("Meta Tags: ", post.tag_count_meta, len(post.tag_list_meta), post.tag_list_meta)
|
229 |
|
230 |
|
231 |
if __name__ == "__main__":
|
|
|
3 |
|
4 |
from peewee import *
|
5 |
|
6 |
+
|
7 |
class MemoryConnection(sqlite3.Connection):
|
8 |
def __init__(self, dbname, *args, **kwargs):
|
9 |
load_conn = sqlite3.connect(dbname)
|
|
|
35 |
finally:
|
36 |
save_conn.close()
|
37 |
|
38 |
+
|
39 |
db: SqliteDatabase = None
|
40 |
tag_cache_map = {}
|
41 |
|
42 |
+
|
43 |
def get_tag_by_id(tag_id):
|
44 |
if tag_id not in tag_cache_map:
|
45 |
tag_cache_map[tag_id] = Tag.get_by_id(tag_id)
|
46 |
return tag_cache_map[tag_id]
|
47 |
|
48 |
+
|
49 |
class EnumField(IntegerField):
|
50 |
def __init__(self, enum_list, *args, **kwargs):
|
51 |
super().__init__(*args, **kwargs)
|
|
|
86 |
return f"<Tag '{self.name}'>"
|
87 |
|
88 |
def __repr__(self):
|
|
|
89 |
return f"<Tag|#{self.id}|{self.name}|{self.type[:2]}>"
|
90 |
|
91 |
+
|
92 |
class Post(BaseModel):
|
93 |
id = IntegerField(primary_key=True)
|
94 |
created_at = CharField()
|
|
|
119 |
file_url = CharField(null=True)
|
120 |
large_file_url = CharField(null=True)
|
121 |
preview_file_url = CharField(null=True)
|
|
|
|
|
122 |
|
123 |
+
updated_at = CharField() # "updated_at": "2024-05-19T01:30:25.096-04:00"
|
124 |
+
|
125 |
+
_tags: ManyToManyField = None # set by tags.bind
|
126 |
_tags_cache = None
|
127 |
|
128 |
+
def __hash__(self):
|
129 |
+
return self.id
|
130 |
+
|
131 |
@property
|
132 |
def tag_count(self):
|
133 |
return len(self.tag_list) if self.tag_list else 0
|
|
|
178 |
def tag_list_meta(self):
|
179 |
return [tag for tag in self.tag_list if tag.type == "meta"]
|
180 |
|
181 |
+
|
182 |
# create table LocalPost Table that can contain "filepath", "latentpath"
|
183 |
class LocalPost(BaseModel):
|
184 |
# Usage : LocalPost.create(filepath="path/to/file", latentpath="path/to/latent", post=post)
|
|
|
193 |
def __repr__(self):
|
194 |
return f"<LocalPost|#{self.id}|{self.filepath}|{self.latentpath}|{self.post}>"
|
195 |
|
196 |
+
|
197 |
class PostTagRelation(BaseModel):
|
198 |
post = ForeignKeyField(Post, backref="post_tags")
|
199 |
tag = ForeignKeyField(Tag, backref="tag_posts")
|
|
|
221 |
assert db is not None, "Database is not loaded"
|
222 |
return db
|
223 |
|
224 |
+
|
225 |
def print_post_info(post):
|
226 |
"""
|
227 |
Debugging function to print out a post's tags
|
228 |
"""
|
229 |
print(f"Post id : {post.id}")
|
|
|
|
|
230 |
print(
|
231 |
+
"General Tags: ",
|
232 |
+
post.tag_count_general,
|
233 |
+
len(post.tag_list_general),
|
234 |
+
post.tag_list_general,
|
235 |
+
)
|
236 |
+
print(
|
237 |
+
"Artist Tags: ",
|
238 |
+
post.tag_count_artist,
|
239 |
+
len(post.tag_list_artist),
|
240 |
+
post.tag_list_artist,
|
241 |
+
)
|
242 |
+
print(
|
243 |
+
"Character Tags: ",
|
244 |
+
post.tag_count_character,
|
245 |
+
len(post.tag_list_character),
|
246 |
+
post.tag_list_character,
|
247 |
+
)
|
248 |
+
print(
|
249 |
+
"Copyright Tags: ",
|
250 |
+
post.tag_count_copyright,
|
251 |
+
len(post.tag_list_copyright),
|
252 |
+
post.tag_list_copyright,
|
253 |
)
|
254 |
print(
|
255 |
+
"Meta Tags: ", post.tag_count_meta, len(post.tag_list_meta), post.tag_list_meta
|
256 |
)
|
|
|
257 |
|
258 |
|
259 |
if __name__ == "__main__":
|