Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
wip

wip render short_desc

wip

wip

wip fixing existing tests
  • Loading branch information
vincentporte committed Jun 29, 2023
1 parent ee9c0f7 commit d305f05
Show file tree
Hide file tree
Showing 45 changed files with 843 additions and 717 deletions.
1 change: 0 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"django_htmx.middleware.HtmxMiddleware",
"machina.apps.forum_permission.middleware.ForumPermissionMiddleware",
"lacommunaute.utils.middleware.VisibleForumsMiddleware",
]

ROOT_URLCONF = "config.urls"
Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/forum/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

class ForumAdmin(BaseForumAdmin):
fieldsets = BaseForumAdmin.fieldsets
fieldsets[0][1]["fields"] += ("short_description",)
fieldsets[1][1]["fields"] += (
"members_group",
"invitation_token",
"is_private",
"is_newsfeed",
"kind",
)
7 changes: 7 additions & 0 deletions lacommunaute/forum/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.db import models


class Kind(models.TextChoices):
PUBLIC_FORUM = "PUBLIC_FORUM", "Espace public"
PRIVATE_FORUM = "PRIVATE_FORUM", "Espace privé"
NEWS = "NEWS", "Actualités"
3 changes: 2 additions & 1 deletion lacommunaute/forum/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class ForumFactory(BaseForumFactory):
type = Forum.FORUM_POST
members_group = factory.SubFactory(GroupFactory, name=factory.SelfAttribute("..name"))
name = factory.Faker("name")
description = factory.Faker("sentence", nb_words=10)
description = factory.Faker("sentence", nb_words=100)
short_description = factory.Faker("sentence", nb_words=10)

@factory.post_generation
def with_public_perms(self, create, extracted, **kwargs):
Expand Down
77 changes: 77 additions & 0 deletions lacommunaute/forum/management/commands/202306_forumpublic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from typing import List

from django.contrib.redirects.models import Redirect
from django.contrib.sites.models import Site
from django.core.management.base import BaseCommand
from taggit.models import Tag

from lacommunaute.forum.models import Forum
from lacommunaute.forum_conversation.models import Topic


public_forums_id_list = [2, 106, 69, 107, 50, 98]
new_forum_name = "Espace d'échanges"
site_id = 1


def create_tags(forum_id_list: List[int]) -> None:
print("Création des nouveaux tags")
return Tag.objects.bulk_create(
[Tag(name=forum.name, slug=forum.slug) for forum in Forum.objects.filter(id__in=forum_id_list)]
)


def create_forum() -> Forum:
print("Création du nouveau forum public")
forum = Forum.objects.create(name=new_forum_name, type=Forum.FORUM_POST)
forum.move_to(Forum.objects.get(tree_id=1, level=0), "left")
return forum


def update_topics(forum_id_list: List[int], new_forum: Forum) -> None:
print("Mise à jour des topics")
for forum in Forum.objects.filter(id__in=public_forums_id_list):
for topic in Topic.objects.filter(forum=forum):
topic.tags.add(forum.name)
topic.forum = new_forum
topic.save()


def add_redirections(forum_id_list: List[int], site_id: int, new_forum: Forum) -> None:
print("Ajout des redirections")
site = Site.objects.get(id=site_id)
Redirect.objects.bulk_create(
[
Redirect(
site=site,
old_path=f"/forum/{forum.slug}-{forum.id}/{topic.slug}-{topic.id}",
new_path=f"/forum/{new_forum.slug}-{new_forum.id}/{topic.slug}-{topic.id}",
)
for forum in Forum.objects.filter(id__in=forum_id_list)
for topic in Topic.objects.filter(forum=forum)
]
)


class Command(BaseCommand):
help = "réorganisation des forums publics"

def handle(self, *args, **options):
# ajouter les nouveaux tags à partir des forums publics existants
create_tags(public_forums_id_list)

# ajouter le nouveau forum public
forum = create_forum()

# ajouter ces tags aux topics concernés
# migrer les topics des forums publics existants vers le nouveau forum public
update_topics(public_forums_id_list, forum)

# ajouter les redirections des anciens forums publics vers le nouveau forum public
add_redirections(public_forums_id_list, site_id, forum)

# supprimer les forums publics existants
print("Suppression des anciens forums publics")
Forum.objects.filter(id__in=public_forums_id_list).delete()

print("Terminé")
49 changes: 49 additions & 0 deletions lacommunaute/forum/migrations/0011_forum_kind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Generated by Django 4.2.2 on 2023-06-28 08:37

from django.db import migrations, models


