Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Montreal latest changes #7

Open
wants to merge 2 commits into
base: montreal
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions ckanext/pages/actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import json
import sqlalchemy as sa

from ckan import model
import ckan.plugins as p
Expand All @@ -17,6 +18,10 @@

from ckanext.pages import db

import logging

log = logging.getLogger(__name__)


class HTMLFirstImage(HTMLParser):
def __init__(self):
Expand Down Expand Up @@ -44,6 +49,8 @@ def _pages_list(context, data_dict):
order_publish_date = data_dict.get('order_publish_date')
page_type = data_dict.get('page_type')
private = data_dict.get('private', True)
search_query = data_dict.get('q')

if ordered:
search['order'] = True
if page_type:
Expand All @@ -52,8 +59,12 @@ def _pages_list(context, data_dict):
search['order_publish_date'] = True
if not org_id:
search['group_id'] = None

try:
p.toolkit.check_access('ckanext_pages_update', context, data_dict)
p.toolkit.check_access(
'ckanext_pages_update',
context, data_dict
)
if not private:
search['private'] = False
except p.toolkit.NotAuthorized:
Expand All @@ -62,29 +73,43 @@ def _pages_list(context, data_dict):
group = context['model'].Group.get(org_id)
user = context['user']
member = authz.has_user_permission_for_group_or_org(
group.id, user, 'read')
group.id, user, 'read'
)
search['group_id'] = org_id

if not member:
search['private'] = False

if search_query:
search['q'] = search_query

out = db.Page.pages(**search)

out_list = []

for pg in out:
parser = HTMLFirstImage()
parser.feed(pg.content)
img = parser.first_image
pg_row = {'title': pg.title,
'content': pg.content,
'name': pg.name,
'publish_date': pg.publish_date.isoformat() if pg.publish_date else None,
'group_id': pg.group_id,
'page_type': pg.page_type,
}
pg_row = {
'title': pg.title,
'content': pg.content,
'name': pg.name,
'publish_date': pg.publish_date.isoformat() if pg.publish_date else None,
'group_id': pg.group_id,
'page_type': pg.page_type,
}

if img:
pg_row['image'] = img

extras = pg.extras

if extras:
pg_row.update(json.loads(pg.extras))

out_list.append(pg_row)

return out_list


Expand Down
16 changes: 16 additions & 0 deletions ckanext/pages/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
from ckan import model
from ckan.model.domain_object import DomainObject

import logging

log = logging.getLogger(__name__)

pages_table = None


Expand Down Expand Up @@ -45,15 +49,27 @@ def pages(cls, **kw):
'''Finds a single entity in the register.'''
order = kw.pop('order', False)
order_publish_date = kw.pop('order_publish_date', False)
search_query = kw.pop("q", None)

query = model.Session.query(cls).autoflush(False)
query = query.filter_by(**kw)

if search_query:
search_query = f'%{search_query}%'
query = query.filter(
sa.or_(
cls.content.ilike(search_query),
cls.title.ilike(search_query),
)
)

if order:
query = query.order_by(sa.cast(cls.order, sa.Integer)).filter(cls.order != '')
elif order_publish_date:
query = query.order_by(cls.publish_date.desc()).filter(cls.publish_date != None) # noqa: E711
else:
query = query.order_by(cls.created.desc())

return query.all()


Expand Down
13 changes: 13 additions & 0 deletions ckanext/pages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
import ckan.logic as logic
import ckan.lib.helpers as helpers

import logging


log = logging.getLogger(__name__)


config = tk.config
_ = tk._

Expand Down Expand Up @@ -35,6 +41,12 @@ def _parse_form_data(request):

def pages_list_pages(page_type):
data_dict = {'org_id': None, 'page_type': page_type}
query = tk.request.params.get('q', tk.request.args.get('q', ''))

if query:
data_dict['q'] = query
tk.c.q = query

if page_type == 'blog':
data_dict['order_publish_date'] = True
tk.c.pages_dict = tk.get_action('ckanext_pages_list')(
Expand All @@ -49,6 +61,7 @@ def pages_list_pages(page_type):

if page_type == 'blog':
return tk.render('ckanext_pages/blog_list.html')

return tk.render('ckanext_pages/pages_list.html')


Expand Down
Loading