Skip to content

Commit

Permalink
Merge pull request #478 from commonknowledge/adjust-slow-middleware-s…
Browse files Browse the repository at this point in the history
…yncing-subs

Run Stripe subscription syncing middleware only on specific pages
  • Loading branch information
Moggach authored Sep 4, 2024
2 parents 2aad3d5 + 1909e02 commit 46440ec
Show file tree
Hide file tree
Showing 17 changed files with 1,001 additions and 998 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ name: pull-request
on:
pull_request:
branches:
- main
- production
jobs:
lint-and-test:
runs-on: ubuntu-latest
container:
image: ghcr.io/commonknowledge/do-app-baseimage-django-node:364385f9d196a2bbe2d5faea025520cc0316501f
image: ghcr.io/commonknowledge/do-app-baseimage-django-node:6afc34140c9df175f6df73e9d7450dae48050eaf
# Workaround for: https://github.com/actions/checkout/issues/211
options: --user 1001
volumes:
Expand Down
20 changes: 6 additions & 14 deletions app/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,14 @@ def update_stripe_customer_subscription(get_response):
# One-time configuration and initialization.

def middleware(request):
stripeless_request = (
request.path.startswith(settings.STATIC_URL)
or request.path.startswith(settings.MEDIA_URL)
or request.path.startswith("/anonymous/")
or settings.SHOPIFY_WEBHOOK_PATH in request.path
or settings.SHOPIFY_WEBHOOK_PATH == request.path
or request.path.startswith("/admin/")
or request.path.startswith("/django/")
or request.path.startswith("/oauth/")
or request.path.startswith("/documents/")
or request.path.startswith("/silk/")
or request.path.startswith("/__debug__/")
or "favicon.ico" in request.path
membership_request = (
"checkout/success" in request.path
or "accounts/cancel" in request.path
or "gift/redeemed" in request.path
or "update-membership/success" in request.path
)

if not stripeless_request:
if membership_request:
# Code to be executed for each request before
# the view (and later middleware) are called.
if request.user.is_authenticated:
Expand Down
32 changes: 19 additions & 13 deletions app/migrations/0065_alter_upsellplansettings_options_and_more.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
# Generated by Django 4.0.8 on 2023-03-21 12:49

import django.core.validators
from django.db import migrations, models
import wagtail.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('app', '0064_reviewfeesettings_donation_text_and_more'),
("app", "0064_reviewfeesettings_donation_text_and_more"),
]

operations = [
migrations.AlterModelOptions(
name='upsellplansettings',
options={'verbose_name': 'Review Fee Settings'},
name="upsellplansettings",
options={"verbose_name": "Review Fee Settings"},
),
migrations.AddField(
model_name='upsellplansettings',
name='upgrade_membership_text',
field=wagtail.fields.RichTextField(default='\n <p>Here is some text that can be edited blah blah blah</p>\n '),
model_name="upsellplansettings",
name="upgrade_membership_text",
field=wagtail.fields.RichTextField(
default="\n <p>Here is some text that can be edited blah blah blah</p>\n "
),
),
migrations.AlterField(
model_name='membershipplanpage',
name='deliveries_per_year',
field=models.PositiveIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0)]),
model_name="membershipplanpage",
name="deliveries_per_year",
field=models.PositiveIntegerField(
default=0, validators=[django.core.validators.MinValueValidator(0)]
),
),
migrations.AlterField(
model_name='upsellplansettings',
name='intro_text',
field=wagtail.fields.RichTextField(default='\n <h1>Review your fee</h1>\n <p>Since you signed up, our operating costs have increased dramatically due to the economic times we’re all living through.</p>\n <p>We’ve increased the price of new memberships and protected your fee, but we are beginning to struggle. Can you afford to increase your membership fee?</p>\n '),
model_name="upsellplansettings",
name="intro_text",
field=wagtail.fields.RichTextField(
default="\n <h1>Review your fee</h1>\n <p>Since you signed up, our operating costs have increased dramatically due to the economic times we’re all living through.</p>\n <p>We’ve increased the price of new memberships and protected your fee, but we are beginning to struggle. Can you afford to increase your membership fee?</p>\n "
),
),
]
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# Generated by Django 4.0.8 on 2023-07-18 14:33

from django.db import migrations
import wagtail.fields
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('app', '0065_alter_upsellplansettings_options_and_more'),
("app", "0065_alter_upsellplansettings_options_and_more"),
]

