-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: start can_allocate() implementation
- Loading branch information
1 parent
e97af2f
commit 91532f9
Showing
5 changed files
with
322 additions
and
10 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
Primary Python API for interacting with Assignment | ||
records and business logic. | ||
""" | ||
from django.db.models import Sum | ||
|
||
from .constants import LearnerContentAssignmentStateChoices | ||
from .models import LearnerContentAssignment | ||
|
||
|
||
def get_assignments_for_policy( | ||
subsidy_access_policy, | ||
state=LearnerContentAssignmentStateChoices.ALLOCATED, | ||
learner_emails=None, | ||
): | ||
""" | ||
Returns a queryset of all ``LearnerContentAssignment`` records | ||
for the given policy, optionally filtered to only those | ||
associated with the given ``learner_emails``. | ||
""" | ||
queryset = LearnerContentAssignment.objects.select_related( | ||
'assignment_policy', | ||
).filter( | ||
assignment_policy=subsidy_access_policy, | ||
state=state, | ||
) | ||
if learner_emails: | ||
queryset = queryset.filter( | ||
learner_email__in=learner_emails, | ||
) | ||
return queryset | ||
|
||
|
||
def get_allocated_quantity_for_policy(subsidy_access_policy): | ||
""" | ||
Returns a float representing the total quantity, in USD cents, currently allocated | ||
via Assignments for the given policy. | ||
""" | ||
assignments_queryset = get_assignments_for_policy(subsidy_access_policy) | ||
aggregate = assignments_queryset.aggregate( | ||
total_quantity=Sum('content_quantity'), | ||
) | ||
return aggregate['total_quantity'] |
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
Oops, something went wrong.