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

fix(generic): only add collections if app is installed #1555

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading