def get(self, request, imsi=None):
"""Handles GET requests."""
user_profile = UserProfile.objects.get(user=request.user)
network = user_profile.network
try:
subscriber = Subscriber.objects.get(imsi=imsi,
network=network)
except Subscriber.DoesNotExist:
return HttpResponseBadRequest()
# Set the context with various stats.
context = {
'networks': get_objects_for_user(request.user, 'view_network', klass=Network),
'currency': CURRENCIES[network.subscriber_currency],
'user_profile': user_profile,
'subscriber': subscriber,
}
try:
context['created'] = subscriber.usageevent_set.order_by(
'date')[0].date
except IndexError:
context['created'] = None
# Set usage info (SMS sent, total call duration, data usage).
sms_kinds = ['free_sms', 'outside_sms', 'incoming_sms', 'local_sms',
'local_recv_sms', 'error_sms']
context['num_sms'] = subscriber.usageevent_set.filter(
kind__in=sms_kinds).count()
call_kinds = ['free_call', 'outside_call', 'incoming_call',
'local_call', 'local_recv_call', 'error_call']
calls = subscriber.usageevent_set.filter(kind__in=call_kinds)
context['number_of_calls'] = len(calls)
context['voice_sec'] = sum([call.voice_sec() for call in calls])
gprs_events = subscriber.usageevent_set.filter(kind='gprs')
up_bytes = sum([g.uploaded_bytes for g in gprs_events])
down_bytes = sum([g.downloaded_bytes for g in gprs_events])
context['up_bytes'] = humanize.naturalsize(up_bytes)
context['down_bytes'] = humanize.naturalsize(down_bytes)
context['total_bytes'] = humanize.naturalsize(up_bytes + down_bytes)
# Render template.
template = get_template('dashboard/subscriber_detail/info.html')
html = template.render(context, request)
return HttpResponse(html)
dashboard.py 文件源码
python
阅读 28
收藏 0
点赞 0
评论 0
评论列表
文章目录