From f222ae71820e6c22e31f20598a7a2b3c9985fffb Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 17 Mar 2017 00:10:33 -0700 Subject: [PATCH] Add 'enable_photo_add_remove_tag' config --- etiquette/constants.py | 1 + etiquette/objects.py | 6 ++++++ etiquette_site.py | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/etiquette/constants.py b/etiquette/constants.py index 8d94b2c..503d9e0 100644 --- a/etiquette/constants.py +++ b/etiquette/constants.py @@ -151,6 +151,7 @@ DEFAULT_CONFIGURATION = { 'enable_new_photo': True, 'enable_new_tag': True, 'enable_new_user': True, + 'enable_photo_add_remove_tag': True, 'min_tag_name_length': 1, 'max_tag_name_length': 32, diff --git a/etiquette/objects.py b/etiquette/objects.py index 6b1aa8e..b9bf63e 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -438,6 +438,9 @@ class Photo(ObjectBase): return 'Photo:{id}'.format(id=self.id) def add_tag(self, tag, *, commit=True): + if not self.photodb.config['enable_photo_add_remove_tag']: + raise exceptions.FeatureDisabled('photo.add_tag, photo.remove_tag') + tag = self.photodb.get_tag(tag) existing = self.has_tag(tag, check_children=False) @@ -707,6 +710,9 @@ class Photo(ObjectBase): self.photodb.commit() def remove_tag(self, tag, *, commit=True): + if not self.photodb.config['enable_photo_add_remove_tag']: + raise exceptions.FeatureDisabled('photo.add_tag, photo.remove_tag') + tag = self.photodb.get_tag(tag) self.photodb.log.debug('Removing tag {t} from photo {p}'.format(t=repr(tag), p=repr(self))) diff --git a/etiquette_site.py b/etiquette_site.py index e5fce4a..d9e7999 100644 --- a/etiquette_site.py +++ b/etiquette_site.py @@ -671,10 +671,18 @@ def post_photo_add_remove_tag_core(photoid, tagname, add_or_remove): photo = P_photo(photoid, response_type='json') tag = P_tag(tagname, response_type='json') - if add_or_remove == 'add': - photo.add_tag(tag) - elif add_or_remove == 'remove': - photo.remove_tag(tag) + try: + if add_or_remove == 'add': + photo.add_tag(tag) + elif add_or_remove == 'remove': + photo.remove_tag(tag) + except exceptions.EtiquetteException as e: + response = { + 'error_type': e.error_type, + 'error_message': e.error_message, + } + response = jsonify.make_json_response(response, status=400) + flask.abort(response) response = {'tagname': tag.name} return jsonify.make_json_response(response)