From 97cd64b1724fec4770b84e4563be5c8670d5594f Mon Sep 17 00:00:00 2001 From: Petar Efnushev Date: Tue, 5 Apr 2016 23:53:14 +0200 Subject: [PATCH] Added harvest_log_list get action --- ckanext/harvest/logic/action/get.py | 38 ++++++++++++++++++++++++++-- ckanext/harvest/logic/dictization.py | 2 ++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/ckanext/harvest/logic/action/get.py b/ckanext/harvest/logic/action/get.py index 77a2de6..773b6c3 100644 --- a/ckanext/harvest/logic/action/get.py +++ b/ckanext/harvest/logic/action/get.py @@ -1,4 +1,5 @@ import logging +from itertools import groupby from sqlalchemy import or_ from ckan.model import User import datetime @@ -12,10 +13,11 @@ from ckan.logic import NotFound, check_access, side_effect_free from ckanext.harvest import model as harvest_model -from ckanext.harvest.model import (HarvestSource, HarvestJob, HarvestObject) +from ckanext.harvest.model import (HarvestSource, HarvestJob, HarvestObject, HarvestLog) from ckanext.harvest.logic.dictization import (harvest_source_dictize, harvest_job_dictize, - harvest_object_dictize) + harvest_object_dictize, + harvest_log_dictize) log = logging.getLogger(__name__) @@ -310,6 +312,38 @@ def harvesters_info_show(context,data_dict): return available_harvesters +@side_effect_free +def harvest_log_list(context,data_dict): + '''Returns a list of harvester log entries grouped by level. + + :param per_page: number of logs to be shown default: 100 + :param offset: use with ``per_page`` default: 0 + :param level: filter log entries by level(debug, info, warning, error, critical) + ''' + + check_access('harvest_log_list', context, data_dict) + + model = context['model'] + session = context['session'] + + per_page = data_dict.get('per_page', 100) + offset = data_dict.get('offset', 0) + level = data_dict.get('level', False) + + query = session.query(HarvestLog) + + if level: + query = query.filter(HarvestLog.level==level.upper()) + + query = query.order_by(HarvestLog.level.desc(), HarvestLog.created.desc()) + logs = query.offset(offset).limit(per_page).all() + + out = dict() + for k, g in groupby(logs, lambda l: l.level): + out.update({k: [harvest_log_dictize(obj, context) for obj in g]}) + + return out + def _get_sources_for_user(context,data_dict): model = context['model'] diff --git a/ckanext/harvest/logic/dictization.py b/ckanext/harvest/logic/dictization.py index 9c102ee..1e0a53f 100644 --- a/ckanext/harvest/logic/dictization.py +++ b/ckanext/harvest/logic/dictization.py @@ -97,6 +97,8 @@ def harvest_object_dictize(obj, context): return out +def harvest_log_dictize(obj, context): + return obj.as_dict() def _get_source_status(source, context): '''