зеркало из https://github.com/mozilla/kitsune.git
Validate topics in the clean method
This commit is contained in:
Родитель
2b5e42fd54
Коммит
c194e4f3f0
|
@ -9,14 +9,9 @@ from django.utils.translation import gettext_lazy as _lazy
|
||||||
from kitsune.products.models import Product, Topic
|
from kitsune.products.models import Product, Topic
|
||||||
from kitsune.sumo.form_fields import MultiUsernameField
|
from kitsune.sumo.form_fields import MultiUsernameField
|
||||||
from kitsune.wiki.config import CATEGORIES, SIGNIFICANCES
|
from kitsune.wiki.config import CATEGORIES, SIGNIFICANCES
|
||||||
from kitsune.wiki.models import (
|
from kitsune.wiki.models import MAX_REVISION_COMMENT_LENGTH, Document, DraftRevision, Revision
|
||||||
MAX_REVISION_COMMENT_LENGTH,
|
|
||||||
Document,
|
|
||||||
DraftRevision,
|
|
||||||
Revision,
|
|
||||||
)
|
|
||||||
from kitsune.wiki.tasks import add_short_links
|
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_REQUIRED = _lazy("Please provide a title.")
|
||||||
TITLE_SHORT = _lazy(
|
TITLE_SHORT = _lazy(
|
||||||
|
@ -158,20 +153,33 @@ class DocumentForm(forms.ModelForm):
|
||||||
return slug
|
return slug
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
c = super(DocumentForm, self).clean()
|
cdata = super(DocumentForm, self).clean()
|
||||||
locale = c.get("locale")
|
locale = cdata.get("locale")
|
||||||
|
|
||||||
# Products are required for en-US
|
# Products are required for en-US
|
||||||
products = c.get("products")
|
product_ids = cdata.get("products", [])
|
||||||
if locale == settings.WIKI_DEFAULT_LANGUAGE and (not products or len(products) < 1):
|
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)
|
raise forms.ValidationError(PRODUCT_REQUIRED)
|
||||||
|
|
||||||
# Topics are required for en-US
|
# Topics are required for en-US
|
||||||
topics = c.get("topics")
|
topic_ids = cdata.get("topics", [])
|
||||||
if locale == settings.WIKI_DEFAULT_LANGUAGE and (not topics or len(topics) < 1):
|
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)
|
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:
|
class Meta:
|
||||||
model = Document
|
model = Document
|
||||||
|
|
|
@ -9,7 +9,8 @@ from django.core.cache import cache
|
||||||
|
|
||||||
from kitsune.products.tests import ProductFactory, TopicFactory
|
from kitsune.products.tests import ProductFactory, TopicFactory
|
||||||
from kitsune.sumo.templatetags.jinja_helpers import urlparams
|
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.sumo.urlresolvers import reverse
|
||||||
from kitsune.users.tests import UserFactory, add_permission
|
from kitsune.users.tests import UserFactory, add_permission
|
||||||
from kitsune.wiki.config import (
|
from kitsune.wiki.config import (
|
||||||
|
@ -974,6 +975,8 @@ class NewRevisionTests(TestCase):
|
||||||
self.d.topics.add(*topics)
|
self.d.topics.add(*topics)
|
||||||
self.assertEqual(self.d.topics.count(), len(topics))
|
self.assertEqual(self.d.topics.count(), len(topics))
|
||||||
new_topics = [topics[0], TopicFactory()]
|
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 = new_document_data(t.id for t in new_topics)
|
||||||
data["form"] = "doc"
|
data["form"] = "doc"
|
||||||
self.client.post(reverse("wiki.edit_document_metadata", args=[self.d.slug]), data)
|
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 django.test import Client
|
||||||
from pyquery import PyQuery as pq
|
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.redis_utils import RedisError, redis_client
|
||||||
from kitsune.sumo.tests import SkipTest, TestCase, template_used
|
from kitsune.sumo.tests import SkipTest, TestCase, template_used
|
||||||
from kitsune.sumo.urlresolvers import reverse
|
from kitsune.sumo.urlresolvers import reverse
|
||||||
|
@ -1656,11 +1656,13 @@ class DocumentEditingTests(TestCase):
|
||||||
d = r.document
|
d = r.document
|
||||||
prod_desktop = ProductFactory(title="desktop")
|
prod_desktop = ProductFactory(title="desktop")
|
||||||
prod_mobile = ProductFactory(title="mobile")
|
prod_mobile = ProductFactory(title="mobile")
|
||||||
|
topic = TopicFactory(products=[prod_desktop, prod_mobile])
|
||||||
|
|
||||||
data = new_document_data()
|
data = new_document_data()
|
||||||
data.update(
|
data.update(
|
||||||
{
|
{
|
||||||
"products": [prod_desktop.id, prod_mobile.id],
|
"products": [prod_desktop.id, prod_mobile.id],
|
||||||
|
"topics": [topic.id],
|
||||||
"title": d.title,
|
"title": d.title,
|
||||||
"slug": d.slug,
|
"slug": d.slug,
|
||||||
"form": "doc",
|
"form": "doc",
|
||||||
|
|
Загрузка…
Ссылка в новой задаче