Skip to content

Commit

Permalink
Make some read-only shadow models writable
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed Feb 28, 2024
1 parent e7ba2a8 commit 752d7eb
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 160 deletions.
4 changes: 2 additions & 2 deletions hub/admin/extend_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from kobo.apps.trash_bin.models.account import AccountTrash
from kobo.apps.trash_bin.utils import move_to_trash
from kpi.deployment_backends.kc_access.shadow_models import (
ReadOnlyKobocatMonthlyXFormSubmissionCounter,
KobocatMonthlyXFormSubmissionCounter,
)
from kpi.models.asset import AssetDeploymentStatus
from .filters import UserAdvancedSearchFilter
Expand Down Expand Up @@ -241,7 +241,7 @@ def monthly_submission_count(self, obj):
displayed in the Django admin user changelist page
"""
today = timezone.now().date()
instances = ReadOnlyKobocatMonthlyXFormSubmissionCounter.objects.filter(
instances = KobocatMonthlyXFormSubmissionCounter.objects.filter(
user_id=obj.id,
year=today.year,
month=today.month,
Expand Down
6 changes: 3 additions & 3 deletions kobo/apps/superuser_stats/models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from kpi.deployment_backends.kc_access.shadow_models import (
ReadOnlyKobocatMonthlyXFormSubmissionCounter,
KobocatMonthlyXFormSubmissionCounter,
)


class SuperuserStatsModel(ReadOnlyKobocatMonthlyXFormSubmissionCounter):
class SuperuserStatsModel(KobocatMonthlyXFormSubmissionCounter):
"""
Spoiler: Kludgy!
The purpose of this model is only to provide an obvious name for
the superuser section in Django Admin.
Django needs a model to register an admin model, so it extends a shadow
model (as a proxy) to avoid creating new migrations.
It extends `ReadOnlyKobocatMonthlyXFormSubmissionCounter` but it could have
It extends `KobocatMonthlyXFormSubmissionCounter` but it could have
been anyone of the (shadow) models since we do not add/update/delete objects
from the admin interface. The HTML template only lists the available reports.
"""
Expand Down
8 changes: 4 additions & 4 deletions kobo/apps/superuser_stats/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
from kobo.static_lists import COUNTRIES
from kpi.constants import ASSET_TYPE_SURVEY
from kpi.deployment_backends.kc_access.shadow_models import (
KobocatMonthlyXFormSubmissionCounter,
KobocatXForm,
KobocatUser,
KobocatUserProfile,
ReadOnlyKobocatInstance,
ReadOnlyKobocatMonthlyXFormSubmissionCounter,
)
from kpi.models.asset import Asset, AssetDeploymentStatus

Expand Down Expand Up @@ -111,7 +111,7 @@ def generate_continued_usage_report(output_filename: str, end_date: str):
date_created__date__range=(twelve_months_time, end_date),
)
submissions_count = (
ReadOnlyKobocatMonthlyXFormSubmissionCounter.objects.annotate(
KobocatMonthlyXFormSubmissionCounter.objects.annotate(
date=Cast(
Concat(F('year'), Value('-'), F('month'), Value('-'), 1),
DateField(),
Expand Down Expand Up @@ -201,7 +201,7 @@ def generate_domain_report(output_filename: str, start_date: str, end_date: str)

# get a count of the submissions
domain_submissions = {
domain: ReadOnlyKobocatMonthlyXFormSubmissionCounter.objects.annotate(
domain: KobocatMonthlyXFormSubmissionCounter.objects.annotate(
date=Cast(
Concat(F('year'), Value('-'), F('month'), Value('-'), 1),
DateField(),
Expand Down Expand Up @@ -478,7 +478,7 @@ def generate_user_statistics_report(

# Get records from SubmissionCounter
records = (
ReadOnlyKobocatMonthlyXFormSubmissionCounter.objects.annotate(
KobocatMonthlyXFormSubmissionCounter.objects.annotate(
date=Cast(
Concat(F('year'), Value('-'), F('month'), Value('-'), 1),
DateField(),
Expand Down
9 changes: 6 additions & 3 deletions kobo/apps/trackers/submission_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from django.utils import timezone
from model_bakery import baker

from kpi.deployment_backends.kc_access.shadow_models import KobocatXForm, ReadOnlyKobocatDailyXFormSubmissionCounter
from kpi.deployment_backends.kc_access.shadow_models import (
KobocatDailyXFormSubmissionCounter,
KobocatXForm,
)
from kpi.models import Asset
from kpi.urls.router_api_v2 import URL_NAMESPACE as ROUTER_URL_NAMESPACE

Expand Down Expand Up @@ -79,7 +82,7 @@ def update_xform_counters(asset: Asset, xform: KobocatXForm = None, submissions:
)
xform.save()

counter = ReadOnlyKobocatDailyXFormSubmissionCounter.objects.filter(
counter = KobocatDailyXFormSubmissionCounter.objects.filter(
date=today.date(),
user_id=asset.owner.id,
).first()
Expand All @@ -90,7 +93,7 @@ def update_xform_counters(asset: Asset, xform: KobocatXForm = None, submissions:
else:
counter = (
baker.make(
ReadOnlyKobocatDailyXFormSubmissionCounter,
KobocatDailyXFormSubmissionCounter,
date=today.date(),
counter=submissions,
xform=xform,
Expand Down
Loading

0 comments on commit 752d7eb

Please sign in to comment.