From 5b98584c576c6745022ad891d60e4b199281d733 Mon Sep 17 00:00:00 2001 From: rizwanisready Date: Fri, 24 May 2024 15:59:39 +0530 Subject: [PATCH] handling active subscription if exist --- goodtimes/settings/base.py | 1 + goodtimes/settings/staging.py | 90 +++++++++++++++---------------- manage_subscriptions/api/views.py | 10 ++-- manage_subscriptions/views.py | 35 ++++++------ 4 files changed, 71 insertions(+), 65 deletions(-) diff --git a/goodtimes/settings/base.py b/goodtimes/settings/base.py index 75e1ce9..7a4febd 100644 --- a/goodtimes/settings/base.py +++ b/goodtimes/settings/base.py @@ -304,6 +304,7 @@ SIMPLE_JWT = { STRIPE_SECRET_KEY = env.str("STRIPE_SECRET_KEY") STRIPE_PUBLISH_KEY = env.str("STRIPE_PUBLISH_KEY") + ONE_SIGNAL_APP_ID = env.str("ONE_SIGNAL_APP_ID") ONE_SIGNAL_API_KEY = env.str("ONE_SIGNAL_API_KEY") diff --git a/goodtimes/settings/staging.py b/goodtimes/settings/staging.py index c3aa480..30b9c16 100644 --- a/goodtimes/settings/staging.py +++ b/goodtimes/settings/staging.py @@ -9,56 +9,56 @@ DEBUG = False ALLOWED_HOSTS = ["staging.goodtimesltd.co.uk", "77.68.8.229"] -# LOGGING_DIR = os.path.join( -# BASE_DIR, "logs" -# ) # Define the directory where log files will be stored +LOGGING_DIR = os.path.join( + BASE_DIR, "logs" +) # Define the directory where log files will be stored # Ensure the directory exists; create it if it doesn't -# if not os.path.exists(LOGGING_DIR): -# os.makedirs(LOGGING_DIR) +if not os.path.exists(LOGGING_DIR): + os.makedirs(LOGGING_DIR) -# LOGGING_LEVEL = env.str( -# "LOG_LEVEL", "INFO" -# ) # Set your desired log level (e.g., DEBUG, INFO, WARNING, ERROR) in the env file +LOGGING_LEVEL = env.str( + "LOG_LEVEL", "INFO" +) # Set your desired log level (e.g., DEBUG, INFO, WARNING, ERROR) -# LOGGING = { -# "version": 1, -# "disable_existing_loggers": False, -# "formatters": { -# "verbose": { -# "()": colorlog.ColoredFormatter, -# "format": "%(cyan)s%(asctime)s%(reset)s | %(red)s[%(levelname)8s]%(reset)s | [ %(yellow)s%(name)s.%(module)s:%(white)s%(lineno)d%(reset)s - %(green)s%(funcName)10s()%(reset)s ] --> %(message)s", -# "datefmt": "%Y-%m-%d %H:%M:%S", -# "log_colors": { -# "DEBUG": "white", -# "INFO": "green", -# "WARNING": "yellow", -# "ERROR": "red", -# "CRITICAL": "bold_red", -# }, -# "secondary_log_colors": {}, -# "style": "%", -# } -# }, -# "handlers": { -# "logfile": { -# "level": LOGGING_LEVEL, -# "class": "logging.handlers.RotatingFileHandler", -# "filename": os.path.join(LOGGING_DIR, "goodtimes_staging_error.log"), -# 'maxBytes': 5242880, # 5*1024*1024 bytes (5MB) -# "backupCount": 10, # Number of log files to keep (15 days' worth of logs) -# "formatter": "verbose", -# }, -# }, -# "loggers": { -# "django": { -# "handlers": ["logfile"], -# "level": LOGGING_LEVEL, -# "propagate": False, -# }, -# }, -# } +LOGGING = { + "version": 1, + "disable_existing_loggers": False, + "formatters": { + "verbose": { + "()": colorlog.ColoredFormatter, + "format": "%(cyan)s%(asctime)s%(reset)s | %(red)s[%(levelname)8s]%(reset)s | [ %(yellow)s%(name)s.%(module)s:%(white)s%(lineno)d%(reset)s - %(green)s%(funcName)10s()%(reset)s ] --> %(message)s", + "datefmt": "%Y-%m-%d %H:%M:%S", + "log_colors": { + "DEBUG": "white", + "INFO": "green", + "WARNING": "yellow", + "ERROR": "red", + "CRITICAL": "bold_red", + }, + "secondary_log_colors": {}, + "style": "%", + } + }, + "handlers": { + "logfile": { + "level": LOGGING_LEVEL, + "class": "logging.handlers.RotatingFileHandler", + "filename": os.path.join(LOGGING_DIR, "godtimes_pro_error.log"), + "maxBytes": 5242880, # 5*1024*1024 bytes (5MB) + "backupCount": 10, # Number of log files to keep (15 days' worth of logs) + "formatter": "verbose", + }, + }, + "loggers": { + "django": { + "handlers": ["logfile"], + "level": LOGGING_LEVEL, + "propagate": False, + }, + }, +} BASE_DOMAIN = "https://staging.goodtimesltd.co.uk" diff --git a/manage_subscriptions/api/views.py b/manage_subscriptions/api/views.py index 05cd2e6..c583aa6 100644 --- a/manage_subscriptions/api/views.py +++ b/manage_subscriptions/api/views.py @@ -195,11 +195,11 @@ class StripeWebhookTest(APIView): ) # Check if there is an active principal subscription - if self._has_active_principal_subscription(principal_id): - return ApiResponse.success( - status=status.HTTP_208_ALREADY_REPORTED, - message="Active principal subscription already exists", - ) + # if self._has_active_principal_subscription(principal_id): + # return ApiResponse.success( + # status=status.HTTP_208_ALREADY_REPORTED, + # message="Active principal subscription already exists", + # ) # payment_service = services.PaymentProcessingService(webhook_data=event) payment_service = PaymentProcessingService(webhook_data=event) diff --git a/manage_subscriptions/views.py b/manage_subscriptions/views.py index dbbdcec..24933d6 100644 --- a/manage_subscriptions/views.py +++ b/manage_subscriptions/views.py @@ -405,6 +405,17 @@ def stripe_config(request): return JsonResponse(stripe_config, safe=False) +def _has_active_principal_subscription(principal_id): + return PrincipalSubscription.objects.filter( + principal__id=principal_id, + active=True, + deleted=False, + cancelled=False, + is_paid=True, + end_date__gte=timezone.now().date(), + ).exists() + + @csrf_exempt @require_POST def create_checkout_session(request): @@ -412,6 +423,13 @@ def create_checkout_session(request): data = json.loads(request.body) print("data: ", data) subscription_id = data.get("subscriptionId", None) + principal_id = request.user.id + + if _has_active_principal_subscription(principal_id): + return ApiResponse.success( + status=status.HTTP_208_ALREADY_REPORTED, + message="Active principal subscription already exists", + ) try: subscription = Subscription.objects.get(id=subscription_id) @@ -437,21 +455,6 @@ def create_checkout_session(request): comment="Principal Subscription Initiated", ) - # subscription_days = subscription.plan.days - # today = timezone.now().date() - # last_date = today + timedelta(days=int(subscription_days)) - - # To Avoid Duplicacy of Principal Subscription - # principal_subscription = PrincipalSubscription.objects.create( - # principal=request.user, - # subscription=subscription, - # is_paid=False, - # order_id=order_id, - # start_date=today, - # end_date=last_date, - # grace_period_end_date=last_date + timedelta(days=15), - # ) - try: # customer = stripe.Customer.create( # email=request.user.email, @@ -485,7 +488,9 @@ def create_checkout_session(request): "quantity": 1, } ], + # allow_promotion_codes=True, mode="payment", + # discounts=[{"coupon": "VLMAsicx"}], success_url=request.build_absolute_uri("/subscriptions/success/"), cancel_url=request.build_absolute_uri("/subscriptions/cancel/"), metadata={