def get(self, request, *args, **kwargs): """Return a basic get request using the StatsResult object""" # Get the StatsResult -- different for each subclass stats_result = self.get_stats_result(request) if stats_result is None: err_dict = dict(status="ERROR",\ message="Unknown processing error") return send_cors_response(JsonResponse(err_dict, status=500)) # Was there an error? If so, return the error message # if stats_result.has_error(): err_dict = dict(status="ERROR", message=stats_result.error_message) if stats_result.bad_http_status_code: status_code = stats_result.bad_http_status_code else: status_code = 400 return send_cors_response( JsonResponse(err_dict, status=status_code)) # Create the dict for the response # resp_dict = OrderedDict() # status is "OK" resp_dict['status'] = "OK" # Is we're in debug and the SQL query is available, # send it in if settings.DEBUG and stats_result.sql_query: resp_dict['debug'] = dict(sql_query=stats_result.sql_query) # Set a timestamp and params resp_dict['info'] = OrderedDict() resp_dict['info']['generation_time'] = datetime.now().strftime( "%Y-%m-%dT%H:%M:%S") if get_metrics_api_cache_time() > 0: resp_dict['info'][ 'cache_time_seconds'] = get_metrics_api_cache_time() resp_dict['info']['params'] = request.GET # Set the actual stats data resp_dict['data'] = stats_result.result_data # Is there a request to send the JSON formatted within HTML tags? if 'pretty' in request.GET: return HttpResponse('<pre>%s</pre>' % json.dumps(resp_dict, indent=4)) if StatsMakerBase.is_param_value_true(request.GET.get('as_csv', None)): return self.get_data_as_csv_response(request, stats_result) if StatsMakerBase.is_param_value_true(request.GET.get( 'as_excel', None)): return self.get_data_as_excel_response(request, stats_result) # Return the actual response return send_cors_response(JsonResponse(resp_dict))
def get(self, request, *args, **kwargs): """Return a basic get request using the StatsResult object""" # Get the StatsResult -- different for each subclass stats_result = self.get_stats_result(request) if stats_result is None: err_dict = dict(status="ERROR",\ message="Unknown processing error") return send_cors_response(JsonResponse(err_dict, status=500)) # Was there an error? If so, return the error message # if stats_result.has_error(): err_dict = dict(status="ERROR", message=stats_result.error_message) if stats_result.bad_http_status_code: status_code = stats_result.bad_http_status_code else: status_code = 400 return send_cors_response(JsonResponse(err_dict, status=status_code)) # Create the dict for the response # resp_dict = OrderedDict() # status is "OK" resp_dict['status'] = "OK" # Is we're in debug and the SQL query is available, # send it in if settings.DEBUG and stats_result.sql_query: resp_dict['debug'] = dict(sql_query=stats_result.sql_query) # Set a timestamp and params resp_dict['info'] = OrderedDict() resp_dict['info']['generation_time'] = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") if get_metrics_api_cache_time() > 0: resp_dict['info']['cache_time_seconds'] = get_metrics_api_cache_time() resp_dict['info']['params'] = request.GET # Set the actual stats data resp_dict['data'] = stats_result.result_data # Is there a request to send the JSON formatted within HTML tags? if 'pretty' in request.GET: return HttpResponse('<pre>%s</pre>' % json.dumps(resp_dict, indent=4)) if StatsMakerBase.is_param_value_true(request.GET.get('as_csv', None)): return self.get_data_as_csv_response(request, stats_result) if StatsMakerBase.is_param_value_true(request.GET.get('as_excel', None)): return self.get_data_as_excel_response(request, stats_result) # Return the actual response return send_cors_response(JsonResponse(resp_dict))
from dv_apps.utils.date_helper import get_timestamp_for_filename from dv_apps.metrics.stats_util_base import StatsMakerBase def send_cors_response(response): """Quick hack to allow CORS....""" response["Access-Control-Allow-Origin"] = "*" #response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS" #response["Access-Control-Max-Age"] = "1000" #response["Access-Control-Allow-Headers"] = "*" return response #@method_decorator(apikey_required, name='get') @method_decorator(cache_page(get_metrics_api_cache_time()), name='get') class StatsViewSwagger(View): """Used to help build the swagger docs""" # params getting a bit out of control, need to make this more manageable BASIC_DATE_PARAMS = ['startDateParam', 'endDateParam', 'selectedYearParam'] PARAM_DV_API_KEY = ['dataverseAPIKey'] PARAM_SELECTED_DV_ALIASES = ['selectedDataverseAliases'] PARAM_INCLUDE_CHILD_DVS = ['includeChildDataverses'] PARAM_AS_CSV = ['asCSV', 'asExcel'] PARAM_BIN_SIZE = ['binSize'] # bin_size PARAM_BIN_SIZE_BYTES = ['binSizeBytes'] # bin_size
stats_files = StatsMakerFiles(**request.GET.dict()) pub_state = self.get_pub_state(request) if pub_state == self.PUB_STATE_ALL: stats_result = stats_files.get_file_count_by_month() elif pub_state == self.PUB_STATE_UNPUBLISHED: stats_result = stats_files.get_file_count_by_month_unpublished() else: stats_result = stats_files.get_file_count_by_month_published() return stats_result @method_decorator(superuser_apikey_required, name='get') @method_decorator(cache_page(get_metrics_api_cache_time()), name='get') class FilesDownloadedByMonthView(StatsViewSwagger): """API View - Downloaded Files counts by Month.""" # Define the swagger attributes # Note: api_path must match the path in urls.py # api_path = '/files/downloads/count/monthly' summary = ('Number of downloaded Files by month') description = ('Returns a list of counts and' ' cumulative counts of all Files downloaded in a month.' ' Superuser access required.') description_200 = 'A list of file download counts by month.' tags = [StatsViewSwagger.TAG_DATAFILES] param_names = StatsViewSwagger.PARAM_DV_API_KEY +\