Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Sep 12, 2024
1 parent 5999020 commit aa06701
Show file tree
Hide file tree
Showing 41 changed files with 799 additions and 229 deletions.
2 changes: 2 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.urls import include, path, re_path
from machina.core.loading import get_class

from lacommunaute.documentation import urls as documentation_urls
from lacommunaute.event import urls as event_urls
from lacommunaute.forum import urls as forum_extension_urls
from lacommunaute.forum_conversation import urls as forum_conversation_extension_urls
Expand All @@ -30,6 +31,7 @@
path("inclusion_connect/", include(inclusion_connect_urls)),
# www.
path("", include(pages_urls)),
path("documentation/", include(documentation_urls)),
path("members/", include(forum_member_urls)),
path("", include(forum_conversation_extension_urls)),
path("", include(forum_extension_urls)),
Expand Down
8 changes: 7 additions & 1 deletion lacommunaute/documentation/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
from lacommunaute.utils.validators import validate_image_size


class AbstractPublication(models.Model):
class AbstractDatedModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
abstract = True

class AbstractPublication(AbstractDatedModel):
name = models.CharField(max_length=100, verbose_name=_("Name"))
slug = models.SlugField(max_length=255, verbose_name=_("Slug"), unique=True)

Expand All @@ -30,3 +35,4 @@ class Meta:
def save(self, *args, **kwargs):
self.slug = slugify(force_str(self.name), allow_unicode=True)
super().save(*args, **kwargs)

38 changes: 38 additions & 0 deletions lacommunaute/documentation/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.contrib import admin

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


class DocumentInlines(admin.TabularInline):
model = Document
extra = 0
fields = ("name", "short_description")
readonly_fields = ("name", "short_description")

def has_delete_permission(self, request, obj=None):
return False

def has_add_permission(self, request, obj=None):
return False

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ("name",)
search_fields = ("name",)
fields = ("name", "short_description", "description", "image")
inlines = [DocumentInlines]

@admin.register(Document)
class DocumentAdmin(admin.ModelAdmin):
list_display = ("name","category")
list_filter = ("category",)
search_fields = ("name",)
fields = ("name", "short_description", "description", "image")

@admin.register(DocumentRating)
class DocumentRatingAdmin(admin.ModelAdmin):
list_display = ("document", "rating", "created_at")
list_filter = ("document",)
list_display_links = ("rating",)
raw_id_fields = ("document", "user")
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@


from django.contrib.contenttypes.models import ContentType
from taggit.models import TaggedItem

from lacommunaute.documentation.models import Category, Document, DocumentRating
from lacommunaute.forum.models import Forum, ForumRating
from lacommunaute.forum_conversation.models import Topic
from lacommunaute.forum_upvote.models import UpVote
from lacommunaute.stats.models import DocumentStat, ForumStat


def create_categories_from_catforums():
transpo_dict = {}
for forum in Forum.objects.filter(type=1, level=0):
category = Category.objects.create(
name=forum.name,
short_description=forum.short_description,
description=forum.description,
image=forum.image,
)
print(f"{category} created")
transpo_dict[forum] = category
return transpo_dict


def create_document_from_forums(category_transpo_dict):
forum_content_type = ContentType.objects.get_for_model(Forum)
document_content_type = ContentType.objects.get_for_model(Document)
transpo_dict = {}
for forum in Forum.objects.filter(parent__type=1):
document = Document.objects.create(
name=forum.name,
short_description=forum.short_description,
description=forum.description,
image=forum.image,
category = category_transpo_dict[forum.parent],
partner = forum.partner,
certified = forum.certified,
)
UpVote.objects.filter(content_type=forum_content_type, object_id=forum.id).update(content_type=document_content_type, object_id=document.id)
TaggedItem.objects.filter(content_type=forum_content_type, object_id=forum.id).update(content_type=document_content_type, object_id=document.id)
transpo_dict[forum] = document
print('Documents created')
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_at=rating.created, updated_at=rating.updated) for rating in ForumRating.objects.all()]
DocumentRating.objects.bulk_create(document_ratings)
ForumRating.objects.all().delete()
print(f"{len(document_ratings)} ratings migrated")

def link_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)
topics.update(document=document, forum=main_forum)
for topic in topics:
topic.save()
forum.save()
print('Topics linked to documents')

def convert_stats(document_transpo_dict):
document_stats = [DocumentStat(document=document_transpo_dict[stat.forum], date=stat.date, period=stat.period, visits=stat.visits, entry_visits=stat.entry_visits, time_spent=stat.time_spent) for stat in ForumStat.objects.all()]

def add_redirections():
# for categories
# for documents
pass

