Skip to content

Commit

Permalink
Merge branch 'master' of gitlab.com:acdh-oeaw/apis/apis-core
Browse files Browse the repository at this point in the history
  • Loading branch information
sennierer committed Mar 25, 2020
2 parents 3d2ae2f + ad5320b commit 455e240
Show file tree
Hide file tree
Showing 15 changed files with 581 additions and 234 deletions.
52 changes: 24 additions & 28 deletions apis_core/apis_entities/detail_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
from django_tables2 import RequestConfig
from django.conf import settings

from apis_core.apis_relations.models import AbstractRelation
from apis_core.helper_functions.utils import access_for_all

from .views import get_highlighted_texts
from .models import Work
from .models import AbstractEntity
from apis_core.apis_labels.models import Label
from apis_core.apis_metainfo.models import Uri
from apis_core.apis_relations.tables import (
get_generic_relations_table, EntityLabelTable, EntityDetailViewLabelTable
)
from apis_core.apis_relations.tables import get_generic_relations_table, LabelTableBase#, EntityDetailViewLabelTable


class GenericEntitiesDetailView(UserPassesTestMixin, View):
Expand All @@ -34,42 +33,38 @@ def get(self, request, *args, **kwargs):

entity = kwargs['entity'].lower()
pk = kwargs['pk']
entity_model = ContentType.objects.get(
app_label='apis_entities', model=entity).model_class()
entity_model = AbstractEntity.get_entity_class_of_name(entity)
instance = get_object_or_404(entity_model, pk=pk)
relations = ContentType.objects.filter(app_label='apis_relations', model__icontains=entity)
relations = AbstractRelation.get_relation_classes_of_entity_name(entity_name=entity)
side_bar = []
for rel in relations:
match = str(rel).split()
match = [
rel.get_related_entity_classA().__name__.lower(),
rel.get_related_entity_classB().__name__.lower()
]
prefix = "{}{}-".format(match[0].title()[:2], match[1].title()[:2])
table = get_generic_relations_table(''.join(match), entity, detail=True)
title_card = ''
table = get_generic_relations_table(relation_class=rel, entity_instance=instance, detail=True)
if match[0] == match[1]:
title_card = entity.title()
dict_1 = {'related_' + entity.lower() + 'A': instance}
dict_2 = {'related_' + entity.lower() + 'B': instance}
if 'apis_highlighter' in settings.INSTALLED_APPS:
object_pre = rel.model_class().annotation_links.filter_ann_proj(request=request).filter(
objects = rel.annotation_links.filter_ann_proj(request=request).filter(
Q(**dict_1) | Q(**dict_2))
else:
object_pre = rel.model_class().objects.filter(
objects = rel.objects.filter(
Q(**dict_1) | Q(**dict_2))
objects = []
for x in object_pre:
objects.append(x.get_table_dict(instance))
else:
if match[0].lower() == entity.lower():
title_card = match[1].title()
else:
title_card = match[0].title()
dict_1 = {'related_' + entity.lower(): instance}
if 'apis_highlighter' in settings.INSTALLED_APPS:
objects = list(rel.model_class()
.annotation_links.filter_ann_proj(request=request)
.filter(**dict_1))
objects = rel.annotation_links.filter_ann_proj(request=request).filter(**dict_1)
else:
objects = list(rel.model_class().objects.filter(**dict_1))
tb_object = table(objects, prefix=prefix)
objects = rel.objects.filter(**dict_1)
tb_object = table(data=objects, prefix=prefix)
tb_object_open = request.GET.get(prefix + 'page', None)
RequestConfig(request, paginate={"per_page": 10}).configure(tb_object)
side_bar.append(
Expand All @@ -78,7 +73,7 @@ def get(self, request, *args, **kwargs):
object_lod = Uri.objects.filter(entity=instance)
object_texts, ann_proj_form = get_highlighted_texts(request, instance)
object_labels = Label.objects.filter(temp_entity=instance)
tb_label = EntityDetailViewLabelTable(object_labels, prefix=entity.title()[:2]+'L-')
tb_label = LabelTableBase(data=object_labels, prefix=entity.title()[:2]+'L-')
tb_label_open = request.GET.get('PL-page', None)
side_bar.append(('Label', tb_label, 'PersonLabel', tb_label_open))
RequestConfig(request, paginate={"per_page": 10}).configure(tb_label)
Expand Down Expand Up @@ -134,10 +129,11 @@ def get(self, request, *args, **kwargs):
))


class WorkDetailView(DetailView):
model = Work
template_name = 'apis_entities/detail_views/work_detail.html'

def get_context_data(self, **kwargs):
context = super(WorkDetailView, self).get_context_data(**kwargs)
return context
# TODO __sresch__ : This seems unused. Remove it once sure
# class WorkDetailView(DetailView):
# model = Work
# template_name = 'apis_entities/detail_views/work_detail.html'
#
# def get_context_data(self, **kwargs):
# context = super(WorkDetailView, self).get_context_data(**kwargs)
# return context
14 changes: 14 additions & 0 deletions apis_core/apis_entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,20 @@ def get_all_entity_classes(cls):
return cls._all_entity_classes


@classmethod
def get_entity_class_of_name(cls, entity_name):
"""
:param entity_name: str : The name of an entity
:return: The model class of the entity respective to the given name
"""

for entity_class in cls.get_all_entity_classes():
if entity_class.__name__.lower() == entity_name.lower():
return entity_class

raise Exception("Could not find entity class of name:", entity_name)


@classmethod
def get_all_entity_names(cls):
"""
Expand Down
77 changes: 26 additions & 51 deletions apis_core/apis_entities/tables.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import django_tables2 as tables
from django_tables2.utils import A
from django.contrib.contenttypes.models import ContentType
from django.conf import settings
from django.utils.safestring import mark_safe

from .models import Person, Place, Institution, Event, Work
from apis_core.apis_entities.models import AbstractEntity
from apis_core.apis_metainfo.tables import (
generic_order_start_date_written,
generic_order_end_date_written,
generic_render_start_date_written,
generic_render_end_date_written
)

input_form = """
<input type="checkbox" name="keep" value="{}" title="keep this"/> |
Expand All @@ -24,9 +28,21 @@ def render(self, value):
)


