Skip to content

Commit 0460c9f

Browse files
authored
only clear auto tags when updating question (#6736)
* only clear auto tags when updating question * improve interface
1 parent a7c4d17 commit 0460c9f

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

kitsune/questions/models.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from kitsune.sumo.urlresolvers import reverse
3636
from kitsune.sumo.utils import chunked
3737
from kitsune.tags.models import BigVocabTaggableManager, SumoTag
38-
from kitsune.tags.utils import add_existing_tag
3938
from kitsune.upload.models import ImageAttachment
4039
from kitsune.wiki.models import Document
4140

@@ -276,16 +275,16 @@ def product_slug(self):
276275

277276
return self._product_slug
278277

279-
def auto_tag(self):
280-
"""Apply tags to myself that are implied by my metadata.
281-
278+
def handle_metadata_tags(self, action):
279+
"""
280+
Add or remove tags that are implied by my metadata.
282281
You don't need to call save on the question after this.
283-
284282
"""
285-
to_add = []
283+
tags = []
284+
286285
if product_config := self.product_config:
287286
for tag in product_config.associated_tags.all():
288-
to_add.append(tag)
287+
tags.append(tag)
289288

290289
version = self.metadata.get("ff_version", "")
291290

@@ -299,27 +298,43 @@ def auto_tag(self):
299298
or version in product_details.firefox_history_stability_releases
300299
or version in product_details.firefox_history_major_releases
301300
):
302-
to_add.append("Firefox %s" % version)
301+
tags.append("Firefox %s" % version)
303302
tenths = _tenths_version(version)
304303
if tenths:
305-
to_add.append("Firefox %s" % tenths)
304+
tags.append("Firefox %s" % tenths)
306305
elif _has_beta(version, dev_releases):
307-
to_add.append("Firefox %s" % version)
308-
to_add.append("beta")
306+
tags.append("Firefox %s" % version)
307+
tags.append("beta")
309308

310-
# Add a tag for the OS if it already exists as a tag:
309+
# Add a tag for the OS but only if it already exists as a tag.
311310
if os := self.metadata.get("os"):
312311
try:
313-
add_existing_tag(os, self.tags)
312+
os_tag = SumoTag.objects.get(name__iexact=os)
314313
except SumoTag.DoesNotExist:
315314
pass
315+
else:
316+
tags.append(os_tag)
317+
316318
product_md = self.metadata.get("product")
317319
topic_md = self.metadata.get("category")
318320
if self.product and not product_md:
319-
to_add.append(self.product.slug)
321+
tags.append(self.product.slug)
320322
if self.topic and not topic_md:
321-
to_add.append(self.topic.slug)
322-
self.tags.add(*to_add)
323+
tags.append(self.topic.slug)
324+
325+
getattr(self.tags, action)(*tags)
326+
327+
def auto_tag(self):
328+
"""
329+
Add tags that are implied by my metadata.
330+
"""
331+
self.handle_metadata_tags("add")
332+
333+
def remove_auto_tags(self):
334+
"""
335+
Remove tags that are implied by my metadata.
336+
"""
337+
self.handle_metadata_tags("remove")
323338

324339
def get_absolute_url(self):
325340
# Note: If this function changes, we need to change it in

kitsune/questions/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,12 @@ def update_question_fields_from_classification(question, result, sumo_bot):
207207
update_fields["topic"] = topic
208208

209209
if update_fields:
210+
# Clear the existing auto tags.
211+
question.remove_auto_tags()
210212
for field, value in update_fields.items():
211213
setattr(question, field, value)
212214
question.save(update_fields=update_fields.keys())
213215
question.clear_cached_tags()
214-
question.tags.clear()
215216
question.auto_tag()
216217

217218

0 commit comments

Comments
 (0)