misc cleanup

This commit is contained in:
voussoir 2016-12-16 17:59:43 -08:00
parent 1c7b736b1a
commit a47cdaaf04
3 changed files with 76 additions and 66 deletions

View file

@ -12,6 +12,7 @@ This is the readme file.
- Move out more helpers - Move out more helpers
- Create objects.py - Create objects.py
- Debate whether the `UserMixin.login` method should accept usernames or I should standardize the usage of IDs only internally. - Debate whether the `UserMixin.login` method should accept usernames or I should standardize the usage of IDs only internally.
- Move config type variables out of constants and create a real config system.
### Changelog ### Changelog

View file

@ -48,7 +48,7 @@ def upgrade_all(database_filename):
needed_version = phototagger.DATABASE_VERSION needed_version = phototagger.DATABASE_VERSION
if current_version == needed_version: if current_version == needed_version:
print('Already up-to-date.') print('Already up-to-date with version %d.' % needed_version)
return return
for version_number in range(current_version + 1, needed_version + 1): for version_number in range(current_version + 1, needed_version + 1):

View file

@ -498,11 +498,12 @@ class PDBAlbumMixin:
if not isinstance(description, str): if not isinstance(description, str):
raise TypeError('Description must be string, not %s' % type(description)) raise TypeError('Description must be string, not %s' % type(description))
data = {} data = {
data['id'] = albumid 'id': albumid,
data['title'] = title 'title': title,
data['description'] = description 'description': description,
data['associated_directory'] = associated_directory 'associated_directory': associated_directory,
}
(qmarks, bindings) = binding_filler(SQL_ALBUM_COLUMNS, data) (qmarks, bindings) = binding_filler(SQL_ALBUM_COLUMNS, data)
query = 'INSERT INTO albums VALUES(%s)' % qmarks query = 'INSERT INTO albums VALUES(%s)' % qmarks
@ -557,23 +558,6 @@ class PDBPhotoMixin:
if count <= 0: if count <= 0:
break break
def purge_deleted_files(self):
'''
Remove Photo entries if their corresponding file is no longer found.
'''
photos = self.get_photos_by_recent()
for photo in photos:
if os.path.exists(photo.real_filepath):
continue
photo.delete()
def purge_empty_albums(self):
albums = self.get_albums()
for album in albums:
if album.children() or album.photos():
continue
album.delete()
def new_photo( def new_photo(
self, self,
filename, filename,
@ -611,21 +595,22 @@ class PDBPhotoMixin:
created = int(getnow()) created = int(getnow())
photoid = self.generate_id('photos') photoid = self.generate_id('photos')
data = {} data = {
data['id'] = photoid 'id': photoid,
data['filepath'] = filename 'filepath': filename,
data['override_filename'] = None 'override_filename': None,
data['extension'] = extension 'extension': extension,
data['created'] = created 'created': created,
data['tagged_at'] = None 'tagged_at': None,
# These will be filled in during the metadata stage. # These will be filled in during the metadata stage.
data['bytes'] = None 'bytes': None,
data['width'] = None 'width': None,
data['height'] = None 'height': None,
data['area'] = None 'area': None,
data['ratio'] = None 'ratio': None,
data['duration'] = None 'duration': None,
data['thumbnail'] = None 'thumbnail': None,
}
(qmarks, bindings) = binding_filler(SQL_PHOTO_COLUMNS, data) (qmarks, bindings) = binding_filler(SQL_PHOTO_COLUMNS, data)
query = 'INSERT INTO photos VALUES(%s)' % qmarks query = 'INSERT INTO photos VALUES(%s)' % qmarks
@ -647,6 +632,23 @@ class PDBPhotoMixin:
self.commit() self.commit()
return photo return photo
def purge_deleted_files(self):
'''
Remove Photo entries if their corresponding file is no longer found.
'''
photos = self.get_photos_by_recent()
for photo in photos:
if os.path.exists(photo.real_filepath):
continue
photo.delete()
def purge_empty_albums(self):
albums = self.get_albums()
for album in albums:
if album.children() or album.photos():
continue
album.delete()
def search( def search(
self, self,
*, *,
@ -1005,11 +1007,12 @@ class PDBUserMixin:
hashed_password = bcrypt.hashpw(password, bcrypt.gensalt()) hashed_password = bcrypt.hashpw(password, bcrypt.gensalt())
created = int(getnow()) created = int(getnow())
data = {} data = {
data['id'] = user_id 'id': user_id,
data['username'] = username 'username': username,
data['password'] = hashed_password 'password': hashed_password,
data['created'] = created 'created': created,
}
(qmarks, bindings) = binding_filler(SQL_USER_COLUMNS, data) (qmarks, bindings) = binding_filler(SQL_USER_COLUMNS, data)
query = 'INSERT INTO users VALUES(%s)' % qmarks query = 'INSERT INTO users VALUES(%s)' % qmarks
@ -1330,7 +1333,7 @@ class PhotoDB(PDBAlbumMixin, PDBPhotoMixin, PDBTagMixin, PDBUserMixin):
return new_id return new_id
def get_thing_by_id(self, thing_type, thing_id): def get_thing_by_id(self, thing_type, thing_id):
thing_map = self.thing_map(thing_type) thing_map = _THING_CLASSES[thing_type]
if isinstance(thing_id, thing_map['class']): if isinstance(thing_id, thing_map['class']):
thing_id = thing_id.id thing_id = thing_id.id
@ -1344,7 +1347,7 @@ class PhotoDB(PDBAlbumMixin, PDBPhotoMixin, PDBTagMixin, PDBUserMixin):
return thing return thing
def get_things(self, thing_type, orderby=None): def get_things(self, thing_type, orderby=None):
thing_map = self.thing_map(thing_type) thing_map = _THING_CLASSES[thing_type]
if orderby: if orderby:
self.cur.execute('SELECT * FROM %s ORDER BY %s' % (thing_map['table'], orderby)) self.cur.execute('SELECT * FROM %s ORDER BY %s' % (thing_map['table'], orderby))
@ -1356,27 +1359,6 @@ class PhotoDB(PDBAlbumMixin, PDBPhotoMixin, PDBTagMixin, PDBUserMixin):
thing = thing_map['class'](self, row_tuple=thing) thing = thing_map['class'](self, row_tuple=thing)
yield thing yield thing
def thing_map(self, thing_type):
return {
'album':
{
'class': Album,
'exception': exceptions.NoSuchAlbum,
'table': 'albums',
},
'tag':
{
'class': Tag,
'exception': exceptions.NoSuchTag,
'table': 'tags',
},
'photo':
{
'class': Photo,
'exception': exceptions.NoSuchPhoto,
'table': 'photos',
},
}[thing_type]
#################################################################################################### ####################################################################################################
#################################################################################################### ####################################################################################################
@ -2158,6 +2140,33 @@ class User(ObjectBase):
return rep return rep
_THING_CLASSES = {
'album':
{
'class': Album,
'exception': exceptions.NoSuchAlbum,
'table': 'albums',
},
'photo':
{
'class': Photo,
'exception': exceptions.NoSuchPhoto,
'table': 'photos',
},
'tag':
{
'class': Tag,
'exception': exceptions.NoSuchTag,
'table': 'tags',
},
'user':
{
'class': User,
'exception': exceptions.NoSuchUser,
'table': 'users',
}
}
if __name__ == '__main__': if __name__ == '__main__':
p = PhotoDB() p = PhotoDB()
print(p) print(p)