Skip to content

Commit

Permalink
Merge pull request #5 from datopian/fix/resource-feed-homepage
Browse files Browse the repository at this point in the history
Fix "latest  Updates" resource feed order on homepage
  • Loading branch information
MuhammadIsmailShahzad committed Dec 9, 2021
2 parents 6748f73 + b53aa01 commit 4cc37e7
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions ckanext/nhs/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import ckan.model as model
from ckan.common import config, is_flask_request, c, request
from ckan.plugins import toolkit
from ckan.lib.dictization import table_dictize
import ckan.lib.dictization.model_dictize as model_dictize
from sqlalchemy import select, join, and_, text
import logging
import ast
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -125,15 +128,34 @@ def get_latest_datasets():
return datasets

def get_latest_resources():
filter_private_resource = '%\\\\"level\\\\": \\\\"public\\\\"%'
resources = model.Session.query(model.Resource) \
.join(model.Package) \
.filter(model.Package.state == 'active') \
.filter(model.Package.private == False) \
.filter(model.Resource.state == 'active') \
.filter("resource.extras ILIKE \'%s\'" %(filter_private_resource)) \
.order_by(model.Resource.last_modified.desc()).limit(5)
return resources
private_resource_dict= '%\\\\"level\\\\": \\\\"public\\\\"%'
j_statement = join(model.Resource, model.Package, model.Package.id == model.Resource.package_id)
sql = select([
model.Resource.id,
model.Resource.name,
model.Resource.url,
model.Resource.format,
model.Resource.description,
model.Resource.size,
model.Resource.last_modified,
model.Resource.extras,
model.Resource.created,
model.Resource.package_id,
text("(CASE WHEN RESOURCE.last_modified <> RESOURCE.created THEN \
RESOURCE.last_modified ELSE RESOURCE.created END ) AS latest")
]).select_from(j_statement) \
.where(
and_(model.Package.state == 'active',
model.Package.private == False,
model.Resource.state == 'active',
text("resource.extras ILIKE \'%s\' or resource.extras ILIKE \'%s\' " % (private_resource_dict, '%{level: public}%' ))
)
).order_by("latest DESC").limit(5)
q_result = model.Session.execute(sql).fetchall()
resource_dict = []
for row in q_result:
resource_dict.append(table_dictize(row, { 'model': model, 'user': '' }))
return resource_dict

def get_cookie_control_config():

Expand Down

0 comments on commit 4cc37e7

Please sign in to comment.