Skip to content

Commit

Permalink
fix(apis_entities): only set collection if the attribute exists
Browse files Browse the repository at this point in the history
`collection` is a leftover of the TempEntityClass. some projects still
use that, so we set it if it exists, but we check first if the object we
are working with even has such an attribute.

Closes: #313
  • Loading branch information
b1rger committed Oct 3, 2023
1 parent 789d906 commit 357d6d0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions apis_core/apis_entities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,18 @@ def sort_fields_list(field_names, entity_name):

def save(self, *args, **kwargs):
obj = super(GenericEntitiesForm, self).save(*args, **kwargs)
if obj.collection.all().count() == 0:
col_name = getattr(
settings, "APIS_DEFAULT_COLLECTION", "manually created entity"
)
col, created = Collection.objects.get_or_create(name=col_name)
obj.collection.add(col)

# backwards compatibility
# collection is a field of TempEntityClass - this block
# can be removed when TempEntityClass is gone from apis_entities
# for now we at least check if the attribute exist
if hasattr(obj, "collection"):
if obj.collection.all().count() == 0:
col_name = getattr(
settings, "APIS_DEFAULT_COLLECTION", "manually created entity"
)
col, created = Collection.objects.get_or_create(name=col_name)
obj.collection.add(col)
return obj

return GenericEntitiesForm
Expand Down

0 comments on commit 357d6d0

Please sign in to comment.