Skip to content

Commit

Permalink
Merge branch 'feature/remove-treebeard' into feature/46-producttype-n…
Browse files Browse the repository at this point in the history
…l-api-2
  • Loading branch information
Floris272 committed Dec 24, 2024
2 parents d1124dc + 499e047 commit 82b6db7
Show file tree
Hide file tree
Showing 21 changed files with 489 additions and 448 deletions.
1 change: 0 additions & 1 deletion requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Pillow # handle images

open-api-framework
django-treebeard
# Framework libraries
# django-rosetta

Expand Down
3 changes: 0 additions & 3 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ django==4.2.17
# django-setup-configuration
# django-simple-certmanager
# django-solo
# django-treebeard
# django-two-factor-auth
# djangorestframework
# djangorestframework-inclusions
Expand Down Expand Up @@ -169,8 +168,6 @@ django-solo==2.4.0
# mozilla-django-oidc-db
# notifications-api-common
# zgw-consumers
django-treebeard==4.7.1
# via -r requirements/base.in
django-two-factor-auth==1.17.0
# via maykin-2fa
djangorestframework==3.15.2
Expand Down
5 changes: 0 additions & 5 deletions requirements/ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ django==4.2.17
# django-setup-configuration
# django-simple-certmanager
# django-solo
# django-treebeard
# django-two-factor-auth
# djangorestframework
# djangorestframework-inclusions
Expand Down Expand Up @@ -303,10 +302,6 @@ django-solo==2.4.0
# mozilla-django-oidc-db
# notifications-api-common
# zgw-consumers
django-treebeard==4.7.1
# via
# -c requirements/base.txt
# -r requirements/base.txt
django-two-factor-auth==1.17.0
# via
# -c requirements/base.txt
Expand Down
5 changes: 0 additions & 5 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ django==4.2.17
# django-setup-configuration
# django-simple-certmanager
# django-solo
# django-treebeard
# django-two-factor-auth
# djangorestframework
# djangorestframework-inclusions
Expand Down Expand Up @@ -334,10 +333,6 @@ django-solo==2.4.0
# mozilla-django-oidc-db
# notifications-api-common
# zgw-consumers
django-treebeard==4.7.1
# via
# -c requirements/ci.txt
# -r requirements/ci.txt
django-two-factor-auth==1.17.0
# via
# -c requirements/ci.txt
Expand Down
1 change: 0 additions & 1 deletion src/open_producten/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# Project applications.
"rest_framework.authtoken",
"localflavor",
"treebeard",
"markdownx",
"django.contrib.gis",
"open_producten.accounts",
Expand Down
113 changes: 54 additions & 59 deletions src/open_producten/producttypen/admin/onderwerp.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,72 @@
from django import forms
from django.contrib import admin
from django.forms import BaseModelFormSet
from django.utils.translation import gettext as _
from django.db.models import Count
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _

from treebeard.admin import TreeAdmin
from treebeard.forms import movenodeform_factory

from ..models import Onderwerp, OnderwerpProductType
from ..models import Onderwerp, ProductType
from .vraag import VraagInline


class ProductTypeInline(admin.TabularInline):
model = OnderwerpProductType
fields = ("product_type",)
extra = 1

model = ProductType.onderwerpen.through
extra = 0

class OnderwerpAdminForm(movenodeform_factory(Onderwerp)):
class Meta:
model = Onderwerp
fields = "__all__"
verbose_name = _("Product type")
verbose_name_plural = _("Product typen")

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

class OnderwerpAdminFormSet(BaseModelFormSet):
def clean(self):
super().clean()
def has_delete_permission(self, request, obj=None):
return False

data = {
form.cleaned_data["id"]: form.cleaned_data["gepubliceerd"]
for form in self.forms
}

for onderwerp, gepubliceerd in data.items():
if children := onderwerp.get_children():
if not gepubliceerd and any([data[child] for child in children]):
raise forms.ValidationError(
_(
"Onderwerpen moeten gepubliceerd zijn met gepubliceerde sub onderwerpen."
)
)
def has_add_permission(self, request, obj=None):
return False


@admin.register(Onderwerp)
class OnderwerpAdmin(TreeAdmin):
form = OnderwerpAdminForm
class OnderwerpAdmin(admin.ModelAdmin):
inlines = (
ProductTypeInline,
VraagInline,
ProductTypeInline,
)
search_fields = ("naam",)
list_display = (
"naam",
"gepubliceerd",
)
list_editable = ("gepubliceerd",)
exclude = ("path", "depth", "numchild")
fieldsets = (
(
None,
{
"fields": (
"naam",
"beschrijving",
"gepubliceerd",
"_position",
"_ref_node_id",
),
},
),
)
search_fields = ("naam", "hoofd_onderwerp__naam")
list_display = ("naam", "hoofd_onderwerp", "gepubliceerd", "product_typen_count")

@admin.display(description=_("Aantal product typen"))
def product_typen_count(self, obj):
return obj.product_typen_count

def get_queryset(self, request):
queryset = super().get_queryset(request)
queryset = queryset.annotate(product_typen_count=Count("product_typen"))
return queryset

list_filter = ["gepubliceerd", "product_typen", "hoofd_onderwerp"]

list_filter = ["gepubliceerd", "product_typen"]
def get_deleted_objects(self, objs, request):
"""
Product_typen need at least one onderwerp.
"""

