diff --git a/src/country_workspace/admin/kobo.py b/src/country_workspace/admin/kobo.py index 6f8f591..db9fdca 100644 --- a/src/country_workspace/admin/kobo.py +++ b/src/country_workspace/admin/kobo.py @@ -1,3 +1,5 @@ +from typing import Any + from django.contrib import admin from .base import BaseModelAdmin @@ -24,3 +26,12 @@ class KoboAssetAdmin(BaseModelAdmin): list_display = ("uid", "name") exclude = ("programs",) inlines = (KoboQuestionAdmin, KoboSubmissionAdmin) + + def has_add_permission(self, *args: Any, **kwargs: Any) -> bool: + return False + + def has_change_permission(self, *args: Any, **kwargs: Any) -> bool: + return False + + def has_delete_permission(self, *args: Any, **kwargs: Any) -> bool: + return False diff --git a/src/country_workspace/tasks.py b/src/country_workspace/tasks.py index 038acac..884139b 100644 --- a/src/country_workspace/tasks.py +++ b/src/country_workspace/tasks.py @@ -11,6 +11,7 @@ from country_workspace.contrib.kobo.client import Client as KoboClient from country_workspace.models import AsyncJob, KoboAsset, KoboSubmission from country_workspace.models.jobs import KoboSyncJob +from country_workspace.models.kobo import KoboQuestion logger = logging.getLogger(__name__) @@ -64,10 +65,14 @@ def removed_expired_jobs(**kwargs): AsyncJob.objects.filter(**kwargs).delete() @app.task -def sync_kobo_assets(job_id: int, version: int) -> None: +def sync_kobo_assets_task(job_id: int, version: int) -> None: _ = KoboSyncJob.objects.get(pk=job_id, version=version) client = KoboClient(base_url="https://kf-hope-stg.unitst.org", token="01f1a122ddad12d7e72f3b86e9d8a637c917bee8") for asset_data in client.assets: - asset_model = KoboAsset.objects.get_or_create(uid=asset_data.uid) + asset_model, _ = KoboAsset.objects.update_or_create(uid=asset_data.uid, defaults={"name": asset_data.name}) + + for question_data in asset_data.questions: + KoboQuestion.objects.update_or_create(asset=asset_model, key=question_data.key, labels=question_data.labels) + for submission_data in asset_data.submissions: - KoboSubmission.objects.get_or_create(uuid=submission_data.uuid, asset=asset_model, data=submission_data.data) + KoboSubmission.objects.update_or_create(uuid=submission_data.uuid, asset=asset_model, data=submission_data.data) diff --git a/tests/contrib/kobo/test_client.py b/tests/contrib/kobo/test_client.py index 901f390..8a626d4 100644 --- a/tests/contrib/kobo/test_client.py +++ b/tests/contrib/kobo/test_client.py @@ -44,4 +44,4 @@ def test_error_is_propagated() -> None: session = Mock(spec=Session) session.get.return_value.raise_for_status.side_effect = Timeout with raises(Timeout): - handle_paginated_response(session, SAMPLE_URL, identity, identity) + tuple(handle_paginated_response(session, SAMPLE_URL, identity, identity)) diff --git a/tests/extras/testutils/factories/kobo.py b/tests/extras/testutils/factories/kobo.py index 4df6239..e592475 100644 --- a/tests/extras/testutils/factories/kobo.py +++ b/tests/extras/testutils/factories/kobo.py @@ -1,10 +1,12 @@ from factory import fuzzy -from factory.declarations import SubFactory -from testutils.factories import AutoRegisterModelFactory, ProgramFactory +from country_workspace.models import KoboAsset +from testutils.factories import AutoRegisterModelFactory class KoboAssetFactory(AutoRegisterModelFactory): uid = fuzzy.FuzzyText() name = fuzzy.FuzzyText() - program = SubFactory(ProgramFactory) + + class Meta: + model = KoboAsset