From dffde094e884ebc420b35e1159968b00d851ca96 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 18 Mar 2018 15:28:26 -0700 Subject: [PATCH] Give Albums and Tags an author_id column. Keeping V11 because I hadn't published previous commits yet. --- etiquette/constants.py | 12 ++++-- etiquette/objects.py | 4 ++ etiquette/photodb.py | 13 +++++-- .../endpoints/album_endpoints.py | 5 ++- .../endpoints/tag_endpoints.py | 3 +- .../database_upgrader/database_upgrader.py | 38 +++++++++---------- utilities/database_upgrader/old_inits.py | 12 ++++-- 7 files changed, 57 insertions(+), 30 deletions(-) diff --git a/etiquette/constants.py b/etiquette/constants.py index 45c53e5..12ea811 100644 --- a/etiquette/constants.py +++ b/etiquette/constants.py @@ -41,9 +41,12 @@ CREATE INDEX IF NOT EXISTS index_users_username on users(username COLLATE NOCASE CREATE TABLE IF NOT EXISTS albums( id TEXT PRIMARY KEY NOT NULL, title TEXT, - description TEXT + description TEXT, + author_id TEXT, + FOREIGN KEY(author_id) REFERENCES users(id) ); CREATE INDEX IF NOT EXISTS index_albums_id on albums(id); +CREATE INDEX IF NOT EXISTS index_albums_author_id on albums(author_id); ---------------------------------------------------------------------------------------------------- CREATE TABLE IF NOT EXISTS bookmarks( id TEXT PRIMARY KEY NOT NULL, @@ -53,7 +56,7 @@ CREATE TABLE IF NOT EXISTS bookmarks( FOREIGN KEY(author_id) REFERENCES users(id) ); CREATE INDEX IF NOT EXISTS index_bookmarks_id on bookmarks(id); -CREATE INDEX IF NOT EXISTS index_bookmarks_author on bookmarks(author_id); +CREATE INDEX IF NOT EXISTS index_bookmarks_author_id on bookmarks(author_id); ---------------------------------------------------------------------------------------------------- CREATE TABLE IF NOT EXISTS photos( id TEXT PRIMARY KEY NOT NULL, @@ -85,10 +88,13 @@ CREATE INDEX IF NOT EXISTS index_photos_searchhidden on photos(searchhidden); CREATE TABLE IF NOT EXISTS tags( id TEXT PRIMARY KEY NOT NULL, name TEXT NOT NULL, - description TEXT + description TEXT, + author_id TEXT, + FOREIGN KEY(author_id) REFERENCES users(id) ); CREATE INDEX IF NOT EXISTS index_tags_id on tags(id); CREATE INDEX IF NOT EXISTS index_tags_name on tags(name); +CREATE INDEX IF NOT EXISTS index_tags_author_id on tags(author_id); ---------------------------------------------------------------------------------------------------- diff --git a/etiquette/objects.py b/etiquette/objects.py index 2018160..5d7700f 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -216,6 +216,8 @@ class Album(ObjectBase, GroupableMixin): self.id = db_row['id'] self.title = db_row['title'] or '' self.description = db_row['description'] or '' + self.author_id = db_row['author_id'] + self.name = 'Album %s' % self.id self.group_getter = self.photodb.get_album @@ -1099,6 +1101,8 @@ class Tag(ObjectBase, GroupableMixin): self.id = db_row['id'] self.name = db_row['name'] self.description = db_row['description'] or '' + self.author_id = db_row['author_id'] + self.group_getter = self.photodb.get_tag self._cached_qualified_name = None diff --git a/etiquette/photodb.py b/etiquette/photodb.py index a6bacf1..e2c2acf 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -108,6 +108,7 @@ class PDBAlbumMixin: description=None, *, associated_directory=None, + author=None, commit=True, photos=None, ): @@ -124,10 +125,14 @@ class PDBAlbumMixin: raise TypeError('Description must be string, not %s' % type(description)) self.log.debug('New Album: %s %s', album_id, title) + + author_id = self.get_user_id_or_none(author) + data = { 'id': album_id, 'title': title, 'description': description, + 'author_id': author_id, } self.sql_insert(table='albums', data=data) @@ -860,7 +865,7 @@ class PDBTagMixin: @decorators.required_feature('tag.new') @decorators.transaction - def new_tag(self, tagname, description=None, *, commit=True): + def new_tag(self, tagname, description=None, *, author=None, commit=True): ''' Register a new tag and return the Tag object. ''' @@ -876,10 +881,12 @@ class PDBTagMixin: tagid = self.generate_id('tags') self._cached_frozen_children = None + author_id = self.get_user_id_or_none(author) data = { 'id': tagid, 'name': tagname, 'description': description, + 'author_id': author_id, } self.sql_insert(table='tags', data=data) @@ -1189,7 +1196,7 @@ class PDBUtilMixin: else: return None - def easybake(self, ebstring): + def easybake(self, ebstring, author=None): ''' Easily create tags, groups, and synonyms with a string like "group1.group2.tag+synonym" @@ -1204,7 +1211,7 @@ class PDBUtilMixin: item = self.get_tag(name=name) note = ('existing_tag', item.qualified_name()) except exceptions.NoSuchTag: - item = self.new_tag(name, commit=False) + item = self.new_tag(name, author=author, commit=False) note = ('new_tag', item.qualified_name()) output_notes.append(note) return item diff --git a/frontends/etiquette_flask/etiquette_flask/endpoints/album_endpoints.py b/frontends/etiquette_flask/etiquette_flask/endpoints/album_endpoints.py index 5c8adb5..69358f0 100644 --- a/frontends/etiquette_flask/etiquette_flask/endpoints/album_endpoints.py +++ b/frontends/etiquette_flask/etiquette_flask/endpoints/album_endpoints.py @@ -193,8 +193,11 @@ def post_albums_create(): if parent is not None: parent = common.P_album(parent) - album = common.P.new_album(title=title, description=description) + user = session_manager.get(request).user + + album = common.P.new_album(title=title, description=description, author=user) if parent is not None: parent.add_child(album) + response = etiquette.jsonify.album(album, minimal=False) return jsonify.make_json_response(response) diff --git a/frontends/etiquette_flask/etiquette_flask/endpoints/tag_endpoints.py b/frontends/etiquette_flask/etiquette_flask/endpoints/tag_endpoints.py index 3074893..ab1f304 100644 --- a/frontends/etiquette_flask/etiquette_flask/endpoints/tag_endpoints.py +++ b/frontends/etiquette_flask/etiquette_flask/endpoints/tag_endpoints.py @@ -97,7 +97,8 @@ def post_tag_create(): Create a tag. ''' easybake_string = request.form['tagname'] - notes = common.P.easybake(easybake_string) + user = session_manager.get(request).user + notes = common.P.easybake(easybake_string, author=user) notes = [{'action': action, 'tagname': tagname} for (action, tagname) in notes] return jsonify.make_json_response(notes) diff --git a/utilities/database_upgrader/database_upgrader.py b/utilities/database_upgrader/database_upgrader.py index 6bc424a..0e420da 100644 --- a/utilities/database_upgrader/database_upgrader.py +++ b/utilities/database_upgrader/database_upgrader.py @@ -178,28 +178,28 @@ def upgrade_9_to_10(photodb): def upgrade_10_to_11(photodb): ''' - Added Primary keys, Foreign keys, and NOT NULL constraints which cannot be - done in-place and requires the reconstruction of all the affected tables. + Added Primary keys, Foreign keys, and NOT NULL constraints. + Added author_id column to Album and Tag tables. ''' - tables_to_delete = [ - 'users', - 'albums', - 'bookmarks', - 'photos', - 'tags', - 'album_associated_directories', - 'album_group_rel', - 'album_photo_rel', - 'id_numbers', - 'photo_tag_rel', - 'tag_group_rel', - 'tag_synonyms', - ] + tables_to_copy = { + 'users': '*', + 'albums': '*, NULL', + 'bookmarks': '*', + 'photos': '*', + 'tags': '*, NULL', + 'album_associated_directories': '*', + 'album_group_rel': '*', + 'album_photo_rel': '*', + 'id_numbers': '*', + 'photo_tag_rel': '*', + 'tag_group_rel': '*', + 'tag_synonyms': '*', + } cur = photodb.sql.cursor() cur.execute('PRAGMA foreign_keys = OFF') print('Renaming existing tables.') - for table in tables_to_delete: + for table in tables_to_copy: statement = 'ALTER TABLE %s RENAME TO %s_old' % (table, table) cur.execute(statement) @@ -215,8 +215,8 @@ def upgrade_10_to_11(photodb): cur.execute(statement) print('Migrating table data.') - for table in tables_to_delete: - statement = 'INSERT INTO %s SELECT * FROM %s_old' % (table, table) + for (table, select_columns) in tables_to_copy.items(): + statement = 'INSERT INTO %s SELECT %s FROM %s_old' % (table, select_columns, table) cur.execute(statement) statement = 'DROP TABLE %s_old' % table cur.execute(statement) diff --git a/utilities/database_upgrader/old_inits.py b/utilities/database_upgrader/old_inits.py index 99e7f04..c5bf7a0 100644 --- a/utilities/database_upgrader/old_inits.py +++ b/utilities/database_upgrader/old_inits.py @@ -17,9 +17,12 @@ CREATE INDEX IF NOT EXISTS index_users_username on users(username COLLATE NOCASE CREATE TABLE IF NOT EXISTS albums( id TEXT PRIMARY KEY NOT NULL, title TEXT, - description TEXT + description TEXT, + author_id TEXT, + FOREIGN KEY(author_id) REFERENCES users(id) ); CREATE INDEX IF NOT EXISTS index_albums_id on albums(id); +CREATE INDEX IF NOT EXISTS index_albums_author_id on albums(author_id); ---------------------------------------------------------------------------------------------------- CREATE TABLE IF NOT EXISTS bookmarks( id TEXT PRIMARY KEY NOT NULL, @@ -29,7 +32,7 @@ CREATE TABLE IF NOT EXISTS bookmarks( FOREIGN KEY(author_id) REFERENCES users(id) ); CREATE INDEX IF NOT EXISTS index_bookmarks_id on bookmarks(id); -CREATE INDEX IF NOT EXISTS index_bookmarks_author on bookmarks(author_id); +CREATE INDEX IF NOT EXISTS index_bookmarks_author_id on bookmarks(author_id); ---------------------------------------------------------------------------------------------------- CREATE TABLE IF NOT EXISTS photos( id TEXT PRIMARY KEY NOT NULL, @@ -61,10 +64,13 @@ CREATE INDEX IF NOT EXISTS index_photos_searchhidden on photos(searchhidden); CREATE TABLE IF NOT EXISTS tags( id TEXT PRIMARY KEY NOT NULL, name TEXT NOT NULL, - description TEXT + description TEXT, + author_id TEXT, + FOREIGN KEY(author_id) REFERENCES users(id) ); CREATE INDEX IF NOT EXISTS index_tags_id on tags(id); CREATE INDEX IF NOT EXISTS index_tags_name on tags(name); +CREATE INDEX IF NOT EXISTS index_tags_author_id on tags(author_id); ----------------------------------------------------------------------------------------------------