Skip to content

Commit

Permalink
move to central_id
Browse files Browse the repository at this point in the history
  • Loading branch information
copelco committed Jan 21, 2025
1 parent 4255b3c commit c9ce099
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
4 changes: 2 additions & 2 deletions apps/odk_publish/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class TemplateVariableAdmin(admin.ModelAdmin):

@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
list_display = ("name", "project_id", "central_server")
search_fields = ("name", "project_id")
list_display = ("name", "central_id", "central_server")
search_fields = ("name", "central_id")
list_filter = ("central_server",)
filter_horizontal = ("template_variables",)

Expand Down
18 changes: 9 additions & 9 deletions apps/odk_publish/etl/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def create_or_update_app_users(form_template: FormTemplate):
) as client:
app_users = client.odk_publish.get_or_create_app_users(
display_names=[app_user_form.app_user.name for app_user_form in app_user_forms],
project_id=form_template.project.project_id,
project_id=form_template.project.central_id,
)
# Link form assignments to app users locally
for app_user_form in app_user_forms:
app_users[app_user_form.app_user.name].xml_form_ids.append(app_user_form.xml_form_id)
# Create or update the form assignments on the server
client.odk_publish.assign_forms(
app_users=app_users.values(), project_id=form_template.project.project_id
app_users=app_users.values(), project_id=form_template.project.central_id
)


Expand All @@ -34,7 +34,7 @@ def generate_and_save_app_user_collect_qrcodes(project: Project):
app_users: QuerySet[AppUser] = project.app_users.all()
logger.info("Generating QR codes", project=project.name, app_users=len(app_users))
with ODKPublishClient(
base_url=project.central_server.base_url, project_id=project.project_id
base_url=project.central_server.base_url, project_id=project.central_id
) as client:
central_app_users = client.odk_publish.get_app_users(
display_names=[app_user.name for app_user in app_users],
Expand All @@ -45,7 +45,7 @@ def generate_and_save_app_user_collect_qrcodes(project: Project):
image = create_app_user_qrcode(
app_user=central_app_users[app_user.name],
base_url=client.session.base_url,
project_id=project.project_id,
central_id=project.central_id,
project_name_prefix=project.name,
)
app_user.qr_code.save(
Expand All @@ -65,28 +65,28 @@ def sync_central_project(base_url: str, project_id: int) -> Project:
# Project
central_project = client.projects.get()
project, created = Project.objects.get_or_create(
project_id=central_project.id,
central_id=central_project.id,
central_server=server,
defaults={"name": central_project.name},
)
logger.info(
f"{'Created' if created else 'Retrieved'} Project",
id=project.id,
project_id=project.project_id,
central_id=project.central_id,
project_name=project.name,
)
# AppUser
central_app_users = client.odk_publish.get_app_users()
for central_app_user in central_app_users.values():
if not central_app_user.deletedAt:
app_user, created = project.app_users.get_or_create(
app_user_id=central_app_user.id,
central_id=central_app_user.id,
defaults={"name": central_app_user.displayName},
)
logger.info(
f"{'Created' if created else 'Retrieved'} AppUser",
id=app_user.id,
app_user_id=app_user.app_user_id,
central_id=app_user.central_id,
app_user_name=app_user.name,
)
# FormTemplate
Expand All @@ -108,7 +108,7 @@ def sync_central_project(base_url: str, project_id: int) -> Project:
)
for app_user in app_users:
app_user_form, created = form_template.app_user_forms.get_or_create(
app_user=project.app_users.get(app_user_id=app_user.id),
app_user=project.app_users.get(central_id=app_user.id),
)
logger.info(
f"{'Created' if created else 'Retrieved'} AppUserFormTemplate",
Expand Down
1 change: 0 additions & 1 deletion apps/odk_publish/etl/transform.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import io
from collections import defaultdict

import openpyxl
from django.core.files.uploadedfile import SimpleUploadedFile
Expand Down
14 changes: 10 additions & 4 deletions apps/odk_publish/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.4 on 2025-01-17 22:27
# Generated by Django 5.1.4 on 2025-01-21 18:01

import django.core.validators
import django.db.models.deletion
Expand Down Expand Up @@ -27,9 +27,9 @@ class Migration(migrations.Migration):
("modified_at", models.DateTimeField(auto_now=True, db_index=True)),
("name", models.CharField(max_length=255)),
(
"app_user_id",
"central_id",
models.PositiveIntegerField(
help_text="The internal ID of this app user in ODK Central.",
help_text="The ID of this app user in ODK Central.",
verbose_name="app user ID",
),
),
Expand Down Expand Up @@ -179,7 +179,13 @@ class Migration(migrations.Migration):
("created_at", models.DateTimeField(auto_now_add=True, db_index=True)),
("modified_at", models.DateTimeField(auto_now=True, db_index=True)),
("name", models.CharField(max_length=255)),
("project_id", models.PositiveIntegerField(verbose_name="project ID")),
(
"central_id",
models.PositiveIntegerField(
help_text="The ID of this project in ODK Central.",
verbose_name="project ID",
),
),
(
"central_server",
models.ForeignKey(
Expand Down
12 changes: 7 additions & 5 deletions apps/odk_publish/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class Meta:

class Project(AbstractBaseModel):
name = models.CharField(max_length=255)
project_id = models.PositiveIntegerField(verbose_name="project ID")
central_id = models.PositiveIntegerField(
verbose_name="project ID", help_text="The ID of this project in ODK Central."
)
central_server = models.ForeignKey(
CentralServer, on_delete=models.CASCADE, related_name="projects"
)
Expand All @@ -69,7 +71,7 @@ class Project(AbstractBaseModel):
)

def __str__(self):
return f"{self.name} ({self.project_id})"
return f"{self.name} ({self.central_id})"


class FormTemplate(AbstractBaseModel):
Expand Down Expand Up @@ -104,7 +106,7 @@ def create_next_version(self, user: User) -> "FormTemplateVersion":
3. Create a new FormTemplateVersion instance with the downloaded file.
"""
with ODKPublishClient(
base_url=self.project.central_server.base_url, project_id=self.project.project_id
base_url=self.project.central_server.base_url, project_id=self.project.central_id
) as client:
version = client.odk_publish.get_unique_version_by_form_id(
xml_form_id_base=self.form_id_base
Expand Down Expand Up @@ -171,8 +173,8 @@ def __str__(self):

class AppUser(AbstractBaseModel):
name = models.CharField(max_length=255)
app_user_id = models.PositiveIntegerField(
verbose_name="app user ID", help_text="The internal ID of this app user in ODK Central."
central_id = models.PositiveIntegerField(
verbose_name="app user ID", help_text="The ID of this app user in ODK Central."
)
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name="app_users")
qr_code = models.ImageField(
Expand Down

0 comments on commit c9ce099

Please sign in to comment.