Give Albums and Tags an author_id column.
Keeping V11 because I hadn't published previous commits yet.
This commit is contained in:
		
							parent
							
								
									cc98cf5407
								
							
						
					
					
						commit
						dffde094e8
					
				
					 7 changed files with 57 additions and 30 deletions
				
			
		|  | @ -41,9 +41,12 @@ CREATE INDEX IF NOT EXISTS index_users_username on users(username COLLATE NOCASE | ||||||
| CREATE TABLE IF NOT EXISTS albums( | CREATE TABLE IF NOT EXISTS albums( | ||||||
|     id TEXT PRIMARY KEY NOT NULL, |     id TEXT PRIMARY KEY NOT NULL, | ||||||
|     title TEXT, |     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_id on albums(id); | ||||||
|  | CREATE INDEX IF NOT EXISTS index_albums_author_id on albums(author_id); | ||||||
| ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ||||||
| CREATE TABLE IF NOT EXISTS bookmarks( | CREATE TABLE IF NOT EXISTS bookmarks( | ||||||
|     id TEXT PRIMARY KEY NOT NULL, |     id TEXT PRIMARY KEY NOT NULL, | ||||||
|  | @ -53,7 +56,7 @@ CREATE TABLE IF NOT EXISTS bookmarks( | ||||||
|     FOREIGN KEY(author_id) REFERENCES users(id) |     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_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( | CREATE TABLE IF NOT EXISTS photos( | ||||||
|     id TEXT PRIMARY KEY NOT NULL, |     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( | CREATE TABLE IF NOT EXISTS tags( | ||||||
|     id TEXT PRIMARY KEY NOT NULL, |     id TEXT PRIMARY KEY NOT NULL, | ||||||
|     name TEXT 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_id on tags(id); | ||||||
| CREATE INDEX IF NOT EXISTS index_tags_name on tags(name); | CREATE INDEX IF NOT EXISTS index_tags_name on tags(name); | ||||||
|  | CREATE INDEX IF NOT EXISTS index_tags_author_id on tags(author_id); | ||||||
| ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -216,6 +216,8 @@ class Album(ObjectBase, GroupableMixin): | ||||||
|         self.id = db_row['id'] |         self.id = db_row['id'] | ||||||
|         self.title = db_row['title'] or '' |         self.title = db_row['title'] or '' | ||||||
|         self.description = db_row['description'] or '' |         self.description = db_row['description'] or '' | ||||||
|  |         self.author_id = db_row['author_id'] | ||||||
|  | 
 | ||||||
|         self.name = 'Album %s' % self.id |         self.name = 'Album %s' % self.id | ||||||
|         self.group_getter = self.photodb.get_album |         self.group_getter = self.photodb.get_album | ||||||
| 
 | 
 | ||||||
|  | @ -1099,6 +1101,8 @@ class Tag(ObjectBase, GroupableMixin): | ||||||
|         self.id = db_row['id'] |         self.id = db_row['id'] | ||||||
|         self.name = db_row['name'] |         self.name = db_row['name'] | ||||||
|         self.description = db_row['description'] or '' |         self.description = db_row['description'] or '' | ||||||
|  |         self.author_id = db_row['author_id'] | ||||||
|  | 
 | ||||||
|         self.group_getter = self.photodb.get_tag |         self.group_getter = self.photodb.get_tag | ||||||
|         self._cached_qualified_name = None |         self._cached_qualified_name = None | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -108,6 +108,7 @@ class PDBAlbumMixin: | ||||||
|             description=None, |             description=None, | ||||||
|             *, |             *, | ||||||
|             associated_directory=None, |             associated_directory=None, | ||||||
|  |             author=None, | ||||||
|             commit=True, |             commit=True, | ||||||
|             photos=None, |             photos=None, | ||||||
|         ): |         ): | ||||||
|  | @ -124,10 +125,14 @@ class PDBAlbumMixin: | ||||||
|             raise TypeError('Description must be string, not %s' % type(description)) |             raise TypeError('Description must be string, not %s' % type(description)) | ||||||
| 
 | 
 | ||||||
|         self.log.debug('New Album: %s %s', album_id, title) |         self.log.debug('New Album: %s %s', album_id, title) | ||||||
|  | 
 | ||||||
|  |         author_id = self.get_user_id_or_none(author) | ||||||
|  | 
 | ||||||
|         data = { |         data = { | ||||||
|             'id': album_id, |             'id': album_id, | ||||||
|             'title': title, |             'title': title, | ||||||
|             'description': description, |             'description': description, | ||||||
|  |             'author_id': author_id, | ||||||
|         } |         } | ||||||
|         self.sql_insert(table='albums', data=data) |         self.sql_insert(table='albums', data=data) | ||||||
| 
 | 
 | ||||||
|  | @ -860,7 +865,7 @@ class PDBTagMixin: | ||||||
| 
 | 
 | ||||||
|     @decorators.required_feature('tag.new') |     @decorators.required_feature('tag.new') | ||||||
|     @decorators.transaction |     @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. |         Register a new tag and return the Tag object. | ||||||
|         ''' |         ''' | ||||||
|  | @ -876,10 +881,12 @@ class PDBTagMixin: | ||||||
| 
 | 
 | ||||||
|         tagid = self.generate_id('tags') |         tagid = self.generate_id('tags') | ||||||
|         self._cached_frozen_children = None |         self._cached_frozen_children = None | ||||||
|  |         author_id = self.get_user_id_or_none(author) | ||||||
|         data = { |         data = { | ||||||
|             'id': tagid, |             'id': tagid, | ||||||
|             'name': tagname, |             'name': tagname, | ||||||
|             'description': description, |             'description': description, | ||||||
|  |             'author_id': author_id, | ||||||
|         } |         } | ||||||
|         self.sql_insert(table='tags', data=data) |         self.sql_insert(table='tags', data=data) | ||||||
| 
 | 
 | ||||||
|  | @ -1189,7 +1196,7 @@ class PDBUtilMixin: | ||||||
|         else: |         else: | ||||||
|             return None |             return None | ||||||
| 
 | 
 | ||||||
|     def easybake(self, ebstring): |     def easybake(self, ebstring, author=None): | ||||||
|         ''' |         ''' | ||||||
|         Easily create tags, groups, and synonyms with a string like |         Easily create tags, groups, and synonyms with a string like | ||||||
|         "group1.group2.tag+synonym" |         "group1.group2.tag+synonym" | ||||||
|  | @ -1204,7 +1211,7 @@ class PDBUtilMixin: | ||||||
|                 item = self.get_tag(name=name) |                 item = self.get_tag(name=name) | ||||||
|                 note = ('existing_tag', item.qualified_name()) |                 note = ('existing_tag', item.qualified_name()) | ||||||
|             except exceptions.NoSuchTag: |             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()) |                 note = ('new_tag', item.qualified_name()) | ||||||
|             output_notes.append(note) |             output_notes.append(note) | ||||||
|             return item |             return item | ||||||
|  |  | ||||||
|  | @ -193,8 +193,11 @@ def post_albums_create(): | ||||||
|     if parent is not None: |     if parent is not None: | ||||||
|         parent = common.P_album(parent) |         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: |     if parent is not None: | ||||||
|         parent.add_child(album) |         parent.add_child(album) | ||||||
|  | 
 | ||||||
|     response = etiquette.jsonify.album(album, minimal=False) |     response = etiquette.jsonify.album(album, minimal=False) | ||||||
|     return jsonify.make_json_response(response) |     return jsonify.make_json_response(response) | ||||||
|  |  | ||||||
|  | @ -97,7 +97,8 @@ def post_tag_create(): | ||||||
|     Create a tag. |     Create a tag. | ||||||
|     ''' |     ''' | ||||||
|     easybake_string = request.form['tagname'] |     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] |     notes = [{'action': action, 'tagname': tagname} for (action, tagname) in notes] | ||||||
|     return jsonify.make_json_response(notes) |     return jsonify.make_json_response(notes) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -178,28 +178,28 @@ def upgrade_9_to_10(photodb): | ||||||
| 
 | 
 | ||||||
| def upgrade_10_to_11(photodb): | def upgrade_10_to_11(photodb): | ||||||
|     ''' |     ''' | ||||||
|     Added Primary keys, Foreign keys, and NOT NULL constraints which cannot be |     Added Primary keys, Foreign keys, and NOT NULL constraints. | ||||||
|     done in-place and requires the reconstruction of all the affected tables. |     Added author_id column to Album and Tag tables. | ||||||
|     ''' |     ''' | ||||||
|     tables_to_delete = [ |     tables_to_copy = { | ||||||
|         'users', |         'users': '*', | ||||||
|         'albums', |         'albums': '*, NULL', | ||||||
|         'bookmarks', |         'bookmarks': '*', | ||||||
|         'photos', |         'photos': '*', | ||||||
|         'tags', |         'tags': '*, NULL', | ||||||
|         'album_associated_directories', |         'album_associated_directories': '*', | ||||||
|         'album_group_rel', |         'album_group_rel': '*', | ||||||
|         'album_photo_rel', |         'album_photo_rel': '*', | ||||||
|         'id_numbers', |         'id_numbers': '*', | ||||||
|         'photo_tag_rel', |         'photo_tag_rel': '*', | ||||||
|         'tag_group_rel', |         'tag_group_rel': '*', | ||||||
|         'tag_synonyms', |         'tag_synonyms': '*', | ||||||
|     ] |     } | ||||||
|     cur = photodb.sql.cursor() |     cur = photodb.sql.cursor() | ||||||
|     cur.execute('PRAGMA foreign_keys = OFF') |     cur.execute('PRAGMA foreign_keys = OFF') | ||||||
| 
 | 
 | ||||||
|     print('Renaming existing tables.') |     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) |         statement = 'ALTER TABLE %s RENAME TO %s_old' % (table, table) | ||||||
|         cur.execute(statement) |         cur.execute(statement) | ||||||
| 
 | 
 | ||||||
|  | @ -215,8 +215,8 @@ def upgrade_10_to_11(photodb): | ||||||
|         cur.execute(statement) |         cur.execute(statement) | ||||||
| 
 | 
 | ||||||
|     print('Migrating table data.') |     print('Migrating table data.') | ||||||
|     for table in tables_to_delete: |     for (table, select_columns) in tables_to_copy.items(): | ||||||
|         statement = 'INSERT INTO %s SELECT * FROM %s_old' % (table, table) |         statement = 'INSERT INTO %s SELECT %s FROM %s_old' % (table, select_columns, table) | ||||||
|         cur.execute(statement) |         cur.execute(statement) | ||||||
|         statement = 'DROP TABLE %s_old' % table |         statement = 'DROP TABLE %s_old' % table | ||||||
|         cur.execute(statement) |         cur.execute(statement) | ||||||
|  |  | ||||||
|  | @ -17,9 +17,12 @@ CREATE INDEX IF NOT EXISTS index_users_username on users(username COLLATE NOCASE | ||||||
| CREATE TABLE IF NOT EXISTS albums( | CREATE TABLE IF NOT EXISTS albums( | ||||||
|     id TEXT PRIMARY KEY NOT NULL, |     id TEXT PRIMARY KEY NOT NULL, | ||||||
|     title TEXT, |     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_id on albums(id); | ||||||
|  | CREATE INDEX IF NOT EXISTS index_albums_author_id on albums(author_id); | ||||||
| ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ||||||
| CREATE TABLE IF NOT EXISTS bookmarks( | CREATE TABLE IF NOT EXISTS bookmarks( | ||||||
|     id TEXT PRIMARY KEY NOT NULL, |     id TEXT PRIMARY KEY NOT NULL, | ||||||
|  | @ -29,7 +32,7 @@ CREATE TABLE IF NOT EXISTS bookmarks( | ||||||
|     FOREIGN KEY(author_id) REFERENCES users(id) |     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_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( | CREATE TABLE IF NOT EXISTS photos( | ||||||
|     id TEXT PRIMARY KEY NOT NULL, |     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( | CREATE TABLE IF NOT EXISTS tags( | ||||||
|     id TEXT PRIMARY KEY NOT NULL, |     id TEXT PRIMARY KEY NOT NULL, | ||||||
|     name TEXT 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_id on tags(id); | ||||||
| CREATE INDEX IF NOT EXISTS index_tags_name on tags(name); | CREATE INDEX IF NOT EXISTS index_tags_name on tags(name); | ||||||
|  | CREATE INDEX IF NOT EXISTS index_tags_author_id on tags(author_id); | ||||||
| ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue