Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(documentation): partie 8, associer les Topic avec les Document #793

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from lacommunaute.documentation.models import Category, Document, DocumentRating
from lacommunaute.forum.models import Forum, ForumRating
from lacommunaute.forum_conversation.models import Topic


def create_categories_from_catforums():
Expand Down Expand Up @@ -68,6 +69,20 @@ def migrate_ratings(document_transpo_dict):
ForumRating.objects.all().delete()


def migrate_topics(document_transpo_dict):
main_forum = Forum.objects.get_main_forum()

for forum, document in document_transpo_dict.items():
topics = Topic.objects.filter(forum=forum)
sys.stdout.write(f"*** {len(topics)} topics to migrate from {forum} ({forum.id}) to {main_forum}\n")

for topic in topics:
topic.document = document
topic.forum = main_forum
topic.save()
forum.save()


def del_forums(category_transpo_dict, document_transpo_dict):
forums_to_delete = list(category_transpo_dict.keys()) + list(document_transpo_dict.keys())
return Forum.objects.filter(pk__in=[forum.pk for forum in forums_to_delete]).delete()
Expand All @@ -88,7 +103,10 @@ def handle(self, *args, **options):
migrate_ratings(document_transpo_dict)
sys.stdout.write("Ratings migrated\n")

## TODO next : Topics and Stats
migrate_topics(document_transpo_dict)
sys.stdout.write("Topics migrated\n")

## TODO next : Stats

deleted_forums = del_forums(category_transpo_dict, document_transpo_dict)
sys.stdout.write(f"{deleted_forums} forums deleted\n")
Expand Down
26 changes: 25 additions & 1 deletion lacommunaute/documentation/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib.auth.mixins import UserPassesTestMixin
from django.contrib.contenttypes.models import ContentType
from django.db.models import Avg, Count
Expand All @@ -8,7 +9,12 @@
from django.views.generic.edit import CreateView, UpdateView
from taggit.models import Tag

from lacommunaute import documentation
from lacommunaute.documentation.models import Category, Document, DocumentRating
from lacommunaute.forum.models import Forum
from lacommunaute.forum_conversation.forms import PostForm
from lacommunaute.forum_conversation.models import Topic
from lacommunaute.forum_conversation.view_mixins import FilteredTopicsListViewMixin


class CategoryListView(ListView):
Expand Down Expand Up @@ -74,11 +80,29 @@ def get_context_data(self, **kwargs):
return context


class DocumentDetailView(RatingMixin, DetailView):
class DocumentDetailView(FilteredTopicsListViewMixin, RatingMixin, DetailView):
model = Document
template_name = "documentation/document_detail.html"
context_object_name = "document"

def get_topics(self):
# needs pagination
return self.filter_queryset(Topic.objects.filter(document=self.object).optimized_for_topics_list(self.request.user.id))

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["topics"] = self.get_topics()
context["form"] = PostForm(user=self.request.user)

context["loadmoretopic_url"] = self.get_load_more_url(self.object.get_absolute_url())
context["filter_dropdown_endpoint"] = (
None if self.request.GET.get("page") else self.object.get_absolute_url())

context["loadmoretopic_suffix"] = "topics"
context["forum"] = Forum.objects.get_main_forum()
context = context | self.get_topic_filter_context()
return context


class DocumentRatingView(RatingMixin, View):
def post(self, request, *args, **kwargs):
Expand Down
25 changes: 25 additions & 0 deletions lacommunaute/forum_conversation/migrations/0009_topic_document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 5.0.9 on 2024-10-02 12:58

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


class Migration(migrations.Migration):
dependencies = [
("documentation", "0002_documentrating"),
("forum_conversation", "0008_remove_topic_likers"),
]

operations = [
migrations.AddField(
model_name="topic",
name="document",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="topics",
to="documentation.document",
),
),
]
3 changes: 3 additions & 0 deletions lacommunaute/forum_conversation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from machina.apps.forum_conversation.abstract_models import AbstractPost, AbstractTopic
from taggit.managers import TaggableManager

from lacommunaute.documentation.models import Document
from lacommunaute.forum_conversation.signals import post_create
from lacommunaute.forum_member.shortcuts import get_forum_member_display_name
from lacommunaute.forum_upvote.models import UpVote
Expand Down Expand Up @@ -37,6 +38,7 @@ def optimized_for_topics_list(self, user_id):
"certified_post",
"certified_post__post",
"certified_post__post__poster",
"document",
)
.prefetch_related(
"poll",
Expand All @@ -52,6 +54,7 @@ def optimized_for_topics_list(self, user_id):

class Topic(AbstractTopic):
tags = TaggableManager()
document = models.ForeignKey(Document, on_delete=models.SET_NULL, null=True, blank=True, related_name="topics")

def get_absolute_url(self, with_fqdn=False):
absolute_url = reverse(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
# serializer version: 1
# name: TestPosterTemplate.test_standalone_topic[False-without_document][without_document]
'''
<small class="text-muted poster-infos">
Par :


<a class="matomo-event" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="member" href="/members/profile/poster_username/">
Jeff B.
</a>,
il y a 0 minute

</small>
'''
# ---
# name: TestPosterTemplate.test_standalone_topic[True-with_document][with_document]
'''
<small class="text-muted poster-infos">
Par :


<a class="matomo-event" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="member" href="/members/profile/poster_username/">
Jeff B.
</a>,


dans <a href="[Document absolute URL]">Test Document</a>,
il y a 0 minute

</small>
'''
# ---
# name: TestPosterTemplate.test_standalone_topic[standalone_topic]
'''
<small class="text-muted poster-infos">
Par :


<a class="matomo-event" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="member" href="/members/profile/poster_username/">
Jeff B.
</a>,
il y a 0 minute

</small>
'''
# ---
# name: TestPosterTemplate.test_topic_from_other_public_forum_in_topics_view[topic_from_other_public_forum_in_topics_view]
'''
<small class="text-muted poster-infos">
Expand Down Expand Up @@ -44,6 +89,23 @@
</small>
'''
# ---
# name: TestPosterTemplate.test_topic_with_document[topic_with_document]
'''
<small class="text-muted poster-infos">
Par :


<a class="matomo-event" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="member" href="/members/profile/poster_username/">
Jeff B.
</a>,


dans <a href="[Document absolute URL]">Test Document</a>,
il y a 0 minute

</small>
'''
# ---
# name: TestTopicCreateCheckView.test_get_method[topic_create_check]
'''
<main class="s-main" id="main" role="main">
Expand Down
64 changes: 17 additions & 47 deletions lacommunaute/forum_conversation/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pytest_django.asserts import assertContains
from taggit.models import Tag

from lacommunaute.documentation.factories import DocumentFactory
from lacommunaute.forum.factories import CategoryForumFactory, ForumFactory
from lacommunaute.forum_conversation.enums import Filters
from lacommunaute.forum_conversation.factories import (
Expand Down Expand Up @@ -1002,53 +1003,22 @@ def test_filter_dropdown_with_tags(self, client, db, public_forum_with_topic, to


class TestPosterTemplate:
def test_topic_in_topics_view(self, client, db, topics_url, snapshot):
topic = TopicFactory(with_post=True, poster=UserFactory(first_name="Jeff", last_name="Buckley"))
response = client.get(topics_url)
soup = parse_response_to_soup(
response, replace_in_href=[(topic.poster.username, "poster_username")], selector=".poster-infos"
)
assert str(soup) == snapshot(name="topic_in_topics_view")

def test_topic_from_other_public_forum_in_topics_view(self, client, db, topics_url, snapshot):
# first_public_forum
ForumFactory(with_public_perms=True)

topic = TopicFactory(
with_post=True,
forum=ForumFactory(with_public_perms=True, name="Abby's Forum"),
poster=UserFactory(first_name="Alan", last_name="Turing"),
)
response = client.get(topics_url)
soup = parse_response_to_soup(
response,
replace_in_href=[
(topic.poster.username, "poster_username"),
(
reverse("forum_extension:forum", kwargs={"slug": topic.forum.slug, "pk": topic.forum.pk}),
"forum_url",
),
],
selector=".poster-infos",
)
assert str(soup) == snapshot(name="topic_from_other_public_forum_in_topics_view")

def test_topic_in_its_own_public_forum(self, client, db, snapshot):
# first_public_forum
ForumFactory(with_public_perms=True)

topic = TopicFactory(
with_post=True,
forum=ForumFactory(with_public_perms=True, name="Joe's Forum"),
poster=UserFactory(first_name="Dermot", last_name="Turing"),
)
response = client.get(
reverse("forum_extension:forum", kwargs={"slug": topic.forum.slug, "pk": topic.forum.pk})
)
soup = parse_response_to_soup(
response, replace_in_href=[(topic.poster.username, "poster_username")], selector=".poster-infos"
)
assert str(soup) == snapshot(name="topic_in_its_own_public_forum")
@pytest.mark.parametrize("with_document,snaphot_name", [(True, "with_document"), (False, "without_document")])
def test_standalone_topic(self, client, db, with_document, snaphot_name, snapshot):
poster = UserFactory(first_name="Jeff", last_name="Buckley")
if with_document:
document = DocumentFactory(for_snapshot=True)
TopicFactory(with_post=True, document=document, poster=poster)
replace_in_href = [
(poster.username, "poster_username"),
(document.get_absolute_url(), "[Document absolute URL]"),
]
else:
TopicFactory(with_post=True, poster=poster)
replace_in_href = [(poster.username, "poster_username")]
response = client.get(reverse("forum_conversation_extension:topics"))
soup = parse_response_to_soup(response, replace_in_href=replace_in_href, selector=".poster-infos")
assert str(soup) == snapshot(name=snaphot_name)


class TestTopicCreateCheckView:
Expand Down
12 changes: 12 additions & 0 deletions lacommunaute/templates/documentation/document_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@
</div>
</div>
</section>
<section class="s-section mt-0">
<div class="s-section__container container">
<div class="s-section__row row">
<div class="s-section__col col-12">
<div class="c-box" id="forum-detail-topic-list">
{% include "forum_conversation/topic_list.html" with forum=forum %}
<!-- note vincentporte : to be optimized -->
</div>
</div>
</div>
</div>
</section>
{% endblock content %}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
{% else %}
{{ poster }},
{% endif %}
{% if forum and forum != topic.forum %}
dans <a href="{% url 'forum_extension:forum' topic.forum.slug topic.forum.pk %}">{{ topic.forum }}</a>,
{% if topic.document %}
dans <a href="{% url 'documentation:document_detail' topic.document.category.id topic.document.slug topic.document.pk %}">{{ topic.document }}</a>,
{% endif %}
{% endwith %}
{% endspaceless %}
Expand Down
Loading