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

Add support for paginating elasticsearch queries for django_tables2 (with HTMX) #35712

Merged
merged 3 commits into from
Feb 4, 2025

Conversation

biyeun
Copy link
Member

@biyeun biyeun commented Jan 30, 2025

Technical Summary

This adds support for paginating elasticsearch queries with django_tables2 with the introduction of the ElasticTable class that hooks into the ElasticTableData class that handles the ordering and pagination of the query based on the ElasticTable's record_class passed to it.

The first ElasticRecord implementation is for CaseSearchES with CaseSearchElasticRecord. From this example, you can see how easy it would be to extend this support for other queries, like FormES

See this commit for how we would create a simple HTMX + django_tables2 view with cases from CaseSearchES: badb29d

Safety Assurance

Safety story

There is no production workflow using these changes yet. The bit of code that was extracted from CaseListExplorer should be very easy to identify that it's safe without needing QA. As a sanity check I made sure CaseListExplorer sorted on @case_id with no issue.

Automated test coverage

I've added new tests.

QA Plan

Not needed

Rollback instructions

  • This PR can be reverted after deploy with no further considerations

Labels & Review

  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

this will be used in other areas to maintain consistency with case list explorer report sorting
this ElasticRecord will support only CaseSearchES for now, as that's what we will need in the near future for data cleaning cases
Copy link
Contributor

@MartinRiese MartinRiese left a comment

Choose a reason for hiding this comment

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

Love the documentation


@property
@abstractmethod
def verbose_name_plural(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps the default implementation could just add a 's' to the verbose_name.

Copy link
Member Author

Choose a reason for hiding this comment

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

i'm following requirements of the base models from django_tables2, and that's not always the case with translated text that plurals end in s, in english or other languages

Copy link
Contributor

Choose a reason for hiding this comment

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

Emphasis on default. But ok.

def __init__(self, **kwargs):
data = kwargs.pop('data')
if not ElasticTableData.validate(data):
raise ValueError(
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

if isinstance(original_exception, TransportError):
if hasattr(original_exception.info, "get"):
if original_exception.info.get('status') == 400:
raise BadRequestError()
Copy link
Contributor

Choose a reason for hiding this comment

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

Does TransportError have any other data in it about what went wrong? I don't like the idea of erasing that and potentially only logging a BadRequestError, which does not give you much to work with when investigating an issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

i'm following the pattern we use with CaseListExplorer for fetching results

Copy link
Contributor

Choose a reason for hiding this comment

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

That does not mean we cannot improve on it.

Copy link
Member Author

Choose a reason for hiding this comment

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

let's table this

if isinstance(original_exception, TransportError):
if hasattr(original_exception.info, "get"):
if original_exception.info.get('status') == 400:
raise BadRequestError()
Copy link
Contributor

Choose a reason for hiding this comment

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

That does not mean we cannot improve on it.

@biyeun biyeun merged commit d1177db into master Feb 4, 2025
14 checks passed
@biyeun biyeun deleted the bmb/htmx-djt-elasticsearch branch February 4, 2025 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product/invisible Change has no end-user visible impact
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants