diff --git a/etiquette/constants.py b/etiquette/constants.py index 319bf90..b816379 100644 --- a/etiquette/constants.py +++ b/etiquette/constants.py @@ -65,6 +65,7 @@ SQL_PHOTO_COLUMNS = [ SQL_TAG_COLUMNS = [ 'id', 'name', + 'description', ] SQL_SYN_COLUMNS = [ 'name', @@ -161,8 +162,9 @@ DEFAULT_CONFIGURATION = { 'enable_new_tag': True, 'enable_new_user': True, 'enable_photo_add_remove_tag': True, - 'enable_photo_reload_metadata': True, 'enable_photo_generate_thumbnail': True, + 'enable_photo_reload_metadata': True, + 'enable_tag_edit': True, 'min_tag_name_length': 1, 'max_tag_name_length': 32, diff --git a/etiquette/jsonify.py b/etiquette/jsonify.py index b3d829d..a99b8a2 100644 --- a/etiquette/jsonify.py +++ b/etiquette/jsonify.py @@ -52,6 +52,7 @@ def tag(t, include_synonyms=False): j = { 'id': t.id, 'name': t.name, + 'description': t.description, 'qualified_name': t.qualified_name(), } if include_synonyms: diff --git a/etiquette/objects.py b/etiquette/objects.py index e4868d5..26d7d87 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -223,8 +223,8 @@ class Album(ObjectBase, GroupableMixin): if isinstance(db_row, (list, tuple)): db_row = dict(zip(constants.SQL_ALBUM_COLUMNS, db_row)) self.id = db_row['id'] - self.title = db_row['title'] - self.description = db_row['description'] + self.title = db_row['title'] or '' + self.description = db_row['description'] or '' self.name = 'Album %s' % self.id self.group_getter = self.photodb.get_album self._sum_bytes_photos = None @@ -501,6 +501,7 @@ class Photo(ObjectBase): if self.duration: self.bitrate = (self.bytes / 128) / self.duration + self.mimetype = helpers.get_mimetype(self.real_filepath) if self.mimetype is None: self.simple_mimetype = None @@ -954,6 +955,7 @@ class Tag(ObjectBase, GroupableMixin): db_row = dict(zip(constants.SQL_TAG_COLUMNS, db_row)) self.id = db_row['id'] self.name = db_row['name'] + self.description = db_row['description'] or '' self.group_getter = self.photodb.get_tag self._cached_qualified_name = None @@ -1063,6 +1065,25 @@ class Tag(ObjectBase, GroupableMixin): self.photodb.log.debug('Committing - delete tag') self.photodb.commit() + @decorators.required_feature('enable_tag_edit') + @decorators.transaction + def edit(self, description=None, *, commit=True): + ''' + Change the description. Leave None to keep current value. + ''' + if description is None: + description = self.description + cur = self.photodb.sql.cursor() + cur.execute( + 'UPDATE tags SET description = ? WHERE id == ?', + [description, self.id] + ) + self.description = description + self._uncache() + if commit: + self.photodb.log.debug('Committing - edit tag') + self.photodb.commit() + def qualified_name(self): ''' Return the 'group1.group2.tag' string for this tag. @@ -1107,7 +1128,8 @@ class Tag(ObjectBase, GroupableMixin): Rename the tag. Does not affect its relation to Photos or tag groups. ''' new_name = self.photodb.normalize_tagname(new_name) - if new_name == self.name: + old_name = self.name + if new_name == old_name: return try: @@ -1124,7 +1146,7 @@ class Tag(ObjectBase, GroupableMixin): if apply_to_synonyms: cur.execute( 'UPDATE tag_synonyms SET mastername = ? WHERE mastername = ?', - [new_name, self.name] + [new_name, old_name] ) self.name = new_name diff --git a/etiquette/photodb.py b/etiquette/photodb.py index a4320d4..4726adf 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.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 = 7 +DATABASE_VERSION = 8 DB_INIT = ''' PRAGMA count_changes = OFF; PRAGMA cache_size = 10000; @@ -125,7 +125,8 @@ CREATE INDEX IF NOT EXISTS index_tag_synonyms_name on tag_synonyms(name); ---------------------------------------------------------------------------------------------------- CREATE TABLE IF NOT EXISTS tags( id TEXT, - name TEXT + name TEXT, + description TEXT ); CREATE INDEX IF NOT EXISTS index_tags_id on tags(id); CREATE INDEX IF NOT EXISTS index_tags_name on tags(name); @@ -961,7 +962,7 @@ class PDBTagMixin: @decorators.required_feature('enable_new_tag') @decorators.transaction - def new_tag(self, tagname, *, commit=True): + def new_tag(self, tagname, description=None, *, commit=True): ''' Register a new tag and return the Tag object. ''' @@ -976,11 +977,18 @@ class PDBTagMixin: tagid = self.generate_id('tags') self._cached_frozen_children = None cur = self.sql.cursor() - cur.execute('INSERT INTO tags VALUES(?, ?)', [tagid, tagname]) + data = { + 'id': tagid, + 'name': tagname, + 'description': description, + } + (qmarks, bindings) = helpers.binding_filler(constants.SQL_TAG_COLUMNS, data) + query = 'INSERT INTO tags VALUES(%s)' % qmarks + cur.execute(query, bindings) if commit: self.log.debug('Committing - new_tag') self.commit() - tag = objects.Tag(self, [tagid, tagname]) + tag = objects.Tag(self, data) return tag def normalize_tagname(self, tagname): diff --git a/frontends/etiquette_flask/etiquette_flask/etiquette_flask.py b/frontends/etiquette_flask/etiquette_flask/etiquette_flask.py index edf4905..ded6aaf 100644 --- a/frontends/etiquette_flask/etiquette_flask/etiquette_flask.py +++ b/frontends/etiquette_flask/etiquette_flask/etiquette_flask.py @@ -524,8 +524,8 @@ def get_search_core(): total_tags = set() for photo in photos: for tag in photo.tags(): - total_tags.add(tag.qualified_name()) - total_tags = sorted(total_tags) + total_tags.add(tag) + total_tags = sorted(total_tags, key=lambda t: t.qualified_name()) # PREV-NEXT PAGE URLS offset = search_kwargs['offset'] or 0 @@ -613,6 +613,7 @@ def get_tags_html(specific_tag=None): 'tags.html', include_synonyms=include_synonyms, session=session, + specific_tag=specific_tag, tags=tags, ) return response diff --git a/frontends/etiquette_flask/templates/search.html b/frontends/etiquette_flask/templates/search.html index 9356dcb..def2597 100644 --- a/frontends/etiquette_flask/templates/search.html +++ b/frontends/etiquette_flask/templates/search.html @@ -270,7 +270,7 @@ form Tags on this page (click to join query):
{{specific_tag.description}}
+ {% endif %}