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( | ||||
|     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); | ||||
| ---------------------------------------------------------------------------------------------------- | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -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 | ||||
| 
 | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
|  | @ -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) | ||||
| 
 | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
|  | @ -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); | ||||
| ---------------------------------------------------------------------------------------------------- | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue