Skip to content

Commit

Permalink
Merge branch 'production' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
janbaykara committed Oct 17, 2022
2 parents 35d62b3 + fb793ce commit 6abf8c9
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 71 deletions.
10 changes: 7 additions & 3 deletions app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ class MemberSignupForm(SignupForm):
first_name = forms.CharField(max_length=150)
last_name = forms.CharField(max_length=150)
gdpr_email_consent = forms.BooleanField(
required=False,
label="Can we email you with news and updates from the Left Book Club?",
required=True,
label="LBC can email me about my books and membership (required)",
)
# promotional_consent = forms.BooleanField(
# required=False,
# label="LBC can email me about special offers and promotions",
# )

field_order = [
"email",
Expand All @@ -32,7 +36,7 @@ class MemberSignupForm(SignupForm):
"password1",
# "password2", # ignored when not present
"gdpr_email_consent",
"terms_and_conditions",
# "promotional_consent",
]

def save(self, request):
Expand Down
21 changes: 11 additions & 10 deletions app/models/django.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

import djstripe
import stripe
from allauth.account.models import EmailAddress
Expand All @@ -17,7 +15,7 @@
subscription_with_promocode,
)

from .stripe import LBCProduct
from .stripe import LBCProduct, LBCSubscription


def custom_user_casual_name(user: AbstractUser) -> str:
Expand Down Expand Up @@ -94,10 +92,11 @@ def stripe_customer_id(self) -> str:
]

@property
def active_subscription(self) -> djstripe.models.Subscription:
def active_subscription(self) -> LBCSubscription:
try:
sub = (
self.stripe_customer.subscriptions.filter(
LBCSubscription.objects.filter(
customer=self.stripe_customer,
# Was started + wasn't cancelled
status__in=self.valid_subscription_statuses,
# Is in period
Expand All @@ -113,7 +112,7 @@ def active_subscription(self) -> djstripe.models.Subscription:
return None

@property
def old_subscription(self) -> djstripe.models.Subscription:
def old_subscription(self) -> LBCSubscription:
try:
sub = (
self.stripe_customer.subscriptions.filter(
Expand All @@ -140,6 +139,10 @@ def has_overdue_payment(self):
== djstripe.enums.SubscriptionStatus.past_due
)

@property
def is_cancelling_member(self):
return self.is_member and self.active_subscription.cancel_at is not None

@property
def is_expired_member(self):
return not self.is_member and self.old_subscription is not None
Expand All @@ -161,7 +164,7 @@ def primary_product(self) -> LBCProduct:
try:
if self.active_subscription is not None:
product = get_primary_product_for_djstripe_subscription(
self.active_subscription
self.active_subscription.lbc
)
return product
except:
Expand Down Expand Up @@ -190,9 +193,7 @@ def gifts_bought(self):
@property
def gift_giver(self):
try:
gift_giver_subscription = self.active_subscription.gift_giver_subscription
sub = djstripe.models.Subscription.objects.get(id=gift_giver_subscription)
user = sub.customer.subscriber
user = self.active_subscription.gift_giver_subscription.customer.subscriber
return user
except:
return None
Expand Down
29 changes: 21 additions & 8 deletions app/models/stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,29 @@ def primary_product_id(self):
def customer_id(self):
return self.customer.id

@property
def gift_giver_subscription(self):
"""
If this subscription was created as a result of a neighbouring gift subscription
"""
related_subscription_id = self.metadata.get("gift_giver_subscription", None)
if related_subscription_id:
return LBCSubscription.objects.filter(id=related_subscription_id).first()

@property
def gift_recipient_subscription(self):
"""
If this subscription was created as a result of a neighbouring gift subscription
"""
related_subscription_id = self.metadata.get("gift_recipient_subscription", None)
if related_subscription_id:
return LBCSubscription.objects.filter(id=related_subscription_id).first()

@property
def is_gift_giver(self):
self.metadata.get("gift_mode", None) is not None

@property
def is_gift_receiver(self):
self.metadata.get("gift_giver_subscription", None) is not None

Expand Down Expand Up @@ -143,14 +163,6 @@ def get_prices_for_country(self, iso_a2: str, **kwargs):
product=self.id, active=True, metadata__shipping=zone.code, **kwargs
).order_by("unit_amount")

def gift_giver_subscription(self):
"""
If this subscription was created as a result of a neighbouring gift subscription
"""
related_subscription_id = self.metadata.get("gift_giver_subscription", None)
if related_subscription_id:
return djstripe.models.Subscription.get(id=related_subscription_id)

@property
def book_types(self):
book_types = self.metadata.get("book_types", None)
Expand All @@ -159,6 +171,7 @@ def book_types(self):
return book_types
return list()

@property
def current_book(self):
from app.models.wagtail import BookPage

Expand Down
1 change: 1 addition & 0 deletions app/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@
SHOPIFY_COLLECTION_ID = os.environ.get("SHOPIFY_COLLECTION_ID", "402936398057")
SHOPIFY_PRIVATE_APP_PASSWORD = os.environ.get("SHOPIFY_PRIVATE_APP_PASSWORD", None)
SHOPIFY_APP_API_SECRET = os.environ.get("SHOPIFY_APP_API_SECRET", "")
SHOPIFY_WEBHOOK_PATH = os.environ.get("SHOPIFY_WEBHOOK_PATH", "shopify/webhooks/")

# CSP
X_FRAME_OPTIONS = "SAMEORIGIN"
Expand Down
6 changes: 6 additions & 0 deletions app/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def cancel_gift_recipient_subscription(event, **kwargs):
def sync(*args, data: shopify.Product, **kwargs):
from app.models.wagtail import BaseShopifyProductPage

print("products_updated", data)

product_id = data.get("id")
print("Product", product_id, "was updated")
BookPage.sync_from_shopify_product_id(product_id)
Expand All @@ -70,6 +72,8 @@ def sync(*args, data: shopify.Product, **kwargs):
def sync(*args, data: shopify.Product, **kwargs):
from app.models.wagtail import BaseShopifyProductPage

print("products_create", data)

product_id = data.get("id")
print("Product", product_id, "was created")
BookPage.sync_from_shopify_product_id(product_id)
Expand All @@ -79,6 +83,8 @@ def sync(*args, data: shopify.Product, **kwargs):
def sync(*args, data: shopify.Product, **kwargs):
from app.models.wagtail import BaseShopifyProductPage

print("products_delete", data)

product_id = data.get("id")
print("Product", product_id, "was deleted")
BookPage.objects.filter(shopify_product_id=product_id).delete()
46 changes: 39 additions & 7 deletions app/templates/account/cancel.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,51 @@
{% block account_content %}
<div>
{% if subscription and subscription.cancel_at %}
<h3 class='my-4 text-center'>This subscription has been cancelled</h3>
{% comment %} Cancelling a cancelled subscription {% endcomment %}
<h3 class='my-4 text-center'>Your subscription has been cancelled</h3>
<p>Membership will expire on {{subscription.cancel_at}}</p>
{% elif gift_mode %}
{% comment %} Cancelling a gift plan {% endcomment %}
{% elif subscription.gift_recipient_subscription %}
{% comment %} Cancelling a gift giver subscription {% endcomment %}
<h3 class='my-4 text-center'>Thinking of cancelling this gift card?</h3>
<p class='text-center'>
You'll no longer be billed by LBC. The gift membership that was redeemed
{% if gift_recipient_subscription %} by {{ gift_recipient_subscription.customer.name }} {% endif %}
will <b>end on {{ subscription.current_period_end }}</b>.
{% if subscription.gift_recipient_subscription %}
This gift card was redeemed by {{ subscription.gift_recipient_subscription.customer.subscriber }}.
If you cancel, they'll stop receiving books <b>on {{ subscription.current_period_end }}</b>.
{% else %}
This gift card was never redeemed.
{% endif %}
You'll no longer be billed by LBC.
</p>
<form method='POST' class='text-center' data-turbo='false'>
{% csrf_token %}
{% bootstrap_button button_class='btn-outline-danger' content="I really want to cancel" %}
</form>
{% elif subscription.gift_giver_subscription %}
{% comment %} Cancelling a gift recipient subscription {% endcomment %}
<h3 class='my-4 text-center'>Thinking of cancelling your gifted membership?</h3>
<p class='text-center'>
Your membership is being paid for by {{ user.gift_giver }}. If you cancel, they'll no longer pay your membership fee and your membership will will <b>end on {{ subscription.current_period_end }}</b>.
</p>
<div class='d-flex flex-column justify-content-center align-items-center my-4'>
{% include "app/includes/membership_card.html" %}
</div>
{% get_books since=user.active_subscription.created types=user.primary_product.book_types as books %}
{% if books|length > 0 %}
<section class='container my-5'>
<div class='row text-center my-4'>
<h3>Books we've read together</h3>
</div>
<div class='row justify-content-center align-items-center text-center g-1'>
{% for book in books %}
{% include "app/includes/simple_book_card.html" with book=book class='col-6 col-sm-4 col-lg-3 col-xl-2' %}
{% endfor %}
</div>
</section>
{% endif %}
<form method='POST' class='text-center' data-turbo='false'>
{% csrf_token %}
{% bootstrap_button button_class='btn-outline-danger' content="I really want to cancel" %}
</form>
{% elif request.user.is_member %}
{% comment %} Cancelling your own plan {% endcomment %}
<h3 class='my-4 text-center'>Thinking of cancelling?</h3>
Expand All @@ -33,7 +64,7 @@ <h3>Books we've read together</h3>
</div>
<div class='row justify-content-center align-items-center text-center g-1'>
{% for book in books %}
{% include "app/includes/simple_book_card.html" with book=book class='col-12 col-md-6' %}
{% include "app/includes/simple_book_card.html" with book=book class='col-6 col-sm-4 col-lg-3 col-xl-2' %}
{% endfor %}
</div>
</section>
Expand All @@ -45,6 +76,7 @@ <h3>Books we've read together</h3>
{% bootstrap_button button_class='btn-outline-danger' content="Cancel" %}
</form>
{% else %}
{% comment %} Cancelling a non-subscription {% endcomment %}
<h3 class='fw-light text-center'>
You're not a member at the moment.
</h3>
Expand Down
9 changes: 7 additions & 2 deletions app/templates/account/login.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
{% extends "account/modal_base.html" %}

{% load url i18n django_bootstrap5 account socialaccount %}
{% load url i18n django_bootstrap5 account socialaccount setting %}

{% block head_title %}{% trans "Sign In" %}{% endblock %}


{% block modal_content %}
{% get_providers as socialaccount_providers %}

<h1 class="text-center h2">Log In</h1>
{% oauth_application_from_query as app %}
{% if app %}
<h1 class="text-center h2">Log in to {{ app.name }}</h1>
{% else %}
<h1 class="text-center h2">Log In</h1>
{% endif %}

<div class="row">

Expand Down
18 changes: 16 additions & 2 deletions app/templates/account/membership.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "base.html" %}
{% load active_link_tags wagtailsettings_tags account django_bootstrap5 gravatar setting gift_card i18n %}
{% load active_link_tags wagtailsettings_tags account django_bootstrap5 gravatar setting gift_card date i18n %}

{% block content_padding %}py-5{% endblock %}

Expand All @@ -21,6 +21,8 @@
<a href="/join" class='d-inline-block badge rounded-pill bg-warning fs-7 tw-tracking-normal text-black tw-no-underline'>
Finish signup
</a>
{% elif user.is_cancelling_member %}
<div class='text-secondary'>Membership cancelling {{ user.active_subscription.cancel_at|to_date|date:"d M Y" }}</div>
{% elif user.is_expired_member %}
<div class='text-secondary'>Expired member</div>
{% elif user.is_member %}
Expand Down Expand Up @@ -57,18 +59,20 @@
{% endif %}

{% if request.user.is_member or request.user.gifts_bought|length > 0 %}
<div class='row'>
<div class='row mt-4'>
{% if request.user.is_member %}
<div class='col-md-6'>
<section>
<h3>
Shipping
</h3>
<div>
{% for key, line in request.user.shipping_address.items %}
{% if line %}
{{ line }}<br />
{% endif %}
{% endfor %}
</div>
<div class='my-4'>
{% url "customerportal" as portal_url %}
{% bootstrap_button button_class='btn-secondary' href=portal_url content="Update shipping" %}
Expand All @@ -82,6 +86,16 @@ <h3>
<h3>
Billing
</h3>
{% if user.gift_giver %}
<div class="alert alert-info" role="alert">
Your membership is being paid for by {{ user.gift_giver }}
</div>
{% endif %}
{% if user.is_cancelling_member %}
<div class="alert alert-warning" role="alert">
Membership is cancelling {{ user.active_subscription.cancel_at|to_date|date:"d M Y" }}
</div>
{% endif %}
{% for si in user.active_subscription.items.all %}
<div class='row'>
<div class='col'>{{ si.plan.product.name }}</div><div class='col'>{{ si.plan.human_readable_price }}</div>
Expand Down
5 changes: 3 additions & 2 deletions app/templates/account/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% load i18n %}
{% load url django_bootstrap5 account socialaccount %}

{% block head_title %}{% trans "Create an account" %}BS{% endblock %}
{% block head_title %}{% trans "Create an account" %}{% endblock %}

{% block modal_content %}
{% get_providers as socialaccount_providers %}
Expand Down Expand Up @@ -43,8 +43,9 @@ <h1 class='text-center h2'>{% trans "Let's get you set up" %}</h1>
{% bootstrap_field form.last_name show_label=False %}
</div>
</div>
{% bootstrap_form form show_label=False exclude="email,first_name,last_name,gdpr_email_consent,password2" %}
{% bootstrap_form form show_label=False exclude="email,first_name,last_name,gdpr_email_consent,promotional_consent,password2" %}
{% bootstrap_field form.gdpr_email_consent show_label=True %}
{% comment %} {% bootstrap_field form.promotional_consent show_label=True %} {% endcomment %}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion app/templates/app/blocks/your_books_grid_block.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h3>Books you've received</h3>
</div>
<div class='row align-items-center g-1'>
{% for book in books %}
{% include "app/includes/simple_book_card.html" with book=book class='col-12 col-sm-6 col-md-4 col-lg-3 col-xl-2' %}
{% include "app/includes/simple_book_card.html" with book=book class='col-6 col-sm-4 col-lg-3 col-xl-2' %}
{% endfor %}
</div>
</section>
Expand Down
2 changes: 1 addition & 1 deletion app/templates/app/frames/shipping_cost.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
{% if request.GET.gift_mode %}
{% bootstrap_alert "At checkout, please add your own shipping address, in case we need to send you any gift card materials. Your gift recipient will be able to enter their own address when they redeem." dismissible=False alert_type="warning" extra_classes='mb-2' %}
{% endif %}
{% if user.is_member and not gift_mode %}
{% if user.is_member and not request.GET.gift_mode %}
<div class='justify-content-center'>
<div class="form-check max-width-card w-100">
<input required class="form-check-input" type="checkbox" value="" id='confirm_cancel_current_subscriptions' name='confirm_cancel_current_subscriptions'>
Expand Down
6 changes: 3 additions & 3 deletions app/templates/app/includes/event_card.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load menu_tags humanize brand groundwork_geo wagtailsettings_tags account setting django_bootstrap5 wagtailcore_tags static wagtailroutablepage_tags django_bootstrap5 setting %}
{% load tz menu_tags humanize brand groundwork_geo wagtailsettings_tags account setting django_bootstrap5 wagtailcore_tags static wagtailroutablepage_tags django_bootstrap5 setting %}

<li
data-fly-to-map-zoom-param="12"
Expand All @@ -22,7 +22,7 @@ <h3 class="tw-text-2xl tw-font-semibold tw-text-gray-900">{{ event.name }}</h3>
<div>
<span class="tw-capitalize">{{ event.location_type|replace:"_| " }}</span>
<span>&middot;</span>
<span>Starts at {{event.starts_at|date:"P"}}</span>
<span>Starts at {{event.starts_at|date:"P T"}}</span>
<span>&middot;</span>
<span>{{event.starts_at|naturaltime}}</span>
</div>
Expand All @@ -41,4 +41,4 @@ <h3 class="tw-text-2xl tw-font-semibold tw-text-gray-900">{{ event.name }}</h3>
{% endif %}
</div>
</div>
</li>
</li>
Loading

0 comments on commit 6abf8c9

Please sign in to comment.