From b5902ba4f1af4f9c9094aa6f9267a21135d2c7aa Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 9 Jan 2018 21:15:50 -0800 Subject: [PATCH] Integrate voussoirkit sqlhelpers. https://github.com/voussoir/else/tree/master/SQLHelpers --- etiquette/helpers.py | 30 ------------------------------ etiquette/objects.py | 3 ++- etiquette/photodb.py | 11 ++++++----- requirements.txt | 2 +- 4 files changed, 9 insertions(+), 37 deletions(-) diff --git a/etiquette/helpers.py b/etiquette/helpers.py index ab7231c..24e6076 100644 --- a/etiquette/helpers.py +++ b/etiquette/helpers.py @@ -51,36 +51,6 @@ def album_zip_filenames(album, recursive=True): return arcnames -def binding_filler(column_names, values, require_all=True): - ''' - Manually aligning question marks and bindings is annoying. - Given the table's column names and a dictionary of {column: value}, - return the question marks and the list of bindings in the right order. - - require_all: - If `values` does not contain one of the column names, should we raise - an exception? - Otherwise, that column will simply receive None. - - Ex: - column_names=['id', 'name', 'score'], - values={'score': 20, 'id': '1111', 'name': 'James'} - -> - returns ('?, ?, ?', ['1111', 'James', 20]) - ''' - values = values.copy() - for column in column_names: - if column in values: - continue - if require_all: - raise ValueError('Missing column "%s"' % column) - else: - values.setdefault(column, None) - qmarks = '?' * len(column_names) - qmarks = ', '.join(qmarks) - bindings = [values[column] for column in column_names] - return (qmarks, bindings) - def checkerboard_image(color_1, color_2, image_size, checker_size): ''' Generate a PIL Image with a checkerboard pattern. diff --git a/etiquette/objects.py b/etiquette/objects.py index 1f28c1a..0368cbb 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -10,6 +10,7 @@ from . import helpers from voussoirkit import bytestring from voussoirkit import pathclass from voussoirkit import spinal +from voussoirkit import sqlhelpers class ObjectBase: @@ -284,7 +285,7 @@ class Album(ObjectBase, GroupableMixin): 'albumid': self.id, 'directory': filepath.absolute_path, } - (qmarks, bindings) = helpers.binding_filler(constants.SQL_ALBUM_DIRECTORY_COLUMNS, data) + (qmarks, bindings) = sqlhelpers.insert_filler(constants.SQL_ALBUM_DIRECTORY_COLUMNS, data) query = 'INSERT INTO album_associated_directories VALUES(%s)' % qmarks cur = self.photodb.sql.cursor() cur.execute(query, bindings) diff --git a/etiquette/photodb.py b/etiquette/photodb.py index 647d6bc..b58cf65 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -21,6 +21,7 @@ from voussoirkit import cacheclass from voussoirkit import expressionmatch from voussoirkit import pathclass from voussoirkit import spinal +from voussoirkit import sqlhelpers logging.basicConfig() @@ -247,7 +248,7 @@ class PDBAlbumMixin: 'description': description, } - (qmarks, bindings) = helpers.binding_filler(constants.SQL_ALBUM_COLUMNS, data) + (qmarks, bindings) = sqlhelpers.insert_filler(constants.SQL_ALBUM_COLUMNS, data) query = 'INSERT INTO albums VALUES(%s)' % qmarks cur.execute(query, bindings) @@ -297,7 +298,7 @@ class PDBBookmarkMixin: 'url': url, } - (qmarks, bindings) = helpers.binding_filler(constants.SQL_BOOKMARK_COLUMNS, data) + (qmarks, bindings) = sqlhelpers.insert_filler(constants.SQL_BOOKMARK_COLUMNS, data) query = 'INSERT INTO bookmarks VALUES(%s)' % qmarks cur = self.sql.cursor() cur.execute(query, bindings) @@ -409,7 +410,7 @@ class PDBPhotoMixin: 'thumbnail': None, } - (qmarks, bindings) = helpers.binding_filler(constants.SQL_PHOTO_COLUMNS, data) + (qmarks, bindings) = sqlhelpers.insert_filler(constants.SQL_PHOTO_COLUMNS, data) query = 'INSERT INTO photos VALUES(%s)' % qmarks cur = self.sql.cursor() cur.execute(query, bindings) @@ -918,7 +919,7 @@ class PDBTagMixin: 'name': tagname, 'description': description, } - (qmarks, bindings) = helpers.binding_filler(constants.SQL_TAG_COLUMNS, data) + (qmarks, bindings) = sqlhelpers.insert_filler(constants.SQL_TAG_COLUMNS, data) query = 'INSERT INTO tags VALUES(%s)' % qmarks cur.execute(query, bindings) if commit: @@ -1086,7 +1087,7 @@ class PDBUserMixin: 'created': created, } - (qmarks, bindings) = helpers.binding_filler(constants.SQL_USER_COLUMNS, data) + (qmarks, bindings) = sqlhelpers.insert_filler(constants.SQL_USER_COLUMNS, data) query = 'INSERT INTO users VALUES(%s)' % qmarks cur = self.sql.cursor() cur.execute(query, bindings) diff --git a/requirements.txt b/requirements.txt index 30f7c2f..ef1156c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,6 @@ bcrypt flask gevent pillow -voussoirkit +voussoirkit>=0.0.18 zipstream git+https://github.com/senko/python-video-converter.git