Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Production Deployment v0.9.0 #100

Merged
merged 14 commits into from
Aug 20, 2024
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ EXPOSE 8080
ENTRYPOINT ["bash", "/code/scripts/docker-entrypoint.sh"]

# Specify tag name to be created on github
LABEL version="0.8.0"
LABEL version="0.9.0"
23 changes: 23 additions & 0 deletions core/migrations/0019_auto_20240630_1037.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.2.28 on 2024-06-30 10:37

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0018_auto_20230319_1707'),
]

operations = [
migrations.AddField(
model_name='subscription',
name='cancelled',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='subscription',
name='cancelled_date',
field=models.DateTimeField(blank=True, null=True),
),
]
2 changes: 2 additions & 0 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,5 @@ class Subscription(models.Model):
on_delete=models.CASCADE,
related_name='organization_subscription'
)
cancelled = models.BooleanField(default=False)
cancelled_date = models.DateTimeField(null=True, blank=True)
110 changes: 60 additions & 50 deletions core/views/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ class SubscriptionViewSet(viewsets.ModelViewSet):
serializer_class = SubscriptionSerializer
permission_classes = (AllowAny, IsAuthenticated)

def get_queryset(self):
queryset = (
super(SubscriptionViewSet, self)
.get_queryset()
.filter(organization=self.request.user.organization)
)

if int(self.request.query_params.get('cancelled', '0')):
queryset = queryset.filter(cancelled=True)
else:
queryset = queryset.filter(cancelled=False)

return queryset.order_by('-create_date')

def create(self, request, *args, **kwargs):
if settings.STRIPE_SECRET:
stripe.api_key = settings.STRIPE_SECRET
Expand All @@ -54,61 +68,39 @@ def create(self, request, *args, **kwargs):
status=status.HTTP_400_BAD_REQUEST
)

# def update(self, request, *args, **kwargs):
# instance = self.get_object()
# if settings.STRIPE_SECRET:
# stripe.api_key = settings.STRIPE_SECRET
# stripe.api_version = '2022-11-15'
# data = self.get_stripe_details()
# if data:
# serializer = self.get_serializer(instance, data=data, partial=True)
# serializer.is_valid(raise_exception=True)
# self.perform_create(serializer)

# product_info = data.get('stripe_product_info', {})
# # send the email
# context = {
# 'frontend_link': settings.FRONTEND_URL,
# 'product_name': product_info.get('name'),
# 'product_description': product_info.get('description')
# }
# subject = 'Subscription Success'
# template_name = 'email/coreuser/subscription.txt'
# html_template_name = 'email/coreuser/subscription.html'
# send_email(
# self.request.user.email,
# subject,
# context,
# template_name,
# html_template_name
# )

# return Response(
# serializer.data,
# status=status.HTTP_201_CREATED
# )
# return Response(
# dict(
# code='stripe_api_error',
# message='There was an error creating subscription'
# ),
# status=status.HTTP_400_BAD_REQUEST
# )

# return Response(
# dict(
# code='missing_stripe_details',
# message='Please pass valid product/card or stripe secret'
# ),
# status=status.HTTP_400_BAD_REQUEST
# )

def perform_create(self, serializer):
serializer.save(
user=self.request.user,
created_by=self.request.user,
)

def destroy(self, request, *args, **kwargs):
instance = self.get_object()
cancelled = self.perform_destroy(instance)
if not cancelled:
return Response(
dict(
code='stripe_api_error',
message='There was an error cancelling subscription',
),
status=status.HTTP_400_BAD_REQUEST
)
return Response(
dict(code='subscription_cancelled'),
status=status.HTTP_204_NO_CONTENT
)

def perform_destroy(self, instance):
# delete the subscription on stripe
cancelled = self.cancel_subscription_on_stripe(instance)

if cancelled:
# delete the subscription
instance.cancelled = True
instance.cancelled_date = timezone.now()
instance.save()
return cancelled

@action(
detail=False,
methods=['get'],
Expand All @@ -124,7 +116,7 @@ def stripe_products(self, request, *args, **kwargs):
if settings.STRIPE_SECRET:
stripe.api_key = settings.STRIPE_SECRET
stripe.api_version = '2022-11-15'
stripe_products = stripe.Product.search(query="active:'true'",)
stripe_products = stripe.Product.search(query="active:'true'", )
products = stripe_products.data

return Response(
Expand Down Expand Up @@ -198,3 +190,21 @@ def get_stripe_details(self):
return None

return data

@staticmethod
def cancel_subscription_on_stripe(instance):
"""
Cancel a subscription
"""
if settings.STRIPE_SECRET:
stripe.api_key = settings.STRIPE_SECRET
# stripe.api_version = '2022-11-15'
try:
# stripe.Subscription.cancel(instance.stripe_subscription_id)
subscription = stripe.Subscription.retrieve(instance.stripe_subscription_id)
subscription.delete()
return True

except Exception as e:
pass
return False
4 changes: 2 additions & 2 deletions templates/email/coreuser/subscription.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
<tr style="height: 104px;">
<td style="width: 691px; height: 104px;">
<h1 style="text-align: center;"><span style="color: #151f56;"><strong><span style="font-size: 40px; font-family: roboto,helvetica neue,helvetica,arial,sans-serif;">Subscription successful!</span></strong></span></h1>
<p style="padding-top: 20px; padding-bottom: 10px; text-align: center; font-family: roboto,helvetica neue,helvetica,arial,sans-serif; color: #737581;"><span style="font-size: 12pt; font-family: roboto,helvetica neue,helvetica,arial,sans-serif; color: #737581;">You have successfully subscribed to<span style="font-family: roboto,helvetica neue,helvetica,arial,sans-serif; color: #151f56;"><strong> Insights </strong></span> on Buildly!<br />Click the button below to return to the app</span></p>
<p style="padding-top: 20px; padding-bottom: 10px; text-align: center; font-family: roboto,helvetica neue,helvetica,arial,sans-serif; color: #737581;"><span style="font-size: 12pt; font-family: roboto,helvetica neue,helvetica,arial,sans-serif; color: #737581;">You have successfully subscribed to<span style="font-family: roboto,helvetica neue,helvetica,arial,sans-serif; color: #151f56;"><strong> Labs </strong></span> on Buildly!<br />Click the button below to return to the app</span></p>
</td>
</tr>
</tbody>
</table>
<table align="center" style="width: 160px;">
<tbody>
<tr style="height: 20px;">
<td style="font-family: Roboto, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18pt; padding: 12pt; background-color: #ff9f00; color: #ffffff; font-weight: bold; text-align: center;"><a href="{{ frontend_link }}" target="_blank" style="font-weight: bold; letter-spacing: normal; line-height: 100%; text-align: center; text-decoration: none; color: #ffffff;">Buildly Insights</a></td>
<td style="font-family: Roboto, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18pt; padding: 12pt; background-color: #ff9f00; color: #ffffff; font-weight: bold; text-align: center;"><a href="{{ frontend_link }}" target="_blank" style="font-weight: bold; letter-spacing: normal; line-height: 100%; text-align: center; text-decoration: none; color: #ffffff;">Buildly Labs</a></td>
</tr>
</tbody>
</table>
Expand Down
2 changes: 1 addition & 1 deletion templates/email/coreuser/subscription.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Subscription successful!

You have successfully subscribed to Insights on Buildly.
You have successfully subscribed to Labs on Buildly.

Go to {{ frontend_link }} to access the app.

Expand Down
Loading