def search_stats(request): period_form = forms.PeriodForm(request.GET, prefix='pe') responce_dict = { 'not_specified': [], 'catalogs': {} } if period_form.is_valid(): from_date, to_date = period_form.get_period_dates() period = period_form.cleaned_data['period'] results = request_group_by_date( from_date=from_date, to_date=to_date, period=period, ) responce_dict['not_specified'] = results catalogs = ['sc2', 'ebooks'] for catalog in catalogs: results = request_group_by_date( from_date=from_date, to_date=to_date, period=period, catalog=catalog ) responce_dict['catalogs'][catalog] = results return HttpResponse(json.dumps(responce_dict, ensure_ascii=False), content_type='application/json')
def org_stats(request): org_code = request.GET.get('org_code', None) org_name = '' libs = Library.objects.filter(code=org_code)[:1] if libs: org_name = libs[0].name if org_code and not Library.objects.filter(code=org_code).exists(): return HttpResponse(u'Org with code %s not exist' % org_code, status=400) responce_dict = { 'org_code': org_code, 'org_name': org_name } period_form = forms.PeriodForm(request.GET, prefix='pe') url_filter = '/' if org_code: url_filter = u'/site/%s/' % org_code if period_form.is_valid(): from_date = period_form.cleaned_data['from_date'] to_date = period_form.cleaned_data['to_date'] period = period_form.cleaned_data['period'] url_filter = url_filter results = models.get_view_count_stats( from_date=from_date, to_date=to_date, period=period, url_filter=url_filter ) responce_dict['views'] = results results = models.get_view_count_stats( from_date=from_date, to_date=to_date, period=period, url_filter=url_filter, visit_type='visit' ) responce_dict['visits'] = results results = request_group_by_date( from_date=from_date, to_date=to_date, period=period, library_code=org_code ) responce_dict['search_requests'] = results else: return HttpResponse(u'Wrong pe params %s' % period_form.errors, status=400) return HttpResponse(json.dumps(responce_dict, ensure_ascii=False), content_type='application/json')
def org_stats(request): params = request.GET.get('params', '') params_parts = [] for param in params.strip().split(','): if param: params_parts.append(param) scheme = request.GET.get('scheme', 'xml') schemes = ['xml', 'json'] if scheme not in schemes: scheme = 'xml' org_code = request.GET.get('code', None) org=None org_name = '' if org_code: try: org = Library.objects.get(code=org_code) org_name = org.name except Library.DoesNotExist: return HttpResponse(u'Org with code %s not exist' % org_code, status=400) # if org_code and not Library.objects.filter(code=org_code).exists(): # return HttpResponse(u'Org with code %s not exist' % org_code, status=400) responce_dict = { 'org_code': org_code, 'org_name': org_name, } period_form = forms.PeriodForm(request.GET, prefix='pe') url_filter = '/' if org_code: url_filter = u'/site/%s/' % org_code if period_form.is_valid(): from_date, to_date = period_form.get_period_dates() period = period_form.cleaned_data['period'] url_filter = url_filter if not params_parts or 'views' in params_parts: results = models.get_view_count_stats( from_date=from_date, to_date=to_date, period=period, url_filter=url_filter ) responce_dict['views'] = results if not params_parts or 'visits' in params_parts: results = models.get_view_count_stats( from_date=from_date, to_date=to_date, period=period, url_filter=url_filter, visit_type='visit' ) responce_dict['visits'] = results if not params_parts or 'search_requests' in params_parts: results = request_group_by_date( from_date=from_date, to_date=to_date, period=period, library_code=org_code ) responce_dict['search_requests'] = results else: return HttpResponse(u'Wrong pe params %s' % period_form.errors, status=400) if scheme == 'xml': return HttpResponse(dicttoxml.dicttoxml( responce_dict, custom_root='fields', attr_type=False), content_type='application/xml' ) return HttpResponse(json.dumps(responce_dict, ensure_ascii=False), content_type='application/json')