Skip to content

Commit

Permalink
refactor: get rid of apis_highlighter integration
Browse files Browse the repository at this point in the history
On the one hand this cleans up the codebase, on the other hand this
frees up the name `apis_highlighter`, so it can be used in a new
Django application.
  • Loading branch information
b1rger committed Oct 10, 2023
1 parent a381b02 commit c7e87d6
Show file tree
Hide file tree
Showing 14 changed files with 1 addition and 383 deletions.
17 changes: 0 additions & 17 deletions apis_core/api_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
from apis_core.utils import caching
from apis_core.core.mixins import ListViewObjectFilterMixin

if "apis_highlighter" in getattr(settings, "INSTALLED_APPS"):
from apis_highlighter.highlighter import highlight_text_new
from apis_highlighter.models import Annotation


try:
MAX_AGE = settings.MAX_AGE
except AttributeError:
Expand Down Expand Up @@ -202,18 +197,6 @@ def add_related_entity(self, triple):
)


if "apis_highlighter" in getattr(settings, "INSTALLED_APPS"):

class AnnotationSerializer(serializers.ModelSerializer):
related_object = VocabsBaseSerializer(
source="get_related_entity", read_only=True, many=False
)

class Meta:
model = Annotation
fields = ["id", "start", "end", "related_object"]


def generic_serializer_creation_factory():
lst_cont = caching.get_all_contenttype_classes()
not_allowed_filter_fields = [
Expand Down
31 changes: 0 additions & 31 deletions apis_core/apis_entities/api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,3 @@
# path(r'getrelatedplaces/', api_views.GetRelatedPlaces.as_view(), name="GetRelatedPlaces"),
# path(r'lifepath/<int:pk>/', api_views.LifePathViewset.as_view(), name="Lifepathviewset")
]

if (
"deep learning" in getattr(settings, "APIS_COMPONENTS", [])
and "apis_highlighter" in settings.INSTALLED_APPS
):
from apis_highlighter.api_views import TestDLModel

urlpatterns.append(
path("nlp_model/", TestDLModel.as_view(), name="TestDLModel"),
)

if "apis_highlighter" in settings.INSTALLED_APPS:
from apis_highlighter.api_views import (
AnnotatorAgreementView,
ShowOverlappingHighlights,
)

urlpatterns.extend(
[
path(
"annotatoragreement/",
AnnotatorAgreementView.as_view(),
name="AnnotatorAgreementView",
),
path(
"overlappinghighlights/",
ShowOverlappingHighlights.as_view(),
name="ShowOverlappingHighlights",
),
]
)
8 changes: 0 additions & 8 deletions apis_core/apis_entities/edit_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
from apis_core.utils.settings import get_entity_settings_by_modelname
from apis_core.apis_entities.mixins import EntityMixin, EntityInstanceMixin

if "apis_highlighter" in settings.INSTALLED_APPS:
from apis_highlighter.forms import SelectAnnotatorAgreement


@method_decorator(login_required, name="dispatch")
class GenericEntitiesEditView(EntityInstanceMixin, View):
Expand Down Expand Up @@ -89,10 +86,6 @@ def get(self, request, *args, **kwargs):
)
form = get_entities_form(self.entity.title())
form = form(instance=self.instance)
if "apis_highlighter" in settings.INSTALLED_APPS:
form_ann_agreement = SelectAnnotatorAgreement()
else:
form_ann_agreement = False
if "apis_bibsonomy" in settings.INSTALLED_APPS:
apis_bibsonomy = getattr(settings, "APIS_BIBSONOMY_FIELDS", [])
apis_bibsonomy_texts = getattr(settings, "APIS_BIBSONOMY_TEXTS", False)
Expand Down Expand Up @@ -127,7 +120,6 @@ def get(self, request, *args, **kwargs):
"right_card": side_bar,
"object_revisions": object_revisions,
"object_lod": object_lod,
"form_ann_agreement": form_ann_agreement,
"apis_bibsonomy": apis_bibsonomy,
}
form_merge_with = GenericEntitiesStanbolForm(self.entity, ent_merge_pk=self.pk)
Expand Down
3 changes: 0 additions & 3 deletions apis_core/apis_entities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
from apis_core.utils import DateParser, caching, settings as apis_settings
from .fields import ListSelect2, Select2Multiple

if "apis_highlighter" in settings.INSTALLED_APPS:
from apis_highlighter.models import AnnotationProject


