Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Jun 28, 2023
1 parent e8e479f commit 20fc6bc
Show file tree
Hide file tree
Showing 40 changed files with 739 additions and 421 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
3 changes: 1 addition & 2 deletions lacommunaute/forum/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ class ForumAdmin(BaseForumAdmin):
fieldsets[1][1]["fields"] += (
"members_group",
"invitation_token",
"is_private",
"is_newsfeed",
"kind",
)
8 changes: 8 additions & 0 deletions lacommunaute/forum/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.db import models


class Kind(models.TextChoices):
PUBLIC_FORUM = "PUBLIC_FORUM", "Espace d'échange"
PRIVATE_FORUM = "PRIVATE_FORUM", "Espace d'échange privé"
NEWS = "NEWS", "Actualités"
DOCUMENTATION = "DOCUMENTATION", "Documentation"
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é")
50 changes: 50 additions & 0 deletions lacommunaute/forum/migrations/0011_forum_kind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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 d'échange"),
("PRIVATE_FORUM", "Espace d'échange privé"),
("NEWS", "Actualités"),
("DOCUMENTATION", "Documentation"),
],
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=160, 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=160, blank=True, null=True, verbose_name="Description courte (SEO)"
)

objects = ForumQuerySet().as_manager()

Expand Down
1 change: 1 addition & 0 deletions lacommunaute/forum/tests/test_CategoryForumListView.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def test_context(client, db):
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):
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(33):
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"),
]
2 changes: 1 addition & 1 deletion lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,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, level=0)
11 changes: 2 additions & 9 deletions lacommunaute/forum_conversation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@

class TopicQuerySet(models.QuerySet):
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])
)
return self.exclude(approved=False).exclude(status=Topic.TOPIC_LOCKED).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 +52,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
Loading

0 comments on commit 20fc6bc

Please sign in to comment.