Skip to content

Commit

Permalink
Add ConfirmedSerumCragDate (note) model, refactor effect_reports to use
Browse files Browse the repository at this point in the history
NoteModel(Admin)Mixin, fix SQL defined report_models, fix imports for
refactored QaReportModelMixin
  • Loading branch information
JonathanWillitts committed Aug 14, 2024
1 parent 59247e0 commit 600afcf
Show file tree
Hide file tree
Showing 14 changed files with 605 additions and 18 deletions.
106 changes: 106 additions & 0 deletions effect_reports/admin/confirmed_serum_crag_date_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from django.contrib import admin
from django.template.loader import render_to_string
from django.urls import NoReverseMatch, reverse
from django.utils.html import format_html
from django_audit_fields import ModelAdminAuditFieldsMixin, audit_fieldset_tuple
from django_revision.modeladmin_mixin import ModelAdminRevisionMixin
from edc_model_admin.dashboard import ModelAdminDashboardMixin
from edc_model_admin.mixins import (
ModelAdminFormAutoNumberMixin,
ModelAdminFormInstructionsMixin,
ModelAdminInstitutionMixin,
ModelAdminNextUrlRedirectMixin,
TemplatesModelAdminMixin,
)
from edc_sites.admin import SiteModelAdminMixin

from ..admin_site import effect_reports_admin
from ..forms import ConfirmedSerumCragDateForm
from ..models import ConfirmedSerumCragDate


@admin.register(ConfirmedSerumCragDate, site=effect_reports_admin)
class ConfirmedSerumCragDateAdmin(
SiteModelAdminMixin,
ModelAdminDashboardMixin,
ModelAdminAuditFieldsMixin,
ModelAdminFormAutoNumberMixin,
ModelAdminFormInstructionsMixin,
ModelAdminRevisionMixin, # add
ModelAdminInstitutionMixin, # add
ModelAdminNextUrlRedirectMixin,
TemplatesModelAdminMixin,
admin.ModelAdmin,
):

form = ConfirmedSerumCragDateForm
ordering = ["site", "subject_identifier"]

note_template_name = "edc_qareports/qa_report_note.html"

fieldsets = (
(
"Screening Serum CrAg Date",
{"fields": ("confirmed_serum_crag_date",)},
),
(
"Notes",
{
"fields": (
"note",
"status",
"subject_identifier",
"report_model",
"report_datetime",
)
},
),
audit_fieldset_tuple,
)

list_display = [
"dashboard",
"subject_identifier",
"report",
"status",
"confirmed_serum_crag_date",
"report_note",
"report_datetime",
]

radio_fields = {"status": admin.VERTICAL}

list_filter = [
"confirmed_serum_crag_date",
"status",
"report_datetime",
"report_model",
"user_created",
"user_modified",
]

search_fields = ["subject_identifier", "name"]

@admin.display(description="Report", ordering="report_name")
def report(self, obj=None):
app_label, model = obj.report_model_cls._meta.label_lower.split(".")
changelist_url = "_".join([app_label, model, "changelist"])
try:
# assume admin site naming convention
url = reverse(f"{app_label}_admin:{changelist_url}")
except NoReverseMatch:
# TODO: find the admin site where this model is registered
url = "#"
return format_html(
'<a data-toggle="tooltip" title="go to report" href="{}?q={}">{}</a>',
*(url, obj.subject_identifier, obj.report_model_cls._meta.verbose_name),
)

@admin.display(description="QA Note", ordering="note")
def report_note(self, obj=None):
context = dict(note=obj.note)
return render_to_string(self.note_template_name, context)

def redirect_url(self, request, obj, post_url_continue=None) -> str | None:
redirect_url = super().redirect_url(request, obj, post_url_continue=post_url_continue)
return f"{redirect_url}?q={obj.subject_identifier}"
36 changes: 34 additions & 2 deletions effect_reports/admin/consented_serum_crag_date_admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from django.apps import apps as django_apps
from django.contrib import admin
from django.utils.html import format_html
from edc_model_admin.dashboard import ModelAdminDashboardMixin
from edc_model_admin.mixins import TemplatesModelAdminMixin
from edc_qareports.admin import QaReportWithNoteModelAdminMixin
from edc_qareports.admin import NoteModelAdminMixin
from edc_qareports.utils import truncate_string
from edc_sites.admin import SiteModelAdminMixin
from edc_utils import escape_braces

