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

Fix AttributeError 'Verificators' on model.verifiers call #128

Merged
merged 3 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
2.4.0 (unreleased)
------------------

- #128 Fix AttributeError 'Verificators' on model.verifiers call
- #127 Support textarea change events for report options


Expand Down
10 changes: 7 additions & 3 deletions src/senaite/impress/analysisrequest/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,14 @@ def verifiers(self):
"""Returns a list of user objects
"""
out = []
userids = reduce(lambda v1, v2: v1+v2,
map(lambda v: v.Verificators.split(","),
self.Analyses))
# extract the ids of the verifiers from all analyses
userids = [analysis.getVerificators() for analysis in self.Analyses]
# flatten the list
userids = list(itertools.chain.from_iterable(userids))
# get the users
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I think it would make more sense to have here some nested for loops instead of iterating multiple times through the list.

I think on something like this (untested):

users = []
for analysis in self.Analyses:
    for verificators in analysis.getVerificators():
        for userid in verificators:
            if not userid:
                continue
            user = api.get_user(userid)
            if not user:
                continue
            if user in users:
                continue
            users.append(user)

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

IMO three nested fors are uglier than this "flatten" approach. Note that you are iterating through the list of users inside the loop each time you get a user, while I simply remove duplicates of userids before looping.

I think that since we don't expect these lists of verifiers to be huge (in most cases, 1 per sample and most samples will share same verifier), the performance impact caused by iterating three times is negligible. I can remove the filter(None, userids) and perform the check inside the loop though.

for userid in set(userids):
if not userid:
continue
user = api.get_user(userid)
if user is None:
logger.warn("Could not find user '{}'".format(userid))
Expand Down