Skip to content

Commit

Permalink
use django_db_views, move models from folder unmanaged to dbview, sql…
Browse files Browse the repository at this point in the history
…ite view statements, enable some migrations for tests
  • Loading branch information
erikvw committed Aug 13, 2024
1 parent 6344cce commit f776a2e
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 65 deletions.
19 changes: 10 additions & 9 deletions edc_qareports/auth_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# role names
QA_REPORTS_ROLE = "qa_reports_role"
QA_REPORTS_SUPER_ROLE = "qa_reports_super_role"
QA_REPORTS_AUDIT_ROLE = "qa_reports_audit_role"

qa_reports_codenames = [
Expand All @@ -16,12 +17,12 @@
]


custom_codenames = [
"edc_qareports.nav_qareports_section",
]

qa_reports_codenames.extend(custom_codenames)

custom_codename_tuples = []
for codename in custom_codenames:
custom_codename_tuples.append((codename, f"Can access {codename.split('.')[1]}"))
# custom_codenames = [
# "edc_qareports.nav_qareports_section",
# ]
#
# qa_reports_codenames.extend(custom_codenames)
#
# custom_codename_tuples = []
# for codename in custom_codenames:
# custom_codename_tuples.append((codename, f"Can access {codename.split('.')[1]}"))
9 changes: 5 additions & 4 deletions edc_qareports/auths.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
QA_REPORTS_AUDIT,
QA_REPORTS_AUDIT_ROLE,
QA_REPORTS_ROLE,
custom_codename_tuples,
QA_REPORTS_SUPER_ROLE,
qa_reports_codenames,
)

site_auths.add_custom_permissions_tuples(
model="edc_qareports.edcpermissions", codename_tuples=custom_codename_tuples
)
# site_auths.add_custom_permissions_tuples(
# model="edc_qareports.edcpermissions", codename_tuples=custom_codename_tuples
# )


# groups
Expand All @@ -21,4 +21,5 @@

# roles
site_auths.add_role(QA_REPORTS, name=QA_REPORTS_ROLE)
site_auths.add_role(QA_REPORTS, name=QA_REPORTS_SUPER_ROLE)
site_auths.add_role(QA_REPORTS_AUDIT, name=QA_REPORTS_AUDIT_ROLE)
5 changes: 3 additions & 2 deletions edc_qareports/migrations/0008_qareportlogsummary.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.11 on 2024-06-19 02:46
# Generated by Django 5.0.7 on 2024-08-12 16:13

from django.db import migrations, models

Expand All @@ -11,7 +11,7 @@ class Migration(migrations.Migration):

operations = [
migrations.CreateModel(
name="QAReportLogSummary",
name="QaReportLogSummary",
fields=[
(
"id",
Expand All @@ -30,6 +30,7 @@ class Migration(migrations.Migration):
"verbose_name_plural": "QA Report Log Summary",
"db_table": "qa_report_log_summary_view",
"managed": False,
"default_permissions": ("view", "export", "viewallsites"),
},
),
]
25 changes: 0 additions & 25 deletions edc_qareports/migrations/0009_auto_20240619_0546.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Generated by Django 4.2.11 on 2024-06-19 02:59
# Generated by Django 5.0.7 on 2024-08-12 16:18

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("edc_qareports", "0009_auto_20240619_0546"),
("edc_qareports", "0008_qareportlogsummary"),
]

operations = []
12 changes: 12 additions & 0 deletions edc_qareports/migrations/0010_auto_20240812_1918.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Django 5.0.7 on 2024-08-12 16:18

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("edc_qareports", "0009_auto_20240812_1918"),
]

operations = []
48 changes: 48 additions & 0 deletions edc_qareports/migrations/0011_auto_20240813_0219.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 5.0.7 on 2024-08-12 23:19

import django_db_views.migration_functions
import django_db_views.operations
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("edc_qareports", "0010_auto_20240812_1918"),
]

