Skip to content

Commit

Permalink
Fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-misuk-im committed Nov 29, 2024
1 parent eb1be3d commit 5554d2b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/country_workspace/admin/kobo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

from django.contrib import admin

from .base import BaseModelAdmin
Expand All @@ -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
11 changes: 8 additions & 3 deletions src/country_workspace/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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")

Check notice

Code scanning / Bandit

Possible hardcoded password: '01f1a122ddad12d7e72f3b86e9d8a637c917bee8' Note

Possible hardcoded password: '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)
2 changes: 1 addition & 1 deletion tests/contrib/kobo/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
8 changes: 5 additions & 3 deletions tests/extras/testutils/factories/kobo.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5554d2b

Please sign in to comment.