diff --git a/kitsune/questions/admin.py b/kitsune/questions/admin.py index dc15f8d69d7..133de50ee57 100644 --- a/kitsune/questions/admin.py +++ b/kitsune/questions/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin -from kitsune.questions.models import QuestionLocale +from kitsune.questions.models import QuestionLocale, AAQConfig class QuestionLocaleAdmin(admin.ModelAdmin): @@ -10,3 +10,11 @@ class QuestionLocaleAdmin(admin.ModelAdmin): admin.site.register(QuestionLocale, QuestionLocaleAdmin) + + +class AAQConfigAdmin(admin.ModelAdmin): + list_display = ("product",) + autocomplete_fields = ("pinned_articles",) + + +admin.site.register(AAQConfig, AAQConfigAdmin) diff --git a/kitsune/questions/migrations/0012_aaqconfig.py b/kitsune/questions/migrations/0012_aaqconfig.py new file mode 100644 index 00000000000..9fc0ee8fc1d --- /dev/null +++ b/kitsune/questions/migrations/0012_aaqconfig.py @@ -0,0 +1,27 @@ +# Generated by Django 2.2.24 on 2021-09-23 04:54 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('products', '0005_auto_20200629_0826'), + ('wiki', '0012_auto_20200629_0826'), + ('questions', '0011_auto_20200629_0826'), + ] + + operations = [ + migrations.CreateModel( + name='AAQConfig', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('pinned_articles', models.ManyToManyField(to='wiki.Document')), + ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='aaq_configs', to='products.Product')), + ], + options={ + 'verbose_name': 'AAQ configuration', + }, + ), + ] diff --git a/kitsune/questions/models.py b/kitsune/questions/models.py index 0fdac9bfc67..47a0b7381a7 100755 --- a/kitsune/questions/models.py +++ b/kitsune/questions/models.py @@ -33,6 +33,7 @@ from kitsune.tags.models import BigVocabTaggableMixin from kitsune.tags.utils import add_existing_tag from kitsune.upload.models import ImageAttachment +from kitsune.wiki.models import Document log = logging.getLogger("k.questions") @@ -754,6 +755,14 @@ class Meta: verbose_name = "AAQ enabled locale" +class AAQConfig(ModelBase): + product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="aaq_configs") + pinned_articles = models.ManyToManyField(Document) + + class Meta: + verbose_name = "AAQ configuration" + + class Answer(ModelBase): """An answer to a support question.""" diff --git a/kitsune/questions/utils.py b/kitsune/questions/utils.py index 8d6a5c9534f..5d036fb8ae2 100644 --- a/kitsune/questions/utils.py +++ b/kitsune/questions/utils.py @@ -1,7 +1,9 @@ import logging import re +from django.conf import settings from kitsune.questions.models import Answer, Question +from kitsune.wiki.utils import get_featured_articles as kb_get_featured_articles log = logging.getLogger("k.questions") @@ -52,3 +54,34 @@ def get_mobile_product_from_ua(user_agent): return None else: return "mobile" + + +def get_featured_articles(product, locale): + """ + Returns 4 featured articles for the AAQ. + + Will return pinned articles first, then fill randomly from the most visited articles. + + For pinned articles in WIKI_DEFAULT_LANGUAGE, return a localized version if it exists. + For pinned articles in other locales, return them only if `locale` matches. + """ + + if config := product.aaq_configs.first(): + pinned_articles = [ + localized_article + for article in config.pinned_articles.filter( + locale__in=(locale, settings.WIKI_DEFAULT_LANGUAGE) + ) + if ( + localized_article := article + if article.locale == locale + else article.translated_to(locale) + ) + ] + else: + pinned_articles = [] + + if len(pinned_articles) < 4: + return (pinned_articles + kb_get_featured_articles(product=product, locale=locale))[:4] + + return pinned_articles diff --git a/kitsune/questions/views.py b/kitsune/questions/views.py index 4a784e1215b..dcc73382100 100644 --- a/kitsune/questions/views.py +++ b/kitsune/questions/views.py @@ -46,7 +46,7 @@ WatchQuestionForm, ) from kitsune.questions.models import Answer, AnswerVote, Question, QuestionLocale, QuestionVote -from kitsune.questions.utils import get_mobile_product_from_ua +from kitsune.questions.utils import get_mobile_product_from_ua, get_featured_articles from kitsune.sumo.decorators import ratelimit, ssl_required from kitsune.sumo.templatetags.jinja_helpers import urlparams from kitsune.sumo.urlresolvers import reverse, split_path @@ -56,7 +56,6 @@ from kitsune.upload.views import upload_imageattachment from kitsune.users.models import Setting from kitsune.wiki.facets import topics_for -from kitsune.wiki.utils import get_featured_articles log = logging.getLogger("k.questions")