Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate topics in the clean method #6240

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions kitsune/wiki/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ Nice! I've overlooked this feature, so thanks for enlightening me!


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
Expand Down
5 changes: 4 additions & 1 deletion kitsune/wiki/tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion kitsune/wiki/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down