class SearchForm(forms.Form):
search = forms.CharField(label="Search")
Expand Down
39 changes: 0 additions & 39 deletions apis_core/apis_entities/serializers_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ class RelationEntitySerializer(serializers.Serializer):
start_date_written = serializers.DateField()
end_date_written = serializers.DateField()
relation_type = serializers.SerializerMethodField(method_name="add_relation_label")
annotation = serializers.SerializerMethodField(method_name="add_annotations")
revisions = serializers.SerializerMethodField(method_name="add_revisions")

def add_revisions(self, obj):
Expand All @@ -197,44 +196,6 @@ def add_revisions(self, obj):
)
return res

def add_annotations(self, obj):
if "apis_highlighter" in settings.INSTALLED_APPS:
res = []
offs = 50
for an in obj.annotation_set.all():
r1 = dict()
r1["id"] = an.pk
r1["user"] = an.user_added.username
text = an.text.text
if offs < an.start:
s = an.start - offs
else:
s = 0
if offs + an.end < len(text):
e = an.end + offs
else:
e = len(text)
r1["annotation"] = text[an.start : an.end]
r1["text"] = text[s:e]
r1["text"] = "{}<annotation>{}</annotation>{}".format(
r1["text"][: an.start - s],
r1["text"][an.start - s : an.end - s],
r1["text"][an.end - s :],
)
r1["text"] = r1["text"].replace("\r\n", "<br/>")
r1["text"] = r1["text"].replace("\r", "<br/>")
r1["text"] = r1["text"].replace("\n", "<br/>")

r1["string_offset"] = "{}-{}".format(an.start, an.end)
# r1["text_url"] = self.context["request"].build_absolute_uri(
# reverse("apis_core:apis_api:text-detail", kwargs={"pk": an.text_id})
# )
r1[
"text_url"
] = f"{base_uri}{reverse('apis_core:apis_api:text-detail', kwargs={'pk': an.text_id})}"
res.append(r1)
return res

def add_entity(self, obj):
return EntitySerializer(
getattr(obj, "related_{}".format(self.entity_type)), depth_ent=0
Expand Down
35 changes: 0 additions & 35 deletions apis_core/apis_entities/templates/apis_entities/edit_generic.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@
{% block scriptHeader %}
{{ block.super }}

{% if highlighter_active %}
<link rel="stylesheet"
href="{% static 'apis_highlighter/apis_highlighter.css' %}" />
<script type="text/javascript"
src="{% static 'apis_highlighter/apis_highlighter.js' %}?arbitrary_tag_to_avoid_cache"></script>
{% endif %}

{% if apis_bibsonomy %}
{% include 'apis_bibsonomy/apis_bibsonomy_include.html' %}
{% endif %}
Expand Down Expand Up @@ -126,7 +119,6 @@ <h4 class="card-title">

</div>
{% endblock editbuttons %}

</form>
</div>

Expand Down Expand Up @@ -205,20 +197,6 @@ <h4 class="card-title">
{% endif %}

{% if object_texts %}

{% if user.is_superuser %}
{% if form_ann_agreement %}
<div class="row">
<div class="col-md-12">
<div class="card card-default">
{% crispy form_ann_agreement form_ann_agreement.helper %}
<div id="ann_agreement_tables"></div>
</div>
</div>
</div>
{% endif %}
{% endif %}

<div class="row">
<div class="col-md-12">

Expand Down Expand Up @@ -426,19 +404,6 @@ <h4 class="card-title">
GetFormAjax("{{obj.2}}");
//unbind_ajax_forms();
{% endfor %}
{% if highlighter_active %}
init_apis_highlighter(1, {{instance.pk}});
if (typeof $.ApisHigh.vars == 'undefined') {
$.ApisHigh.vars = {}; };
$.ApisHigh.vars.entity_type = '{{entity_type}}';
$.ApisHigh.vars.instance_pk = '{{instance.pk}}';
$.ApisHigh.vars.urls = {};
$.ApisHigh.vars.urls.get_form_ajax = "{% url 'apis:apis_relations:get_form_ajax' %}";
$.ApisHigh.vars.urls.annotatoragreementview = "{% url 'apis:apis_api2:AnnotatorAgreementView' %}";
$.ApisHigh.vars.urls.showoverlappinghighlights = "{% url 'apis:apis_api2:ShowOverlappingHighlights' %}";
activate_context_menu_highlighter();
$('body').on("click", 'mark.highlight', highlight_detail);
{% endif %}
};
function deactivate_editing(){
$('.reldelete').addClass("disabled");
Expand Down
4 changes: 0 additions & 4 deletions apis_core/apis_entities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
)
from .tables import get_entities_table