def add_kind_to_forums(apps, schema_editor):
Forum = apps.get_model("forum", "Forum")
Forum.objects.filter(is_newsfeed=True).update(kind="NEWS")
Forum.objects.filter(is_newsfeed=False, is_private=True).update(kind="PRIVATE_FORUM")
Forum.objects.filter(is_newsfeed=False, is_private=False, type=1).update(kind="DOCUMENTATION")


def remove_kind_from_forums(apps, schema_editor):
Forum = apps.get_model("forum", "Forum")
Forum.objects.filter(kind="NEWS").update(is_newsfeed=True, is_private=False)
Forum.objects.filter(kind="PRIVATE_FORUM").update(is_private=True, is_newsfeed=False)
Forum.objects.filter(kind="DOCUMENTATION").update(is_private=False, is_newsfeed=False, type=1)


class Migration(migrations.Migration):
dependencies = [
("forum", "0010_forum_is_newsfeed"),
]

operations = [
migrations.AddField(
model_name="forum",
name="kind",
field=models.CharField(
choices=[
("PUBLIC_FORUM", "Espace public"),
("PRIVATE_FORUM", "Espace privé"),
("NEWS", "Actualités"),
],
default="PUBLIC_FORUM",
max_length=20,
verbose_name="Type",
),
),
migrations.RunPython(add_kind_to_forums, remove_kind_from_forums),
migrations.RemoveField(
model_name="forum",
name="is_newsfeed",
),
migrations.RemoveField(
model_name="forum",
name="is_private",
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.2 on 2023-06-28 08:54

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("forum", "0011_forum_kind"),
]

operations = [
migrations.AddField(
model_name="forum",
name="short_description",
field=models.CharField(blank=True, max_length=400, null=True, verbose_name="Description courte (SEO)"),
),
]
11 changes: 8 additions & 3 deletions lacommunaute/forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
from django.utils.functional import cached_property
from machina.apps.forum.abstract_models import AbstractForum

from lacommunaute.forum.enums import Kind as Forum_Kind
from lacommunaute.forum_conversation.models import Topic


class ForumQuerySet(models.QuerySet):
def public(self):
return self.filter(is_private=False)
return self.filter(kind=Forum_Kind.PUBLIC_FORUM)


class Forum(AbstractForum):
members_group = models.ForeignKey(
Group, blank=True, null=True, on_delete=models.CASCADE, verbose_name=("Members Group")
)
invitation_token = models.UUIDField(default=uuid.uuid4, unique=True)
is_private = models.BooleanField(default=False, verbose_name="privée")
is_newsfeed = models.BooleanField(default=False, verbose_name="fil d'actualité")
kind = models.CharField(
max_length=20, choices=Forum_Kind.choices, default=Forum_Kind.PUBLIC_FORUM, verbose_name="Type"
)
short_description = models.CharField(
max_length=400, blank=True, null=True, verbose_name="Description courte (SEO)"
)

objects = ForumQuerySet().as_manager()

Expand Down
8 changes: 5 additions & 3 deletions lacommunaute/forum/tests/test_CategoryForumListView.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import pytest # noqa
from django.urls import reverse

from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.factories import ForumFactory
from lacommunaute.forum.models import Forum


def test_context(client, db):
url = reverse("forum_extension:categories")
url = reverse("forum_extension:documentation")
response = client.get(url)
assert response.status_code == 200
assert "forum/category_forum_list.html" == response.templates[0].name
assert reverse("pages:statistiques") in str(response.content)


def test_queryset(client, db):
forum = ForumFactory(type=Forum.FORUM_CAT)
unvisible_forums = (
ForumFactory(type=Forum.FORUM_CAT, parent=forum),
ForumFactory(type=Forum.FORUM_CAT, is_private=True),
ForumFactory(type=Forum.FORUM_CAT, kind=ForumKind.PRIVATE_FORUM),
ForumFactory(),
ForumFactory(type=Forum.FORUM_LINK),
)
url = reverse("forum_extension:categories")
url = reverse("forum_extension:documentation")
response = client.get(url)
assert response.status_code == 200
assert forum in response.context_data["forums"]
Expand Down
6 changes: 4 additions & 2 deletions lacommunaute/forum/tests/tests_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from machina.core.db.models import get_model
from machina.core.loading import get_class

from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.factories import ForumFactory
from lacommunaute.forum.models import Forum
from lacommunaute.forum_conversation.factories import TopicFactory
Expand All @@ -20,11 +21,12 @@

class ForumManagerTest(TestCase):
def test_private_forum(self):
ForumFactory(is_private=True)
ForumFactory(kind=ForumKind.NEWS)
ForumFactory(kind=ForumKind.PRIVATE_FORUM)
self.assertEqual(Forum.objects.public().count(), 0)

def test_public_forum(self):
forum = ForumFactory(is_private=False)
forum = ForumFactory(kind=ForumKind.PUBLIC_FORUM)
self.assertEqual(Forum.objects.public().count(), 1)
self.assertIn(forum, Forum.objects.public())

Expand Down
2 changes: 1 addition & 1 deletion lacommunaute/forum/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def test_cannot_submit_post(self):
def test_queries(self):
TopicFactory.create_batch(20, with_post=True)
self.client.force_login(self.user)
with self.assertNumQueries(31):
with self.assertNumQueries(27):
self.client.get(self.url)

def test_certified_post_display(self):
Expand Down
2 changes: 1 addition & 1 deletion lacommunaute/forum/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

urlpatterns = [
path("forum/<str:slug>-<int:pk>/", ForumView.as_view(), name="forum"),
path("forum/categories/", CategoryForumListView.as_view(), name="categories"),
path("forum/documentation/", CategoryForumListView.as_view(), name="documentation"),
]
6 changes: 5 additions & 1 deletion lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from machina.apps.forum.views import ForumView as BaseForumView
from machina.core.loading import get_class

from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.models import Forum
from lacommunaute.forum_conversation.forms import PostForm
from lacommunaute.forum_conversation.models import Topic
Expand Down Expand Up @@ -52,6 +53,9 @@ def get_context_data(self, **kwargs):
.annotate(likes=Count("likers"))
.annotate(has_liked=Exists(User.objects.filter(topic_likes=OuterRef("id"), id=self.request.user.id)))
)
if forum.parent and forum.parent.type == Forum.FORUM_CAT:
context["forums"] = Forum.objects.filter(parent=forum.parent).order_by("lft")
context["parent_forum"] = forum.parent
return context


