Skip to content

Commit

Permalink
Merge pull request #94 from arthur-schnitzler/main
Browse files Browse the repository at this point in the history
code updates
  • Loading branch information
csae8092 authored Oct 10, 2024
2 parents f4b5f27 + 8b3fb6c commit cfab1b5
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 54 deletions.
33 changes: 33 additions & 0 deletions apis_core/apis_relations/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import django_tables2 as tables

FIELDS_TO_EXCLUDE = [
"start_start_date",
"start_end_date",
"end_start_date",
"end_end_date",
"status",
"source",
"published",
"tempentityclass_ptr",
"review",
"name",
"img_url",
"img_last_checked",
]


CRUD_COLUMN = tables.TemplateColumn(
"""
<a href="{{ record.get_edit_url }}">
<i class="bi bi-pencil-square p-1 fs-5" title="Verbindung bearbeiten" aria-hidden="true">
<span class="visually-hidden">Verbindung bearbeiten</span>
</i>
</a>
<a href="{{ record.get_copy_url }}">
<i class="bi bi-clipboard p-1 fs-5"></i>
</a>
<i class="bi bi-trash p-1 fs-5"></i>
""",
verbose_name="Ändern, Kopieren oder Löschen",
orderable=False,
)
28 changes: 24 additions & 4 deletions apis_core/apis_relations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ def get_related_entity_field_namea(cls):
:return: the name of the field of the A side of the current relation class or instance
E.g. PersonWork -> "related_person"
"""
print(dir(cls))
return None

@classmethod
Expand All @@ -339,7 +338,7 @@ def get_related_entity_field_nameb(cls):
class PersonPerson(AbstractRelation):
@classmethod
def get_listview_url(self):
return reverse_lazy("apis:apis_relations:person_person")
return reverse_lazy(f"apis:apis_relations:{self.__name__.lower()}")

@classmethod
def get_icon(self):
Expand All @@ -357,7 +356,7 @@ def get_color(self):
class PersonPlace(AbstractRelation):
@classmethod
def get_listview_url(self):
return reverse_lazy("apis:apis_relations:person_place")
return reverse_lazy(f"apis:apis_relations:{self.__name__.lower()}")

@classmethod
def get_icon(self):
Expand All @@ -371,6 +370,27 @@ def get_second_icon(self):
def get_color(self):
return "#720e07"

@classmethod
def get_createview_url(self):
return reverse_lazy(f"apis:apis_relations:{self.__name__.lower()}_create")

def get_object_list_view(self):
list_url = self.get_listview_url()
main_id = self.get_related_entity_instancea().id
return f"{list_url}?{self.get_related_entity_field_namea()}={main_id}&sort=-updated"

def get_edit_url(self):
return reverse_lazy(
f"apis:apis_relations:{self.__class__.__name__.lower()}_edit",
kwargs={"pk": self.id},
)

def get_copy_url(self):
return reverse_lazy(
"apis:apis_relations:copy_relation",
kwargs={"pk": self.id, "relation_class": self.__class__.__name__.lower()},
)


class PersonInstitution(AbstractRelation):
@classmethod
Expand All @@ -387,7 +407,7 @@ def get_listview_url(self):
class PersonWork(AbstractRelation):
@classmethod
def get_listview_url(self):
return reverse_lazy("apis:apis_relations:person_work")
return reverse_lazy(f"apis:apis_relations:{self.__name__.lower()}")

@classmethod
def get_icon(self):
Expand Down
16 changes: 2 additions & 14 deletions apis_core/apis_relations/person_person_relation_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,10 @@
from apis_core.apis_entities.models import Person
from apis_core.apis_vocabularies.models import PersonPersonRelation

from .config import FIELDS_TO_EXCLUDE
from .models import PersonPerson


excluded_cols = [
"start_start_date",
"start_end_date",
"end_start_date",
"end_end_date",
"status",
"source",
"published",
"tempentityclass_ptr",
"review",
]


class PersonPersonListFilter(FilterSet):
related_persona = ModelMultipleChoiceFilter(
queryset=Person.objects.all(),
Expand Down Expand Up @@ -134,6 +122,6 @@ class PersonPersonListView(GenericListView):
"related_personb",
]
verbose_name = "Personen und Personen"
exclude_columns = excluded_cols
exclude_columns = FIELDS_TO_EXCLUDE
enable_merge = False
template_name = "apis_relations/list_view.html"
95 changes: 79 additions & 16 deletions apis_core/apis_relations/person_place_relation_views.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,85 @@
from browsing.browsing_utils import GenericListView
from django import forms
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.urls import reverse_lazy

from browsing.browsing_utils import GenericListView, BaseCreateView, BaseUpdateView
from dal import autocomplete
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout
from django.urls import reverse_lazy
from crispy_forms.layout import Layout, Submit
from django_filters import FilterSet, ModelMultipleChoiceFilter, RangeFilter
import django_tables2 as tables

from apis_core.apis_entities.models import Person, Place
from apis_core.apis_vocabularies.models import PersonPlaceRelation

from .models import PersonPlace
from .config import FIELDS_TO_EXCLUDE, CRUD_COLUMN


class PersonPlaceForm(forms.ModelForm):

class Meta:
model = PersonPlace
exclude = FIELDS_TO_EXCLUDE + [
"collection",
]
widgets = {
"related_person": autocomplete.ModelSelect2(
url=reverse_lazy(
"apis:apis_entities:generic_entities_autocomplete",
kwargs={"entity": "person"},
),
attrs={"data-html": True},
),
"related_place": autocomplete.ModelSelect2(
url=reverse_lazy(
"apis:apis_entities:generic_entities_autocomplete",
kwargs={"entity": "place"},
),
attrs={"data-html": True},
),
}

def __init__(self, *args, **kwargs):
super(PersonPlaceForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = True
self.fields["related_person"].required = True
self.fields["related_place"].required = True
self.fields["relation_type"].required = True
self.helper.form_class = "form-horizontal"
self.helper.label_class = "col-md-3"
self.helper.field_class = "col-md-9"
self.helper.add_input(
Submit("submit", "save"),
)


class PersonPlaceCreate(BaseCreateView):

model = PersonPlace
form_class = PersonPlaceForm

def get_success_url(self):
return self.object.get_object_list_view()

@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(PersonPlaceCreate, self).dispatch(*args, **kwargs)


class PersonPlaceUpdate(BaseUpdateView):

model = PersonPlace
form_class = PersonPlaceForm

def get_success_url(self):
return self.object.get_object_list_view()

excluded_cols = [
"start_start_date",
"start_end_date",
"end_start_date",
"end_end_date",
"status",
"source",
"published",
"tempentityclass_ptr",
"review",
]
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(PersonPlaceUpdate, self).dispatch(*args, **kwargs)


class PersonPlaceListFilter(FilterSet):
Expand Down Expand Up @@ -91,7 +148,9 @@ def __init__(self, *args, **kwargs):

class PersonPlaceTable(tables.Table):
related_person = tables.TemplateColumn(
"""<a href="{{ record.related_person.get_absolute_url }}">{{ record.related_person }}</a>""",
"""
<a href="{{ record.related_person.get_absolute_url }}">{{ record.related_person }}</a>
""",
verbose_name="Person",
)
related_place = tables.TemplateColumn(
Expand All @@ -109,6 +168,7 @@ class PersonPlaceTable(tables.Table):
"{% if record.end_date_written %} {{ record.end_date_written }} {% endif %}",
verbose_name="End",
)
crud = CRUD_COLUMN

class Meta:
model = PersonPlace
Expand All @@ -118,6 +178,8 @@ class Meta:
"relation_type",
"related_place",
"start_date_written",
"end_date_written",
"crud",
)


Expand All @@ -132,8 +194,9 @@ class PersonPlaceListView(GenericListView):
"related_person",
"relation_type",
"related_place",
"crud",
]
verbose_name = "Personen und Orte"
exclude_columns = excluded_cols
exclude_columns = FIELDS_TO_EXCLUDE
enable_merge = False
template_name = "apis_relations/list_view.html"
16 changes: 2 additions & 14 deletions apis_core/apis_relations/person_work_relation_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,10 @@
from apis_core.apis_entities.models import Person, Work
from apis_core.apis_vocabularies.models import PersonWorkRelation

from .config import FIELDS_TO_EXCLUDE
from .models import PersonWork


excluded_cols = [
"start_start_date",
"start_end_date",
"end_start_date",
"end_end_date",
"status",
"source",
"published",
"tempentityclass_ptr",
"review",
]


class PersonWorkListFilter(FilterSet):
related_person = ModelMultipleChoiceFilter(
queryset=Person.objects.all(),
Expand Down Expand Up @@ -134,6 +122,6 @@ class PersonWorkListView(GenericListView):
"related_work",
]
verbose_name = "Personen und Werke"
exclude_columns = excluded_cols
exclude_columns = FIELDS_TO_EXCLUDE
enable_merge = False
template_name = "apis_relations/list_view.html"
23 changes: 20 additions & 3 deletions apis_core/apis_relations/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,41 @@
from . import person_place_relation_views
from . import person_work_relation_views
from . import person_person_relation_views
from .views import copy_relation


app_name = "apis_relations"

urlpatterns = [
path(
"copy/<relation_class>/<int:pk>",
copy_relation,
name="copy_relation",
),
path(
"person-place/",
person_place_relation_views.PersonPlaceListView.as_view(),
name="person_place",
name="personplace",
),
path(
"person-place/create/",
person_place_relation_views.PersonPlaceCreate.as_view(),
name="personplace_create",
),
path(
"person-place/edit/<int:pk>",
person_place_relation_views.PersonPlaceUpdate.as_view(),
name="personplace_edit",
),
path(
"person-work/",
person_work_relation_views.PersonWorkListView.as_view(),
name="person_work",
name="personwork",
),
path(
"person-person/",
person_person_relation_views.PersonPersonListView.as_view(),
name="person_person",
name="personperson",
),
path(
"delete/<int:relation_id>/",
Expand Down
12 changes: 12 additions & 0 deletions apis_core/apis_relations/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import inspect
import re

from django.apps import apps
from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.template import loader

from apis_core.apis_entities.models import (
Expand Down Expand Up @@ -41,6 +43,16 @@
form_module_list = [relation_form_module]


@login_required
def copy_relation(request, relation_class, pk):
cur_model = apps.get_model(app_label="apis_relations", model_name=relation_class)
original_object = get_object_or_404(cur_model, id=pk)
original_object.id = None
original_object.pk = None
original_object.save()
return redirect(original_object.get_edit_url())


def turn_form_modules_into_dict(form_module_list):
"""
Since form classes are loaded dynamically from the respective modules and it's settings-dependent which modules
Expand Down
6 changes: 6 additions & 0 deletions browsing/browsing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ def get_table(self, **kwargs):
all_cols = table.base_columns.keys()
selected_cols = self.request.GET.getlist("columns") + default_cols
exclude_vals = [x for x in all_cols if x not in selected_cols]
if self.request.user.is_authenticated:
pass
elif "crud" in all_cols:
exclude_vals += [
"crud",
]
table.exclude = exclude_vals
return table

Expand Down
Loading

0 comments on commit cfab1b5

Please sign in to comment.