operations = [
django_db_views.operations.ViewRunPython(
code=django_db_views.migration_functions.ForwardViewMigration(
"select *, uuid() as `id`, now() as `created` from (\n select site_id, username, report_model, min(accessed) as `first_accessed`,\n max(accessed) as `last_accessed`, count(*) as `access_count`\n from edc_qareports_qareportlog\n group by username, report_model, site_id\n ) as A\n order by username, report_model",
"qa_report_log_summary_view",
engine="django.db.backends.mysql",
),
reverse_code=django_db_views.migration_functions.BackwardViewMigration(
"", "qa_report_log_summary_view", engine="django.db.backends.mysql"
),
atomic=False,
),
django_db_views.operations.ViewRunPython(
code=django_db_views.migration_functions.ForwardViewMigration(
"select *, get_random_uuid() as id, now() as created (\n select site_id, username, report_model, min(accessed) as first_accessed,\n max(accessed) as last_accessed, count(*) as access_count\n from edc_qareports_qareportlog\n group by username, report_model, site_id\n ) as A\n order by username, report_model",
"qa_report_log_summary_view",
engine="django.db.backends.postgresql",
),
reverse_code=django_db_views.migration_functions.BackwardViewMigration(
"", "qa_report_log_summary_view", engine="django.db.backends.postgresql"
),
atomic=False,
),
django_db_views.operations.ViewRunPython(
code=django_db_views.migration_functions.ForwardViewMigration(
"SELECT *, lower(\n hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-' || '4' ||\n substr(hex( randomblob(2)), 2) || '-' ||\n substr('AB89', 1 + (abs(random()) % 4) , 1) ||\n substr(hex(randomblob(2)), 2) || '-' ||\n hex(randomblob(6))\n ) as id, datetime() as created from (\n select site_id, username, report_model, min(accessed) as first_accessed,\n max(accessed) as last_accessed, count(*) as access_count\n from edc_qareports_qareportlog\n group by username, report_model, site_id\n ) as A\n order by username, report_model",
"qa_report_log_summary_view",
engine="django.db.backends.sqlite3",
),
reverse_code=django_db_views.migration_functions.BackwardViewMigration(
"", "qa_report_log_summary_view", engine="django.db.backends.sqlite3"
),
atomic=False,
),
]
2 changes: 1 addition & 1 deletion edc_qareports/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .dbviews import QaReportLogSummary
from .edc_permissions import EdcPermissions
from .qa_report_model_mixin import QaReportModelMixin
from .qa_report_note import QaReportNote
from .qa_reports_log import QaReportLog
from .unmanaged import QaReportLogSummary
1 change: 1 addition & 0 deletions edc_qareports/models/dbviews/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .unmanaged_model import QaReportLogSummary
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
from django.contrib.sites.models import Site
from django.db import models
from django_db_views.db_view import DBView

from ..qa_report_model_mixin import qa_reports_permissions
from .view_definition import get_view_definition

class QaReportLogSummary(models.Model):

class QaReportLogSummary(DBView):
username = models.CharField(max_length=100)
site = models.ForeignKey(Site, on_delete=models.CASCADE)
report_model = models.CharField(max_length=100)
first_accessed = models.DateTimeField()
last_accessed = models.DateTimeField()
access_count = models.IntegerField()

view_definition = get_view_definition()

class Meta:
managed = False
db_table = "qa_report_log_summary_view"
verbose_name = "QA Report Log Summary"
verbose_name_plural = "QA Report Log Summary"
default_permissions = qa_reports_permissions
43 changes: 43 additions & 0 deletions edc_qareports/models/dbviews/view_definition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
mysql_view: str = """
select *, uuid() as `id`, now() as `created` from (
select site_id, username, report_model, min(accessed) as `first_accessed`,
max(accessed) as `last_accessed`, count(*) as `access_count`
from edc_qareports_qareportlog
group by username, report_model, site_id
) as A
order by username, report_model
"""

