From cd358ce73f320c190430aee4bcccfa1140d46b5b Mon Sep 17 00:00:00 2001 From: Cal Ellowitz Date: Wed, 11 Oct 2023 15:37:17 -0400 Subject: [PATCH] use username for payment import and export --- commcare_connect/opportunity/export.py | 4 +-- commcare_connect/opportunity/visit_import.py | 28 +++++++++---------- .../opportunity/opportunity_detail.html | 4 +-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/commcare_connect/opportunity/export.py b/commcare_connect/opportunity/export.py index 89186a11..947fdc67 100644 --- a/commcare_connect/opportunity/export.py +++ b/commcare_connect/opportunity/export.py @@ -64,11 +64,11 @@ def _schema_sort(item): def export_empty_payment_table(opportunity: Opportunity) -> Dataset: - headers = ["Phone Number", "Name", "Payment Amount"] + headers = ["Username", "Phone Number", "Name", "Payment Amount"] dataset = Dataset(title="Export", headers=headers) access_objects = OpportunityAccess.objects.filter(opportunity=opportunity).select_related("user") for access in access_objects: - row = (access.user.phone_number, access.user.name, "") + row = (access.user.username, access.user.phone_number, access.user.name, "") dataset.append(row) return dataset diff --git a/commcare_connect/opportunity/visit_import.py b/commcare_connect/opportunity/visit_import.py index a54e479f..33b607bf 100644 --- a/commcare_connect/opportunity/visit_import.py +++ b/commcare_connect/opportunity/visit_import.py @@ -18,7 +18,7 @@ VISIT_ID_COL = "visit id" STATUS_COL = "status" -PHONE_COL = "phone number" +USERNAME_COL = "username" AMOUNT_COL = "payment amount" REQUIRED_COLS = [VISIT_ID_COL, STATUS_COL] @@ -54,7 +54,7 @@ def __len__(self): def get_missing_message(self): joined = ", ".join(self.missing_visits) missing = textwrap.wrap(joined, width=115, break_long_words=False, break_on_hyphens=False) - return f"
{len(self.missing_users)} phone numbers were not found:
{'
'.join(missing)}" + return f"
{len(self.missing_users)} usernames were not found:
{'
'.join(missing)}" def bulk_update_visit_status(opportunity: Opportunity, file: UploadedFile) -> VisitImportStatus: @@ -148,22 +148,22 @@ def _bulk_update_payments(opportunity: Opportunity, imported_data: Dataset) -> P if not headers: raise ImportException("The uploaded file did not contain any headers") - phone_col_index = _get_header_index(headers, PHONE_COL) + username_col_index = _get_header_index(headers, USERNAME_COL) amount_col_index = _get_header_index(headers, AMOUNT_COL) invalid_rows = [] payments = {} for row in imported_data: row = list(row) - phone = str(row[phone_col_index]) + username = str(row[username_col_index]) amount_raw = row[amount_col_index] if amount_raw: - if not phone: - invalid_rows.append((row, "phone number required")) + if not username: + invalid_rows.append((row, "username required")) try: amount = int(amount_raw) except ValueError: invalid_rows.append((row, "amount must be an integer")) - payments[phone] = amount + payments[username] = amount if invalid_rows: raise ImportException(f"{len(invalid_rows)} have errors", invalid_rows) @@ -171,12 +171,12 @@ def _bulk_update_payments(opportunity: Opportunity, imported_data: Dataset) -> P seen_users = set() missing_users = set() with transaction.atomic(): - phone_numbers = list(payments) - users = OpportunityAccess.objects.filter( - user__phone_number__in=phone_numbers, opportunity=opportunity - ).select_related("user") + usernames = list(payments) + users = OpportunityAccess.objects.filter(user__username__in=usernames, opportunity=opportunity).select_related( + "user" + ) for access in users: - Payment.objects.create(opportunity_access=access, amount=payments[access.user.phone_number]) - seen_users.add(access.user.phone_number) - missing_users = set(phone_numbers) - seen_users + Payment.objects.create(opportunity_access=access, amount=payments[access.user.username]) + seen_users.add(access.user.username) + missing_users = set(usernames) - seen_users return PaymentImportStatus(seen_users, missing_users) diff --git a/commcare_connect/templates/opportunity/opportunity_detail.html b/commcare_connect/templates/opportunity/opportunity_detail.html index d3f76c6f..2e9d950a 100644 --- a/commcare_connect/templates/opportunity/opportunity_detail.html +++ b/commcare_connect/templates/opportunity/opportunity_detail.html @@ -179,7 +179,7 @@

Learn App Modules

- {% include "tables/table_placeholder.html" with num_cols=2 %} + {% include "tables/table_placeholder.html" with num_cols=4 %}
@@ -295,7 +295,7 @@

{% translate "Import Payment Records" %}

{% blocktrans %} - The file must contain at least the "Phone Number" and "Amount" column. + The file must contain at least the "Username" and "Amount" column. {% endblocktrans %}