from ..admin_site import effect_reports_admin
from ..consented_serum_crag_date_df import ConsentedSerumCragDateDf
Expand All @@ -11,12 +15,16 @@

@admin.register(ConsentedSerumCragDate, site=effect_reports_admin)
class ConsentedSerumCragDateAdmin(
QaReportWithNoteModelAdminMixin,
NoteModelAdminMixin,
SiteModelAdminMixin,
ModelAdminDashboardMixin,
TemplatesModelAdminMixin,
admin.ModelAdmin,
):
qa_report_list_display_insert_pos = 5

note_model_cls = django_apps.get_model("effect_reports.confirmedserumcragdate")

ordering = ("subject_identifier",)
list_display = [
"dashboard",
Expand Down Expand Up @@ -48,6 +56,30 @@ def screening(self, obj=None):
def subject(self, obj=None):
return obj.subject_identifier

@admin.display(description="Conf. ser CrAg Date")
def notes(self, obj=None):
"""Returns url to add or edit qa_report model note"""
return super().notes(obj=obj)

def get_notes_label(self, obj=None, field_name=None):
if not obj:
label = "Add"
else:
date = obj.confirmed_serum_crag_date
note = obj.note
if date and not note:
label = date
elif date and note:
label = format_html(
f"{date.strftime('%-d %b %Y')}<br>"
f"({escape_braces(truncate_string(note, max_length=35))})"
)
elif note:
label = truncate_string(note, max_length=35)
else:
label = "Edit"
return label

def get_queryset(self, request):
cls = ConsentedSerumCragDateDf()
cls.to_model()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from edc_appointment.models import Appointment
from edc_model_admin.dashboard import ModelAdminDashboardMixin
from edc_model_admin.mixins import TemplatesModelAdminMixin
from edc_qareports.admin import QaReportWithNoteModelAdminMixin
from edc_qareports.admin import NoteModelAdminMixin
from edc_sites.admin import SiteModelAdminMixin
from edc_visit_schedule.admin import ScheduleStatusListFilter

Expand All @@ -17,7 +17,7 @@

@admin.register(Rm792KwInCurrentSxGteG3Other, site=effect_reports_admin)
class Rm792KwInCurrentSxGteG3OtherAdmin(
QaReportWithNoteModelAdminMixin,
NoteModelAdminMixin,
SiteModelAdminMixin,
ModelAdminDashboardMixin,
TemplatesModelAdminMixin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from edc_appointment.models import Appointment
from edc_model_admin.dashboard import ModelAdminDashboardMixin
from edc_model_admin.mixins import TemplatesModelAdminMixin
from edc_qareports.admin import QaReportWithNoteModelAdminMixin
from edc_qareports.admin import NoteModelAdminMixin
from edc_sites.admin import SiteModelAdminMixin
from edc_visit_schedule.admin import ScheduleStatusListFilter

Expand All @@ -17,7 +17,7 @@

@admin.register(Rm792KwInCurrentSxOther, site=effect_reports_admin)
class Rm792KwInCurrentSxOtherAdmin(
QaReportWithNoteModelAdminMixin,
NoteModelAdminMixin,
SiteModelAdminMixin,
ModelAdminDashboardMixin,
TemplatesModelAdminMixin,
Expand Down
1 change: 1 addition & 0 deletions effect_reports/forms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .confirmed_serum_crag_date_form import ConfirmedSerumCragDateForm
28 changes: 28 additions & 0 deletions effect_reports/forms/confirmed_serum_crag_date_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django import forms
from edc_form_validators import FormValidatorMixin
from edc_model_form.mixins import BaseModelFormMixin
from edc_sites.modelform_mixins import SiteModelFormMixin
from effect_form_validators.effect_reports import ConfirmedSerumCragDateFormValidator

from ..models import ConfirmedSerumCragDate


class ConfirmedSerumCragDateForm(
SiteModelFormMixin,
BaseModelFormMixin,
FormValidatorMixin,
forms.ModelForm,
):

report_datetime_field_attr = "report_datetime"
form_validator_cls = ConfirmedSerumCragDateFormValidator

class Meta:
model = ConfirmedSerumCragDate
fields = "__all__"
help_text = {"subject_identifier": "(read-only)", "name": "(read-only)"}
widgets = {
"report_model": forms.TextInput(attrs={"readonly": "readonly"}),
"subject_identifier": forms.TextInput(attrs={"readonly": "readonly"}),
"name": forms.TextInput(attrs={"readonly": "readonly"}),
}
Loading

0 comments on commit 600afcf

Please sign in to comment.