Skip to content

Managers

dj-stripe model managers

Classes

djstripe.managers.ChargeManager

Bases: Manager

Manager used by models.Charge.

Source code in djstripe/managers.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
class ChargeManager(models.Manager):
    """Manager used by models.Charge."""

    def during(self, year, month):
        """Return Charges between a certain time range based on `created`."""
        return self.filter(created__year=year, created__month=month)

    def paid_totals_for(self, year, month):
        """
        Return paid Charges during a certain year, month with total amount,
        fee and refunded annotated.
        """
        return (
            self.during(year, month)
            .filter(paid=True)
            .aggregate(
                total_amount=models.Sum("amount"),
                total_refunded=models.Sum("amount_refunded"),
            )
        )

Functions

djstripe.managers.ChargeManager.during(year, month)

Return Charges between a certain time range based on created.

Source code in djstripe/managers.py
86
87
88
def during(self, year, month):
    """Return Charges between a certain time range based on `created`."""
    return self.filter(created__year=year, created__month=month)
djstripe.managers.ChargeManager.paid_totals_for(year, month)

Return paid Charges during a certain year, month with total amount, fee and refunded annotated.

Source code in djstripe/managers.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def paid_totals_for(self, year, month):
    """
    Return paid Charges during a certain year, month with total amount,
    fee and refunded annotated.
    """
    return (
        self.during(year, month)
        .filter(paid=True)
        .aggregate(
            total_amount=models.Sum("amount"),
            total_refunded=models.Sum("amount_refunded"),
        )
    )

djstripe.managers.StripeModelManager

Bases: Manager

Manager used in StripeModel.

Source code in djstripe/managers.py
 9
10
11
12
class StripeModelManager(models.Manager):
    """Manager used in StripeModel."""

    pass

djstripe.managers.SubscriptionManager

Bases: Manager

Manager used in models.Subscription.

Source code in djstripe/managers.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class SubscriptionManager(models.Manager):
    """Manager used in models.Subscription."""

    def started_during(self, year, month):
        """Return Subscriptions not in trial status between a certain time range."""
        return self.exclude(status="trialing").filter(
            start_date__year=year, start_date__month=month
        )

    def active(self):
        """Return active Subscriptions."""
        return self.filter(status="active")

    def canceled(self):
        """Return canceled Subscriptions."""
        return self.filter(status="canceled")

    def canceled_during(self, year, month):
        """Return Subscriptions canceled during a certain time range."""
        return self.canceled().filter(canceled_at__year=year, canceled_at__month=month)

    def started_plan_summary_for(self, year, month):
        """Return started_during Subscriptions with plan counts annotated."""
        return (
            self.started_during(year, month)
            .values("plan")
            .order_by()
            .annotate(count=models.Count("plan"))
        )

    def active_plan_summary(self):
        """Return active Subscriptions with plan counts annotated."""
        return (
            self.active().values("plan").order_by().annotate(count=models.Count("plan"))
        )

    def canceled_plan_summary_for(self, year, month):
        """
        Return Subscriptions canceled within a time range with plan counts annotated.
        """
        return (
            self.canceled_during(year, month)
            .values("plan")
            .order_by()
            .annotate(count=models.Count("plan"))
        )

    def churn(self):
        """Return number of canceled Subscriptions divided by active Subscriptions."""
        canceled = self.canceled().count()
        active = self.active().count()
        return decimal.Decimal(str(canceled)) / decimal.Decimal(str(active))

Functions

djstripe.managers.SubscriptionManager.active()

Return active Subscriptions.

Source code in djstripe/managers.py
24
25
26
def active(self):
    """Return active Subscriptions."""
    return self.filter(status="active")
djstripe.managers.SubscriptionManager.active_plan_summary()

Return active Subscriptions with plan counts annotated.

Source code in djstripe/managers.py
45
46
47
48
49
def active_plan_summary(self):
    """Return active Subscriptions with plan counts annotated."""
    return (
        self.active().values("plan").order_by().annotate(count=models.Count("plan"))
    )
djstripe.managers.SubscriptionManager.canceled()

Return canceled Subscriptions.

Source code in djstripe/managers.py
28
29
30
def canceled(self):
    """Return canceled Subscriptions."""
    return self.filter(status="canceled")
djstripe.managers.SubscriptionManager.canceled_during(year, month)

Return Subscriptions canceled during a certain time range.

Source code in djstripe/managers.py
32
33
34
def canceled_during(self, year, month):
    """Return Subscriptions canceled during a certain time range."""
    return self.canceled().filter(canceled_at__year=year, canceled_at__month=month)
djstripe.managers.SubscriptionManager.canceled_plan_summary_for(year, month)

Return Subscriptions canceled within a time range with plan counts annotated.

Source code in djstripe/managers.py
51
52
53
54
55
56
57
58
59
60
def canceled_plan_summary_for(self, year, month):
    """
    Return Subscriptions canceled within a time range with plan counts annotated.
    """
    return (
        self.canceled_during(year, month)
        .values("plan")
        .order_by()
        .annotate(count=models.Count("plan"))
    )
djstripe.managers.SubscriptionManager.churn()

Return number of canceled Subscriptions divided by active Subscriptions.

Source code in djstripe/managers.py
62
63
64
65
66
def churn(self):
    """Return number of canceled Subscriptions divided by active Subscriptions."""
    canceled = self.canceled().count()
    active = self.active().count()
    return decimal.Decimal(str(canceled)) / decimal.Decimal(str(active))
djstripe.managers.SubscriptionManager.started_during(year, month)

Return Subscriptions not in trial status between a certain time range.

Source code in djstripe/managers.py
18
19
20
21
22
def started_during(self, year, month):
    """Return Subscriptions not in trial status between a certain time range."""
    return self.exclude(status="trialing").filter(
        start_date__year=year, start_date__month=month
    )
djstripe.managers.SubscriptionManager.started_plan_summary_for(year, month)

Return started_during Subscriptions with plan counts annotated.

Source code in djstripe/managers.py
36
37
38
39
40
41
42
43
def started_plan_summary_for(self, year, month):
    """Return started_during Subscriptions with plan counts annotated."""
    return (
        self.started_during(year, month)
        .values("plan")
        .order_by()
        .annotate(count=models.Count("plan"))
    )

djstripe.managers.TransferManager

Bases: Manager

Manager used by models.Transfer.

Source code in djstripe/managers.py
69
70
71
72
73
74
75
76
77
78
79
80
class TransferManager(models.Manager):
    """Manager used by models.Transfer."""

    def during(self, year, month):
        """Return Transfers between a certain time range."""
        return self.filter(created__year=year, created__month=month)

    def paid_totals_for(self, year, month):
        """
        Return paid Transfers during a certain year, month with total amounts annotated.
        """
        return self.during(year, month).aggregate(total_amount=models.Sum("amount"))

Functions

djstripe.managers.TransferManager.during(year, month)

Return Transfers between a certain time range.

Source code in djstripe/managers.py
72
73
74
def during(self, year, month):
    """Return Transfers between a certain time range."""
    return self.filter(created__year=year, created__month=month)
djstripe.managers.TransferManager.paid_totals_for(year, month)

Return paid Transfers during a certain year, month with total amounts annotated.

Source code in djstripe/managers.py
76
77
78
79
80
def paid_totals_for(self, year, month):
    """
    Return paid Transfers during a certain year, month with total amounts annotated.
    """
    return self.during(year, month).aggregate(total_amount=models.Sum("amount"))