operations = [
migrations.AlterField(
model_name='upsellplansettings',
name='upgrade_membership_text',
field=wagtail.fields.RichTextField(default='\n <p>You’re currently paying {{ old_price }}. Select this option if it’s all you can afford right now — that is totally OK.</p>\n <p>Other members paying solidarity rates will make it possible for us to continue offering this, so please consider if you can afford to increase your rate or if you genuinely need to stay here.</p>\n '),
model_name="upsellplansettings",
name="upgrade_membership_text",
field=wagtail.fields.RichTextField(
default="\n <p>You’re currently paying {{ old_price }}. Select this option if it’s all you can afford right now — that is totally OK.</p>\n <p>Other members paying solidarity rates will make it possible for us to continue offering this, so please consider if you can afford to increase your rate or if you genuinely need to stay here.</p>\n "
),
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
class Migration(migrations.Migration):

dependencies = [
('app', '0066_alter_upsellplansettings_upgrade_membership_text'),
("app", "0066_alter_upsellplansettings_upgrade_membership_text"),
]

operations = [
migrations.RemoveField(
model_name='bookpage',
name='authors',
model_name="bookpage",
name="authors",
),
migrations.RemoveField(
model_name='bookpage',
name='forward_by',
model_name="bookpage",
name="forward_by",
),
migrations.RemoveField(
model_name='bookpage',
name='image_urls',
model_name="bookpage",
name="image_urls",
),
migrations.RemoveField(
model_name='merchandisepage',
name='image_urls',
model_name="merchandisepage",
name="image_urls",
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,48 @@
class Migration(migrations.Migration):

dependencies = [
('app', '0067_remove_bookpage_authors_remove_bookpage_forward_by_and_more'),
("app", "0067_remove_bookpage_authors_remove_bookpage_forward_by_and_more"),
]

operations = [
migrations.AddField(
model_name='bookpage',
name='authors',
field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(blank=True, max_length=300), blank=True, null=True, size=None),
model_name="bookpage",
name="authors",
field=django.contrib.postgres.fields.ArrayField(
base_field=models.CharField(blank=True, max_length=300),
blank=True,
null=True,
size=None,
),
),
migrations.AddField(
model_name='bookpage',
name='forward_by',
field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(blank=True, max_length=300), blank=True, null=True, size=None),
model_name="bookpage",
name="forward_by",
field=django.contrib.postgres.fields.ArrayField(
base_field=models.CharField(blank=True, max_length=300),
blank=True,
null=True,
size=None,
),
),
migrations.AddField(
model_name='bookpage',
name='image_urls',
field=django.contrib.postgres.fields.ArrayField(base_field=models.URLField(blank=True, max_length=500), blank=True, null=True, size=None),
model_name="bookpage",
name="image_urls",
field=django.contrib.postgres.fields.ArrayField(
base_field=models.URLField(blank=True, max_length=500),
blank=True,
null=True,
size=None,
),
),
migrations.AddField(
model_name='merchandisepage',
name='image_urls',
field=django.contrib.postgres.fields.ArrayField(base_field=models.URLField(blank=True, max_length=500), blank=True, null=True, size=None),
model_name="merchandisepage",
name="image_urls",
field=django.contrib.postgres.fields.ArrayField(
base_field=models.URLField(blank=True, max_length=500),
blank=True,
null=True,
size=None,
),
),
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 4.2 on 2024-05-16 11:59

from django.db import migrations
import djmoney.models.fields
from django.db import migrations


class Migration(migrations.Migration):
Expand Down
2 changes: 1 addition & 1 deletion app/models/wagtail.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ def sync_from_shopify_product_id(cls, shopify_product_id):
product = shopify.Product.find(shopify_product_id)
metafields = product.metafields()
metafields = metafields_to_dict(metafields)

if cls.objects.filter(shopify_product_id=shopify_product_id).exists():
return cls.update_instance_for_product(product, metafields)
else:
Expand Down
8 changes: 4 additions & 4 deletions app/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"mathfilters",
"djmoney",
"anymail",
'wagtail_rangefilter',
'rangefilter',
"wagtail_rangefilter",
"rangefilter",
"rest_framework",
"groundwork.core",
"groundwork.geo",
Expand Down Expand Up @@ -87,8 +87,8 @@
]

MIDDLEWARE += [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
Expand Down
4 changes: 2 additions & 2 deletions app/templates/account/email.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
{% for emailaddress in user.emailaddress_set.all %}
<tr>
<td>
<label class="radio"
for="email_radio_{{ forloop.counter }}"
<label
for="radio email_radio_{{ forloop.counter }}"
class="{% if emailaddress.primary %}primary_email{% endif %}">
<input id="email_radio_{{ forloop.counter }}"
type="radio"
Expand Down
5 changes: 3 additions & 2 deletions app/templatetags/books.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def get_books(since=None, types=None, limit=None):
def get_merch():
return MerchandisePage.objects.live().public()

@register.filter(name='strikethrough')

@register.filter(name="strikethrough")
def strikethrough(text):
"""Convert text to strikethrough using Unicode characters."""
return ''.join(char + '\u0336' for char in str(text))
return "".join(char + "\u0336" for char in str(text))
10 changes: 8 additions & 2 deletions app/utils/books.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ def get_current_book(book_types: Union[None, str, List[str]]):

if len(book_types) > 0 and "all-books" not in book_types:
return (
BookPage.objects.filter(type__in=book_types, published_date__isnull=False)
BookPage.objects.filter(
type__in=book_types, published_date__isnull=False
)
.order_by("-published_date")
.first()
)
return BookPage.objects.filter(published_date__isnull=False).order_by("-published_date").first()
return (
BookPage.objects.filter(published_date__isnull=False)
.order_by("-published_date")
.first()
)
4 changes: 3 additions & 1 deletion app/utils/mailchimp.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def mailchimp_contact_for_user(user: User, list_id=settings.MAILCHIMP_LIST_ID):
{
"email_address": user.primary_email,
"merge_fields": {"FNAME": user.first_name, "LNAME": user.last_name},
"status": "subscribed" if user.gdpr_email_consent else "unsubscribed",
"status": "subscribed"
if user.gdpr_email_consent
else "unsubscribed",
},
)
member = apply_gdpr_consent(member)
Expand Down
14 changes: 7 additions & 7 deletions app/utils/stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,15 @@ def create_gift_recipient_subscription(
promo_code_id, expand=["coupon.applies_to"]
)
if (
hasattr(promo_code.coupon, 'applies_to') and
hasattr(promo_code.coupon.applies_to, 'products') and
product_id in promo_code.coupon.applies_to.products
): # the gift card is fit for purpose
hasattr(promo_code.coupon, "applies_to")
and hasattr(promo_code.coupon.applies_to, "products")
and product_id in promo_code.coupon.applies_to.products
): # the gift card is fit for purpose
discount_args = {"promotion_code": promo_code_id}

else:
# if they're in this situation of the gift card being for an old product
# get the right coupon for the target product
# if they're in this situation of the gift card being for an old product
# get the right coupon for the target product
coupon = get_gift_card_coupon(product_id)
# invalidate the gift card's promo code so it can't be used again
promo_code = stripe.PromotionCode.modify(promo_code_id, active=False)
Expand Down
4 changes: 0 additions & 4 deletions app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from sentry_sdk import capture_exception, capture_message
from wagtail.models import Page


from app import analytics
from app.forms import (
CountrySelectorForm,
Expand Down Expand Up @@ -588,7 +587,6 @@ def get_context_data(self, price_id, product_id, country_id="GB", **kwargs):
}

return context



class SubscriptionCheckoutView(TemplateView):
Expand Down Expand Up @@ -633,7 +631,6 @@ def create_checkout_context(

checkout_args["shipping_address_collection"] = {
"allowed_countries": ShippingZone.all_country_codes

}
next = reverse_lazy("completed_gift_purchase")
else:
Expand Down Expand Up @@ -1299,7 +1296,6 @@ def create_checkout_context(
checkout_args["metadata"]["gift_mode"] = True
checkout_args["shipping_address_collection"] = {
"allowed_countries": ShippingZone.all_country_codes

}
next = reverse_lazy("completed_gift_purchase")
else:
Expand Down
5 changes: 3 additions & 2 deletions app/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from djstripe.enums import SubscriptionStatus
from wagtail import hooks
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from wagtail_rangefilter.filters import DateTimeRangeFilter

from app.models.django import User
from app.models.stripe import LBCCustomer, LBCProduct, LBCSubscription, ShippingZone
from app.models.wagtail import MembershipPlanPage, MembershipPlanPrice, ReadingOption
from app.utils import ensure_list
from wagtail_rangefilter.filters import DateTimeRangeFilter


@hooks.register("insert_global_admin_css")
Expand Down Expand Up @@ -148,7 +148,7 @@ class CustomerAdmin(ModelAdmin):
export_filename = "lbc_members"

def get_list_filter(self, request):
if request.GET.get('status') == 'expired':
if request.GET.get("status") == "expired":
return (("ended_at", DateTimeRangeFilter),)
return ()

Expand All @@ -162,6 +162,7 @@ def get_queryset(self, request):
.select_related("plan__product", "customer__subscriber")
)


# Now you just need to register your customised ModelAdmin class with Wagtail
modeladmin_register(CustomerAdmin)

Expand Down
Loading

0 comments on commit 46440ec

Please sign in to comment.