Skip to content

Commit

Permalink
fix(generic): only add collections if app is installed
Browse files Browse the repository at this point in the history
Unconditionally importing the SkosCollection class leads to an error, if
the `collections` app is not enabled. This commit fixes this by using
the app registry to access the model.

Closes: #1554
  • Loading branch information
b1rger committed Jan 27, 2025
1 parent f8519bb commit a709d33
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions apis_core/generic/forms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import logging

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from dal import autocomplete
from django import forms
from django.apps import apps
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse

from apis_core.collections.models import SkosCollection
from apis_core.generic.abc import GenericModel
from apis_core.generic.forms.fields import ModelImportChoiceField

logger = logging.getLogger(__name__)


class GenericImportForm(forms.Form):
class Meta:
Expand Down Expand Up @@ -65,13 +69,17 @@ class Meta:

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["collections"] = forms.ModelMultipleChoiceField(
required=False, queryset=SkosCollection.objects.all()
)
if instance := kwargs.get("instance"):
self.fields["collections"].initial = SkosCollection.objects.by_instance(
instance
).values_list("pk", flat=True)
try:
skoscollection = apps.get_model("collections.SkosCollection")
self.fields["collections"] = forms.ModelMultipleChoiceField(
required=False, queryset=skoscollection.objects.all()
)
if instance := kwargs.get("instance"):
self.fields["collections"].initial = skoscollection.objects.by_instance(
instance
).values_list("pk", flat=True)
except LookupError as e:
logger.debug("Not adding collections to form: %s", e)

self.helper = FormHelper(self)
self.helper.add_input(Submit("submit", "Submit"))
Expand All @@ -98,11 +106,15 @@ def __init__(self, *args, **kwargs):

def save(self, *args, **kwargs):
instance = super().save(*args, **kwargs)
if collections := self.cleaned_data.get("collections"):
for collection in SkosCollection.objects.exclude(pk__in=collections):
collection.remove(instance)
for collection in SkosCollection.objects.filter(pk__in=collections):
collection.add(instance)
try:
skoscollection = apps.get_model("collections.SkosCollection")
if collections := self.cleaned_data.get("collections"):
for collection in skoscollection.objects.exclude(pk__in=collections):
collection.remove(instance)
for collection in skoscollection.objects.filter(pk__in=collections):
collection.add(instance)
except LookupError as e:
logger.debug("Not creating collections from form: %s", e)
return instance


Expand Down

0 comments on commit a709d33

Please sign in to comment.