def sms_daily_report(self, request): opts = SMSMessage._meta kwargs = {} form = AdminSMSSearchForm(request.POST or None) if form.is_valid(): kwargs = sms_record_common_fun(request) request.session['from_date'] = request.POST.get('from_date') request.session['to_date'] = request.POST.get('to_date') request.session['status'] = request.POST.get('status') request.session['smscampaign'] = request.POST.get('smscampaign') else: kwargs = sms_record_common_fun(request) tday = datetime.utcnow().replace(tzinfo=utc) if len(kwargs) == 0: kwargs['send_date__gte'] = datetime(tday.year, tday.month, tday.day, 0, 0, 0, 0).replace(tzinfo=utc) select_data = {"send_date": "SUBSTR(CAST(send_date as CHAR(30)),1,10)"} # Get Total Records from VoIPCall Report table for Daily Call Report total_data = SMSMessage.objects.extra(select=select_data)\ .values('send_date').filter(**kwargs).annotate(Count('send_date'))\ .order_by('-send_date') # Following code will count total sms calls, duration if total_data.count() != 0: total_sms = sum([x['send_date__count'] for x in total_data]) else: total_sms = 0 ctx = RequestContext( request, { 'form': form, 'total_data': total_data.reverse(), 'total_sms': total_sms, 'opts': opts, 'model_name': opts.object_name.lower(), 'app_label': _('SMS module'), 'title': _('SMS aggregate report'), }) return render_to_response('admin/mod_sms/smsmessage/sms_report.html', context_instance=ctx)
def changelist_view(self, request, extra_context=None): """Override changelist_view method of django-admin for search parameters **Attributes**: * ``form`` - AdminSMSSearchForm * ``template`` - admin/mod_sms/smsmessage/sms_report.html **Logic Description**: * SMSMessage report Record Listing with search option search Parameters: by date, by status and by billed. """ opts = SMSMessage._meta query_string = '' form = AdminSMSSearchForm(request.POST or None) if form.is_valid(): query_string = sms_search_admin_form_fun(request) return HttpResponseRedirect("/admin/" + opts.app_label + "/" + opts.object_name.lower() + "/?" + query_string) else: status = '' from_date = '' to_date = '' smscampaign = '' if request.GET.get('send_date__gte'): from_date = variable_value(request, 'send_date__gte') if request.GET.get('send_date__lte'): to_date = variable_value(request, 'send_date__lte')[0:10] if request.GET.get('status__exact'): status = variable_value(request, 'status__exact') if request.GET.get('sms_campaign'): smscampaign = variable_value(request, 'sms_campaign') form = AdminSMSSearchForm( initial={ 'status': status, 'from_date': from_date, 'to_date': to_date, 'smscampaign': smscampaign }) ChangeList = self.get_changelist(request) try: cl = ChangeList(request, self.model, self.list_display, self.list_display_links, self.list_filter, self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_max_show_all, self.list_editable, self) except IncorrectLookupParameters: if ERROR_FLAG in request.GET.keys(): return render_to_response('admin/invalid_setup.html', {'title': _('Database error')}) return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1') kwargs = {} if request.META['QUERY_STRING'] == '': tday = datetime.utcnow().replace(tzinfo=utc) kwargs['send_date__gte'] = datetime(tday.year, tday.month, tday.day, 0, 0, 0, 0).replace(tzinfo=utc) cl.root_query_set.filter(**kwargs) cl.formset = None # Session variable is used to get record set with searched option into export file request.session['admin_sms_record_qs'] = cl.root_query_set selection_note_all = ungettext('%(total_count)s selected', 'All %(total_count)s selected', cl.result_count) ctx = { 'selection_note': _('0 of %(cnt)s selected') % { 'cnt': len(cl.result_list) }, 'selection_note_all': selection_note_all % { 'total_count': cl.result_count }, 'cl': cl, 'form': form, 'opts': opts, 'model_name': opts.object_name.lower(), 'app_label': _('SMS module'), 'title': _('SMS report'), } return super(SMSMessageAdmin, self).changelist_view(request, extra_context=ctx)