Skip to content

Commit

Permalink
relations can be created/deleted and modfied
Browse files Browse the repository at this point in the history
  • Loading branch information
csae8092 committed Dec 12, 2023
1 parent 7638871 commit ec902df
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,20 @@ <h4>{{ obj.0 }}</h4>
<div id="tableDiv__{{ obj.2 }}">
{% render_table obj.1 %}
</div>
<div id="InitCreateFormDiv"
hx-trigger="revealed"
<div id="editFormDiv_{{ obj.2 }}"></div>
<button class="btn"
type="button"
hx-trigger="click"
hx-target="#editFormDiv_{{ obj.2 }}"
hx-post="/apis/relations/ajax/get/"
hx-vals='{
"SiteID": "{{ instance.id }}",
"FormName": "{{ obj.2 }}Form",
"ButtonText": "create",
"ButtonText": "speichern",
"entity_type": "{{ entity_type }}"
}'>
create new instance
</div>
<i class="bi bi-plus-lg" title="neue Verbindung anlegen"></i>
</button>
</div>
{% endfor %}
</div>
Expand Down
10 changes: 3 additions & 7 deletions apis_core/apis_relations/forms2.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ def save(self, site_instance, instance=None, commit=True):
:return: instance of relation
"""
cd = self.cleaned_data
print(cd)
if instance:
x = self.relation_form.objects.get(pk=instance)
else:
Expand All @@ -88,17 +87,14 @@ def save(self, site_instance, instance=None, commit=True):
x.end_date_written = cd["end_date_written"]
x.notes = cd["notes"]
x.references = cd["references"]
print(self.rel_accessor)
setattr(x, self.rel_accessor[3], site_instance)
target = AbstractEntity.get_entity_class_of_name(self.rel_accessor[0])
t1 = target.get_or_create_uri(cd["target"])
if not t1:
print(self.rel_accessor)
t1 = RDFParser(cd["target"], self.rel_accessor[0]).get_or_create()
setattr(x, self.rel_accessor[2], t1)
if commit:
x.save()
print("saved: {}".format(x))
return x

def get_text_id(self):
Expand All @@ -120,8 +116,8 @@ def get_html_table(self, entity_type, request, site_instance, form_match):
)
prefix = prefix.group(1) + prefix.group(2) + "-"
if form_match.group(1) == form_match.group(2):
dic_a = {"related_" + entity_type.lower() + "A": site_instance}
dic_b = {"related_" + entity_type.lower() + "B": site_instance}
dic_a = {"related_" + entity_type.lower() + "a": site_instance}
dic_b = {"related_" + entity_type.lower() + "b": site_instance}
if "apis_highlighter" in settings.INSTALLED_APPS:
objects = self.relation_form.objects.filter_ann_proj(
request=request
Expand Down Expand Up @@ -315,7 +311,7 @@ def __init__(self, siteID=None, highlighter=False, *args, **kwargs):
help_text=help_text_target,
)
else:
print("no hit rel_accessor")
pass
if instance and instance.id:
self.fields["target"].choices = [
(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<form hx-post="/apis/relations/ajax/save/{{ entity_type }}/{{ form_name }}/{{ SiteID }}/" hx-target="#tableDiv__{{ relation_name }}" hx-swap="innerHTML">
<form hx-post="/apis/relations/ajax/save/{{ entity_type }}/{{ form_name }}/{{ SiteID }}{% if ObjectID %}/{{ ObjectID }}{% endif %}/" hx-target="#tableDiv__{{ relation_name }}" hx-swap="innerHTML">
{% load crispy_forms_tags %}
{% crispy form form.helper %}
<div class="form-actions">
<button type="submit" class="btn btn-primary btn-sm">{{ button_text }}</button>
</div>

<button type="submit" class="btn btn-primary">{{ button_text }}</button>
</form>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<button
class="btn btn-danger"
class="btn"
type="button"
title="Verbindung löschen"
hx-get="/apis/relations/delete/{{ record.pk }}/"
hx-target="closest tr" hx-swap="outerHTML swap:1s"
hx-confirm="Sicher, weil was weg ist ist weg"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{% load apis_helpers %}
<a
hx-trigger="click"
hx-target="#InitCreateFormDiv"
hx-post="/apis/relations/ajax/get/"
hx-target="#editFormDiv_{{ record|content_type }}"
hx-vals='{
"FormName": "{{ record|content_type }}Form",
"SiteId": "{{ site_id }}",
"ObjectId": "{% if record.relation_pk %}{{record.relation_pk}}{% else %}{{ record.pk}}{% endif %}",
"SiteID": "{{ site_id }}",
"ObjectID": "{% if record.relation_pk %}{{record.relation_pk}}{% else %}{{ record.pk}}{% endif %}",
"entity_type": "{{ entity_type }}",
"ButtonText": "edit"
"ButtonText": "speichern"
}'>
edit
</a>
45 changes: 15 additions & 30 deletions apis_core/apis_relations/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import json
import re
import inspect

from icecream import ic
from apis_core.apis_metainfo.models import TempEntityClass
from django.template import loader

from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponse, Http404, JsonResponse
from django.http import HttpResponse, Http404
from apis_core.apis_relations import forms as relation_form_module

from apis_core.apis_entities.models import (
Expand Down Expand Up @@ -37,7 +35,7 @@
PlaceWork,
EventWork,
WorkWork,
EventEvent
EventEvent,
)

# from .forms import PersonLabelForm, InstitutionLabelForm, PlaceLabelForm, EventLabelForm
Expand Down Expand Up @@ -121,15 +119,6 @@ def get_form_ajax(request):
ButtonText = request.POST.get("ButtonText")
ObjectID = request.POST.get("ObjectID")
entity_type_str = request.POST.get("entity_type")
print("###########################")
print("###########################")
print(f"FormName: {FormName}")
print(f"SiteID: {SiteID}")
print(f"ButtonText: {ButtonText}")
print(f"ObjectID: {ObjectID}")
print(f"entity_type_str: {entity_type_str}")
print("###########################")
print("###########################")
relation_name = FormName.replace("Form", "")
form_match = re.match(r"([A-Z][a-z]+)([A-Z][a-z]+)(Highlighter)?Form", FormName)
form_match2 = re.match(r"([A-Z][a-z]+)(Highlighter)?Form", FormName)
Expand Down Expand Up @@ -173,18 +162,17 @@ def get_form_ajax(request):
else:
form_class = form_class_dict[FormName]
form = form_class(**form_dict)
tab = FormName[:-4]
template = loader.get_template("apis_relations/_ajax_form.html")
form_context = {
"entity_type": entity_type_str,
"form": form,
"form_name": FormName,
"relation_name": relation_name,
"url2": "save_ajax_" + FormName,
"button_text": ButtonText,
"ObjectID": ObjectID,
"SiteID": SiteID,
}
"entity_type": entity_type_str,
"form": form,
"form_name": FormName,
"relation_name": relation_name,
"url2": "save_ajax_" + FormName,
"button_text": ButtonText,
"ObjectID": ObjectID,
"SiteID": SiteID,
}

return HttpResponse(template.render(form_context, request))

Expand All @@ -196,10 +184,7 @@ def save_ajax_form(request, entity_type, kind_form, SiteID, ObjectID=False):
raise Http404
entity_type_str = entity_type
entity_type = AbstractEntity.get_entity_class_of_name(entity_type)
ic(entity_type)
ic(kind_form)
ic(SiteID)

object_id = ObjectID
form_match = re.match(r"([A-Z][a-z]+)([A-Z][a-z]+)?(Highlighter)?Form", kind_form)
form_dict = {"data": request.POST, "entity_type": entity_type, "request": request}

Expand All @@ -218,8 +203,8 @@ def save_ajax_form(request, entity_type, kind_form, SiteID, ObjectID=False):
if form.is_valid():
site_instance = entity_type.objects.get(pk=SiteID)

if ObjectID:
form.save(instance=ObjectID, site_instance=site_instance)
if object_id:
form.save(instance=object_id, site_instance=site_instance)
else:
form.save(site_instance=site_instance)
if test_form_relations.count() > 0:
Expand Down Expand Up @@ -248,4 +233,4 @@ def save_ajax_form(request, entity_type, kind_form, SiteID, ObjectID=False):
def delete_relation_view(request, relation_id):
instance = TempEntityClass.objects.get(id=relation_id)
instance.delete()
return HttpResponse(f"<small>gelöschte Verbindung: {relation_id}</small> ")
return HttpResponse(f"<small>gelöschte Verbindung: {relation_id}</small> ")

0 comments on commit ec902df

Please sign in to comment.