Skip to content

Commit

Permalink
refactor: refactor odk appuser + qrcode function naming
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jan 19, 2024
1 parent 10c31fc commit 126f80a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
23 changes: 13 additions & 10 deletions src/backend/app/central/central_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ async def delete_odk_project(
return "Could not delete project from central odk"


def create_appuser(
def create_odk_app_user(
project_id: int, name: str, odk_credentials: project_schemas.ODKCentral = None
):
"""Create an app-user on a remote ODK Server.
"""Create an app user specific to a project on ODK Central.
If odk credentials of the project are provided, use them to create an app user.
"""
Expand All @@ -180,23 +180,23 @@ def create_appuser(
user = settings.ODK_CENTRAL_USER
pw = settings.ODK_CENTRAL_PASSWD

app_user = OdkAppUser(url, user, pw)
odk_app_user = OdkAppUser(url, user, pw)

log.debug(
"ODKCentral: attempting user creation: name: " f"{name} | project: {project_id}"
)
result = app_user.create(project_id, name)
result = odk_app_user.create(project_id, name)

log.debug(f"ODKCentral response: {result.json()}")
return result


def delete_app_user(
def delete_odk_app_user(
project_id: int, name: str, odk_central: project_schemas.ODKCentral = None
):
"""Delete an app-user from a remote ODK Server."""
appuser = get_odk_app_user(odk_central)
result = appuser.delete(project_id, name)
odk_app_user = get_odk_app_user(odk_central)
result = odk_app_user.delete(project_id, name)
return result


Expand Down Expand Up @@ -537,16 +537,19 @@ def generate_updated_xform(
return outfile


async def create_qrcode(
async def encode_qrcode_json(
project_id: int, token: str, name: str, odk_central_url: str = None
):
"""Create the QR Code for an app-user."""
"""Assemble the ODK Collect JSON and base64 encode.
The base64 encoded string is used to generate a QR code later.
"""
if not odk_central_url:
log.debug("ODKCentral connection variables not set in function")
log.debug("Attempting extraction from environment variables")
odk_central_url = settings.ODK_CENTRAL_URL

# Qr code text json in the format acceptable by odk collect.
# QR code text json in the format acceptable by odk collect
qr_code_setting = {
"general": {
"server_url": f"{odk_central_url}/v1/key/{token}/projects/{project_id}",
Expand Down
21 changes: 1 addition & 20 deletions src/backend/app/central/central_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from app.central import central_crud
from app.db import database
from app.projects import project_crud, project_schemas
from app.projects import project_schemas

router = APIRouter(
prefix="/central",
Expand All @@ -54,25 +54,6 @@ async def list_projects():
return JSONResponse(content={"projects": projects})


@router.get("/appuser")
async def create_appuser(
project_id: int,
name: str,
db: Session = Depends(database.get_db),
):
"""Create an appuser in Central."""
appuser = central_crud.create_appuser(project_id, name=name)
return await project_crud.create_qrcode(db, project_id, appuser.get("token"), name)


# @router.get("/list_submissions")
# async def list_submissions(project_id: int):
# """List the submissions data from Central"""
# submissions = central_crud.list_submissions(project_id)
# log.info("/central/list_submissions is Unimplemented!")
# return {"data": submissions}


@router.get("/list-forms")
async def get_form_lists(
db: Session = Depends(database.get_db), skip: int = 0, limit: int = 100
Expand Down
4 changes: 2 additions & 2 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ def generate_task_files(

# Create an app user for the task
project_log.info(f"Creating odkcentral app user for task {task_id}")
appuser = central_crud.create_appuser(odk_id, name, odk_credentials)
appuser = central_crud.create_odk_app_user(odk_id, name, odk_credentials)

# If app user could not be created, raise an exception.
if not appuser:
Expand Down Expand Up @@ -1493,7 +1493,7 @@ async def create_qrcode(
"""Create a QR code for a task."""
# Make QR code for an app_user.
log.debug(f"Generating base64 encoded QR settings for token: {token}")
qrcode_data = await central_crud.create_qrcode(
qrcode_data = await central_crud.encode_qrcode_json(
odk_id, token, project_name, odk_central_url
)

Expand Down

0 comments on commit 126f80a

Please sign in to comment.