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
some stock optimizations #6838
Merged
Merged
some stock optimizations #6838
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,19 +45,33 @@ def state_stock_category(state): | |
def get_current_ledger_transactions(case_id): | ||
""" | ||
Given a case returns a dict of all current ledger data. | ||
{ | ||
"section_id": { | ||
"product_id": StockTransaction, | ||
"product_id": StockTransaction, | ||
... | ||
}, | ||
... | ||
} | ||
""" | ||
trans = get_current_ledger_transactions_multi([case_id]) | ||
return trans[case_id] | ||
from corehq.apps.commtrack.models import StockState | ||
results = StockState.objects.filter(case_id=case_id).values_list('case_id', 'section_id', 'product_id') | ||
|
||
ret = {} | ||
for case_id, section_id, product_id in results: | ||
sections = ret.setdefault(section_id, {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just for my own education, any reason to use this over There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No reason in this case. |
||
sections[product_id] = StockTransaction.latest(case_id, section_id, product_id) | ||
return ret | ||
|
||
|
||
def get_current_ledger_transactions_multi(case_ids): | ||
def get_current_ledger_state(case_ids): | ||
""" | ||
Given a list of cases returns a dict of all current ledger data of the following format: | ||
{ | ||
"case_id": { | ||
"section_id": { | ||
"product_id": StockTransaction, | ||
"product_id": StockTransaction, | ||
"product_id": StockState, | ||
"product_id": StockState, | ||
... | ||
}, | ||
... | ||
|
@@ -66,16 +80,16 @@ def get_current_ledger_transactions_multi(case_ids): | |
} | ||
Where you get one stock transaction per product/section which is the last one seen. | ||
""" | ||
from corehq.apps.commtrack.models import StockState | ||
if not case_ids: | ||
return {} | ||
|
||
results = StockTransaction.objects.filter( | ||
states = StockState.objects.filter( | ||
case_id__in=case_ids | ||
).values_list('case_id', 'section_id', 'product_id').distinct() | ||
|
||
) | ||
ret = {case_id: {} for case_id in case_ids} | ||
for case_id, section_id, product_id in results: | ||
sections = ret[case_id].setdefault(section_id, {}) | ||
sections[product_id] = StockTransaction.latest(case_id, section_id, product_id) | ||
for state in states: | ||
sections = ret[state.case_id].setdefault(state.section_id, {}) | ||
sections[state.product_id] = state | ||
|
||
return ret |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my guess is that we may be able to remove this method entirely and update the callers to use states too but that's definitely out of scope
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, makes sense. I think this is the only place that actually needs data which isn't in the
StockState
(it usestransaction.report.form_id
in the template): https://github.com/dimagi/commcare-hq/blob/master/corehq/ex-submodules/casexml/apps/case/templatetags/case_tags.py#L95