Skip to content

Commit

Permalink
Merge branch 'release/0.1.19' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Oct 9, 2024
2 parents 1dc2900 + 0fd8fb2 commit 94f762f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
44 changes: 42 additions & 2 deletions edc_qareports/model_mixins/qa_report_model_mixin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import sys

from django.conf import settings
from django.contrib.sites.models import Site
from django.db import models
from django.db import connection, models
from django.db.models import DO_NOTHING, Index
from edc_utils import get_utcnow

from .qa_reports_permissions import qa_reports_permissions

Expand All @@ -13,7 +17,43 @@ class QaReportModelMixin(models.Model):

site = models.ForeignKey(Site, on_delete=DO_NOTHING)

created = models.DateTimeField()
created = models.DateTimeField(default=get_utcnow)

@classmethod
def recreate_db_view(cls, drop: bool | None = None, verbose: bool | None = None):
"""Manually recreate the database view for models declared
with `django_db_views.DBView`.
Mostly useful when Django raises an OperationalError with a
restored DB complaining of 'The user specified as a definer
(user@host) does not exist'.
This does not replace generating a migration with `viewmigration`
and running the migration.
For example:
from intecomm_reports.models import Vl
Vl.recreate_db_view()
"""
drop = True if drop is None else drop
try:
sql = cls.view_definition.get(settings.DATABASES["default"]["ENGINE"]) # noqa
except AttributeError as e:
raise AttributeError(
f"Is this model linked to a view? Declare model with `DBView`. Got {e}"
)
else:
sql = sql.replace(";", "")
if verbose:
print(f"create view {cls._meta.db_table} as {sql};")
with connection.cursor() as c:
if drop:
c.execute(f"drop view {cls._meta.db_table};")
c.execute(f"create view {cls._meta.db_table} as {sql};")
sys.stdout.write(
f"Done. Refreshed DB VIEW `{cls._meta.db_table}` for model {cls}."
)

class Meta:
abstract = True
Expand Down
1 change: 1 addition & 0 deletions edc_qareports/sql_generator/requisition_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class RequisitionCase(CrfCase):
panel: str = None
subjectrequisition_dbtable: str | None = None
panel_dbtable: str | None = None
requisition_id_field: str | None = None

@property
def sql(self):
Expand Down
5 changes: 4 additions & 1 deletion edc_qareports/sql_generator/requisition_subquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class RequisitionSubquery(CrfSubquery):
panel: str = None
subjectrequisition_dbtable: str | None = None
panel_dbtable: str | None = None
requisition_id_field: str | None = None
template: str = field(
init=False,
default=Template(
Expand All @@ -32,7 +33,7 @@ class RequisitionSubquery(CrfSubquery):
"v.schedule_name, req.modified, '${label_lower}' as label_lower, "
"'${label}' as label, count(*) as records "
"from ${subjectrequisition_dbtable} as req "
"left join ${dbtable} as crf on req.id=crf.requisition_id "
"left join ${dbtable} as crf on req.id=crf.${requisition_id_field} "
"left join ${subjectvisit_dbtable} as v on v.id=req.subject_visit_id "
"${left_joins} "
"left join ${panel_dbtable} as panel on req.panel_id=panel.id "
Expand All @@ -57,3 +58,5 @@ def __post_init__(self):
)
if not self.panel_dbtable:
self.panel_dbtable = "edc_lab_panel"
if not self.requisition_id_field:
self.requisition_id_field = "requisition_id"

0 comments on commit 94f762f

Please sign in to comment.