def get_entities_table(entity, edit_v, default_cols=['name', ]):
def get_entities_table(entity, edit_v, default_cols):

if default_cols is None:
default_cols = ['name', ]

class GenericEntitiesTable(tables.Table):

# reuse the logic for ordering and rendering *_date_written
# Important: The names of these class variables must correspond to the column field name,
# e.g. for start_date_written, the methods must be named order_start_date_written and render_start_date_written
order_start_date_written = generic_order_start_date_written
order_end_date_written = generic_order_end_date_written
render_start_date_written = generic_render_start_date_written
render_end_date_written = generic_render_end_date_written

if edit_v:
name = tables.LinkColumn(
'apis:apis_entities:generic_entities_edit_view',
Expand All @@ -48,59 +64,18 @@ class GenericEntitiesTable(tables.Table):
if 'id' in default_cols:
id = tables.LinkColumn()


class Meta:
model = ContentType.objects.get(
app_label__startswith='apis_', model=entity.lower()).model_class()
model = AbstractEntity.get_entity_class_of_name(entity_name=entity)

fields = default_cols
attrs = {"class": "table table-hover table-striped table-condensed"}
return GenericEntitiesTable


class PersonTable(tables.Table):
name = tables.LinkColumn('apis:apis_entities:person_edit', args=[A('pk')])

class Meta:
model = Person
fields = ['name', 'first_name', 'start_date', 'end_date']
# add class="paleblue" to <table> tag
attrs = {"class": "table table-hover table-striped table-condensed"}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

class PlaceTable(tables.Table):
name = tables.LinkColumn('apis:apis_entities:place_edit', args=[A('pk')])

class Meta:
model = Place
fields = ['name', 'status', 'lng', 'lat']
# add class="paleblue" to <table> tag
attrs = {"class": "table table-hover table-striped table-condensed"}


class InstitutionTable(tables.Table):
name = tables.LinkColumn('apis:apis_entities:institution_edit', args=[A('pk')])

class Meta:
model = Institution
fields = ['name', 'start_date', 'end_date', 'kind']
# add class="paleblue" to <table> tag
attrs = {"class": "table table-hover table-striped table-condensed"}


class EventTable(tables.Table):
name = tables.LinkColumn('apis:apis_entities:event_edit', args=[A('pk')])

class Meta:
model = Event
fields = ['name', 'start_date', 'end_date', 'kind']
# add class="paleblue" to <table> tag
attrs = {"class": "table table-hover table-striped table-condensed"}

return GenericEntitiesTable

class WorkTable(tables.Table):
name = tables.LinkColumn('apis:apis_entities:work_edit', args=[A('pk')])

class Meta:
model = Work
fields = ['name', 'start_date', 'end_date', 'kind']
# add class="paleblue" to <table> tag
attrs = {"class": "table table-hover table-striped table-condensed"}
7 changes: 5 additions & 2 deletions apis_core/apis_entities/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@
url(r'^autocomplete-network/(?P<entity>[a-zA-Z0-9-]+)/$',
GenericNetworkEntitiesAutocomplete.as_view(),
name='generic_network_entities_autocomplete'),
url(r'^detail/work/(?P<pk>[0-9]+)$',
detail_views.WorkDetailView.as_view(), name='work_detail'),

# TODO __sresch__ : This seems unused. Remove it once sure
# url(r'^detail/work/(?P<pk>[0-9]+)$',
# detail_views.WorkDetailView.as_view(), name='work_detail'),

url(r'^place/geojson/$', views.getGeoJson, name='getGeoJson'),
url(r'^place/geojson/list/$', views.getGeoJsonList, name='getGeoJsonList'),
url(r'^place/network/list/$', views.getNetJsonList, name='getNetJsonList'),
Expand Down
5 changes: 1 addition & 4 deletions apis_core/apis_entities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
NetworkVizFilterForm, PersonResolveUriForm,
get_entities_form, GenericEntitiesStanbolForm
)
from .tables import (
PersonTable, PlaceTable, InstitutionTable, EventTable, WorkTable,
get_entities_table
)
from .tables import get_entities_table


