diff --git a/kitsune/wiki/forms.py b/kitsune/wiki/forms.py index ab5ad63452e..47de272ed9d 100644 --- a/kitsune/wiki/forms.py +++ b/kitsune/wiki/forms.py @@ -9,14 +9,9 @@ from kitsune.products.models import Product, Topic from kitsune.sumo.form_fields import MultiUsernameField from kitsune.wiki.config import CATEGORIES, SIGNIFICANCES -from kitsune.wiki.models import ( - MAX_REVISION_COMMENT_LENGTH, - Document, - DraftRevision, - Revision, -) +from kitsune.wiki.models import MAX_REVISION_COMMENT_LENGTH, Document, DraftRevision, Revision from kitsune.wiki.tasks import add_short_links -from kitsune.wiki.widgets import ProductsWidget, TopicsWidget, RelatedDocumentsWidget +from kitsune.wiki.widgets import ProductsWidget, RelatedDocumentsWidget, TopicsWidget TITLE_REQUIRED = _lazy("Please provide a title.") TITLE_SHORT = _lazy( @@ -158,20 +153,33 @@ def clean_slug(self): return slug def clean(self): - c = super(DocumentForm, self).clean() - locale = c.get("locale") + cdata = super(DocumentForm, self).clean() + locale = cdata.get("locale") # Products are required for en-US - products = c.get("products") - if locale == settings.WIKI_DEFAULT_LANGUAGE and (not products or len(products) < 1): + product_ids = cdata.get("products", []) + products = Product.active.filter( + id__in=[int(product_id) for product_id in product_ids if product_id] + ) + if locale == settings.WIKI_DEFAULT_LANGUAGE and (not product_ids or len(product_ids) < 1): raise forms.ValidationError(PRODUCT_REQUIRED) # Topics are required for en-US - topics = c.get("topics") - if locale == settings.WIKI_DEFAULT_LANGUAGE and (not topics or len(topics) < 1): + topic_ids = cdata.get("topics", []) + topics = Topic.active.filter(id__in=[int(topic_id) for topic_id in topic_ids if topic_id]) + if locale == settings.WIKI_DEFAULT_LANGUAGE and (not topic_ids or len(topic_ids) < 1): raise forms.ValidationError(TOPIC_REQUIRED) - return c + associated_topics = Topic.active.filter(products__in=products).distinct() + invalid_topics = topics.difference(associated_topics) + + if invalid_topics: + topic_titles = ", ".join([topic.title for topic in invalid_topics]) + raise forms.ValidationError( + _lazy(f"Topics {topic_titles} are not associated with the selected products.") + ) + + return cdata class Meta: model = Document diff --git a/kitsune/wiki/tests/test_templates.py b/kitsune/wiki/tests/test_templates.py index 419e2519f5b..54118a35229 100644 --- a/kitsune/wiki/tests/test_templates.py +++ b/kitsune/wiki/tests/test_templates.py @@ -9,7 +9,8 @@ from kitsune.products.tests import ProductFactory, TopicFactory from kitsune.sumo.templatetags.jinja_helpers import urlparams -from kitsune.sumo.tests import SumoPyQuery as pq, TestCase, attrs_eq, get, post +from kitsune.sumo.tests import SumoPyQuery as pq +from kitsune.sumo.tests import TestCase, attrs_eq, get, post from kitsune.sumo.urlresolvers import reverse from kitsune.users.tests import UserFactory, add_permission from kitsune.wiki.config import ( @@ -974,6 +975,8 @@ def test_edit_document_POST_removes_old_tags(self): self.d.topics.add(*topics) self.assertEqual(self.d.topics.count(), len(topics)) new_topics = [topics[0], TopicFactory()] + self.d.topics.clear() + self.d.topics.add(*new_topics) data = new_document_data(t.id for t in new_topics) data["form"] = "doc" self.client.post(reverse("wiki.edit_document_metadata", args=[self.d.slug]), data) diff --git a/kitsune/wiki/tests/test_views.py b/kitsune/wiki/tests/test_views.py index a2872aa8171..58e09150ea3 100644 --- a/kitsune/wiki/tests/test_views.py +++ b/kitsune/wiki/tests/test_views.py @@ -8,7 +8,7 @@ from django.test import Client from pyquery import PyQuery as pq -from kitsune.products.tests import ProductFactory +from kitsune.products.tests import ProductFactory, TopicFactory from kitsune.sumo.redis_utils import RedisError, redis_client from kitsune.sumo.tests import SkipTest, TestCase, template_used from kitsune.sumo.urlresolvers import reverse @@ -1656,11 +1656,13 @@ def test_changing_products(self): d = r.document prod_desktop = ProductFactory(title="desktop") prod_mobile = ProductFactory(title="mobile") + topic = TopicFactory(products=[prod_desktop, prod_mobile]) data = new_document_data() data.update( { "products": [prod_desktop.id, prod_mobile.id], + "topics": [topic.id], "title": d.title, "slug": d.slug, "form": "doc",