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

🚧 Cleanup how we do bulk actions from list views #2873

Merged
merged 16 commits into from Jul 20, 2020
Merged

Conversation

rowanseymour
Copy link
Member

@rowanseymour rowanseymour commented Jul 9, 2020

Addresses rapidpro#1244. Current setup is pretty weird.. if you want to add new actions you need to add them to a mixin class in temba.utils which needs knowledge of how all the different model classes work, what permissions different actions can throw etc. It returns error like things but they get ignored... or sometimes just throws exceptions

@codecov
Copy link

codecov bot commented Jul 9, 2020

Codecov Report

Merging #2873 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##            master     #2873    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files          371       370     -1     
  Lines        25385     25247   -138     
==========================================
- Hits         25385     25247   -138     
Impacted Files Coverage Δ
temba/campaigns/models.py 100.00% <ø> (ø)
temba/triggers/models.py 100.00% <ø> (ø)
temba/campaigns/views.py 100.00% <100.00%> (ø)
temba/contacts/models.py 100.00% <100.00%> (ø)
temba/contacts/views.py 100.00% <100.00%> (ø)
temba/flows/models.py 100.00% <100.00%> (ø)
temba/flows/views.py 100.00% <100.00%> (ø)
temba/msgs/models.py 100.00% <100.00%> (ø)
temba/msgs/views.py 100.00% <100.00%> (ø)
temba/tickets/models.py 100.00% <100.00%> (ø)
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b4b53d0...e083421. Read the comment docs.

def apply_action_close(cls, tickets):
return cls.bulk_close(tickets[0].org, tickets)["changed_ids"]
def apply_action_close(cls, user, tickets):
cls.bulk_close(tickets[0].org, tickets)
Copy link
Member Author

Choose a reason for hiding this comment

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

the return value of these apply_ methods is never used - adding user to make the signatures consistent

fields = ("contact", "subject", "body", "opened_on")
select_related = ("ticketer", "contact")
default_order = ("-opened_on",)
bulk_actions = ()
Copy link
Member Author

Choose a reason for hiding this comment

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

using bulk_actions to differentiate between these and actions on a SmartCRUDL

if action_error:
print(action_error)

response["Temba-Toast"] = action_error
Copy link
Member Author

Choose a reason for hiding this comment

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

toast messages look weird and aren't used much in RP but the pjax fetch call is setup to use them so just trying to make the error handling consistent on the Python side and we can change how these errors are surfaced to the user later.


def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["actions"] = self.bulk_actions
Copy link
Member Author

Choose a reason for hiding this comment

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

should be renamed to bulk_actions.. but avoiding any changes to templates right now whilst the great refresh is underway

@rowanseymour rowanseymour marked this pull request as ready for review July 10, 2020 21:41
@@ -2415,24 +2415,6 @@ msgstr ""
msgid "Stopped"
msgstr ""

msgid "Add to Group"
Copy link
Member Author

Choose a reason for hiding this comment

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

these were on the action form classes but never surfaced in the templates

@@ -185,8 +185,6 @@ def apply_action_archive(cls, user, campaigns):
for campaign in campaigns:
EventFire.update_campaign_events(campaign)

return [each_campaign.pk for each_campaign in campaigns]
Copy link
Member Author

Choose a reason for hiding this comment

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

these return values were never used for anything

if action == "archive":
ignored = objects.filter(is_archived=False)
if ignored:
flow_names = " and ".join([f.name for f in ignored])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't seem we can do it this way for localization purposes. Maybe needs to be a list below the warning message?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah - this is just me copying the original text but we can definitely improve this

"""
Need to allow posts which are otherwise not allowed on list views
"""
return super().dispatch(*args, **kwargs)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can add POST but should disallow the other verbs here. (DELETE, etc..)

"""
Need to allow posts which are otherwise not allowed on list views
"""
return super().dispatch(*args, **kwargs)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you need this? Thought the post method below did what you needed 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.

oops turns out no

label = form.cleaned_data.get("label")

# convert objects queryset to one based only on id
objects = self.model._default_manager.filter(id__in=[o.id for o in objects])
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we filter by org generically 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.

these ids are coming from a queryset which is filtered by org - is the desire to filter by org again just to be doubly sure?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, because the list is derived from get_queryset? Hadn't noticed that, probably fine. That said, if everything that has actions on it does have an org_id then being extra paranoid doesn't sound like the worst idea in the world.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah so what you get from the form is the original list view's queryset + id filtering. This then just converts it to a new queryset that only has id filtering. I needed this because things the original queryset has additional filtering by attributes .. attributes which might be changed by the bulk action, and then objects disappear from the queryset

action_error = ", ".join(e.messages)
except Exception:
logger.exception(f"error applying '{action}' to {self.model.__name__} objects")
action_error = _("Oops, so sorry. Something went wrong!")
Copy link
Collaborator

Choose a reason for hiding this comment

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

An error occurred while making your changes. Please try again.

Copy link
Member Author

Choose a reason for hiding this comment

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

( another case of me reusing the original text )

@rowanseymour rowanseymour merged commit 406ea4b into master Jul 20, 2020
@rowanseymour rowanseymour deleted the list_actions branch July 20, 2020 19:38
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.

None yet

3 participants