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

feat(partner): lien avec les fiches pratiques #761

Merged
merged 9 commits into from
Sep 4, 2024
2 changes: 1 addition & 1 deletion lacommunaute/forum/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ForumAdmin(BaseForumAdmin):
fieldsets = BaseForumAdmin.fieldsets
fieldsets[0][1]["fields"] += ("short_description", "certified", "tags")
fieldsets[0][1]["fields"] += ("short_description", "certified", "tags", "partner")
fieldsets[1][1]["fields"] += (
"members_group",
"invitation_token",
Expand Down
8 changes: 8 additions & 0 deletions lacommunaute/forum/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def with_tags(self, create, extracted, **kwargs):
for tag in extracted:
self.tags.add(tag)

@factory.post_generation
def with_partner(self, create, extracted, **kwargs):
if not create or not extracted:
return

self.partner = extracted
self.save()


class CategoryForumFactory(ForumFactory):
type = Forum.FORUM_CAT
Expand Down
8 changes: 7 additions & 1 deletion lacommunaute/forum/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from taggit.models import Tag

from lacommunaute.forum.models import Forum
from lacommunaute.partner.models import Partner


def wrap_iframe_in_div_tag(text):
Expand Down Expand Up @@ -38,6 +39,11 @@ class ForumForm(forms.ModelForm):
widget=forms.FileInput(attrs={"accept": settings.SUPPORTED_IMAGE_FILE_TYPES.keys()}),
)
certified = forms.BooleanField(required=False, label="Certifiée par la communauté de l'inclusion")
partner = forms.ModelChoiceField(
label="Sélectionner un partenaire",
queryset=Partner.objects.all(),
required=False,
)
tags = ModelMultipleChoiceField(
label="Sélectionner un ou plusieurs tags",
queryset=Tag.objects.all(),
Expand Down Expand Up @@ -67,4 +73,4 @@ def save(self, commit=True):

class Meta:
model = Forum
fields = ["name", "short_description", "description", "image", "certified"]
fields = ["name", "short_description", "description", "image", "certified", "partner"]
26 changes: 17 additions & 9 deletions lacommunaute/forum/management/commands/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,41 @@ class Command(BaseCommand):
help = "hydratation d'un site de validation"

def handle(self, *args, **options):
tags = ["Pirmadienis", "Poniedziałek", "Lundi", "Montag"]

UserFactory(username="communaute", password="password", is_superuser=True, is_staff=True)
sys.stdout.write("superuser created\n")

partners = PartnerFactory.create_batch(5, with_logo=True)
sys.stdout.write("partners created\n")

forum = ForumFactory(name="Espace d'échanges", with_public_perms=True)
TopicFactory.create_batch(20, forum=forum, with_post=True)
TopicFactory.create_batch(
3, forum=forum, with_post=True, with_tags=["Pirmadienis", "Poniedziałek", "Lundi", "Montag"]
)
TopicFactory.create_batch(3, forum=forum, with_post=True, with_tags=tags)
TopicFactory(forum=forum, with_post=True, with_certified_post=True)
AnonymousTopicFactory.create_batch(2, forum=forum, with_post=True)
PostFactory.create_batch(3, topic=TopicFactory(forum=forum, with_post=True))
sys.stdout.write("public forum created\n")

for i in range(1, 3):
for i in range(1, 4):
parent = CategoryForumFactory(with_public_perms=True, name=f"Thème {i}")
for j in range(1, 3):
for j in range(1, 4):
TopicFactory.create_batch(
2, forum=ForumFactory(parent=parent, with_public_perms=True, name=f"Fiche {i}-{j}"), with_post=True
2,
forum=ForumFactory(
parent=parent,
with_public_perms=True,
with_partner=partners[j],
with_tags=tags[:j],
name=f"Fiche {i}-{j}",
),
with_post=True,
)
sys.stdout.write("documentation created\n")

EventFactory.create_batch(5)
sys.stdout.write("events created\n")

PartnerFactory.create_batch(5, with_logo=True)
sys.stdout.write("partners created\n")

# refresh materialized view
with connection.cursor() as cursor:
cursor.execute("REFRESH MATERIALIZED VIEW search_commonindex")
Expand Down
21 changes: 21 additions & 0 deletions lacommunaute/forum/migrations/0018_forum_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.0.8 on 2024-08-29 14:34

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


class Migration(migrations.Migration):
dependencies = [
("forum", "0017_forum_tags"),
("partner", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="forum",
name="partner",
field=models.ForeignKey(
blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to="partner.partner"
),
),
]
2 changes: 2 additions & 0 deletions lacommunaute/forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from lacommunaute.forum.enums import Kind as Forum_Kind
from lacommunaute.forum_conversation.models import Topic
from lacommunaute.forum_upvote.models import UpVote
from lacommunaute.partner.models import Partner
from lacommunaute.utils.validators import validate_image_size


Expand Down Expand Up @@ -42,6 +43,7 @@ class Forum(AbstractForum):
upvotes = GenericRelation(UpVote, related_query_name="forum")

tags = TaggableManager()
partner = models.ForeignKey(Partner, on_delete=models.CASCADE, null=True, blank=True)

objects = ForumQuerySet().as_manager()

Expand Down
118 changes: 67 additions & 51 deletions lacommunaute/forum/tests/__snapshots__/tests_views.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -153,28 +153,28 @@
# name: TestDocumentationCategoryForumContent.test_documentation_category_subforum_list[documentation_category_subforum_list]
'''
<div class="row mt-4" id="documentation-category-subforums">

<div class="col-12 col-md-4 mb-3 mb-md-5">
<div class="card c-card has-one-link-inside h-100">

<div class="card-header card-header-thumbnail rounded">
<img alt="Test Forum" class="img-fitcover img-fluid" loading="lazy" src="[img src]"/>
</div>

<div class="card-body pb-0">
<p class="h3 lh-base">Test Forum</p>

<div class="mt-3">Test description</div>
</div>
<div class="card-footer text-end">
<a class="btn btn-sm btn-ico btn-link stretched-link matomo-event" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="fiches_techniques" href="/forum/test-forum-10000/">
<i class="ri-arrow-right-line ri-lg"></i>
</a>

<div class="col-12 col-md-4 mb-3 mb-md-5">
<div class="card c-card has-one-link-inside h-100">

<div class="card-header card-header-thumbnail rounded">
<img alt="Test Forum" class="img-fitcover img-fluid" loading="lazy" src="[img src]"/>
</div>

<div class="card-body pb-0">
<p class="h3 lh-base">Test Forum</p>

<div class="mt-3">Test description</div>
</div>
<div class="card-footer text-end">
<a class="btn btn-sm btn-ico btn-link stretched-link matomo-event" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="fiches_techniques" href="/forum/test-forum-10000/">
<i class="ri-arrow-right-line ri-lg"></i>
</a>
</div>
</div>

</div>
</div>

</div>
'''
# ---
# name: TestDocumentationCategoryForumContent.test_show_subforum_tags[documentation_category_subforum_tags]
Expand Down Expand Up @@ -235,44 +235,45 @@
<div class="s-section__col col-12">

<div class="row mt-4" id="documentation-category-subforums">

<div class="col-12 col-md-4 mb-3 mb-md-5">
<div class="card c-card has-one-link-inside h-100">

<div class="col-12 col-md-4 mb-3 mb-md-5">
<div class="card c-card has-one-link-inside h-100">

<div class="card-body pb-0">
<p class="h3 lh-base">Test-1</p>

<div class="card-body pb-0">
<p class="h3 lh-base">Test-1</p>

<div class="mt-3">Test description</div>
</div>
<div class="card-footer text-end">
<a class="btn btn-sm btn-ico btn-link stretched-link matomo-event" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="fiches_techniques" href="/forum/test-1-10000/">
<i class="ri-arrow-right-line ri-lg"></i>
</a>
</div>
<div class="mt-3">Test description</div>
</div>
<div class="card-footer text-end">
<a class="btn btn-sm btn-ico btn-link stretched-link matomo-event" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="fiches_techniques" href="/forum/test-1-10000/">
<i class="ri-arrow-right-line ri-lg"></i>
</a>
</div>
</div>

<div class="col-12 col-md-4 mb-3 mb-md-5">
<div class="card c-card has-one-link-inside h-100">

<div class="card-header card-header-thumbnail rounded">
<img alt="Test-2" class="img-fitcover img-fluid" loading="lazy" src="[img src]"/>
</div>

<div class="card-body pb-0">
<p class="h3 lh-base">Test-2</p>
<div><span class="tag bg-info-lighter text-info">tag1</span><span class="tag bg-info-lighter text-info">tag2</span></div>
<div class="mt-3">Test description</div>
</div>
<div class="card-footer text-end">
<a class="btn btn-sm btn-ico btn-link stretched-link matomo-event" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="fiches_techniques" href="/forum/test-2-10001/">
<i class="ri-arrow-right-line ri-lg"></i>
</a>
</div>

<div class="col-12 col-md-4 mb-3 mb-md-5">
<div class="card c-card has-one-link-inside h-100">

<div class="card-header card-header-thumbnail rounded">
<img alt="Test-2" class="img-fitcover img-fluid" loading="lazy" src="[img src]"/>
</div>

<div class="card-body pb-0">
<p class="h3 lh-base">Test-2</p>
<div><span class="tag bg-info-lighter text-info">tag1</span><span class="tag bg-info-lighter text-info">tag2</span></div>
<div class="mt-3">Test description</div>
</div>
<div class="card-footer text-end">
<a class="btn btn-sm btn-ico btn-link stretched-link matomo-event" data-matomo-action="view" data-matomo-category="engagement" data-matomo-option="fiches_techniques" href="/forum/test-2-10001/">
<i class="ri-arrow-right-line ri-lg"></i>
</a>
</div>
</div>

</div>
</div>

</div>


</div>
</div>
Expand Down Expand Up @@ -310,6 +311,21 @@
</div>
'''
# ---
# name: TestDocumentationForumContent.test_documentation_forum_with_partner[documentation_forum_with_partner]
'''
[<div class="col-12 col-sm-auto mt-3" id="partner_area">
<a href="/partners/best-partner-ever-[PK of Partner]/">
<div class="d-flex gap-3 align-items-center mb-2">

<img alt="Best Partner Ever" class="img-fluid" loading="lazy" src="[img src]" width="100"/>

<h4>Fiche co-rédigée en partenariat avec Best Partner Ever</h4>
</div>
</a>

</div>]
'''
# ---
# name: TestForumDetailContent.test_forum_detail_foot_content[forum_detail_forum_actions_block]
'''
<div class="col-12 col-sm-auto forum-actions-block">
Expand Down
32 changes: 32 additions & 0 deletions lacommunaute/forum/tests/test_forum_updateview.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from taggit.models import Tag

from lacommunaute.forum.factories import CategoryForumFactory, ForumFactory
from lacommunaute.partner.factories import PartnerFactory
from lacommunaute.users.factories import UserFactory
from lacommunaute.utils.testing import parse_response_to_soup, reset_model_sequence_fixture

Expand Down Expand Up @@ -163,3 +164,34 @@ def test_update_forum_without_tag(client, db):

forum.refresh_from_db()
assert forum.tags.count() == 0


@pytest.mark.parametrize(
"initial_partner,post_partner",
[
(lambda: None, lambda: None),
(lambda: None, lambda: PartnerFactory()),
(lambda: PartnerFactory(), lambda: None),
(lambda: PartnerFactory(), lambda: PartnerFactory()),
],
)
def test_select_partner(client, db, initial_partner, post_partner):
initial_partner = initial_partner()
post_partner = post_partner()
forum = ForumFactory(partner=initial_partner)
client.force_login(UserFactory(is_superuser=True))

url = reverse("forum_extension:edit_forum", kwargs={"pk": forum.pk, "slug": forum.slug})
response = client.post(
url,
data={
"name": forum.name,
"short_description": forum.short_description,
"description": forum.description.raw,
"partner": post_partner.id if post_partner else "",
},
)
assert response.status_code == 302

forum.refresh_from_db()
assert forum.partner == post_partner
3 changes: 2 additions & 1 deletion lacommunaute/forum/tests/tests_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ def test_saved_forum_description(db):
def test_form_field():
form = ForumForm()
assert form.Meta.model == Forum
assert form.Meta.fields == ["name", "short_description", "description", "image", "certified"]
assert form.Meta.fields == ["name", "short_description", "description", "image", "certified", "partner"]
assert form.fields["name"].required
assert form.fields["short_description"].required
assert not form.fields["description"].required
assert not form.fields["image"].required
assert not form.fields["certified"].required
assert not form.fields["partner"].required
assert not form.fields["tags"].required
assert not form.fields["new_tags"].required
8 changes: 8 additions & 0 deletions lacommunaute/forum/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from lacommunaute.forum_conversation.factories import CertifiedPostFactory, PostFactory, TopicFactory
from lacommunaute.forum_conversation.forms import PostForm
from lacommunaute.forum_conversation.models import Topic
from lacommunaute.partner.factories import PartnerFactory
from lacommunaute.users.factories import UserFactory
from lacommunaute.utils.testing import parse_response_to_soup, reset_model_sequence_fixture

Expand Down Expand Up @@ -641,6 +642,13 @@ def test_documentation_forum_header_content(self, client, db, snapshot, reset_fo

assert len(content.find_all("a", href=sibling_forum.get_absolute_url())) == 1

def test_documentation_forum_with_partner(self, client, db, snapshot, documentation_forum):
documentation_forum.partner = PartnerFactory(for_snapshot=True, with_logo=True)
documentation_forum.save()
response = client.get(documentation_forum.get_absolute_url())
content = parse_response_to_soup(response, replace_in_href=[documentation_forum.partner], replace_img_src=True)
assert str(content.select("#partner_area")) == snapshot(name="documentation_forum_with_partner")


class TestDocumentationCategoryForumContent:
def test_documentation_category_subforum_list(
Expand Down
10 changes: 2 additions & 8 deletions lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
from lacommunaute.forum_conversation.forms import PostForm
from lacommunaute.forum_conversation.view_mixins import FilteredTopicsListViewMixin
from lacommunaute.forum_upvote.models import UpVote
from lacommunaute.utils.perms import add_public_perms_on_forum
from lacommunaute.utils.perms import add_public_perms_on_forum, forum_visibility_content_tree_from_forums


logger = logging.getLogger(__name__)

PermissionRequiredMixin = get_class("forum_permission.viewmixins", "PermissionRequiredMixin")
ForumVisibilityContentTree = get_class("forum.visibility", "ForumVisibilityContentTree")


class ForumView(BaseForumView, FilteredTopicsListViewMixin):
Expand Down Expand Up @@ -86,12 +85,7 @@ def get_context_data(self, **kwargs):
context = context | self.get_topic_filter_context()

# vincentporte, overide the method to add the sub_forums, not testing permissions ^v^
context["sub_forums"] = ForumVisibilityContentTree.from_forums(
self.request.forum_permission_handler.forum_list_filter(
self.get_descendants(),
self.request.user,
),
)
context["sub_forums"] = forum_visibility_content_tree_from_forums(self.request, self.get_descendants())

if self.will_render_documentation_variant():
context["sibling_forums"] = forum.get_siblings(include_self=True)
Expand Down
Loading
Loading