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 7, Rating, modèle, migration et vue #792

Closed
wants to merge 11 commits into from
Closed
10 changes: 9 additions & 1 deletion lacommunaute/documentation/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin

from lacommunaute.documentation.models import Category, Document
from lacommunaute.documentation.models import Category, Document, DocumentRating


class DocumentInlines(admin.TabularInline):
Expand Down Expand Up @@ -30,3 +30,11 @@ class DocumentAdmin(admin.ModelAdmin):
list_filter = ("category", "partner")
search_fields = ("name",)
fields = ("name", "short_description", "description", "image", "category", "partner", "certified", "tags")


@admin.register(DocumentRating)
class DocumentRatingAdmin(admin.ModelAdmin):
list_display = ("document", "rating", "created")
list_filter = ("document",)
list_display_links = ("rating",)
raw_id_fields = ("document", "user")
17 changes: 16 additions & 1 deletion lacommunaute/documentation/factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import factory
from faker import Faker

from lacommunaute.documentation.models import Category, Document
from lacommunaute.documentation.models import Category, Document, DocumentRating


faker = Faker()
Expand Down Expand Up @@ -46,3 +46,18 @@ def with_tags(self, create, extracted, **kwargs):
if isinstance(extracted, list):
for tag in extracted:
self.tags.add(tag)


class DocumentRatingFactory(factory.django.DjangoModelFactory):
document = factory.SubFactory(DocumentFactory)
rating = factory.Faker("random_int", min=1, max=5)

class Meta:
model = DocumentRating
skip_postgeneration_save = True

@factory.post_generation
def set_created(self, create, extracted, **kwargs):
if extracted:
self.created = extracted
self.save()
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from django.core.management.base import BaseCommand
from taggit.models import TaggedItem

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


def create_categories_from_catforums():
Expand Down Expand Up @@ -52,6 +52,22 @@ def create_document_from_forums(category_transpo_dict):
return transpo_dict


def migrate_ratings(document_transpo_dict):
document_ratings = [
DocumentRating(
document=document_transpo_dict[rating.forum],
session_id=rating.session_id,
rating=rating.rating,
user=rating.user,
created=rating.created,
updated=rating.updated,
)
for rating in ForumRating.objects.all()
]
DocumentRating.objects.bulk_create(document_ratings)
ForumRating.objects.all().delete()


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 @@ -69,7 +85,10 @@ def handle(self, *args, **options):
document_transpo_dict = create_document_from_forums(category_transpo_dict)
sys.stdout.write("Documents created\n")

## TODO next : migrate Ratings, Topics and Stats
migrate_ratings(document_transpo_dict)
sys.stdout.write("Ratings migrated\n")

## TODO next : Topics and Stats

deleted_forums = del_forums(category_transpo_dict, document_transpo_dict)
sys.stdout.write(f"{deleted_forums} forums deleted\n")
Expand Down
40 changes: 40 additions & 0 deletions lacommunaute/documentation/migrations/0002_documentrating.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 5.0.9 on 2024-10-01 10:02

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


class Migration(migrations.Migration):
dependencies = [
("documentation", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="DocumentRating",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("created", models.DateTimeField(blank=True, null=True)),
("updated", models.DateTimeField(blank=True, null=True)),
("session_id", models.CharField(max_length=40)),
("rating", models.PositiveSmallIntegerField()),
(
"document",
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="documentation.document"),
),
(
"user",
models.ForeignKey(
blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
),
],
options={
"verbose_name": "Notation d'un document",
"verbose_name_plural": "Notations des documents",
"ordering": ("-created",),
},
),
]
16 changes: 16 additions & 0 deletions lacommunaute/documentation/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.urls import reverse
Expand Down Expand Up @@ -43,3 +44,18 @@ def get_absolute_url(self):
return reverse(
"documentation:document_detail", kwargs={"category_pk": self.category.pk, "pk": self.pk, "slug": self.slug}
)


# TODO : use AbstractDatedModel after ForumRating migration
class DocumentRating(models.Model):
created = models.DateTimeField(null=True, blank=True)
updated = models.DateTimeField(null=True, blank=True)
session_id = models.CharField(max_length=40)
document = models.ForeignKey(Document, on_delete=models.CASCADE)
rating = models.PositiveSmallIntegerField()
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True)

