Skip to content

Commit

Permalink
feat: let projects override entities forms using classes
Browse files Browse the repository at this point in the history
Using `get_member_for_entity` for retreiving the form for entities in
the edit views, we allow projects to define forms in their ontology in
the `forms` module.
  • Loading branch information
b1rger committed Dec 12, 2023
1 parent 2211c93 commit 14a2489
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions apis_core/apis_entities/edit_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from apis_core.utils import helpers
from apis_core.utils.settings import get_entity_settings_by_modelname
from apis_core.apis_entities.mixins import EntityMixin, EntityInstanceMixin
from apis_core.utils.helpers import get_member_for_entity


@method_decorator(login_required, name="dispatch")
Expand Down Expand Up @@ -84,7 +85,10 @@ def get(self, request, *args, **kwargs):
tb_object_open,
)
)
form = get_entities_form(self.entity.title())

form = get_member_for_entity(self.entity_model, suffix="Form")
if form is None:
form = get_entities_form(self.entity.title())
form = form(instance=self.instance)
if "apis_bibsonomy" in settings.INSTALLED_APPS:
apis_bibsonomy = getattr(settings, "APIS_BIBSONOMY_FIELDS", [])
Expand Down Expand Up @@ -125,7 +129,9 @@ def get(self, request, *args, **kwargs):
return HttpResponse(template.render(request=request, context=context))

def post(self, request, *args, **kwargs):
form = get_entities_form(self.entity.title())
form = get_member_for_entity(self.entity_model, suffix="Form")
if form is None:
form = get_entities_form(self.entity.title())
form = form(request.POST, instance=self.instance)
if form.is_valid():
entity_2 = form.save()
Expand Down Expand Up @@ -154,7 +160,9 @@ def post(self, request, *args, **kwargs):
@method_decorator(login_required, name="dispatch")
class GenericEntitiesCreateView(EntityMixin, View):
def get(self, request, *args, **kwargs):
form = get_entities_form(self.entity.title())
form = get_member_for_entity(self.entity_model, suffix="Form")
if form is None:
form = get_entities_form(self.entity.title())
form = form()
permissions = {
"create": request.user.has_perm("entities.add_{}".format(self.entity))
Expand All @@ -172,7 +180,9 @@ def get(self, request, *args, **kwargs):
)

def post(self, request, *args, **kwargs):
form = get_entities_form(self.entity.title())
form = get_member_for_entity(self.entity_model, suffix="Form")
if form is None:
form = get_entities_form(self.entity.title())
form = form(request.POST)
if form.is_valid():
entity_2 = form.save()
Expand Down

0 comments on commit 14a2489

Please sign in to comment.