Validate topics in the clean method

This commit is contained in:
Tasos Katsoulas 2024-09-23 17:45:52 +03:00
Родитель 2b5e42fd54
Коммит c194e4f3f0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 66D7743B4FCCFCBC
3 изменённых файлов: 29 добавлений и 16 удалений

Просмотреть файл

@ -9,14 +9,9 @@ from django.utils.translation import gettext_lazy as _lazy
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 @@ class DocumentForm(forms.ModelForm):
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

Просмотреть файл

@ -9,7 +9,8 @@ from django.core.cache import cache
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 @@ class NewRevisionTests(TestCase):
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)

Просмотреть файл

@ -8,7 +8,7 @@ from django.contrib.sites.models import Site
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 @@ class DocumentEditingTests(TestCase):
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",