def del_forums():
#categories
#documents
pass


# main
category_transpo_dict = create_categories_from_catforums()
document_transpo_dict = create_document_from_forums(category_transpo_dict)
migrate_ratings(document_transpo_dict)
link_topics(document_transpo_dict)
81 changes: 81 additions & 0 deletions lacommunaute/documentation/migrations/0003_document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Generated by Django 5.0.9 on 2024-09-11 13:48

import django.db.models.deletion
import lacommunaute.utils.validators
import machina.models.fields
import storages.backends.s3
import taggit.managers
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("documentation", "0002_alter_category_options"),
("partner", "0002_alter_partner_options"),
("taggit", "0006_rename_taggeditem_content_type_object_id_taggit_tagg_content_8fc721_idx"),
]

operations = [
migrations.CreateModel(
name="Document",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("name", models.CharField(max_length=100, verbose_name="Name")),
("slug", models.SlugField(max_length=255, unique=True, verbose_name="Slug")),
(
"description",
machina.models.fields.MarkupTextField(
blank=True, no_rendered_field=True, null=True, verbose_name="Description"
),
),
(
"short_description",
models.CharField(blank=True, max_length=400, null=True, verbose_name="Description courte (SEO)"),
),
(
"image",
models.ImageField(
storage=storages.backends.s3.S3Storage(bucket_name="private-bucket", file_overwrite=False),
upload_to="",
validators=[lacommunaute.utils.validators.validate_image_size],
),
),
(
"certified",
models.BooleanField(default=False, verbose_name="Certifié par la communauté de l'inclusion"),
),
("_description_rendered", models.TextField(blank=True, editable=False, null=True)),
(
"category",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="documents",
to="documentation.category",
),
),
(
"partner",
models.ForeignKey(
blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to="partner.partner"
),
),
(
"tags",
taggit.managers.TaggableManager(
help_text="A comma-separated list of tags.",
through="taggit.TaggedItem",
to="taggit.Tag",
verbose_name="Tags",
),
),
],
options={
"verbose_name": "Document",
"verbose_name_plural": "Documents",
"ordering": ["-created_at"],
},
),
]
41 changes: 41 additions & 0 deletions lacommunaute/documentation/migrations/0004_documentrating.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 5.0.9 on 2024-09-12 14:14

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


class Migration(migrations.Migration):

dependencies = [
("documentation", "0003_document"),
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_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=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_at",),
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.0.9 on 2024-09-12 14:20

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("documentation", "0004_documentrating"),
]

operations = [
migrations.AlterField(
model_name="documentrating",
name="created_at",
field=models.DateTimeField(blank=True, null=True),
),
migrations.AlterField(
model_name="documentrating",
name="updated_at",
field=models.DateTimeField(blank=True, null=True),
),
]
40 changes: 39 additions & 1 deletion lacommunaute/documentation/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
from lacommunaute.documentation.abstract_models import AbstractPublication
from pyexpat import model

from django.conf import settings
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from taggit.managers import TaggableManager

from lacommunaute.documentation.abstract_models import AbstractDatedModel, AbstractPublication
from lacommunaute.forum_upvote.models import UpVote
from lacommunaute.partner.models import Partner


class Category(AbstractPublication):
Expand All @@ -9,3 +18,32 @@ class Meta:

def __str__(self):
return f"{self.name}"

class Document(AbstractPublication):
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="documents")
partner = models.ForeignKey(Partner, on_delete=models.CASCADE, null=True, blank=True)
upvotes = GenericRelation(UpVote, related_query_name="document")
certified = models.BooleanField(default=False, verbose_name="Certifié par la communauté de l'inclusion")
tags = TaggableManager()

class Meta:
verbose_name = "Document"
verbose_name_plural = "Documents"
ordering = ["-created_at"]

def __str__(self):
return f"{self.name}"

# use AbstractDatedModel after ForumRanting migration
class DocumentRating(models.Model):
created_at = models.DateTimeField(null=True, blank=True)
updated_at = 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_at",)
13 changes: 13 additions & 0 deletions lacommunaute/documentation/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.urls import path

from lacommunaute.documentation.views import CategoryDetailView, CategoryListView, DocumentDetailView


app_name = "documentation"


urlpatterns = [
path("", CategoryListView.as_view(), name="category_list"),
path("<str:slug>-<int:pk>/", CategoryDetailView.as_view(), name="category_detail"),
path("<str:category_slug>-<int:category_pk>/<str:slug>-<int:pk>/", DocumentDetailView.as_view(), name="document_detail"),
]
Loading

0 comments on commit aa06701

Please sign in to comment.