def test_time_ago_from_timestamp(date, exp): assert h.time_ago_from_timestamp(date) == exp
def dataset_comments(request, c, pkg_id): # [{ # 'date': ..., # 'thread': ..., # 'comment_body': ..., # 'user': ... # }] url = config.get('ckanext.canada.drupal_url') if not url: return [] r = requests.get(url + '/jsonapi/external_comment/external_comment', params={ '_format': 'api_json', 'filter[ckan][condition][path]': 'commented_external_entity', 'filter[ckan][condition][value]': 'ckan-{0}'.format(pkg_id), 'filter[status-filter][condition][path]': 'status', 'filter[status-filter][condition][value]': '1' }, auth=(config.get('ckanext.canada.drupal_user'), config.get('ckanext.canada.drupal_pass'))) j = r.json() comment_list = [{ 'date': h.time_ago_from_timestamp( datetime.datetime.utcfromtimestamp( rec['attributes']['changed']).replace(tzinfo=utc)), 'thread': rec['attributes']['thread'], 'user': rec['attributes']['name'], 'comment_body': clean_html(rec['attributes']['comment_body']['value']) } for rec in j['data']] sort_by = request.params.get('sort', 'time_descend') asc = sort_by == 'time_ascend' params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params, pkg_id) cbt = comments_by_thread(comment_list, asc), c.page = h.Page(collection=cbt, page=1, url=pager_url, item_count=len(comment_list), items_per_page=len(comment_list)) c.sort = sort_by return cbt[0]