def get_changelist_formset(self, request, **kwargs):
kwargs["formset"] = OnderwerpAdminFormSet
return super().get_changelist_formset(request, **kwargs)
def get_product_type_url(instance):
return reverse("admin:producttypen_producttype_change", args=(instance.id,))

def get_current_product_type_onderwerpen(instance):
return ", ".join(instance.onderwerpen.values_list("naam", flat=True))

errors = []
for product_type in ProductType.objects.filter(onderwerpen__in=objs).distinct():
if product_type.onderwerpen.count() <= objs.count():
errors.append(
format_html(
"Product Type <a href='{}'>{}</a> moet aan een minimaal één onderwerp zijn gelinkt. Huidige onderwerpen: {}.",
get_product_type_url(product_type),
product_type,
get_current_product_type_onderwerpen(product_type),
)
)
if errors:
return [], [], [], errors
return super().get_deleted_objects(objs, request)
98 changes: 59 additions & 39 deletions src/open_producten/producttypen/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.16 on 2024-11-29 13:18
# Generated by Django 4.2.17 on 2024-12-20 13:43

import datetime
from decimal import Decimal
Expand Down Expand Up @@ -53,9 +53,6 @@ class Migration(migrations.Migration):
verbose_name="Updated on",
),
),
("path", models.CharField(max_length=255, unique=True)),
("depth", models.PositiveIntegerField()),
("numchild", models.PositiveIntegerField(default=0)),
(
"naam",
models.CharField(
Expand All @@ -69,37 +66,27 @@ class Migration(migrations.Migration):
markdownx.models.MarkdownxField(
blank=True,
default="",
help_text="Beschrijving van het onderwerp.",
help_text="Beschrijving van het onderwerp, ondersteund markdown format.",
verbose_name="beschrijving",
),
),
],
options={
"verbose_name": "Onderwerp",
"verbose_name_plural": "Onderwerpen",
"ordering": ("path",),
},
),
migrations.CreateModel(
name="OnderwerpProductType",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"onderwerp",
"hoofd_onderwerp",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
blank=True,
help_text="Het hoofd onderwerp van het onderwerp.",
null=True,
on_delete=django.db.models.deletion.RESTRICT,
related_name="sub_onderwerpen",
to="producttypen.onderwerp",
verbose_name="hoofd onderwerp",
),
),
],
options={
"verbose_name": "Onderwerp",
"verbose_name_plural": "Onderwerpen",
},
),
migrations.CreateModel(
name="Prijs",
Expand All @@ -117,7 +104,6 @@ class Migration(migrations.Migration):
"actief_vanaf",
models.DateField(
help_text="De datum vanaf wanneer de prijs actief is.",
unique=True,
validators=[
django.core.validators.MinValueValidator(
datetime.date.today
Expand Down Expand Up @@ -173,7 +159,7 @@ class Migration(migrations.Migration):
models.CharField(
help_text="naam van het product type.",
max_length=100,
verbose_name="naam",
verbose_name="product type naam",
),
),
(
Expand All @@ -188,7 +174,7 @@ class Migration(migrations.Migration):
(
"beschrijving",
markdownx.models.MarkdownxField(
help_text="Product type beschrijving met WYSIWYG editor.",
help_text="Product type beschrijving, ondersteund markdown format.",
verbose_name="beschrijving",
),
),
Expand All @@ -209,7 +195,6 @@ class Migration(migrations.Migration):
blank=True,
help_text="onderwerpen waaraan het product type is gelinkt.",
related_name="product_typen",
through="producttypen.OnderwerpProductType",
to="producttypen.onderwerp",
verbose_name="onderwerp",
),
Expand Down Expand Up @@ -275,12 +260,26 @@ class Migration(migrations.Migration):
serialize=False,
),
),
("vraag", models.CharField(max_length=250, verbose_name="vraag")),
("antwoord", markdownx.models.MarkdownxField(verbose_name="antwoord")),
(
"vraag",
models.CharField(
help_text="De vraag die wordt beantwoord.",
max_length=250,
verbose_name="vraag",
),
),
(
"antwoord",
markdownx.models.MarkdownxField(
help_text="Het antwoord op de vraag, ondersteund markdown format.",
verbose_name="antwoord",
),
),
(
"onderwerp",
models.ForeignKey(
blank=True,
help_text="Het onderwerp waarbij deze vraag hoort.",
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="vragen",
Expand All @@ -292,6 +291,7 @@ class Migration(migrations.Migration):
"product_type",
models.ForeignKey(
blank=True,
help_text="Het product type waarbij deze vraag hoort.",
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="vragen",
Expand Down Expand Up @@ -375,13 +375,33 @@ class Migration(migrations.Migration):
verbose_name="product type",
),
),
migrations.AddField(
model_name="onderwerpproducttype",
name="product_type",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="producttypen.producttype",
),
migrations.CreateModel(
name="OnderwerpProductType",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"onderwerp",
models.ForeignKey(
on_delete=django.db.models.deletion.RESTRICT,
to="producttypen.onderwerp",
),
),
(
"product_type",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="producttypen.producttype",
),
),
],
),
migrations.CreateModel(
name="Link",
Expand Down
Loading

0 comments on commit 82b6db7

Please sign in to comment.