Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/okfn/ckan
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Jun 22, 2012
2 parents fb86d64 + 62e4d53 commit e759ef9
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 172 deletions.
176 changes: 104 additions & 72 deletions ckan/controllers/api.py

Large diffs are not rendered by default.

31 changes: 2 additions & 29 deletions ckan/controllers/package.py
Expand Up @@ -94,14 +94,6 @@ def _guess_package_type(self, expecting_name=False):

return pt

def _setup_follow_button(self, context):
'''Setup some template context variables used for the Follow button.'''

# If the user is logged in set the am_following variable.
if c.user:
c.pkg_dict['am_following'] = get_action('am_following_dataset')(
context, {'id': c.pkg.id})

authorizer = ckan.authz.Authorizer()

def search(self):
Expand Down Expand Up @@ -293,10 +285,6 @@ def read(self, id, format='html'):
get_action('package_activity_list_html')(context,
{'id': c.current_package_id})

c.num_followers = get_action('dataset_follower_count')(context,
{'id':c.pkg.id})
self._setup_follow_button(context)

PackageSaver().render_package(c.pkg_dict, context)

template = self._read_template( package_type )
Expand Down Expand Up @@ -360,10 +348,6 @@ def history(self, id):
except NotFound:
abort(404, _('Dataset not found'))

c.num_followers = get_action('dataset_follower_count')(
context, {'id':c.pkg.id})
self._setup_follow_button(context)

format = request.params.get('format', '')
if format == 'atom':
# Generate and return Atom 1.0 document.
Expand Down Expand Up @@ -486,10 +470,6 @@ def edit(self, id, data=None, errors=None, error_summary=None):
else:
c.form = render(self._package_form(package_type=package_type), extra_vars=vars)

c.num_followers = get_action('dataset_follower_count')(context,
{'id':c.pkg.id})
self._setup_follow_button(context)

if (c.action == u'editresources'):
return render('package/editresources.html')
else:
Expand Down Expand Up @@ -592,6 +572,8 @@ def _save_new(self, context, package_type=None):

def _save_edit(self, name_or_id, context):
from ckan.lib.search import SearchIndexError
log.debug('Package save request name: %s POST: %r',
name_or_id, request.POST)
try:
data_dict = clean_dict(unflatten(
tuplize_dict(parse_params(request.POST))))
Expand Down Expand Up @@ -665,10 +647,6 @@ def authz(self, id):
roles = self._handle_update_of_authz(pkg)
self._prepare_authz_info_for_render(roles)

c.num_followers = get_action('dataset_follower_count')(context,
{'id':c.pkg.id})
self._setup_follow_button(context)

# c.related_count = len(pkg.related)

return render('package/authz.html')
Expand Down Expand Up @@ -757,9 +735,6 @@ def resource_read(self, id, resource_id):
qualified=True)

c.related_count = len(c.pkg.related)
c.num_followers = get_action('dataset_follower_count')(context,
{'id':c.pkg.id})
self._setup_follow_button(context)
return render('package/resource_read.html')

def resource_download(self, id, resource_id):
Expand Down Expand Up @@ -791,8 +766,6 @@ def followers(self, id=None):
c.pkg = context['package']
c.followers = get_action('dataset_follower_list')(context,
{'id': c.pkg_dict['id']})
c.num_followers = len(c.followers)
self._setup_follow_button(context)

c.related_count = len(c.pkg.related)
except NotFound:
Expand Down
7 changes: 0 additions & 7 deletions ckan/controllers/related.py
Expand Up @@ -34,12 +34,5 @@ def list(self, id):

c.related_count = len(c.pkg.related)

c.num_followers = logic.get_action('dataset_follower_count')(context,
{'id': c.pkg_dict['id']})
# If the user is logged in set the am_following variable.
if c.user:
c.pkg_dict['am_following'] = logic.get_action('am_following_dataset')(
context, {'id': c.pkg.id})

return base.render( "package/related_list.html")

19 changes: 0 additions & 19 deletions ckan/controllers/user.py
Expand Up @@ -46,22 +46,6 @@ def _db_to_edit_form_schema(self):
'''This is an interface to manipulate data from the database
into a format suitable for the form (optional)'''

def _setup_follow_button(self, context):
'''Setup some template context variables needed for the Follow/Unfollow
button.
'''

# If the user is logged in set the am_following variable.
userid = context.get('user')
if not userid:
return
userobj = model.User.get(userid)
if not userobj:
return
c.user_dict['am_following'] = get_action('am_following_user')(context,
{'id': c.user_dict['id']})

def _setup_template_variables(self, context, data_dict):
c.is_sysadmin = Authorizer().is_sysadmin(c.user)
try:
Expand All @@ -72,9 +56,6 @@ def _setup_template_variables(self, context, data_dict):
abort(401, _('Not authorized to see this page'))
c.user_dict = user_dict
c.is_myself = user_dict['name'] == c.user
c.num_followers = get_action('user_follower_count')(context,
{'id':c.user_dict['id']})
self._setup_follow_button(context)

## end hooks

Expand Down
6 changes: 6 additions & 0 deletions ckan/lib/authenticator.py
@@ -1,8 +1,12 @@
import logging

from zope.interface import implements
from repoze.who.interfaces import IAuthenticator

from ckan.model import User, Session

log = logging.getLogger(__name__)

class OpenIDAuthenticator(object):
implements(IAuthenticator)

Expand All @@ -25,8 +29,10 @@ def authenticate(self, environ, identity):
return None
user = User.by_name(identity.get('login'))
if user is None:
log.debug('Login failed - username %r not found', identity.get('login'))
return None
if user.validate_password(identity.get('password')):
return user.name
log.debug('Login as %r failed - password not valid', identity.get('login'))
return None

3 changes: 3 additions & 0 deletions ckan/lib/base.py
Expand Up @@ -29,6 +29,8 @@
from ckan.lib.helpers import json
import ckan.model as model

log = logging.getLogger(__name__)

PAGINATE_ITEMS_PER_PAGE = 50

APIKEY_HEADER_NAME_KEY = 'apikey_header_name'
Expand Down Expand Up @@ -139,6 +141,7 @@ def render_template():
response.headers["Cache-Control"] = "private"
# Prevent any further rendering from being cached.
request.environ['__no_cache__'] = True
log.debug('Template cache-control: %s' % response.headers["Cache-Control"])

# Render Time :)
try:
Expand Down
2 changes: 2 additions & 0 deletions ckan/lib/create_test_data.py
Expand Up @@ -511,6 +511,7 @@ def create(cls, auth_profile="", package_type=None):

model.repo.commit_and_remove()

# method used in DGU and all good tests elsewhere
@classmethod
def create_users(cls, user_dicts):
needs_commit = False
Expand All @@ -537,6 +538,7 @@ def _create_user_without_commit(cls, name='', **user_dict):
user = model.User(name=unicode(name), **user_dict)
model.Session.add(user)
cls.user_refs.append(user_ref)
return user

@classmethod
def create_user(cls, name='', **kwargs):
Expand Down
54 changes: 54 additions & 0 deletions ckan/lib/helpers.py
Expand Up @@ -819,6 +819,58 @@ def process_names(items):
items.append(item)
return items

# these are the types of objects that can be followed
_follow_objects = ['dataset', 'user']

def follow_button(obj_type, obj_id):
'''Return a follow button for the given object type and id.
If the user is not logged in return an empty string instead.
:param obj_type: the type of the object to be followed when the follow
button is clicked, e.g. 'user' or 'dataset'
:type obj_type: string
:param obj_id: the id of the object to be followed when the follow button
is clicked
:type obj_id: string
:returns: a follow button as an HTML snippet
:rtype: string
'''
import ckan.logic as logic
obj_type = obj_type.lower()
assert obj_type in _follow_objects
# If the user is logged in show the follow/unfollow button
if c.user:
context = {'model' : model, 'session':model.Session, 'user':c.user}
action = 'am_following_%s' % obj_type
following = logic.get_action(action)(context, {'id': obj_id})
return snippet('snippets/follow_button.html',
following=following,
obj_id=obj_id,
obj_type=obj_type)
return ''

def follow_count(obj_type, obj_id):
'''Return the number of followers of an object.
:param obj_type: the type of the object, e.g. 'user' or 'dataset'
:type obj_type: string
:param obj_id: the id of the object
:type obj_id: string
:returns: the number of followers of the object
:rtype: int
'''
import ckan.logic as logic
obj_type = obj_type.lower()
assert obj_type in _follow_objects
action = '%s_follower_count' % obj_type
context = {'model' : model, 'session':model.Session, 'user':c.user}
return logic.get_action(action)(context, {'id': obj_id})


# these are the functions that will end up in `h` template helpers
# if config option restrict_template_vars is true
Expand Down Expand Up @@ -873,6 +925,8 @@ def process_names(items):
'activity_div',
'lang_native_name',
'unselected_facet_items',
'follow_button',
'follow_count',
# imported into ckan.lib.helpers
'literal',
'link_to',
Expand Down
13 changes: 7 additions & 6 deletions ckan/lib/search/__init__.py
Expand Up @@ -128,33 +128,34 @@ def rebuild(package_id=None,only_missing=False,force=False,refresh=False):
If a dataset id is provided, only this dataset will be reindexed.
When reindexing all datasets, if only_missing is True, only the
datasets not already indexed will be processed. If force equals
True, if an execption is found, the exception will be logged, but
True, if an exception is found, the exception will be logged, but
the process will carry on.
'''
from ckan import model
log.debug("Rebuilding search index...")
log.info("Rebuilding search index...")

package_index = index_for(model.Package)

if package_id:
pkg_dict = get_action('package_show')(
{'model': model, 'ignore_auth': True, 'validate': False},
{'id': package_id})
log.info('Indexing just package %r...', pkg_dict['name'])
package_index.remove_dict(pkg_dict)
package_index.insert_dict(pkg_dict)
else:
package_ids = [r[0] for r in model.Session.query(model.Package.id).filter(model.Package.state == 'active').all()]
if only_missing:
log.debug('Indexing only missing packages...')
log.info('Indexing only missing packages...')
package_query = query_for(model.Package)
indexed_pkg_ids = set(package_query.get_all_entity_ids(max_results=len(package_ids)))
package_ids = set(package_ids) - indexed_pkg_ids # Packages not indexed

if len(package_ids) == 0:
log.debug('All datasets are already indexed')
log.info('All datasets are already indexed')
return
else:
log.debug('Rebuilding the whole index...')
log.info('Rebuilding the whole index...')
# When refreshing, the index is not previously cleared
if not refresh:
package_index.clear()
Expand All @@ -176,7 +177,7 @@ def rebuild(package_id=None,only_missing=False,force=False,refresh=False):
raise

model.Session.commit()
log.debug('Finished rebuilding search index.')
log.info('Finished rebuilding search index.')

def check():
from ckan import model
Expand Down

0 comments on commit e759ef9

Please sign in to comment.