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

Refactor Grant Summary implementation in Admin #3654

Merged
merged 13 commits into from
Dec 30, 2023
Merged

Conversation

estyxx
Copy link
Member

@estyxx estyxx commented Dec 29, 2023

Why

This PR introduces a significant refactor of the Grant summary view in the Django admin panel. The previous implementation, which involved an auxiliary model GrantRecap and a convoluted override of the changelist_view, has been replaced with a cleaner, more maintainable, and extendable approach.

How to test it

Copy link

vercel bot commented Dec 29, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
pycon ✅ Ready (Inspect) Visit Preview Dec 30, 2023 3:05pm

Copy link

codecov bot commented Dec 29, 2023

Codecov Report

Merging #3654 (baf1f75) into main (3d0df50) will increase coverage by 0.02%.
Report is 3 commits behind head on main.
The diff coverage is n/a.

❗ Current head baf1f75 differs from pull request most recent head fe3e75e. Consider uploading reports for the commit fe3e75e to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3654      +/-   ##
==========================================
+ Coverage   90.16%   90.18%   +0.02%     
==========================================
  Files         254      254              
  Lines        6974     6969       -5     
  Branches     1028     1028              
==========================================
- Hits         6288     6285       -3     
+ Misses        610      609       -1     
+ Partials       76       75       -1     

statuses = Grant.Status.choices

# Apply filters to the Grant queryset
filter_params = request.GET.dict()
Copy link
Member

Choose a reason for hiding this comment

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

uhm this is probably fine because it is in admin, but I am not a big fan of just forwarding whatever is in the GET parameters to filter 🤔

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 moved this logic in _filter_and_format_grants to filter only by the allowed filters

<tbody>
{% for key, counts in summary.items %}
{% with continent=key.0 country=key.1 %}
{% ifchanged continent %}
Copy link
Member

Choose a reason for hiding this comment

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

💯

var checkboxes = document.querySelectorAll('.toggle-column');
checkboxes.forEach(function(checkbox) {
checkbox.addEventListener('change', function() {
var countryColumn = document.getElementById('grantsByCountry').querySelectorAll('td:nth-child(' + checkbox.dataset.column + '),th:nth-child(' + checkbox.dataset.column + ')');
Copy link
Member

Choose a reason for hiding this comment

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

why are you using both getElementById and querySelectorAll? can you not just use querySelectorAll?

Copy link
Member

Choose a reason for hiding this comment

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

or maybe using JS template strings would look better

cell.style.display = checkbox.checked ? '' : 'none';
});

var genderColumn = document.getElementById('grantsByGender').querySelectorAll('td:nth-child(' + checkbox.dataset.column + '),th:nth-child(' + checkbox.dataset.column + ')');
Copy link
Member

Choose a reason for hiding this comment

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

using nth-child feels very hacky. Could we maybe use data attributes?

What are you trying to do here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Add the option to show and hide status columns

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 changed to use the data-column


def _format_filters_for_display(self, filter_params):
"""
Formats raw filter keys to user-friendly names.
Copy link
Member

Choose a reason for hiding this comment

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

this comment doesn't look useful as it just explains what's the code after 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.

I changed this method and updated the description, let me know if now is more useful :)

</table>
<script>
document.addEventListener('DOMContentLoaded', function() {
// This script allows users to toggle the visibility of columns in the table
Copy link
Member

Choose a reason for hiding this comment

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

is this comment useful?

@marcoacierno
Copy link
Member

marcoacierno commented Dec 30, 2023

There are still various comments that explain what the code right below does, which I don't believe are helpful, but I will leave it to you to decide what you think makes sense to keep and what not.

@estyxx estyxx merged commit 3779e68 into main Dec 30, 2023
6 checks passed
@estyxx estyxx deleted the grants-recap-update branch December 30, 2023 17:28
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