Skip to content

Commit

Permalink
help category detail page and management command
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonian committed Mar 6, 2024
1 parent fc4a585 commit a2b42c3
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 8 deletions.
28 changes: 28 additions & 0 deletions apps/help/management/commands/help_articles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.core.management.base import BaseCommand
from django.utils.text import slugify
from faker import Faker
import random

from ...models import Article, Category

fake = Faker()


class Command(BaseCommand):
help = "Populate the database with sample data for Help Articles model"

def handle(self, *args, **kwargs):
categories = Category.objects.all()
for _ in range(50):
title = fake.sentence(nb_words=5)
slug = slugify(title)
content = fake.paragraph(nb_sentences=10)
category = random.choice(categories)

Article.objects.create(
title=title,
slug=slug,
content=content,
category=category,
)
self.stdout.write(self.style.SUCCESS("Successfully added 50 Help Articles"))
9 changes: 8 additions & 1 deletion apps/help/management/commands/help_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
"User Experience",
]

icons = ["user", "cog", "file", "question", "lock", "chart-bar"]
icons = [
"user",
"cog",
"file",
"question",
"lock",
"chart-bar",
]

descriptions = [
"Manage your account settings and security.",
Expand Down
25 changes: 25 additions & 0 deletions apps/help/migrations/0003_alter_article_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 5.0.2 on 2024-03-06 09:53

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("help", "0002_article_category"),
]

operations = [
migrations.AlterField(
model_name="article",
name="category",
field=models.ForeignKey(
help_text="Category linked to this Help Article.",
on_delete=django.db.models.deletion.PROTECT,
related_name="articles",
to="help.category",
verbose_name="category",
),
),
]
1 change: 1 addition & 0 deletions apps/help/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Article(models.Model):
"Category",
on_delete=models.PROTECT,
verbose_name=_("category"),
related_name=_("articles"),
help_text=_("Category linked to this Help Article."),
)
title = models.CharField(
Expand Down
54 changes: 52 additions & 2 deletions apps/help/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
from django.test import TestCase
# from django.test import TestCase
# from django.urls import reverse
# from django.utils.text import slugify

# Create your tests here.
# from .models import Category
# from apps.help.management.commands.populate_categories import Command as PopulateCategoriesCommand


# class CategoryModelTests(TestCase):

# def setUp(self):
# self.category_data = {
# "name": "Test Category",
# "slug": "test-category",
# "description": "This is a test category.",
# "icon": "test-icon",
# }

# def test_category_creation(self):
# category = Category.objects.create(**self.category_data)
# self.assertEqual(category.name, "Test Category")
# self.assertEqual(category.slug, "test-category")
# self.assertEqual(category.description, "This is a test category.")
# self.assertEqual(category.icon, "test-icon")

# def test_get_absolute_url(self):
# category = Category.objects.create(**self.category_data)
# url = reverse("help:category", kwargs={"slug": category.slug})
# self.assertEqual(category.get_absolute_url(), url)

# def test_get_article_count(self):
# category = Category.objects.create(**self.category_data)
# # Assuming you have an 'Article' model, you need to import it
# from apps.help.models import Article

# Article.objects.create(category=category, title="Test Article", content="Article content")
# self.assertEqual(category.get_article_count(), 1)


# class PopulateCategoriesCommandTests(TestCase):

# def test_populate_categories_command(self):
# command = PopulateCategoriesCommand()
# command.handle()

# # Check if the categories have been created
# self.assertEqual(Category.objects.count(), 6)

# # Check if the icon field is chosen from the provided list
# icons = [category.icon for category in Category.objects.all()]
# self.assertIn(icons[0], ["user", "cog", "file", "question", "lock", "chart-bar"])

# # Add more assertions as needed based on your specific requirements
2 changes: 1 addition & 1 deletion apps/help/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def article(request, category_slug, article_slug):
def category(request, slug):
category = get_object_or_404(Category, slug=slug)
articles = Article.objects.filter(category=category)
random_categories = Category.objects.exclude(id=category.id).order_by("?")[:3]
random_categories = Category.objects.exclude(id=category.id).exclude(articles__isnull=True).order_by("?")[:3]

template = "help/category.html"
context = {
Expand Down
14 changes: 12 additions & 2 deletions templates/help/category.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "base.html" %}
{% load i18n core_utils %}
{% load i18n static %}

{% block title %}{% trans 'Help Center' %} | {{ category.name }}{% endblock %}

Expand All @@ -13,7 +13,8 @@
<nav style="--bs-breadcrumb-divider: '>';" aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="{% url 'help:index' %}"
class="text-decoration-none">{% trans 'Help Center' %}</a></li>
class="link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover">{% trans 'Help Center' %}</a>
</li>
<li class="breadcrumb-item active" aria-current="page">{{ category.name }}</li>
</ol>
</nav>
Expand Down Expand Up @@ -46,9 +47,17 @@ <h6 class="fw-normal">{{ category.description }}</h6>
<div class="col-lg-4 col-sm-6">
{% include 'partials/_related_article.html' %}
</div>
{% empty %}
<div class="text-center pt-5">
<div class="d-block">
<img src="{% static 'img/empty.svg' %}" class="img-fluid w-25" alt="empty">
</div>
<p class="d-flex justify-content-center mt-5 text-body-secondary">No Help Articles Yet</p>
</div>
{% endfor %}
</div>

{% if random_categories %}
<h4>Other Helpful Categories</h4>
<p class="text-body-secondary mb-5">If you did not find what you needed, these could help!</p>

Expand All @@ -59,6 +68,7 @@ <h4>Other Helpful Categories</h4>
</div>
{% endfor %}
</div>
{% endif %}
</div>
</div>
</div>
Expand Down
5 changes: 3 additions & 2 deletions templates/partials/_related_article.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<a href="{# article.get_absolute_url #}" class="text-decoration-none"><i
class="fa-solid fa-file-lines me-2"></i>{{ article.title }}</a>
<a href="{# article.get_absolute_url #}"
class="link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover"><i
class="fa-solid fa-file-lines me-2"></i>{{ article.title }}</a>

0 comments on commit a2b42c3

Please sign in to comment.