Skip to content

Commit 7d796b3

Browse files
Merge pull request #357 from MuckRock/organization-collective-fixes
Some fixes for organization-collectives
2 parents 6d0903c + 8667234 commit 7d796b3

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

documentcloud/organizations/models.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,31 @@ def get_total_number_ai_credits(self):
249249
return number_ai_credits
250250

251251
def get_total_monthly_ai_credits(self):
252-
"""Get total monthly AI credits including parent and groups"""
252+
"""Get total monthly AI credits remaining including parent and groups"""
253253
monthly_ai_credits = self.monthly_ai_credits
254254
if self.parent and self.parent.share_resources:
255255
monthly_ai_credits += self.parent.monthly_ai_credits
256256
for group in self.groups.filter(share_resources=True):
257257
monthly_ai_credits += group.monthly_ai_credits
258258
return monthly_ai_credits
259259

260+
def get_total_monthly_ai_credits_allowance(self):
261+
"""
262+
Get the total monthly AI credits allowance, including parent and shared groups.
263+
This is the amount that monthly_credits will reset to each month.
264+
"""
265+
total = self.ai_credits_per_month
266+
267+
# Include parent if it shares resources
268+
if self.parent and self.parent.share_resources:
269+
total += self.parent.ai_credits_per_month
270+
271+
# Include groups that share resources
272+
for group in self.groups.filter(share_resources=True):
273+
total += group.ai_credits_per_month
274+
275+
return total
276+
260277

261278
class AICreditLog(models.Model):
262279
"""Log usage of AI Credits"""

documentcloud/organizations/serializers.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@ class OrganizationSerializer(serializers.ModelSerializer):
1919
"Only viewable by organization members."
2020
),
2121
)
22-
monthly_credits = serializers.IntegerField(
23-
source="monthly_ai_credits",
22+
monthly_credits = serializers.SerializerMethodField(
23+
label=_("Monthly Credits"),
2424
read_only=True,
2525
help_text=(
2626
"Number of monthly premium credits this organization has left. "
2727
"This will reset to monthly_credit_allowance on credit_reset_date. "
28+
"This includes shared credits from parents and groups. "
2829
"Only viewable be organization members."
2930
),
3031
)
31-
purchased_credits = serializers.IntegerField(
32-
source="number_ai_credits",
32+
purchased_credits = serializers.SerializerMethodField(
33+
label=_("Purchased Credits"),
3334
read_only=True,
3435
help_text=(
3536
"Number of purchased premium credits. "
3637
"These do not reset or expire. "
38+
"This includes shared credits from parents and groups. "
3739
"Only viewable by organization members."
3840
),
3941
)
@@ -45,8 +47,7 @@ class OrganizationSerializer(serializers.ModelSerializer):
4547
"Only viewable by organization members."
4648
),
4749
)
48-
monthly_credit_allowance = serializers.IntegerField(
49-
source="ai_credits_per_month",
50+
monthly_credit_allowance = serializers.SerializerMethodField(
5051
read_only=True,
5152
help_text=(
5253
"The amount of credits that monthly_credits will reset to. "
@@ -102,6 +103,15 @@ def get_plan(self, obj):
102103
else:
103104
return "Free"
104105

106+
def get_monthly_credits(self, obj):
107+
return obj.get_total_monthly_ai_credits()
108+
109+
def get_purchased_credits(self, obj):
110+
return obj.get_total_number_ai_credits()
111+
112+
def get_monthly_credit_allowance(self, obj):
113+
return obj.get_total_monthly_ai_credits_allowance()
114+
105115

106116
class AICreditSerializer(serializers.Serializer):
107117
"""Serializer for the AI credit endpoint"""

0 commit comments

Comments
 (0)