mirror of
https://github.com/lemeow125/DRF_Template.git
synced 2025-06-29 08:35:44 +08:00
Clean up docker-compose and run Black formatter over entire codebase
This commit is contained in:
parent
6c232b3e89
commit
069aba80b1
60 changed files with 1946 additions and 1485 deletions
|
@ -4,6 +4,7 @@ from .models import UserSubscription, StripePrice, SubscriptionPlan
|
|||
from django.core.cache import cache
|
||||
from config.settings import STRIPE_SECRET_KEY
|
||||
import stripe
|
||||
|
||||
stripe.api_key = STRIPE_SECRET_KEY
|
||||
|
||||
# Template for running actions after user have paid for a subscription
|
||||
|
@ -12,7 +13,7 @@ stripe.api_key = STRIPE_SECRET_KEY
|
|||
@receiver(post_save, sender=SubscriptionPlan)
|
||||
def clear_cache_after_plan_updates(sender, instance, **kwargs):
|
||||
# Clear cache
|
||||
cache.delete('subscriptionplans')
|
||||
cache.delete("subscriptionplans")
|
||||
|
||||
|
||||
@receiver(post_save, sender=UserSubscription)
|
||||
|
@ -25,8 +26,8 @@ def scan_after_payment(sender, instance, **kwargs):
|
|||
|
||||
@receiver(post_migrate)
|
||||
def create_subscriptions(sender, **kwargs):
|
||||
if sender.name == 'subscriptions':
|
||||
print('Importing data from Stripe')
|
||||
if sender.name == "subscriptions":
|
||||
print("Importing data from Stripe")
|
||||
created_prices = 0
|
||||
created_plans = 0
|
||||
skipped_prices = 0
|
||||
|
@ -35,16 +36,19 @@ def create_subscriptions(sender, **kwargs):
|
|||
prices = stripe.Price.list(expand=["data.tiers"], active=True)
|
||||
|
||||
# Create the StripePrice
|
||||
for price in prices['data']:
|
||||
annual = (price['recurring']['interval'] ==
|
||||
'year') if price['recurring'] else False
|
||||
for price in prices["data"]:
|
||||
annual = (
|
||||
(price["recurring"]["interval"] == "year")
|
||||
if price["recurring"]
|
||||
else False
|
||||
)
|
||||
STRIPE_PRICE, CREATED = StripePrice.objects.get_or_create(
|
||||
stripe_price_id=price['id'],
|
||||
price=price['unit_amount'] / 100,
|
||||
stripe_price_id=price["id"],
|
||||
price=price["unit_amount"] / 100,
|
||||
annual=annual,
|
||||
lookup_key=price['lookup_key'],
|
||||
prorated=price['recurring']['usage_type'] == 'metered',
|
||||
currency=price['currency']
|
||||
lookup_key=price["lookup_key"],
|
||||
prorated=price["recurring"]["usage_type"] == "metered",
|
||||
currency=price["currency"],
|
||||
)
|
||||
if CREATED:
|
||||
created_prices += 1
|
||||
|
@ -52,13 +56,13 @@ def create_subscriptions(sender, **kwargs):
|
|||
skipped_prices += 1
|
||||
|
||||
# Create the SubscriptionPlan
|
||||
for product in products['data']:
|
||||
for product in products["data"]:
|
||||
ANNUAL_PRICE = None
|
||||
MONTHLY_PRICE = None
|
||||
for price in prices['data']:
|
||||
if price['product'] == product['id']:
|
||||
for price in prices["data"]:
|
||||
if price["product"] == product["id"]:
|
||||
STRIPE_PRICE = StripePrice.objects.get(
|
||||
stripe_price_id=price['id'],
|
||||
stripe_price_id=price["id"],
|
||||
)
|
||||
if STRIPE_PRICE.annual:
|
||||
ANNUAL_PRICE = STRIPE_PRICE
|
||||
|
@ -66,12 +70,12 @@ def create_subscriptions(sender, **kwargs):
|
|||
MONTHLY_PRICE = STRIPE_PRICE
|
||||
if ANNUAL_PRICE or MONTHLY_PRICE:
|
||||
SUBSCRIPTION_PLAN, CREATED = SubscriptionPlan.objects.get_or_create(
|
||||
name=product['name'],
|
||||
description=product['description'],
|
||||
stripe_product_id=product['id'],
|
||||
name=product["name"],
|
||||
description=product["description"],
|
||||
stripe_product_id=product["id"],
|
||||
annual_price=ANNUAL_PRICE,
|
||||
monthly_price=MONTHLY_PRICE,
|
||||
group_exclusive=product['metadata']['group_exclusive'] == 'True'
|
||||
group_exclusive=product["metadata"]["group_exclusive"] == "True",
|
||||
)
|
||||
if CREATED:
|
||||
created_plans += 1
|
||||
|
@ -79,13 +83,12 @@ def create_subscriptions(sender, **kwargs):
|
|||
skipped_plans += 1
|
||||
# Skip over plans with missing pricing rates
|
||||
else:
|
||||
print('Skipping plan' +
|
||||
product['name'] + 'with missing pricing data')
|
||||
print("Skipping plan" + product["name"] + "with missing pricing data")
|
||||
|
||||
# Assign the StripePrice to the SubscriptionPlan
|
||||
SUBSCRIPTION_PLAN.save()
|
||||
|
||||
print('Created', created_plans, 'new plans')
|
||||
print('Skipped', skipped_plans, 'existing plans')
|
||||
print('Created', created_prices, 'new prices')
|
||||
print('Skipped', skipped_prices, 'existing prices')
|
||||
print("Created", created_plans, "new plans")
|
||||
print("Skipped", skipped_plans, "existing plans")
|
||||
print("Created", created_prices, "new prices")
|
||||
print("Skipped", skipped_prices, "existing prices")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue