def notify_expiry(): expiry = [] # Expiring subscriptions subscriptions = Subscription.objects.filter( expires__lte=timezone.now() + timedelta(days=30) ).exclude(payment=None) for subscription in subscriptions: payment = subscription.payment_obj # Skip one-time payments and the ones with recurrence configured if not subscription.get_repeat() or payment.recurring: continue expiry.append( ( str(subscription), subscription.service.users.values_list("email", flat=True), ) ) # Expiring donations donations = Donation.objects.filter( active=True, expires__lte=timezone.now() + timedelta(days=3) ).exclude(payment=None) for donation in donations: payment = donation.payment_obj if not payment.recurring: expiry.append((str(donation), [donation.user.email])) # Notify admins if expiry: send_notification( "expiring_subscriptions", settings.NOTIFY_SUBSCRIPTION, expiry=expiry, )
def notify_expiry(weekday=0): expiry = [] expires_notify = timezone.now() + timedelta(days=30) payment_notify_start = timezone.now() + timedelta(days=7) if weekday == 0: # Monday - Wednesday next week payment_notify_end = timezone.now() + timedelta(days=10) else: # Thursday - Sunday next week payment_notify_end = timezone.now() + timedelta(days=11) # Expiring subscriptions subscriptions = Subscription.objects.filter( expires__lte=expires_notify).exclude(payment=None) for subscription in subscriptions: payment = subscription.payment_obj # Skip one-time payments and the ones with recurrence configured if not subscription.get_repeat(): continue notify_user = (payment_notify_start <= subscription.expires <= payment_notify_end) if payment.recurring: if notify_user: subscription.send_notification("payment_upcoming") continue if notify_user: subscription.send_notification("payment_missing") if not subscription.could_be_obsolete(): expiry.append(( str(subscription), subscription.service.users.values_list("email", flat=True), )) # Expiring donations donations = Donation.objects.filter( active=True, expires__lte=expires_notify).exclude(payment=None) for donation in donations: payment = donation.payment_obj notify_user = payment_notify_start <= donation.expires <= payment_notify_end if payment.recurring: if notify_user: donation.send_notification("payment_upcoming") continue if notify_user: donation.send_notification("payment_missing") expiry.append(( f"{donation.user}: {donation.get_payment_description()}", [donation.user.email], )) # Notify admins if expiry: send_notification( "expiring_subscriptions", settings.NOTIFY_SUBSCRIPTION, expiry=expiry, )
def send_notification(self, notification): send_notification( notification, [user.email for user in self.service.users.all()], subscription=self, ) with override("en"): send_notification( notification, settings.NOTIFY_SUBSCRIPTION, subscription=self, )
def send_notification(self, notification): send_notification( notification, [self.user.email], donation=self, ) with override("en"): send_notification( notification, settings.NOTIFY_SUBSCRIPTION, donation=self, )
def process_subscription(payment): if payment.state != Payment.ACCEPTED: raise ValueError("Can not process not accepted payment") if payment.repeat: # Update existing subscription = Subscription.objects.get(payment=payment.repeat.pk) payment.start = subscription.expires subscription.expires += get_period_delta(payment.repeat.recurring) payment.end = subscription.expires subscription.save() elif isinstance(payment.extra["subscription"], int): subscription = Subscription.objects.get( pk=payment.extra["subscription"]) if subscription.payment: subscription.pastpayments_set.create(payment=subscription.payment) payment.start = subscription.expires subscription.expires += get_period_delta(subscription.get_repeat()) payment.end = subscription.expires subscription.payment = payment.pk subscription.save() else: user = User.objects.get(pk=payment.customer.user_id) package = Package.objects.get(name=payment.extra["subscription"]) # Calculate expiry repeat = package.get_repeat() if repeat: expires = timezone.now() payment.start = expires expires += get_period_delta(repeat) payment.end = expires else: expires = timezone.now() # Create new service = get_service(payment, user) subscription = Subscription.objects.create( service=service, payment=payment.pk, package=package.name, expires=expires, ) with override("en"): send_notification( "new_subscription", settings.NOTIFY_SUBSCRIPTION, subscription=subscription, service=subscription.service, ) if service.was_created and service.needs_token: subscription.send_notification("subscription_intro") # Flag payment as processed payment.state = Payment.PROCESSED payment.save() return subscription
def send_notification(self, notification): send_notification( notification, [self.user.email], donation=self, )