pg_view: str = """
select *, get_random_uuid() as id, now() as created (
select site_id, username, report_model, min(accessed) as first_accessed,
max(accessed) as last_accessed, count(*) as access_count
from edc_qareports_qareportlog
group by username, report_model, site_id
) as A
order by username, report_model
"""

sqlite3_view = """
SELECT *, lower(
hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-' || '4' ||
substr(hex( randomblob(2)), 2) || '-' ||
substr('AB89', 1 + (abs(random()) % 4) , 1) ||
substr(hex(randomblob(2)), 2) || '-' ||
hex(randomblob(6))
) as id, datetime() as created from (
select site_id, username, report_model, min(accessed) as first_accessed,
max(accessed) as last_accessed, count(*) as access_count
from edc_qareports_qareportlog
group by username, report_model, site_id
) as A
order by username, report_model
"""


def get_view_definition() -> dict:
return {
"django.db.backends.mysql": mysql_view,
"django.db.backends.postgresql": pg_view,
"django.db.backends.sqlite3": sqlite3_view,
}
3 changes: 3 additions & 0 deletions edc_qareports/models/qa_report_model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.db import models
from django.db.models import PROTECT

qa_reports_permissions = ("view", "export", "viewallsites")


class QaReportModelMixin(models.Model):

Expand All @@ -15,3 +17,4 @@ class QaReportModelMixin(models.Model):

class Meta:
abstract = True
default_permissions = qa_reports_permissions
1 change: 0 additions & 1 deletion edc_qareports/models/unmanaged/__init__.py

This file was deleted.

9 changes: 0 additions & 9 deletions edc_qareports/models/unmanaged/qa_report_log_summary.sql

This file was deleted.

18 changes: 9 additions & 9 deletions edc_qareports/navbar_item.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from edc_navbar import NavbarItem

qa_navbar_item = NavbarItem(
name="qa_reports_home",
title="QA Reports",
label="QA",
codename="edc_qareports.nav_qareports_section",
url_name="meta_reports_admin:index",
)
# from edc_navbar import NavbarItem
#
# qa_navbar_item = NavbarItem(
# name="qa_reports_home",
# title="QA Reports",
# label="QA",
# codename="edc_qareports.nav_qareports_section",
# url_name="meta_reports_admin:index",
# )
11 changes: 10 additions & 1 deletion edc_qareports/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

from django.conf import settings

"""
use DBView instead
"""


def read_unmanaged_model_sql(
filename: str | None = None,
app_name: str | None = None,
fullpath: str | Path | None = None,
) -> str:
uuid_func = "uuid()"
if settings.DATABASES["default"]["ENGINE"] == "django.db.backends.postgresql":
uuid_func = "gen_random_uuid()"

if not fullpath:
fullpath = Path(settings.BASE_DIR) / app_name / "models" / "unmanaged" / filename
else:
Expand All @@ -23,4 +31,5 @@ def read_unmanaged_model_sql(
if line:
parsed_sql.append(line)

return " ".join(parsed_sql)
sql = " ".join(parsed_sql)
return sql.replace("uuid()", uuid_func)
5 changes: 4 additions & 1 deletion runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"django.contrib.sites",
"django_crypto_fields.apps.AppConfig",
"multisite",
"django_db_views",
"edc_action_item.apps.AppConfig",
"edc_appointment.apps.AppConfig",
"edc_auth.apps.AppConfig",
Expand All @@ -41,13 +42,15 @@
"edc_notification.apps.AppConfig",
"edc_registration.apps.AppConfig",
"edc_sites.apps.AppConfig",
"edc_subject_dashboard.apps.AppConfig",
"edc_visit_tracking.apps.AppConfig",
"edc_visit_schedule.apps.AppConfig",
"edc_qareports.apps.AppConfig",
"edc_appconfig.apps.AppConfig",
],
add_dashboard_middleware=False,
add_dashboard_middleware=True,
add_lab_dashboard_middleware=False,
add_adverse_event_dashboard_middleware=False,
).settings


Expand Down

0 comments on commit f776a2e

Please sign in to comment.