Give each object class a `table` and centralize db_row normer.

master
voussoir 2019-04-26 18:52:56 -07:00
parent fa97dd5d3c
commit ebde5c7ee8
1 changed files with 21 additions and 11 deletions

View File

@ -18,6 +18,12 @@ from . import exceptions
from . import helpers from . import helpers
def normalize_db_row(db_row, table):
if isinstance(db_row, (list, tuple)):
db_row = dict(zip(constants.SQL_COLUMNS[table], db_row))
return db_row
class ObjectBase: class ObjectBase:
def __init__(self, photodb): def __init__(self, photodb):
super().__init__() super().__init__()
@ -228,13 +234,13 @@ class GroupableMixin:
class Album(ObjectBase, GroupableMixin): class Album(ObjectBase, GroupableMixin):
table = 'albums'
group_table = 'album_group_rel' group_table = 'album_group_rel'
group_sql_index = constants.SQL_INDEX[group_table] group_sql_index = constants.SQL_INDEX[group_table]
def __init__(self, photodb, db_row): def __init__(self, photodb, db_row):
super().__init__(photodb) super().__init__(photodb)
if isinstance(db_row, (list, tuple)): db_row = normalize_db_row(db_row, self.table)
db_row = dict(zip(constants.SQL_COLUMNS['albums'], db_row))
self.id = db_row['id'] self.id = db_row['id']
self.title = self.normalize_title(db_row['title']) self.title = self.normalize_title(db_row['title'])
@ -549,10 +555,11 @@ class Album(ObjectBase, GroupableMixin):
class Bookmark(ObjectBase): class Bookmark(ObjectBase):
table = 'bookmarks'
def __init__(self, photodb, db_row): def __init__(self, photodb, db_row):
super().__init__(photodb) super().__init__(photodb)
if isinstance(db_row, (list, tuple)): db_row = normalize_db_row(db_row, self.table)
db_row = dict(zip(constants.SQL_COLUMNS['bookmarks'], db_row))
self.id = db_row['id'] self.id = db_row['id']
self.title = self.normalize_title(db_row['title']) self.title = self.normalize_title(db_row['title'])
@ -634,10 +641,11 @@ class Photo(ObjectBase):
Photo objects cannot exist without a corresponding PhotoDB object, because Photo objects cannot exist without a corresponding PhotoDB object, because
Photos are not the actual image data, just the database entry. Photos are not the actual image data, just the database entry.
''' '''
table = 'photos'
def __init__(self, photodb, db_row): def __init__(self, photodb, db_row):
super().__init__(photodb) super().__init__(photodb)
if isinstance(db_row, (list, tuple)): db_row = normalize_db_row(db_row, self.table)
db_row = dict(zip(constants.SQL_COLUMNS['photos'], db_row))
self.real_path = db_row['filepath'] self.real_path = db_row['filepath']
self.real_path = helpers.remove_path_badchars(self.real_path, allowed=':\\/') self.real_path = helpers.remove_path_badchars(self.real_path, allowed=':\\/')
@ -1127,15 +1135,15 @@ class Tag(ObjectBase, GroupableMixin):
''' '''
A Tag, which can be applied to Photos for organization. A Tag, which can be applied to Photos for organization.
''' '''
table = 'tags'
group_table = 'tag_group_rel' group_table = 'tag_group_rel'
group_sql_index = constants.SQL_INDEX[group_table] group_sql_index = constants.SQL_INDEX[group_table]
def __init__(self, photodb, db_row): def __init__(self, photodb, db_row):
super().__init__(photodb) super().__init__(photodb)
if isinstance(db_row, (list, tuple)): db_row = normalize_db_row(db_row, self.table)
db_row = dict(zip(constants.SQL_COLUMNS['tags'], db_row))
self.id = db_row['id']
self.id = db_row['id']
# Do not pass the name through the normalizer. It may be grandfathered # Do not pass the name through the normalizer. It may be grandfathered
# from previous character / length rules. # from previous character / length rules.
self.name = db_row['name'] self.name = db_row['name']
@ -1407,10 +1415,12 @@ class User(ObjectBase):
''' '''
A dear friend of ours. A dear friend of ours.
''' '''
table = 'users'
def __init__(self, photodb, db_row): def __init__(self, photodb, db_row):
super().__init__(photodb) super().__init__(photodb)
if isinstance(db_row, (list, tuple)): db_row = normalize_db_row(db_row, self.table)
db_row = dict(zip(constants.SQL_COLUMNS['users'], db_row))
self.id = db_row['id'] self.id = db_row['id']
self.username = db_row['username'] self.username = db_row['username']
self.created = db_row['created'] self.created = db_row['created']