From 232f8231e0b263983e99e67c76866798710ec230 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 20 Dec 2016 14:54:23 -0800 Subject: [PATCH] Add author column to photos --- constants.py | 1 + etiquette_upgrader.py | 7 +++++++ objects.py | 4 ++++ phototagger.py | 16 ++++++++++++++-- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/constants.py b/constants.py index 79bdbd6..6933f8d 100644 --- a/constants.py +++ b/constants.py @@ -48,6 +48,7 @@ SQL_PHOTO_COLUMNS = [ 'created', 'thumbnail', 'tagged_at', + 'author_id', ] SQL_TAG_COLUMNS = [ 'id', diff --git a/etiquette_upgrader.py b/etiquette_upgrader.py index 02a47d5..31f9c12 100644 --- a/etiquette_upgrader.py +++ b/etiquette_upgrader.py @@ -31,6 +31,13 @@ def upgrade_2_to_3(sql): cur.execute('CREATE INDEX IF NOT EXISTS index_user_id ON users(id)') cur.execute('CREATE INDEX IF NOT EXISTS index_user_username ON users(username COLLATE NOCASE)') +def upgrade_3_to_4(sql): + ''' + Add an `author_id` column to Photos. + ''' + cur = sql.cursor() + cur.execute('ALTER TABLE photos ADD COLUMN author_id TEXT') + cur.execute('CREATE INDEX IF NOT EXISTS index_photo_author on photos(author_id)') def upgrade_all(database_filename): ''' diff --git a/objects.py b/objects.py index 0f8ba26..ac186d8 100644 --- a/objects.py +++ b/objects.py @@ -287,6 +287,7 @@ class Photo(ObjectBase): self.created = row_tuple['created'] self.thumbnail = row_tuple['thumbnail'] self.tagged_at = row_tuple['tagged_at'] + self.author_id = row_tuple['author_id'] def __reinit__(self): ''' @@ -336,6 +337,9 @@ class Photo(ObjectBase): albums = [self.photodb.get_album(f[0]) for f in fetch] return albums + def author(self): + return self.photodb.get_user(id=self.author_id) + def bytestring(self): return bytestring.bytestring(self.bytes) diff --git a/phototagger.py b/phototagger.py index ed2f287..4c84898 100644 --- a/phototagger.py +++ b/phototagger.py @@ -28,7 +28,7 @@ logging.getLogger('PIL.PngImagePlugin').setLevel(logging.WARNING) # Note: Setting user_version pragma in init sequence is safe because it only # happens after the out-of-date check occurs, so no chance of accidentally # overwriting it. -DATABASE_VERSION = 3 +DATABASE_VERSION = 4 DB_INIT = ''' PRAGMA count_changes = OFF; PRAGMA cache_size = 10000; @@ -52,7 +52,8 @@ CREATE TABLE IF NOT EXISTS photos( bytes INT, created INT, thumbnail TEXT, - tagged_at INT + tagged_at INT, + author_id TEXT ); CREATE TABLE IF NOT EXISTS tags( id TEXT, @@ -96,6 +97,7 @@ CREATE INDEX IF NOT EXISTS index_photo_path on photos(filepath COLLATE NOCASE); CREATE INDEX IF NOT EXISTS index_photo_fakepath on photos(override_filename COLLATE NOCASE); CREATE INDEX IF NOT EXISTS index_photo_created on photos(created); CREATE INDEX IF NOT EXISTS index_photo_extension on photos(extension); +CREATE INDEX IF NOT EXISTS index_photo_author on photos(author_id); -- Tag CREATE INDEX IF NOT EXISTS index_tag_id on tags(id); @@ -435,6 +437,7 @@ class PDBPhotoMixin: filename, *, allow_duplicates=False, + author=None, commit=True, do_metadata=True, do_thumbnail=True, @@ -461,6 +464,14 @@ class PDBPhotoMixin: exc.photo = existing raise exc + if isinstance(author, objects.User): + if author.photodb != self: + raise ValueError('That user does not belong to this photodb') + author_id = author.id + elif author is not None: + # Just to confirm + author_id = self.get_user(id=author).id + extension = os.path.splitext(filename)[1] extension = extension.replace('.', '') extension = self.normalize_tagname(extension) @@ -474,6 +485,7 @@ class PDBPhotoMixin: 'extension': extension, 'created': created, 'tagged_at': None, + 'author_id': author_id, # These will be filled in during the metadata stage. 'bytes': None, 'width': None,