Skip to content

Commit

Permalink
Merge pull request #8 from arthur-schnitzler/main
Browse files Browse the repository at this point in the history
tests and stuff
  • Loading branch information
csae8092 authored Dec 22, 2023
2 parents 2c84572 + 6ac10c6 commit aa9c734
Show file tree
Hide file tree
Showing 69 changed files with 463 additions and 564 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,4 @@ pmb_dump.sql
.secret
play.ipynb
pmb_play_dump.sql
notes.sql
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[![flake8 Lint](https://github.com/arthur-schnitzler/pmb-service/actions/workflows/lint.yml/badge.svg)](https://github.com/arthur-schnitzler/pmb-service/actions/workflows/lint.yml)
[![Test](https://github.com/arthur-schnitzler/pmb-service/actions/workflows/test.yml/badge.svg)](https://github.com/arthur-schnitzler/pmb-service/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/arthur-schnitzler/pmb-service/graph/badge.svg?token=P98WIT0K84)](https://codecov.io/gh/arthur-schnitzler/pmb-service)

# PMB-SERVICE

lightweight APIS-PMB instance to generate TEI/XML dumps
Expand Down
13 changes: 6 additions & 7 deletions apis_core/api_routers.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
from functools import reduce
from django.conf import settings

from django.conf import settings
from django.urls import reverse
from rest_framework import pagination, serializers, viewsets
from rest_framework import renderers
from django_filters import rest_framework as filters
from rest_framework import pagination, renderers, serializers, viewsets
from rest_framework.filters import OrderingFilter
from rest_framework.response import Response

from apis_core.helper_functions.ContentType import GetContentTypes

from django_filters import rest_framework as filters
from rest_framework.filters import OrderingFilter
from .apis_metainfo.models import TempEntityClass
from .api_renderers import NetJsonRenderer
from apis_core.helper_functions.ContentType import GetContentTypes
from .apis_metainfo.models import TempEntityClass

try:
MAX_AGE = settings.MAX_AGE
Expand Down
3 changes: 2 additions & 1 deletion apis_core/apis_entities/autocomplete3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
from django.core.exceptions import FieldError
from django.db.models import Q

from apis_core.apis_metainfo.models import Uri, Collection
from apis_core.apis_metainfo.models import Collection, Uri
from apis_core.apis_vocabularies.models import VocabsBaseClass

from .models import AbstractEntity


Expand Down
13 changes: 5 additions & 8 deletions apis_core/apis_entities/detail_views.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
from django.conf import settings
from django.db.models import Q
from django.http import HttpResponse, Http404
from django.shortcuts import get_object_or_404
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.template.loader import select_template
from django.views import View
from django_tables2 import RequestConfig
from django.shortcuts import redirect

from apis_core.apis_labels.models import Label
from apis_core.apis_metainfo.models import Uri
from apis_core.apis_relations.models import AbstractRelation
from apis_core.apis_relations.tables import (
get_generic_relations_table,
LabelTableBase,
)
from .models import TempEntityClass, BASE_URI
from apis_core.apis_relations.tables import LabelTableBase, get_generic_relations_table

from .models import BASE_URI, TempEntityClass


def get_object_from_pk_or_uri(request, pk):
Expand Down
5 changes: 2 additions & 3 deletions apis_core/apis_entities/filters.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from functools import reduce
import importlib
from functools import reduce

import django_filters
from django.conf import settings

from apis_core.apis_entities.models import *
from apis_core.apis_vocabularies.models import RelationBaseClass
from apis_core.apis_metainfo.models import Collection

from apis_core.apis_vocabularies.models import RelationBaseClass

# The following classes define the filter sets respective to their models.
# Also by what was enabled in the global settings file (or disabled by not explicitley enabling it).
Expand Down
259 changes: 46 additions & 213 deletions apis_core/apis_entities/forms.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,60 @@
# -*- coding: utf-8 -*-
from crispy_forms.bootstrap import Accordion, AccordionGroup
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset
from crispy_forms.layout import Submit
from crispy_forms.layout import Fieldset, Layout, Submit
from dal import autocomplete
from django import forms
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.db.models.fields import BLANK_CHOICE_DASH
from django.forms import ModelMultipleChoiceField, ModelChoiceField
from django.forms import ModelChoiceField, ModelMultipleChoiceField
from django.urls import reverse

from apis_core.apis_metainfo.models import Text, Uri, Collection
from apis_core.apis_vocabularies.models import TextType
from apis_core.apis_metainfo.models import Collection
from apis_core.helper_functions import DateParser

from .fields import ListSelect2, Select2Multiple
from .models import AbstractEntity


class SearchForm(forms.Form):
search = forms.CharField(label="Search")
class MergeForm(forms.Form):
def save(self, *args, **kwargs):
return self.entity

@property
def helper(self):
helper = FormHelper()
helper.form_id = "searchForm"
helper.form_tag = False
helper.add_input(Submit("fieldn", "search"))
helper.form_method = "GET"
return helper
def __init__(self, entity, *args, **kwargs):
attrs = {
"data-placeholder": "Type to get suggestions",
"data-minimum-input-length": 1,
"data-html": True,
"style": "width: auto",
}
ent_merge_pk = kwargs.pop("ent_merge_pk", False)
super(MergeForm, self).__init__(*args, **kwargs)
self.entity = entity
self.helper = FormHelper()
form_kwargs = {"entity": entity}
url = reverse(
"apis:apis_entities:generic_entities_autocomplete",
args=[entity.title(), "remove"],
)
label = "Create {} from reference resources".format(entity.title())
button_label = "Create"
if ent_merge_pk:
form_kwargs["ent_merge_pk"] = ent_merge_pk
url = reverse(
"apis:apis_entities:generic_entities_autocomplete",
args=[entity.title(), ent_merge_pk],
)
label = f"Suche nach Objekten vom Type: {entity.title()}"
button_label = "Merge"
self.helper.form_action = reverse(
"apis:apis_entities:merge_view", kwargs=form_kwargs
)
self.helper.add_input(Submit("submit", button_label))
self.fields["entity"] = autocomplete.Select2ListCreateChoiceField(
label=label,
widget=ListSelect2(url=url, attrs=attrs),
# validators=[URLValidator],
)


def get_entities_form(entity):
Expand Down Expand Up @@ -120,7 +146,8 @@ def __init__(self, *args, **kwargs):
except ValueError:
res = ""
if f not in acc_grp2:
# append to unsorted list, so that it can be sorted and afterwards attached to accordion group acc_grp1
# append to unsorted list, so that it can be sorted and
# afterwards attached to accordion group acc_grp1
fields_list_unsorted.append(f)

def sort_fields_list(list_unsorted, entity_label):
Expand Down Expand Up @@ -176,7 +203,8 @@ def sort_fields_list(list_unsorted, entity_label):

raise Exception(
"An item of the entity setting 'form_order' list was not used. \n"
"This propably indicates that the 'form_order' settings is out of sync with the effective django models.\n"
"This propably indicates that the 'form_order' settings is out \n"
"of sync with the effective django models.\n"
f"The relevant entity is: {entity_label}\n"
f"And the differences between used list and settings list are: {differences}"
)
Expand Down Expand Up @@ -237,198 +265,3 @@ def save(self, *args, **kwargs):
return obj

return GenericEntitiesForm


class FullTextForm(forms.Form):
def save(self, entity):
cd = self.cleaned_data
text = None
for f in cd.keys():
text_type = TextType.objects.get(pk=f.split("_")[1])
text = Text.objects.filter(tempentityclass=entity, kind=text_type)
if text.count() == 1:
text = text[0]
text.text = cd[f]
text.save()
elif text.count() == 0:
text = Text(text=cd[f], kind=text_type)
text.save()
entity.text.add(text)
return text

def __init__(self, *args, **kwargs):
if "entity" in kwargs.keys():
entity = kwargs.pop("entity", None)
else:
entity = None
if "instance" in kwargs.keys():
instance = kwargs.pop("instance", None)
else:
instance = None
super(FullTextForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = "FullTextForm"
self.helper.form_tag = False
self.helper.help_text_inline = True
collections = []
if instance:
for i in instance.collection.all():
collections.append(i)
try:
if len(collections) > 0:
q = TextType.objects.filter(
entity__iexact=entity, collections__in=collections
)
else:
q = TextType.objects.filter(entity__iexact=entity)
for txt in q:
self.fields["text_" + str(txt.pk)] = forms.CharField(
label=txt.name,
help_text=txt.description,
required=False,
widget=forms.Textarea,
)
if instance:
for t in instance.text.all():
if "text_" + str(t.kind.pk) in self.fields.keys():
self.fields["text_" + str(t.kind.pk)].initial = t.text
except:
pass


class PersonResolveUriForm(forms.Form):
# person = forms.CharField(label=False, widget=al.TextWidget('PersonAutocomplete'))
person = forms.CharField(label=False)
person_uri = forms.CharField(required=False, widget=forms.HiddenInput())

def save(self, site_instance, instance=None, commit=True):
cd = self.cleaned_data
if cd["person"].startswith("http"):
uri = Uri.objects.create(uri=cd["person"], entity=site_instance)
else:
uri = Uri.objects.create(uri=cd["person_uri"], entity=site_instance)
return uri

def __init__(self, *args, **kwargs):
entity_type = kwargs.pop("entity_type", False)
self.request = kwargs.pop("request", False)
super(PersonResolveUriForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False

def clean(self):
cleaned_data = super(PersonResolveUriForm, self).clean()
if Uri.objects.filter(uri=cleaned_data["person_uri"]).exists():
self.add_error("person", "This Person has already been added to the DB.")
elif cleaned_data["person"].startswith("http"):
if Uri.objects.filter(uri=cleaned_data["person"]).exists():
self.add_error("person", "This URI has already been added to the DB.")


class NetworkVizFilterForm(forms.Form):
ann_include_all = forms.BooleanField(
required=False,
label="Include general relations",
help_text="""Not all relations are connected to an annotation.\
If checked relations that are not attached to an annotation are include.\
This setting is only used when an Annotation project is specified.""",
)
start_date = forms.CharField(
label="Start date",
required=False,
widget=forms.TextInput(
attrs={"data-provide": "datepicker", "data-date-format": "dd.mm.yyyy"}
),
)
end_date = forms.CharField(
label="End date",
required=False,
widget=forms.TextInput(
attrs={"data-provide": "datepicker", "data-date-format": "dd.mm.yyyy"}
),
)

def __init__(self, *args, **kwargs):
rel_attrs = {
"data-placeholder": "Type to get suggestions",
"data-minimum-input-length": getattr(settings, "APIS_MIN_CHAR", 3),
"data-html": True,
}
attrs = {
"data-placeholder": "Type to get suggestions",
"data-minimum-input-length": getattr(settings, "APIS_MIN_CHAR", 3),
"data-html": True,
}
super(NetworkVizFilterForm, self).__init__(*args, **kwargs)
self.fields["select_relation"] = forms.ChoiceField(
label="Relation type",
choices=list(
("-".join(x.name.split()), x.name)
for x in ContentType.objects.filter(app_label="apis_relations")
),
help_text="Include only relations related to this annotation project \
(See the include general relations checkbox)",
)
self.fields["select_relation"].initial = ("person-place", "person place")
self.fields["search_source"] = autocomplete.Select2ListCreateChoiceField(
label="Search source",
widget=ListSelect2(
url=reverse(
"apis:apis_entities:generic_network_entities_autocomplete",
kwargs={"entity": "person"},
),
attrs=attrs,
),
)
self.fields["search_target"] = autocomplete.Select2ListCreateChoiceField(
label="Search target",
widget=ListSelect2(
url=reverse(
"apis:apis_entities:generic_network_entities_autocomplete",
kwargs={"entity": "place"},
),
attrs=attrs,
),
)
self.fields["select_kind"] = autocomplete.Select2ListCreateChoiceField(
label="Select kind",
widget=ListSelect2(
url=reverse(
"apis:apis_vocabularies:generic_vocabularies_autocomplete",
kwargs={"vocab": "personplacerelation", "direct": "normal"},
),
attrs=rel_attrs,
),
)
if "apis_highlighter" in settings.INSTALLED_APPS:
self.fields["annotation_proj"] = forms.ChoiceField(
label="Annotation Project",
choices=BLANK_CHOICE_DASH
+ list((x.pk, x.name) for x in AnnotationProject.objects.all()),
required=False,
help_text="Include only relations related to this annotation project \
(See the include general relations checkbox)",
)
self.helper = FormHelper()
self.helper.form_class = "FilterNodesForm"
self.helper.form_action = reverse("apis:apis_core:NetJson-list")
self.helper.add_input(Submit("Submit", "Add nodes"))
self.order_fields(
(
"select_relation",
"ann_include_all",
"annotation_proj",
"search_source",
"select_kind",
"search_target",
)
)


class GenericFilterFormHelper(FormHelper):
def __init__(self, *args, **kwargs):
super(GenericFilterFormHelper, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.form_class = "genericFilterForm"
self.form_method = "GET"
self.form_tag = False
Loading

0 comments on commit aa9c734

Please sign in to comment.