class Meta:
verbose_name = "Notation d'un document"
verbose_name_plural = "Notations des documents"
ordering = ("-created",)
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,39 @@
</div>


<div class="mb-5 rating" id="rating-area">
<hr/>

<h3 class="h3">Cette fiche vous a-t-elle été utile ?</h3>
<form hx-post="/documentation/[PK of Category]/[Slug of Document]-[PK of Document]/rate/" hx-swap="outerHTML" hx-target="#rating-area" id="rating-button">
<div aria-label="Notation de la fiche pratique" class="btn-group btn-group-lg" role="group">

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="1">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="2">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="3">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="4">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="5">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

</div>
</form>

<hr/>
</div>

</div>
</div>
<div class="col-12 col-lg-3 vertical-line">
Expand Down Expand Up @@ -166,6 +199,39 @@
</div>


<div class="mb-5 rating" id="rating-area">
<hr/>

<h3 class="h3">Cette fiche vous a-t-elle été utile ?</h3>
<form hx-post="/documentation/[PK of Category]/[Slug of Document]-[PK of Document]/rate/" hx-swap="outerHTML" hx-target="#rating-area" id="rating-button">
<div aria-label="Notation de la fiche pratique" class="btn-group btn-group-lg" role="group">

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="1">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="2">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="3">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="4">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="5">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

</div>
</form>

<hr/>
</div>

</div>
</div>
<div class="col-12 col-lg-3 vertical-line">
Expand Down Expand Up @@ -273,6 +339,39 @@

</div>

<div class="mb-5 rating" id="rating-area">
<hr/>

<h3 class="h3">Cette fiche vous a-t-elle été utile ?</h3>
<form hx-post="/documentation/[PK of Category]/[Slug of Document]-[PK of Document]/rate/" hx-swap="outerHTML" hx-target="#rating-area" id="rating-button">
<div aria-label="Notation de la fiche pratique" class="btn-group btn-group-lg" role="group">

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="1">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="2">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="3">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="4">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="5">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

</div>
</form>

<hr/>
</div>

</div>
</div>
<div class="col-12 col-lg-3 vertical-line">
Expand Down Expand Up @@ -370,6 +469,39 @@
</div>


<div class="mb-5 rating" id="rating-area">
<hr/>

<h3 class="h3">Cette fiche vous a-t-elle été utile ?</h3>
<form hx-post="/documentation/[PK of Category]/[Slug of Document]-[PK of Document]/rate/" hx-swap="outerHTML" hx-target="#rating-area" id="rating-button">
<div aria-label="Notation de la fiche pratique" class="btn-group btn-group-lg" role="group">

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="1">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="2">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="3">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="4">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

<button class="btn btn-ico-only btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="5">
<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>
</button>

</div>
</form>

<hr/>
</div>

</div>
</div>
<div class="col-12 col-lg-3 vertical-line">
Expand Down Expand Up @@ -402,3 +534,50 @@
</main>
'''
# ---
# name: test_detail_view_with_rated_document[document_detail_view_with_rated_document]
'''
<div class="mb-5 rating" id="rating-area">
<hr/>

<h3 class="h3">Merci d'avoir noté cette fiche !</h3>
<div class="d-flex gap-3 align-items-center mb-2">
<div aria-label="Notation de la fiche pratique" class="btn-group btn-group-lg" role="group">

<div class="btn btn-ico-only">

<i aria-hidden="true" class="ri-star-fill font-weight-medium"></i>

</div>

<div class="btn btn-ico-only">

<i aria-hidden="true" class="ri-star-fill font-weight-medium"></i>

</div>

<div class="btn btn-ico-only">

<i aria-hidden="true" class="ri-star-fill font-weight-medium"></i>

</div>

<div class="btn btn-ico-only">

<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>

</div>

<div class="btn btn-ico-only">

<i aria-hidden="true" class="ri-star-line font-weight-medium"></i>

</div>

</div>
<div class="text-muted">1 avis, moyenne 3,00</div>
</div>

<hr/>
</div>
'''
# ---
Loading
Loading