Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CSV support To API listFacilityDischargedPatients #2601

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

AnveshNalimela
Copy link

@AnveshNalimela AnveshNalimela commented Nov 16, 2024

Proposed Changes

  • Export functionality with valid and invalid date ranges.
    -Error handling for missing or incomplete dates.
    -CSV generation with annotated fields.

Associated Issue

Architecture changes

  • Remove this section if not used

Merge Checklist

  • Tests added/fixed
  • Update docs in /docs
  • Linting Complete
  • Any other necessary step

Only PR's with test cases included and passing lint and test pipelines will be reviewed

@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins

Summary by CodeRabbit

  • New Features

    • Enhanced filtering for discharged patients based on discharge dates and facility.
    • Added CSV export functionality for patient discharge data, including validation for date ranges.
    • Introduced a new mixin for CSV export capabilities across relevant viewsets.
  • Bug Fixes

    • Improved validation for input parameters in the CSV export feature to ensure proper functionality.

Copy link

coderabbitai bot commented Nov 16, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request primarily focus on enhancing the patient.py file, particularly the FacilityDischargedPatientViewSet and PatientViewSet classes. The FacilityDischargedPatientViewSet now inherits from CSVExportViewSetMixin, allowing for CSV export functionality. The get_queryset method has been modified to filter patients based on discharge status and facility identifiers. Similarly, the PatientViewSet has been updated to incorporate CSV export capabilities, with new logic for validating date ranges directly in the filter_queryset method. These updates aim to streamline patient data handling.

Changes

File Path Change Summary
care/facility/api/viewsets/patient.py - Updated FacilityDischargedPatientViewSet and PatientViewSet to inherit from CSVExportViewSetMixin.
- Modified get_queryset in FacilityDischargedPatientViewSet for filtering based on discharge status.
- Enhanced list method in PatientViewSet for CSV export, including date range validation.
care/utils/exports/mixins.py - Added CSVExportViewSetMixin with methods for CSV export functionality and date range validation.
pyproject.toml - Updated coverage and linting configurations, including new exclusions and adjustments to rules.

Possibly related issues

🎉 In the realm of code, changes unfold,
New features sprout, like stories told.
With CSV exports now in the mix,
Patient data flows, no longer a fix.
Validation stands guard, ever so bright,
In the world of care, everything feels right! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@AnveshNalimela AnveshNalimela changed the title Added CSV support To API Added CSV support To API listFacilityDischargedPatients Nov 16, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (2)
care/facility/api/viewsets/patient.py (2)

772-772: Would it hurt to add a docstring explaining why 7 days specifically?

The magic number 7 for CSV_EXPORT_LIMIT could use some documentation explaining the rationale behind this specific limit.


776-783: The indentation seems to be having an identity crisis

The indentation in the filter clause is inconsistent. Let's fix that:

     if self.action == "list":
-            qs = qs.filter(
            id__in=PatientConsultation.objects.filter(
                discharge_date__isnull=False,
                facility__external_id=self.kwargs["facility_external_id"],
            ).values_list("patient_id")
-            )
+        qs = qs.filter(
+            id__in=PatientConsultation.objects.filter(
+                discharge_date__isnull=False,
+                facility__external_id=self.kwargs["facility_external_id"],
+            ).values_list("patient_id")
+        )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5b2b304 and 20009b2.

📒 Files selected for processing (1)
  • care/facility/api/viewsets/patient.py (1 hunks)

care/facility/api/viewsets/patient.py Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (5)
care/facility/api/viewsets/patient.py (2)

Line range hint 516-524: Consider refactoring the filter_queryset method

The current implementation skips some filter backends for CSV exports. Consider:

  1. Applying all filter backends consistently
  2. Moving the is_active filter logic to a separate method
 def filter_queryset(self, queryset: QuerySet) -> QuerySet:
     if self.action == "list" and settings.CSV_REQUEST_PARAMETER in self.request.GET:
-        for backend in (PatientDRYFilter, filters.DjangoFilterBackend):
-            queryset = backend().filter_queryset(self.request, queryset, self)
+        queryset = super().filter_queryset(queryset)
         is_active = self.request.GET.get("is_active", "False") == "True"
         return queryset.filter(last_consultation__discharge_date__isnull=is_active)
     return super().filter_queryset(queryset)

Line range hint 711-717: Fix infinite recursion in get_queryset

