-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
43 lines (35 loc) · 1.54 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
import json
import os
from webapp.apps.users.models import Project
from .models import SubscriptionItem, UsageRecord
USE_STRIPE = os.environ.get("USE_STRIPE", "false").lower() == "true"
class ChargeRunMixin:
"""
Add charge_run method to outputs view. This class makes it easy to test
the logic for charging users for model runs.
"""
def charge_run(self, sim, meta_dict, use_stripe=True):
sim.run_time = sum(meta_dict["task_times"])
sim.run_cost = sim.project.run_cost(sim.run_time)
if use_stripe and sim.project.pay_per_sim:
quantity = sim.project.run_cost(sim.run_time, adjust=True)
plan = sim.project.product.plans.get(usage_type="metered")
# The sponsor is also stored on the Simulation object. However, the
# Project object should be considered the single source of truth
# for sending usage records.
sponsor = sim.project.sponsor
if sponsor is not None:
customer = sponsor.user.customer
else:
customer = sim.owner.user.customer
si = SubscriptionItem.objects.get(
subscription__customer=customer, plan=plan
)
stripe_ur = UsageRecord.create_stripe_object(
quantity=Project.dollar_to_penny(quantity),
timestamp=None,
subscription_item=si,
)
UsageRecord.construct(stripe_ur, si)
def has_payment_method(user):
return hasattr(user, "customer") and user.customer is not None