Skip to main content

Classes for QA Reports in clinicedc/edc projects

Project description

pypi actions codecov downloads

edc-qareports

This module helps you represent SQL VIEWS as QA Reports using Django Admin.

In clinicedc/edc projects, QA reports are in the <my_app>_reports module.

Installation

Add to settings.INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "edc_qareports.apps.AppConfig",
    ...
    ]

Add to project URLS:

# urls.py
urlpatterns = [
    ...
    *paths_for_urlpatterns("edc_qareports"),
    ...
    ]

Custom QA / data management reports using SQL VIEWS

Although not absolutely necessary, it is convenient to base a QA report on an SQL VIEW. As issues are resolved, the SQL VIEW reflects the changes.

A QA report based on an SQL VIEW can be represented by a model class. By registering the model class in Admin, all the functionality of the ModelAdmin class is available to show the report.

First, within your EDC project, create a <myapp>_reports app. For example Meta Reports.

meta_edc
meta_edc/meta_reports
meta_edc/meta_reports/admin
meta_edc/meta_reports/admin/unmanaged
meta_edc/meta_reports/admin/unmanaged/my_view_in_sql_admin.py
meta_edc/meta_reports/admin/report_note_admin.py
meta_edc/meta_reports/migrations
meta_edc/meta_reports/migrations/0001_myviewinsql.py
meta_edc/meta_reports/models
meta_edc/meta_reports/models/unmanaged
meta_edc/meta_reports/models/unmanaged/my_view_in_sql.py
meta_edc/meta_reports/models/unmanaged/my_view_in_sql.sql
meta_edc/meta_reports/admin_site.py
meta_edc/meta_reports/apps.py
meta_edc/ ...

the apps.py might look like this:

from django.apps import AppConfig as DjangoAppConfig

class AppConfig(DjangoAppConfig):
    name = "meta_reports"
    verbose_name = "META Reports"
    include_in_administration_section = True

QA Report as an SQL VIEW

Now that you have created the basic structure for the Reports App, create an SQL VIEW. Some rules apply:

  • To show the model class in Admin, the SQL VIEW needs at least an ID column.

  • To use the EDC ModelAdmin classes, include id, subject_identifier, site_id, created and report_model.

  • Columns id, created and report_model are generated columns from the SQL VIEW, not values coming from the underlying SQL statement / data tables.

  • Column report_model is in label_lower format.

  • Suffix the view name with _view.

create view my_view_in_sql_view as (
    select *, uuid() as 'id', now() as 'created',
        'meta_reports.myviewinsql' as report_model
        from (
            select  distinct `subject_identifier`, `site_id`, col1, col2, col3
            from some_crf_table
            where col1 is null
        ) as A

Using a model class to represent your QA Report

An SQL VIEW is not a table so configure an unmanaged model class by setting managed=False. makemigrations creates migrations for unmanaged models but never calls CreateModel.

The unmanaged model class would be something like this:

class MyViewInSql(QaReportModelMixin, models.Model):

    col1 = models.CharField(max_length=25)

    col2 = models.IntegerField()

    col3 = models.DateTimeField()

    class Meta:
        managed = False
        db_table = "my_view_in_sql_view"
        verbose_name = "blah blah"
        verbose_name_plural = "blah blah"

You can store the SQL statement anywhere but we put it in the same folder as the model class using the same file name as the model class but with file extension .sql

Using a migration to read the SQL statement

Create an empty migration in the reports app and read the SQL file in the migration

...

operations = [
    migrations.RunSQL(
        read_unmanaged_model_sql("my_view_in_sql.sql", app_name="meta_reports")
    ),
]

IMPORTANT: If you change the SQL VIEW, update the .sql file and create a new migration that drops and re-creates the SQL VIEW.

...

operations = [
    migrations.RunSQL("drop view my_view_in_sql_view"),
    migrations.RunSQL(
        read_unmanaged_model_sql("my_view_in_sql.sql", app_name="meta_reports")
    ),
]

Linking QaReportNote with your QA Report

You can link your QA Report in Admin to model QaReportNote. The QaReportNote model class is used to track the status of the report item and provide a space for any notes.

To use QaReportNote with your QA report, declare the QA Report admin class with QaReportWithNoteModelAdminMixin.

from django.contrib import admin
from edc_model_admin.dashboard import ModelAdminDashboardMixin
from edc_model_admin.mixins import TemplatesModelAdminMixin
from edc_qareports.admin import QaReportWithNoteModelAdminMixin
from edc_sites.admin import SiteModelAdminMixin
from edc_visit_schedule.admin import ScheduleStatusListFilter

from ...admin_site import meta_reports_admin
from ...models import MyViewInSql


@admin.register(MyViewInSql, site=meta_reports_admin)
class MyViewInSqlAdmin(
    QaReportWithNoteModelAdminMixin,
    SiteModelAdminMixin,
    ModelAdminDashboardMixin,
    TemplatesModelAdminMixin,
    admin.ModelAdmin,
):
    ordering = ["site", "subject_identifier"]

    list_display = [
        "dashboard",
        "subject",
        "col1",
        "col2",
        "col3",
        "created",
    ]

    list_filter = [ScheduleStatusListFilter, "col1", "col3"]

    search_fields = ["id", "subject_identifier"]

    @admin.display(description="Subject", ordering="subject_identifier")
    def subject(self, obj):
        return obj.subject_identifier

Granting access to your QA Report

Add the QA report codenames to your local app, create a group and add the group to the QA_REPORTS_ROLE.

In this example the app is called meta_reports and the group is META_REPORTS.

(Note: If your app has an auth module (e.g. meta_auth) put these lines there.)

# meta_reports/auth_objects.py

reports_codenames = [c for c in get_app_codenames("meta_reports")]
# meta_reports/auths.py

site_auths.add_group(*reports_codenames, name=META_REPORTS)
# add the group to the QA_REPORTS role
site_auths.update_role(META_REPORTS, name=QA_REPORTS_ROLE)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

edc_qareports-0.1.7.tar.gz (40.3 kB view details)

Uploaded Source

Built Distribution

edc_qareports-0.1.7-py3-none-any.whl (49.8 kB view details)

Uploaded Python 3

File details

Details for the file edc_qareports-0.1.7.tar.gz.

File metadata

  • Download URL: edc_qareports-0.1.7.tar.gz
  • Upload date:
  • Size: 40.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for edc_qareports-0.1.7.tar.gz
Algorithm Hash digest
SHA256 4bc0832a7556eb761b988958ab863650f688a5bf4da273a36363419bbf85d496
MD5 9d2ece90ea81fc1975bd066598c2aa1b
BLAKE2b-256 447be8b53f72dbfb44bd523954c9489a334464ba256b256188f8f59493bf5918

See more details on using hashes here.

File details

Details for the file edc_qareports-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for edc_qareports-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 7184e3ae174d1a5a09ab0c27492b91db2ff4243d514a684aa3e3fc6e69b83def
MD5 ae6df1f016acddd6e71a6627ba66e9c2
BLAKE2b-256 bb8d9d03e1c3362723aec1c73b03f1a4f9408e60e966c02cf958cc0bdf8d6a46

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page