if "apis_highlighter" in settings.INSTALLED_APPS:
from apis_highlighter.forms import SelectAnnotationProject
from apis_highlighter.highlighter import highlight_text_new

if "charts" in settings.INSTALLED_APPS:
from charts.models import ChartConfig
from charts.views import create_payload
Expand Down
3 changes: 0 additions & 3 deletions apis_core/apis_metainfo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@

NEXT_PREV = getattr(settings, "APIS_NEXT_PREV", True)

if "apis_highlighter" in settings.INSTALLED_APPS:
from apis_highlighter.models import Annotation


@reversion.register()
class RootObject(models.Model):
Expand Down
3 changes: 0 additions & 3 deletions apis_core/apis_relations/forms2.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@

# from dal.autocomplete import ListSelect2

if "apis_highlighter" in settings.INSTALLED_APPS:
pass


class GenericTripleForm(forms.ModelForm):
# TODO RDF : Add Notes and references
Expand Down
4 changes: 1 addition & 3 deletions apis_core/apis_relations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ def filter_ann_proj(self, request=None, ann_proj=1, include_all=True):
)

def filter_for_user(self):
if hasattr(
settings, "APIS_SHOW_ONLY_PUBLISHED"
) or "apis_highlighter" in getattr(settings, "INSTALLED_APPS"):
if hasattr(settings, "APIS_SHOW_ONLY_PUBLISHED"):
return self.get_queryset().filter_for_user()
else:
return self.get_queryset()
Expand Down
4 changes: 0 additions & 4 deletions apis_core/apis_vocabularies/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ class VocabsBaseClass(RootObject):
vocab_name = models.ForeignKey(
VocabNames, blank=True, null=True, on_delete=models.SET_NULL
)
if "apis_highlighter" in settings.INSTALLED_APPS:
from apis_highlighter.models import Annotation

annotation_set = GenericRelation(Annotation)

def __str__(self):
return self.label
Expand Down
4 changes: 0 additions & 4 deletions apis_core/context_processors/custom_context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ def list_apis_settings(request):
"request": request,
"basetemplate": getattr(settings, "BASE_TEMPLATE", "base.html"),
}
if "apis_highlighter" in settings.INSTALLED_APPS:
res["highlighter_active"] = True
else:
res["highlighter_active"] = False
if "apis_bibsonomy" in settings.INSTALLED_APPS:
res["bibsonomy_active"] = True
else:
Expand Down
22 changes: 0 additions & 22 deletions apis_core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,6 @@
additional_serializer.name,
)

if "apis_highlighter" in settings.INSTALLED_APPS:
from apis_highlighter.api_views import (
HighlighterProjectViewSet,
HighlighterTextHighViewSet,
HighlighterMenuEntryViewSet,
HighlighterAnnotationViewSet,
)

router.register(r"HLProjects", HighlighterProjectViewSet)
router.register(r"HLTextHigh", HighlighterTextHighViewSet)
router.register(r"HLMenuEntry", HighlighterMenuEntryViewSet)
# router.register(
# r"HLTextHighlighter", HighlighterHighlightTextViewSet, "HLTextHighlighter"
# )
# router.register(r"HLVocabularyAPI", HighlighterVocabularyAPIViewSet)
router.register(r"HLAnnotation", HighlighterAnnotationViewSet)

# router.register(r"users", UserViewSet)
# router.register(r"GeoJsonPlace", PlaceGeoJsonViewSet, "PlaceGeoJson")
# router.register(r"NetJson", NetJsonViewSet, "NetJson")
Expand Down Expand Up @@ -187,11 +170,6 @@ def build_apis_mock_request(method, path, view, original_request, **kwargs):
# url(r'^accounts/', include('registration.backends.simple.urls')),
]

if "apis_highlighter" in settings.INSTALLED_APPS:
urlpatterns.append(
path("highlighter/", include("apis_highlighter.urls", namespace="highlighter"))
)

if "apis_fulltext_download" in settings.INSTALLED_APPS:
urlpatterns.append(
path(
Expand Down
Loading

0 comments on commit c7e87d6

Please sign in to comment.