Source code for nlpmed_portal.annotations.views

# SPDX-FileCopyrightText: Copyright (C) 2026 Omid Jafari <omidjafari.com>
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from auditlog.models import LogEntry
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from django.forms.models import model_to_dict
from django.views.generic import DetailView
from django.views.generic import TemplateView

from nlpmed_portal.annotations.models import Patient
from nlpmed_portal.annotations.models import Project
from nlpmed_portal.annotations.permissions import ScopedPermissionRequiredMixin
from nlpmed_portal.nlp.constants import BLEEDING_KEYWORD_EXC_LIST
from nlpmed_portal.nlp.constants import BLEEDING_KEYWORD_INC_LIST
from nlpmed_portal.nlp.constants import BLEEDING_SECTION_EXC_LIST
from nlpmed_portal.nlp.constants import BLEEDING_SECTION_INC_LIST
from nlpmed_portal.nlp.constants import VTE_KEYWORD_EXC_LIST
from nlpmed_portal.nlp.constants import VTE_KEYWORD_INC_LIST
from nlpmed_portal.nlp.constants import VTE_SECTION_EXC_LIST
from nlpmed_portal.nlp.constants import VTE_SECTION_INC_LIST
from nlpmed_portal.nlp.forms import DuplicateCheckerForm
from nlpmed_portal.nlp.forms import EncodingFixerForm
from nlpmed_portal.nlp.forms import JoinerForm
from nlpmed_portal.nlp.forms import MLInferenceForm
from nlpmed_portal.nlp.forms import NLPForm
from nlpmed_portal.nlp.forms import NoteFilterForm
from nlpmed_portal.nlp.forms import PatternReplacerForm
from nlpmed_portal.nlp.forms import SectionFilterForm
from nlpmed_portal.nlp.forms import SectionSplitterForm
from nlpmed_portal.nlp.forms import SentenceExpanderForm
from nlpmed_portal.nlp.forms import SentenceFilterForm
from nlpmed_portal.nlp.forms import SentenceSegmenterForm
from nlpmed_portal.nlp.forms import WordMaskerForm


[docs] class ProjectView(ScopedPermissionRequiredMixin, TemplateView): permission_required = "view_project" template_name = "annotations/projects_list.html" COMPONENT_MODELS = [ ("encoding_fixer", EncodingFixerForm), ("pattern_replacer", PatternReplacerForm), ("word_masker", WordMaskerForm), ("note_filter", NoteFilterForm), ("section_splitter", SectionSplitterForm), ("section_filter", SectionFilterForm), ("sentence_segmenter", SentenceSegmenterForm), ("duplicate_checker", DuplicateCheckerForm), ("sentence_filter", SentenceFilterForm), ("sentence_expander", SentenceExpanderForm), ("joiner", JoinerForm), ("ml_inference", MLInferenceForm), ]
[docs] def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) forms = {} for key, form_class in self.COMPONENT_MODELS: empty_form = form_class(prefix=key) initial_data = model_to_dict(empty_form.instance) for field, value in initial_data.items(): if isinstance(value, list): initial_data[field] = "\n".join(value) elif value is None: initial_data[field] = "" forms[key] = form_class(prefix=key, initial=initial_data) nlp_form = NLPForm(prefix="nlp") context.update( { "component_forms": forms, "nlp_form": nlp_form, "VTE_SECTION_INC_LIST": VTE_SECTION_INC_LIST, "VTE_SECTION_EXC_LIST": VTE_SECTION_EXC_LIST, "VTE_KEYWORD_INC_LIST": VTE_KEYWORD_INC_LIST, "VTE_KEYWORD_EXC_LIST": VTE_KEYWORD_EXC_LIST, "BLEEDING_SECTION_INC_LIST": BLEEDING_SECTION_INC_LIST, "BLEEDING_SECTION_EXC_LIST": BLEEDING_SECTION_EXC_LIST, "BLEEDING_KEYWORD_INC_LIST": BLEEDING_KEYWORD_INC_LIST, "BLEEDING_KEYWORD_EXC_LIST": BLEEDING_KEYWORD_EXC_LIST, }, ) return context
[docs] def dispatch(self, request, *args, **kwargs): ct = ContentType.objects.get_for_model(Site) LogEntry.objects.create( content_type=ct, object_repr=f"annotations | {self.__class__.__name__}", action=LogEntry.Action.ACCESS, ) return super().dispatch(request, *args, **kwargs)
[docs] class PatientView(ScopedPermissionRequiredMixin, TemplateView): permission_required = "view_patient" template_name = "annotations/patients_list.html"
[docs] def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) project_id = self.request.GET.get("project_id") project = Project.objects.filter(id=project_id).first() if project_id is not None else None context["project_id"] = project.id if project else None context["project_name"] = project.name if project else None context["annotation_type"] = project.annotation_type if project else None return context
[docs] def dispatch(self, request, *args, **kwargs): ct = ContentType.objects.get_for_model(Site) project_id = request.GET.get("project_id") LogEntry.objects.create( content_type=ct, object_repr=f"annotations | {self.__class__.__name__}" + (f" | project_id={project_id}" if project_id else ""), action=LogEntry.Action.ACCESS, ) return super().dispatch(request, *args, **kwargs)
[docs] class PatientDetailView(ScopedPermissionRequiredMixin, DetailView): model = Patient permission_required = "view_patient" queryset = Patient.objects.select_related("project").only( "id", "pat_id", "index_date", "project_id", "project__annotation_type", )
[docs] def get_template_names(self): if self.object.project.annotation_type == Project.NER: return ["annotations/patient_details_ner.html"] return ["annotations/patient_details.html"]
[docs] def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) patient = self.object context.update( { "project_id": patient.project_id, "patient_id": patient.id, "pat_id": patient.pat_id, "index_date": patient.index_date.strftime(settings.DATE_FORMAT), }, ) return context
[docs] def dispatch(self, request, *args, **kwargs): ct = ContentType.objects.get_for_model(Site) self.object = self.get_object() patient_id = getattr(self.object, "id", None) LogEntry.objects.create( content_type=ct, object_repr=f"annotations | {self.__class__.__name__}" + (f" | patient_id={patient_id}" if patient_id else ""), action=LogEntry.Action.ACCESS, ) return super().dispatch(request, *args, **kwargs)
[docs] class ProjectMembershipView(ScopedPermissionRequiredMixin, TemplateView): permission_required = "view_projectmembership" template_name = "annotations/project_memberships_list.html"
[docs] def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) project_id = self.request.GET.get("project_id") if project_id: if not project_id.isdigit(): return None context["project_id"] = project_id return context
[docs] def dispatch(self, request, *args, **kwargs): ct = ContentType.objects.get_for_model(Site) project_id = request.GET.get("project_id") LogEntry.objects.create( content_type=ct, object_repr=f"annotations | {self.__class__.__name__}" + (f" | project_id={project_id}" if project_id else ""), action=LogEntry.Action.ACCESS, ) return super().dispatch(request, *args, **kwargs)
[docs] class TaskView(ScopedPermissionRequiredMixin, TemplateView): permission_required = "view_task" template_name = "annotations/tasks_list.html"
[docs] def dispatch(self, request, *args, **kwargs): ct = ContentType.objects.get_for_model(Site) LogEntry.objects.create( content_type=ct, object_repr=f"annotations | {self.__class__.__name__}", action=LogEntry.Action.ACCESS, ) return super().dispatch(request, *args, **kwargs)