Skip to content

Commit

Permalink
allow pinning articles in the AAQ flow
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoMcA committed Sep 27, 2021
1 parent 029d8c8 commit b8cb682
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
10 changes: 9 additions & 1 deletion kitsune/questions/admin.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
27 changes: 27 additions & 0 deletions kitsune/questions/migrations/0012_aaqconfig.py
Original file line number Diff line number Diff line change
@@ -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',
},
),
]
9 changes: 9 additions & 0 deletions kitsune/questions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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."""

Expand Down
33 changes: 33 additions & 0 deletions kitsune/questions/utils.py
Original file line number Diff line number Diff line change
@@ -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")

Expand Down Expand Up @@ -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
3 changes: 1 addition & 2 deletions kitsune/questions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

Expand Down

0 comments on commit b8cb682

Please sign in to comment.