Expand All @@ -60,4 +64,4 @@ class CategoryForumListView(ListView):
context_object_name = "forums"

def get_queryset(self) -> QuerySet[Any]:
return Forum.objects.exclude(is_private=True).filter(type=Forum.FORUM_CAT, level=0)
return Forum.objects.filter(type=Forum.FORUM_CAT, kind=ForumKind.PUBLIC_FORUM, level=0)
8 changes: 3 additions & 5 deletions lacommunaute/forum_conversation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ def unanswered(self):
return (
self.exclude(approved=False)
.exclude(status=Topic.TOPIC_LOCKED)
.filter(posts_count=1, type__in=[Topic.TOPIC_POST, Topic.TOPIC_STICKY])
.exclude(type=Topic.TOPIC_ANNOUNCE)
.filter(posts_count=1)
)

def optimized_for_topics_list(self, user_id):
return (
self.exclude(approved=False)
.filter(type__in=[Topic.TOPIC_POST, Topic.TOPIC_STICKY, Topic.TOPIC_NEWS])
.filter(type__in=[Topic.TOPIC_POST, Topic.TOPIC_STICKY])
.annotate(likes=Count("likers"))
.annotate(has_liked=Exists(User.objects.filter(topic_likes=OuterRef("id"), id=user_id)))
.select_related(
Expand Down Expand Up @@ -56,9 +57,6 @@ class Topic(AbstractTopic):

tags = TaggableManager()

TOPIC_NEWS = 3
TYPE_CHOICES = AbstractTopic.TYPE_CHOICES + ((TOPIC_NEWS, _("News")),)

def get_absolute_url(self):
return reverse(
"forum_conversation:topic",
Expand Down
4 changes: 1 addition & 3 deletions lacommunaute/forum_conversation/tests/tests_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def test_unanswered(self):
TopicFactory(forum=forum, posts_count=1, type=Topic.TOPIC_ANNOUNCE)
TopicFactory(forum=forum, posts_count=1, approved=False)

self.assertEqual(Topic.objects.unanswered().count(), 1)
self.assertIn(topic, Topic.objects.unanswered())
self.assertEqual(Topic.objects.unanswered().get(), topic)

def test_optimized_for_topics_list_disapproved(self):
TopicFactory(approved=False)
Expand Down Expand Up @@ -125,4 +124,3 @@ def test_topic_types(self):
self.assertEqual(0, Topic.TOPIC_POST)
self.assertEqual(1, Topic.TOPIC_STICKY)
self.assertEqual(2, Topic.TOPIC_ANNOUNCE)
self.assertEqual(3, Topic.TOPIC_NEWS)
Loading

0 comments on commit d305f05

Please sign in to comment.