def deactivate(self):
"""Deactivate a subscriber.
Send an async post to the BTS to deactivate the subscriber. Sign the
request using JWT. Note that we do not also send deactivate number
commands -- the BTS will handle that on its own. If the sub does not
have an associated BTS, the sub's previous tower may have been deleted.
We can still delete the sub we just do not have to notify a tower.
"""
if self.bts:
url = '%s/config/deactivate_subscriber' % self.bts.inbound_url
data = {
'imsi': self.imsi,
# Add a UUID as a nonce for the message.
'msgid': str(uuid.uuid4()),
}
serializer = itsdangerous.JSONWebSignatureSerializer(
self.bts.secret)
signed_data = {
'jwt': serializer.dumps(data),
}
# Retry the async_post for three months until it succeeds.
retry_delay = 60 * 10
three_months = 3 * 30 * 24 * 60 * 60.
max_retries = int(three_months / retry_delay)
celery_app.send_task(
'endagaweb.tasks.async_post', (url, signed_data),
max_retries=max_retries)
# Deactivate all associated Numbers from this Sub.
numbers = Number.objects.filter(subscriber=self)
with transaction.atomic():
now = django.utils.timezone.now()
# Create a 'delete_imsi' UsageEvent.
bts_uuid = None
if self.bts:
bts_uuid = self.bts.uuid
event = UsageEvent.objects.create(
subscriber=self, date=now, bts=self.bts, kind='delete_imsi',
subscriber_imsi=self.imsi, bts_uuid=bts_uuid,
oldamt=self.balance, newamt=self.balance, change=0,
reason='deactivated subscriber: %s' % self.imsi)
event.save()
for number in numbers:
reason = 'deactivated phone number: %s' % number.number
event = UsageEvent.objects.create(
subscriber=self, date=now, bts=self.bts,
kind='deactivate_number', to_number=number.number,
reason=reason, oldamt=self.balance, newamt=self.balance,
change=0)
event.save()
number.network = None
number.subscriber = None
number.state = 'available'
number.save()
# Actually delete the subscriber. Note that all associated
# PendingCreditUpdates will be deleted automatically by the default
# deletion CASCADE behavior.
self.delete()
models.py 文件源码
python
阅读 20
收藏 0
点赞 0
评论 0
评论列表
文章目录