Skip to content

Commit

Permalink
Merge pull request breatheco-de#1402 from jefer94/development
Browse files Browse the repository at this point in the history
add task manager signals and expire subscriptions no paid
  • Loading branch information
jefer94 authored Jun 27, 2024
2 parents beec74b + 55d3157 commit b2d2e55
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 113 deletions.
48 changes: 24 additions & 24 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions breathecode/admissions/signals.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from django.dispatch import Signal
from task_manager.django.dispatch import Emisor

emisor = Emisor('breathecode.admissions')

# add your receives here
student_edu_status_updated = Signal()
cohort_saved = Signal()
cohort_log_saved = Signal()
cohort_user_created = Signal()
cohort_stage_updated = Signal()
student_edu_status_updated = emisor.signal('student_edu_status_updated')
cohort_saved = emisor.signal('cohort_saved')
cohort_log_saved = emisor.signal('cohort_log_saved')
cohort_user_created = emisor.signal('cohort_user_created')
cohort_stage_updated = emisor.signal('cohort_stage_updated')

academy_saved = Signal()
academy_saved = emisor.signal('academy_saved')

# happens when any asset gets update inside the syllabus json for any version
syllabus_asset_slug_updated = Signal()
syllabus_asset_slug_updated = emisor.signal('syllabus_asset_slug_updated')

syllabus_version_json_updated = Signal()
syllabus_version_json_updated = emisor.signal('syllabus_version_json_updated')

timeslot_saved = Signal()
timeslot_saved = emisor.signal('timeslot_saved')
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def setUp(self):
# the behavior of permissions is not exact, this changes every time you add a model
self.latest_content_type_id = content_type.id
self.latest_permission_id = permission.id
self.job_content_type_id = self.latest_content_type_id - 59
self.can_delete_job_permission_id = self.latest_permission_id - 237
self.job_content_type_id = self.latest_content_type_id - 61
self.can_delete_job_permission_id = self.latest_permission_id - 245

"""
🔽🔽🔽 format of PERMISSIONS
Expand Down
12 changes: 6 additions & 6 deletions breathecode/certificate/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
For each signal you want other apps to be able to receive, you have to
declare a new variable here like this:
"""
from django.dispatch import Signal
"""For each signal you want other apps to be able to receive, you have to declare a new variable here like this."""

user_specialty_saved = Signal()
from task_manager.django.dispatch import Emisor

emisor = Emisor('breathecode.certificate')

user_specialty_saved = emisor.signal('user_specialty_saved')
12 changes: 6 additions & 6 deletions breathecode/commons/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
For each signal you want other apps to be able to receive, you have to
declare a new variable here like this:
"""
from django.dispatch import Signal
"""For each signal you want other apps to be able to receive, you have to declare a new variable here like this."""

update_cache = Signal()
from task_manager.django.dispatch import Emisor

emisor = Emisor('breathecode.commons')

update_cache = emisor.signal('update_cache')
18 changes: 9 additions & 9 deletions breathecode/events/signals.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
For each signal you want other apps to be able to receive, you have to
declare a new variable here like this:
"""
from django.dispatch import Signal
"""For each signal you want other apps to be able to receive, you have to declare a new variable here like this."""

event_saved = Signal()
event_status_updated = Signal()
new_event_attendee = Signal()
new_event_order = Signal()
from task_manager.django.dispatch import Emisor

emisor = Emisor('breathecode.events')

event_saved = emisor.signal('event_saved')
event_status_updated = emisor.signal('event_status_updated')
new_event_attendee = emisor.signal('new_event_attendee')
new_event_order = emisor.signal('new_event_order')
2 changes: 1 addition & 1 deletion breathecode/events/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
response_207,
)
from breathecode.utils.api_view_extensions.api_view_extensions import APIViewExtensions
from breathecode.utils.decorators import consume, has_permission
from breathecode.utils.decorators import consume
from breathecode.utils.i18n import translation
from breathecode.utils.multi_status_response import MultiStatusResponse
from breathecode.utils.views import private_view, render_message
Expand Down
16 changes: 8 additions & 8 deletions breathecode/marketing/signals.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
For each signal you want other apps to be able to receive, you have to
declare a new variable here like this:
"""
from django.dispatch import Signal
"""For each signal you want other apps to be able to receive, you have to declare a new variable here like this."""

downloadable_saved = Signal()
from task_manager.django.dispatch import Emisor

form_entry_won_or_lost = Signal()
new_form_entry_deal = Signal()
emisor = Emisor('breathecode.marketing')

downloadable_saved = emisor.signal('downloadable_saved')

form_entry_won_or_lost = emisor.signal('form_entry_won_or_lost')
new_form_entry_deal = emisor.signal('new_form_entry_deal')
6 changes: 5 additions & 1 deletion breathecode/payments/management/commands/make_charges.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Command(BaseCommand):

def handle(self, *args, **options):
utc_now = timezone.now()
statuses = ['CANCELLED', 'DEPRECATED', 'FREE_TRIAL']
statuses = ['CANCELLED', 'DEPRECATED', 'FREE_TRIAL', 'EXPIRED']

avoid_expire_these_statuses = Q(status='EXPIRED') | Q(status='ERROR') | Q(status='PAYMENT_ISSUE') | Q(
status='FULLY_PAID') | Q(status='FREE_TRIAL') | Q(status='CANCELLED') | Q(status='DEPRECATED')
Expand All @@ -26,6 +26,10 @@ def handle(self, *args, **options):
'next_payment_at__lte': utc_now + timedelta(days=1),
}

Subscription.objects.filter(Q(valid_until__gte=utc_now) | Q(valid_until__isnull=True),
next_payment_at__lte=utc_now - timedelta(days=7),
status='PAYMENT_ISSUE').update(status='EXPIRED')

Subscription.objects.filter(valid_until__lte=utc_now).exclude(avoid_expire_these_statuses).update(
status='EXPIRED')

Expand Down
28 changes: 14 additions & 14 deletions breathecode/payments/signals.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
"""
For each signal you want other apps to be able to receive, you have to
declare a new variable here like this:
"""
from django.dispatch import Signal
"""For each signal you want other apps to be able to receive, you have to declare a new variable here like this."""

from task_manager.django.dispatch import Emisor

emisor = Emisor('breathecode.payments')

# consume a service
consume_service = Signal()
consume_service = emisor.signal('consume_service')

# refund the units in case of error
reimburse_service_units = Signal()
reimburse_service_units = emisor.signal('reimburse_service_units')

# manage of permissions for the service
lose_service_permissions = Signal()
grant_service_permissions = Signal()
revoke_service_permissions = Signal()
lose_service_permissions = emisor.signal('lose_service_permissions')
grant_service_permissions = emisor.signal('grant_service_permissions')
revoke_service_permissions = emisor.signal('revoke_service_permissions')

# proxy to m2m_changed in Event.service_items
update_plan_m2m_service_items = Signal()
update_plan_m2m_service_items = emisor.signal('update_plan_m2m_service_items')

# Plan aquired
planfinancing_created = Signal()
subscription_created = Signal()
# Plan adquired
planfinancing_created = emisor.signal('planfinancing_created')
subscription_created = emisor.signal('subscription_created')
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,66 @@ def test_with_two_subscriptions__expired(bc: Breathecode, delta, status, status_
assert tasks.charge_subscription.delay.call_args_list == []


@pytest.mark.parametrize('delta_valid_until, delta_next_payment_at', [
(relativedelta(0), -relativedelta(days=7, seconds=1)),
(relativedelta(days=28), -relativedelta(days=7, seconds=1)),
])
def test_with_two_subscriptions__payment_issue__gt_7_days(bc: Breathecode, delta_valid_until, delta_next_payment_at,
utc_now):

if delta_valid_until:
valid_until = utc_now + delta_valid_until

else:
valid_until = None

next_payment_at = utc_now + delta_next_payment_at

subscription = {'valid_until': valid_until, 'next_payment_at': next_payment_at, 'status': 'PAYMENT_ISSUE'}

model = bc.database.create(subscription=(2, subscription))

command = Command()
result = command.handle()

assert result == None
db = bc.format.to_dict(model.subscription)
for i in range(len(db)):
db[i]['status'] = 'EXPIRED'

assert bc.database.list_of('payments.Subscription') == db
assert tasks.charge_subscription.delay.call_args_list == []


@pytest.mark.parametrize('delta_valid_until, delta_next_payment_at', [
(relativedelta(0), -relativedelta(days=6)),
(relativedelta(days=28), -relativedelta(days=6)),
])
def test_with_two_subscriptions__payment_issue__lt_7_days(bc: Breathecode, delta_valid_until, delta_next_payment_at,
utc_now):

if delta_valid_until:
valid_until = utc_now + delta_valid_until

else:
valid_until = None

next_payment_at = utc_now + delta_next_payment_at

subscription = {'valid_until': valid_until, 'next_payment_at': next_payment_at, 'status': 'PAYMENT_ISSUE'}

model = bc.database.create(subscription=(2, subscription))

command = Command()
result = command.handle()

assert result == None
db = bc.format.to_dict(model.subscription)

assert bc.database.list_of('payments.Subscription') == db
assert tasks.charge_subscription.delay.call_args_list == [call(1), call(2)]


@pytest.mark.parametrize('delta, status', [
(relativedelta(days=2, seconds=1), 'ACTIVE'),
(relativedelta(days=2, seconds=1), 'ERROR'),
Expand Down
12 changes: 6 additions & 6 deletions breathecode/provisioning/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
For each signal you want other apps to be able to receive, you have to
declare a new variable here like this:
"""
from django.dispatch import Signal
"""For each signal you want other apps to be able to receive, you have to declare a new variable here like this."""

process_stripe_event = Signal()
from task_manager.django.dispatch import Emisor

emisor = Emisor('breathecode.provisioning')

process_stripe_event = emisor.signal('process_stripe_event')
1 change: 0 additions & 1 deletion breathecode/registry/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from breathecode.services.seo import SEOAnalyzer
from breathecode.utils import GenerateLookupsMixin, capable_of, consume
from breathecode.utils.api_view_extensions.api_view_extensions import APIViewExtensions
from breathecode.utils.decorators.has_permission import has_permission
from breathecode.utils.i18n import translation
from breathecode.utils.views import render_message
from capyc.rest_framework.exceptions import ValidationException
Expand Down
Loading

0 comments on commit b2d2e55

Please sign in to comment.