if 'apis_highlighter' in settings.INSTALLED_APPS:
Expand Down
29 changes: 14 additions & 15 deletions apis_core/apis_entities/views2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
from guardian.core import ObjectPermissionChecker
from reversion.models import Version

from apis_core.apis_relations.models import AbstractRelation
from .views import get_highlighted_texts
from apis_core.apis_labels.models import Label
from apis_core.apis_metainfo.models import Uri
from apis_core.apis_relations.tables import get_generic_relations_table, EntityLabelTable
from apis_core.apis_relations.tables import get_generic_relations_table, LabelTableEdit
from .forms import get_entities_form, FullTextForm, GenericEntitiesStanbolForm
from .views import set_session_variables
from ..apis_vocabularies.models import TextType
Expand All @@ -37,39 +38,37 @@ def get(self, request, *args, **kwargs):
app_label='apis_entities', model=entity).model_class()
instance = get_object_or_404(entity_model, pk=pk)
request = set_session_variables(request)
relations = ContentType.objects.filter(app_label='apis_relations', model__icontains=entity)
relations = AbstractRelation.get_relation_classes_of_entity_name(entity_name=entity)
side_bar = []
for rel in relations:
match = str(rel).split()
match = [
rel.get_related_entity_classA().__name__.lower(),
rel.get_related_entity_classB().__name__.lower()
]
prefix = "{}{}-".format(match[0].title()[:2], match[1].title()[:2])
table = get_generic_relations_table(''.join(match), entity)
table = get_generic_relations_table(relation_class=rel, entity_instance=instance, detail=False)
title_card = ''
if match[0] == match[1]:
title_card = entity.title()
dict_1 = {'related_' + entity.lower() + 'A': instance}
dict_2 = {'related_' + entity.lower() + 'B': instance}
if 'apis_highlighter' in settings.INSTALLED_APPS:
object_pre = rel.model_class().annotation_links.filter_ann_proj(request=request).filter(
objects = rel.annotation_links.filter_ann_proj(request=request).filter(
Q(**dict_1) | Q(**dict_2))
else:
object_pre = rel.model_class().objects.filter(
objects = rel.objects.filter(
Q(**dict_1) | Q(**dict_2))
objects = []
for x in object_pre:
objects.append(x.get_table_dict(instance))
else:
if match[0].lower() == entity.lower():
title_card = match[1].title()
else:
title_card = match[0].title()
dict_1 = {'related_' + entity.lower(): instance}
if 'apis_highlighter' in settings.INSTALLED_APPS:
objects = list(rel.model_class()
.annotation_links.filter_ann_proj(request=request)
.filter(**dict_1))
objects = rel.annotation_links.filter_ann_proj(request=request).filter(**dict_1)
else:
objects = list(rel.model_class().objects.filter(**dict_1))
tb_object = table(objects, prefix=prefix)
objects = rel.objects.filter(**dict_1)
tb_object = table(data=objects, prefix=prefix)
tb_object_open = request.GET.get(prefix + 'page', None)
RequestConfig(request, paginate={"per_page": 10}).configure(tb_object)
side_bar.append((title_card, tb_object, ''.join([x.title() for x in match]), tb_object_open))
Expand All @@ -93,7 +92,7 @@ def get(self, request, *args, **kwargs):
object_lod = Uri.objects.filter(entity=instance)
object_texts, ann_proj_form = get_highlighted_texts(request, instance)
object_labels = Label.objects.filter(temp_entity=instance)
tb_label = EntityLabelTable(object_labels, prefix=entity.title()[:2]+'L-')
tb_label = LabelTableEdit(data=object_labels, prefix=entity.title()[:2] + 'L-')
tb_label_open = request.GET.get('PL-page', None)
side_bar.append(('Label', tb_label, 'PersonLabel', tb_label_open))
RequestConfig(request, paginate={"per_page": 10}).configure(tb_label)
Expand Down
Loading

0 comments on commit 455e240

Please sign in to comment.