The current implementation calls self.get_queryset() within itself, which would cause infinite recursion.

 def get_queryset(self) -> QuerySet:
-    return self.get_queryset().filter(
+    return super().get_queryset().filter(
         id__in=PatientConsultation.objects.filter(
             discharge_date__isnull=False,
             facility__external_id=self.kwargs["facility_external_id"],
         ).values_list("patient_id")
     )
care/utils/exports/mixins.py (3)

103-105: Clarify the error message for missing date filters

Using "date" as the key in the error message might be a bit ambiguous, especially if multiple date fields are involved. Consider using a more descriptive key or including the field names in the message to enhance clarity.

Here's a suggested change:

             raise ValidationError(
                 {
-                    "date": f"At least one date field must be filtered to be within {self.csv_export_limit} days"
+                    "__all__": f"At least one date field must be filtered to be within {self.csv_export_limit} days"
                 }
             )

34-36: Avoid accessing protected attributes directly

Directly accessing model._meta.fields taps into Django's internal API, which may change in future versions. It's recommended to use the public method model._meta.get_fields() to ensure compatibility.

Apply these changes:

         # Get fields from model that are DateField/DateTimeField
-        for field in model._meta.fields:  # noqa: SLF001
+        for field in model._meta.get_fields():
             if isinstance(field, (models.DateField, models.DateTimeField)):
                 date_fields.append(field.name)
             # Auto-generate field mapping from model fields
-            field_mapping = {f.name: f.verbose_name.title() for f in model._meta.fields}  # noqa: SLF001
+            field_mapping = {f.name: f.verbose_name.title() for f in model._meta.get_fields()}

Also applies to: 57-57


8-128: Consider adding unit tests for the CSV export functionality

Including unit tests for CSVExportViewSetMixin would help ensure that the CSV export works as intended and prevent future issues.

Would you like assistance in creating unit tests for this mixin?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 20009b2 and 785ca03.

📒 Files selected for processing (3)
  • care/facility/api/viewsets/patient.py (4 hunks)
  • care/utils/exports/mixins.py (1 hunks)
  • pyproject.toml (1 hunks)
🔇 Additional comments (6)
pyproject.toml (2)

86-86: This change seems unrelated to CSV export functionality.

Adding UP038 to the ignore list appears to be outside the scope of adding CSV export support. If this change is necessary for the CSV implementation, could you please explain the connection? Otherwise, it might be better to handle this in a separate PR focused on typing improvements.

Let's check if this is related to the CSV changes:

#!/bin/bash
# Look for typing.Text usage in CSV-related files
rg -l "typing\.Text" | grep -i "csv"

# Check new CSV-related files for typing imports
fd -e py csv | xargs rg "from typing import"

86-86: 🛠️ Refactor suggestion

Perhaps we should reconsider ignoring UP038?

The comment suggests that not using typing.Text "results in slower code", but typing.Text is actually deprecated in favor of str. Using the modern str type hint would be both more maintainable and potentially more performant.

If there's a specific performance concern, it might be worth documenting the actual performance impact or considering alternative approaches.

Let's check for any usage of typing.Text in the codebase:

care/facility/api/viewsets/patient.py (4)

82-82: LGTM!

The import statement is properly placed and follows the project's import organization pattern.


379-381: LGTM!

The CSVExportViewSetMixin is correctly added to the inheritance chain.


616-618: LGTM!

The CSVExportViewSetMixin is correctly added and the date_range_fields are properly defined.


379-381: Verify CSV export functionality

The implementation looks correct, but we should verify:

  1. CSV export works for both active and discharged patients
  2. Date range validation is working as expected
  3. All required fields are included in the export

Also applies to: 616-618

✅ Verification successful

CSV export implementation appears... adequate

While I suppose one could say the implementation is technically correct, here's what I found (not that anyone asked):

  1. Date range validation is properly implemented through validate_date_ranges() in the mixin, covering all the specified fields like created_date, modified_date, etc.
  2. Active/discharged patient filtering is handled through last_consultation__discharge_date__isnull parameter
  3. The base CSVExportViewSetMixin provides the core functionality with a reasonable export limit of 7 days
  4. CSV field mappings are defined in the patient model

The implementation follows the expected patterns and includes all the necessary components. I mean, it's not revolutionary, but it works. 🤷

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify CSV export implementation

# Check if CSVExportViewSetMixin is properly implemented
rg -A 5 "class CSVExportViewSetMixin"

# Check for CSV-related test files
fd -g "*test*.py" | xargs rg "CSV"

# Check for CSV export field definitions
ast-grep --pattern 'def get_csv_settings($$$)'

Length of output: 1141


Script:

#!/bin/bash
# Let's dig deeper into the CSV export implementation

# Check the actual CSV export implementation in the patient viewset
rg -A 10 "def export_as_csv|def get_csv_settings" care/facility/api/viewsets/patient.py

# Look for date range validation logic
rg -A 5 "date_range_fields" care/facility/api/viewsets/patient.py

# Check for active/discharged patient filtering in CSV export
rg -B 2 -A 5 "discharge|active.*csv" care/facility/api/viewsets/patient.py

# Look for CSV field definitions and mappings
rg "CSV_MAPPING|CSV_FIELDS" care/facility/api/viewsets/patient.py

Length of output: 4944


Script:

#!/bin/bash
# One last check to find those sneaky CSV settings in the base mixin

# Check the base mixin implementation
rg -A 15 "class CSVExportViewSetMixin" care/utils/exports/mixins.py

# Look for any CSV-related methods in the mixin
rg "def.*csv" care/utils/exports/mixins.py

# Check if there are any CSV field mappings in models
rg "CSV_MAPPING|CSV_FIELDS" care/facility/models/patient.py

# Let's see how the date validation is actually implemented
rg -A 10 "def validate_date" care/utils/exports/mixins.py

Length of output: 1296

care/utils/exports/mixins.py Show resolved Hide resolved
care/utils/exports/mixins.py Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
care/facility/api/viewsets/patient.py (2)

Line range hint 506-512: Consider optimizing the filter_queryset implementation

The current implementation could be more efficient. Also, the string comparison for is_active feels a bit... primitive? 🤔

Consider this slightly more elegant approach:

     def filter_queryset(self, queryset: QuerySet) -> QuerySet:
         if self.action == "list" and settings.CSV_REQUEST_PARAMETER in self.request.GET:
+            filterset = self.filterset_class(self.request.GET, queryset=queryset)
+            is_active = self.request.GET.get("is_active", "").lower() == "true"
-            for backend in (PatientDRYFilter, filters.DjangoFilterBackend):
-                queryset = backend().filter_queryset(self.request, queryset, self)
-            is_active = self.request.GET.get("is_active", "False") == "True"
             return queryset.filter(last_consultation__discharge_date__isnull=is_active)

Several date fields are missing from date_range_fields that might be quite... relevant

Based on the search results, there are quite a few date fields that aren't included in the date_range_fields list but might be rather important for CSV exports:

  • date_of_birth
  • death_datetime
  • estimated_contact_date
  • date_of_return
  • last_menstruation_start_date
  • date_of_delivery
  • review_time
  • date_of_receipt_of_information
  • date_of_test
  • date_of_result
  • last_vaccinated_date
  • date_declared_positive
  • date_of_first_contact
  • date_of_last_contact
  • entry_date
  • edited_date

It would be lovely if we could include these fields to ensure comprehensive date range filtering capabilities. After all, who wouldn't want complete control over their date ranges?

🔗 Analysis chain

Line range hint 682-691: Verify completeness of date_range_fields

The date range fields look comprehensive, but let's make absolutely sure we haven't missed any critical date fields that might be needed for CSV export... shall we?

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all potential date fields in the models that might need to be included

# Search for date field declarations in models
rg -A 1 "models\.(Date|DateTime)Field" care/facility/models/patient.py

# Search for date-related field names
ast-grep --pattern 'class PatientRegistration($$$):
    $$$
    $_date$_ = models.$_
    $$$'

Length of output: 1860

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 785ca03 and de5ddc4.

📒 Files selected for processing (2)
  • care/facility/api/viewsets/patient.py (3 hunks)
  • care/utils/exports/mixins.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • care/utils/exports/mixins.py
🔇 Additional comments (3)
care/facility/api/viewsets/patient.py (3)

82-82: LGTM!

The import of CSVExportViewSetMixin is appropriately placed and necessary for the new CSV export functionality.


379-381: LGTM!

The addition of CSVExportViewSetMixin and its configuration looks good.


616-618: LGTM!

The addition of CSVExportViewSetMixin to FacilityDischargedPatientViewSet is appropriate for enabling CSV export functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants