-
Notifications
You must be signed in to change notification settings - Fork 36
Sprint2__Subscriber Features 1 of 2 (Client dependent) #43
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,11 +246,11 @@ def vacuum_inactive_subscribers(self): | |
# Do nothing if subscriber vacuuming is disabled for the network. | ||
if not network.sub_vacuum_enabled: | ||
continue | ||
inactives = network.get_outbound_inactive_subscribers( | ||
network.sub_vacuum_inactive_days) | ||
inactives = Subscriber.objects.filter( | ||
state='recycle', network_id=network.id, | ||
prevent_automatic_deactivation=False | ||
) | ||
for subscriber in inactives: | ||
if subscriber.prevent_automatic_deactivation: | ||
continue | ||
print 'vacuuming %s from network %s' % (subscriber.imsi, network) | ||
subscriber.deactivate() | ||
# Sleep a bit in between each deactivation so we don't flood the | ||
|
@@ -439,3 +439,124 @@ def req_bts_log(self, obj, retry_delay=60*10, max_retries=432): | |
raise | ||
finally: | ||
obj.save() | ||
|
||
|
||
@app.task(bind=True) | ||
def subscriber_validity_state(self): | ||
""" Updates the subscribers state to inactive/active/""" | ||
|
||
today = django.utils.timezone.now() | ||
subscribers = Subscriber.objects.filter( | ||
number__valid_through__lte=today) | ||
today = today.date() | ||
for subscriber in subscribers: | ||
try: | ||
number = subscriber.number_set.all()[0] | ||
if number.valid_through is None: | ||
continue | ||
except IndexError: | ||
continue | ||
subscriber_validity = number.valid_through.date() | ||
first_expire = subscriber_validity + datetime.timedelta( | ||
days=subscriber.network.sub_vacuum_inactive_days) | ||
recycle = first_expire + datetime.timedelta( | ||
days=subscriber.network.sub_vacuum_grace_days) | ||
current_state = str(subscriber.state) | ||
|
||
if subscriber_validity < today: | ||
if today <= first_expire: | ||
# Do nothing if it's already first expired | ||
if current_state != 'first_expired': | ||
subscriber.state = 'first_expired' | ||
subscriber.save() | ||
print "Updating subscriber(%s) state to 'First Expired'" % ( | ||
subscriber.imsi,) | ||
elif today > recycle: | ||
# Let deactivation of subscriber be handled by | ||
# vacuum_inactive_subscribers | ||
# Do nothing if it's already recycle | ||
if current_state != 'recycle': | ||
subscriber.state = 'recycle' | ||
subscriber.save() | ||
print "Updating subscriber(%s) state to 'Recycle'" % ( | ||
subscriber.imsi,) | ||
else: | ||
if current_state != 'expired': | ||
subscriber.state = 'expired' | ||
subscriber.save() | ||
print "Updating subscriber(%s) state to 'Expired'" % ( | ||
subscriber.imsi,) | ||
|
||
|
||
@app.task(bind=True) | ||
def validity_expiry_sms(self, days=7): | ||
"""Sends SMS to the number whose validity is: | ||
about to get expire, | ||
if expired (i.e 1st expired), or | ||
if the number is in grace period and is about to recycle. | ||
|
||
Args: | ||
days: Days prior (state change) which the SMS is sent to Subscriber. | ||
Runs as everyday task managed by celerybeat. | ||
""" | ||
today = django.utils.timezone.datetime.now().date() | ||
for subscriber in Subscriber.objects.iterator(): | ||
# Do nothing if subscriber vacuuming is disabled for the network. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all numbers :) |
||
if not subscriber.network.sub_vacuum_enabled: | ||
continue | ||
try: | ||
number = subscriber.number_set.all()[0] | ||
subscriber_validity = number.valid_through | ||
# In case where number has no validity | ||
if subscriber_validity is None: | ||
print '%s has no validity' % (subscriber.imsi,) | ||
continue | ||
except IndexError: | ||
print 'No number attached to subscriber %s' % (subscriber.imsi,) | ||
continue | ||
|
||
subscriber_validity = subscriber_validity.date() | ||
inactive_period = subscriber.network.sub_vacuum_inactive_days | ||
grace_period = subscriber.network.sub_vacuum_grace_days | ||
|
||
prior_first_expire = subscriber_validity + datetime.timedelta( | ||
days=inactive_period) - datetime.timedelta(days=days) | ||
|
||
prior_recycle = prior_first_expire + datetime.timedelta( | ||
days=grace_period) | ||
|
||
# Prior to expiry state (one on last day and before defined days) | ||
if subscriber_validity > today and ( | ||
(subscriber_validity - datetime.timedelta( | ||
days=days) | ||
) == today or today == ( | ||
subscriber_validity - datetime.timedelta( | ||
days=1)) or today == subscriber_validity): | ||
body = 'Your validity is about to get expired on %s , Please ' \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs be translated so this will need to live on the client. you can construct a key/short phrase that you send that the client translates with local i18n There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will update. |
||
'recharge to continue the service. Please ignore if ' \ | ||
'already done! ' % (subscriber_validity,) | ||
sms_notification(body=body, to=number) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This SMS will go through SMPP SMSC/Nexmo rather than into the internal network. We should add an endpoint on federer that will get triggered via RPC. Can you change this to an RPC POST to the client? We can implement the actual client handler later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure will try to do that way. |
||
# Prior 1st_expired state | ||
elif subscriber_validity < today: | ||
if prior_first_expire == today or today == ( | ||
prior_first_expire + datetime.timedelta( | ||
days=days - 1)): | ||
body = 'Your validity has expired on %s, Please recharge ' \ | ||
'immediately to activate your services again! ' % ( | ||
subscriber_validity,) | ||
sms_notification(body=body, to=number) | ||
# Prior to recycle state | ||
elif prior_recycle == today or today == ( | ||
prior_recycle + datetime.timedelta(days=days - 1)): | ||
body = 'Warning: Your validity has expired on %s , Please ' \ | ||
'recharge immediately to avoid deactivation of your ' \ | ||
'connection! ' % (subscriber_validity,) | ||
sms_notification(body=body, to=number) | ||
# SMS on same day of expiry | ||
elif subscriber_validity == today: | ||
body = 'Your validity expiring today %s, Please recharge ' \ | ||
'immediately to continue your services again!, ' \ | ||
'Ignore if already done! ' % (subscriber_validity,) | ||
sms_notification(body=body, to=number) | ||
else: | ||
return # Do nothing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should have a loop on each of the subscriber's number. there can be more than the one at index 0