Skip to content

Commit

Permalink
Merge branch 'feature-1607-dgu-maintentance-refactor'
Browse files Browse the repository at this point in the history
Conflicts:
	ckan/controllers/group.py
	ckan/controllers/package.py
	ckan/controllers/user.py
	ckan/lib/alphabet_paginate.py
	ckan/lib/create_test_data.py
	ckan/lib/dictization/model_dictize.py
	ckan/lib/dictization/model_save.py
	ckan/templates/package/edit.html
	ckan/tests/functional/test_group.py
	ckan/tests/lib/test_dictization.py

The source of much conflict was the moving of much code from the
controllers/{package, group}.py modules into lib/plugins.py.  It
turns out there wasn't too much to do there, but it required a
fair bit of checking.
  • Loading branch information
Ian Murray committed Mar 9, 2012
2 parents a6aca28 + 0efe8a7 commit d1ba07f
Show file tree
Hide file tree
Showing 35 changed files with 647 additions and 295 deletions.
5 changes: 5 additions & 0 deletions ckan/config/deployment.ini_tmpl
Expand Up @@ -111,6 +111,11 @@ ckan.site_url =
## Favicon (default is the CKAN software favicon)
ckan.favicon = /images/icons/ckan.ico

## The gravatar default to use. This can be any of the pre-defined strings
## as defined on http://en.gravatar.com/site/implement/images/ (e.g. "identicon"
## or "mm"). Or it can be a url, e.g. "http://example.com/images/avatar.jpg"
ckan.gravatar_default = identicon

## Solr support
#solr_url = http://127.0.0.1:8983/solr

Expand Down
27 changes: 6 additions & 21 deletions ckan/controllers/group.py
Expand Up @@ -18,7 +18,6 @@

log = logging.getLogger(__name__)


class GroupController(BaseController):

## hooks for subclasses
Expand Down Expand Up @@ -244,28 +243,14 @@ def edit(self, id, data=None, errors=None, error_summary=None):

def _get_group_type(self, id):
"""
Given the id of a group it determines the plugin to load
based on the group's type name (type). The plugin found
will be returned, or None if there is no plugin associated with
the type.
Uses a minimal context to do so. The main use of this method
is for figuring out which plugin to delegate to.
aborts if an exception is raised.
Given the id of a group it determines the type of a group given
a valid id/name for the group.
"""
global _controller_behaviour_for

context = {'model': model, 'session': model.Session,
'user': c.user or c.author}
try:
data = get_action('group_show')(context, {'id': id})
except NotFound:
abort(404, _('Group not found'))
except NotAuthorized:
abort(401, _('Unauthorized to read group %s') % id)
return data['type']
group = model.Group.get( id )
if not group:
return None

return group.type

def _save_new(self, context, group_type=None):
try:
Expand Down
4 changes: 2 additions & 2 deletions ckan/controllers/package.py
Expand Up @@ -325,6 +325,7 @@ def new(self, data=None, errors=None, error_summary=None):
errors = errors or {}
error_summary = error_summary or {}
vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
c.errors_json = json.dumps(errors)

self._setup_template_variables(context, {'id': id})

Expand Down Expand Up @@ -370,6 +371,7 @@ def edit(self, id, data=None, errors=None, error_summary=None):

errors = errors or {}
vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
c.errors_json = json.dumps(errors)

self._setup_template_variables(context, {'id': id}, package_type=package_type)

Expand Down Expand Up @@ -450,8 +452,6 @@ def _get_package_type(self, id):
aborts if an exception is raised.
"""
global _controller_behaviour_for

context = {'model': model, 'session': model.Session,
'user': c.user or c.author}
try:
Expand Down
20 changes: 10 additions & 10 deletions ckan/controllers/user.py
Expand Up @@ -11,7 +11,7 @@
from ckan.logic import NotFound, NotAuthorized, ValidationError
from ckan.logic import check_access, get_action
from ckan.logic import tuplize_dict, clean_dict, parse_params
from ckan.logic.schema import user_new_form_schema, user_edit_form_schema
from ckan.logic.schema import user_new_form_schema, user_edit_form_schema
from ckan.logic.action.get import user_activity_list_html
from ckan.lib.captcha import check_recaptcha, CaptchaError

Expand All @@ -28,7 +28,7 @@ def __before__(self, action, **env):
if c.action not in ('login','request_reset','perform_reset',):
abort(401, _('Not authorized to see this page'))

## hooks for subclasses
## hooks for subclasses
new_user_form = 'user/new_user_form.html'
edit_user_form = 'user/edit_user_form.html'

Expand Down Expand Up @@ -207,10 +207,10 @@ def edit(self, id=None, data=None, errors=None, error_summary=None):
abort(404, _('User not found'))

user_obj = context.get('user_obj')

if not (ckan.authz.Authorizer().is_sysadmin(unicode(c.user)) or c.user == user_obj.name):
abort(401, _('User %s not authorized to edit %s') % (str(c.user), id))

errors = errors or {}
vars = {'data': data, 'errors': errors, 'error_summary': error_summary}

Expand Down Expand Up @@ -255,7 +255,7 @@ def login(self):
return render('user/login.html')
else:
return render('user/logout_first.html')

def logged_in(self):
if c.user:
context = {'model': model,
Expand All @@ -277,14 +277,14 @@ def logged_in(self):
h.flash_error('Login failed. Bad username or password.' + \
' (Or if using OpenID, it hasn\'t been associated with a user account.)')
h.redirect_to(controller='user', action='login')

def logged_out(self):
c.user = None
response.delete_cookie("ckan_user")
response.delete_cookie("ckan_display_name")
response.delete_cookie("ckan_apikey")
return render('user/logout.html')

def request_reset(self):
if request.method == 'POST':
id = request.params.get('user')
Expand Down Expand Up @@ -346,7 +346,7 @@ def perform_reset(self, id):

if request.method == 'POST':
try:
context['reset_password'] = True
context['reset_password'] = True
new_password = self._get_form_password()
user_dict['password'] = new_password
user_dict['reset_key'] = c.reset_key
Expand Down Expand Up @@ -374,7 +374,7 @@ def _format_about(self, about):
log.error('Could not print "about" field Field: %r Error: %r', about, e)
html = _('Error: Could not parse About text')
return html

def _get_form_password(self):
password1 = request.params.getone('password1')
password2 = request.params.getone('password2')
Expand All @@ -384,4 +384,4 @@ def _get_form_password(self):
elif not password1 == password2:
raise ValueError(_("The passwords you entered do not match."))
return password1

9 changes: 6 additions & 3 deletions ckan/lib/alphabet_paginate.py
Expand Up @@ -45,11 +45,14 @@ def __init__(self, collection, alpha_attribute, page, other_text, paging_thresho
self.controller_name = controller_name
self.available = dict( (c,0,) for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" )
for c in self.collection:
x = c[0] if isinstance( c, unicode ) else getattr(c, self.alpha_attribute)[0]
if isinstance(c, unicode):
x = c[0]
elif isinstance(c, dict):
x = c[self.alpha_attribute][0]
else:
x = getattr(c, self.alpha_attribute)[0]
self.available[x] = self.available.get(x, 0) + 1



def pager(self, q=None):
'''Returns pager html - for navigating between the pages.
e.g. Something like this:
Expand Down
8 changes: 4 additions & 4 deletions ckan/lib/authenticator.py
Expand Up @@ -5,7 +5,7 @@

class OpenIDAuthenticator(object):
implements(IAuthenticator)

def authenticate(self, environ, identity):
if 'repoze.who.plugins.openid.userid' in identity:
openid = identity.get('repoze.who.plugins.openid.userid')
Expand All @@ -15,16 +15,16 @@ def authenticate(self, environ, identity):
else:
return user.name
return None


class UsernamePasswordAuthenticator(object):
implements(IAuthenticator)

def authenticate(self, environ, identity):
if not 'login' in identity or not 'password' in identity:
return None
user = User.by_name(identity.get('login'))
if user is None:
if user is None:
return None
if user.validate_password(identity.get('password')):
return user.name
Expand Down
44 changes: 22 additions & 22 deletions ckan/lib/create_test_data.py
Expand Up @@ -22,7 +22,7 @@ class CreateTestData(cli.CkanCommand):
tag_names = []
group_names = set()
user_refs = []

pkg_core_fields = ['name', 'title', 'version', 'url', 'notes',
'author', 'author_email',
'maintainer', 'maintainer_email',
Expand Down Expand Up @@ -89,7 +89,7 @@ def create_test_user(cls):

@classmethod
def create_arbitrary(cls, package_dicts, relationships=[],
extra_user_names=[], extra_group_names=[],
extra_user_names=[], extra_group_names=[],
admins=[]):
'''Creates packages and a few extra objects as well at the
same time if required.
Expand All @@ -101,7 +101,7 @@ def create_arbitrary(cls, package_dicts, relationships=[],
@param extra_group_names - a list of group names to create. No
properties get set though.
@param admins - a list of user names to make admins of all the
packages created.
packages created.
'''
assert isinstance(relationships, (list, tuple))
assert isinstance(extra_user_names, (list, tuple))
Expand All @@ -111,11 +111,11 @@ def create_arbitrary(cls, package_dicts, relationships=[],
new_user_names = extra_user_names
new_group_names = set()
new_groups = {}
rev = model.repo.new_revision()

rev = model.repo.new_revision()
rev.author = cls.author
rev.message = u'Creating test packages.'

admins_list = defaultdict(list) # package_name: admin_names
if package_dicts:
if isinstance(package_dicts, dict):
Expand All @@ -131,7 +131,7 @@ def create_arbitrary(cls, package_dicts, relationships=[],
if isinstance(val, str):
val = unicode(val)
if attr=='name':
continue
continue
if attr in cls.pkg_core_fields:
pass
elif attr == 'download_url':
Expand Down Expand Up @@ -160,7 +160,7 @@ def create_arbitrary(cls, package_dicts, relationships=[],
if not tag:
tag = model.Tag(name=tag_name)
cls.tag_names.append(tag_name)
model.Session.add(tag)
model.Session.add(tag)
pkg.add_tag(tag)
model.Session.flush()
elif attr == 'groups':
Expand Down Expand Up @@ -208,8 +208,8 @@ def create_arbitrary(cls, package_dicts, relationships=[],
model.repo.commit_and_remove()

needs_commit = False
rev = model.repo.new_revision()

rev = model.repo.new_revision()
for group_name in extra_group_names:
group = model.Group(name=unicode(group_name))
model.Session.add(group)
Expand Down Expand Up @@ -258,7 +258,7 @@ def create_arbitrary(cls, package_dicts, relationships=[],
needs_commit = False

if relationships:
rev = model.repo.new_revision()
rev = model.repo.new_revision()
rev.author = cls.author
rev.message = u'Creating package relationships.'

Expand All @@ -270,7 +270,7 @@ def pkg(pkg_name):
needs_commit = True

model.repo.commit_and_remove()


@classmethod
def create_groups(cls, group_dicts, admin_user_name=None, auth_profile=""):
Expand Down Expand Up @@ -324,7 +324,7 @@ def create(cls, auth_profile="", package_type=None):

cls.pkg_names = [u'annakarenina', u'warandpeace']
pkg1 = model.Package(name=cls.pkg_names[0], type=package_type)
if auth_profile == "publisher":
if auth_profile == "publisher":
pkg1.group = publisher_group
model.Session.add(pkg1)
pkg1.title = u'A Novel By Tolstoy'
Expand Down Expand Up @@ -368,7 +368,7 @@ def create(cls, auth_profile="", package_type=None):
u with umlaut \xfc
66-style quote \u201c
foreign word: th\xfcmb
Needs escaping:
left arrow <
Expand All @@ -379,7 +379,7 @@ def create(cls, auth_profile="", package_type=None):
tag1 = model.Tag(name=u'russian')
tag2 = model.Tag(name=u'tolstoy')

if auth_profile == "publisher":
if auth_profile == "publisher":
pkg2.group = publisher_group

# Flexible tag, allows spaces, upper-case,
Expand Down Expand Up @@ -407,12 +407,12 @@ def create(cls, auth_profile="", package_type=None):
type=auth_profile or 'group')
for obj in [david, roger]:
model.Session.add(obj)

cls.group_names.add(u'david')
cls.group_names.add(u'roger')

model.Session.flush()

model.Session.add(model.Member(table_id=pkg1.id, table_name='package', group=david))
model.Session.add(model.Member(table_id=pkg2.id, table_name='package', group=david))
model.Session.add(model.Member(table_id=pkg1.id, table_name='package', group=roger))
Expand Down Expand Up @@ -447,7 +447,7 @@ def create(cls, auth_profile="", package_type=None):

# Create a couple of authorization groups
for ag_name in [u'anauthzgroup', u'anotherauthzgroup']:
ag=model.AuthorizationGroup.by_name(ag_name)
ag=model.AuthorizationGroup.by_name(ag_name)
if not ag: #may already exist, if not create
ag=model.AuthorizationGroup(name=ag_name)
model.Session.add(ag)
Expand Down Expand Up @@ -559,7 +559,7 @@ def get_all_data(cls):
'groups':'ukgov test1 test2 penguin',
'license':'odc-by',
'notes':u'''From <http://www.gpoaccess.gov/gils/about.html>
> The Government Information Locator Service (GILS) is an effort to identify, locate, and describe publicly available Federal
> Because this collection is decentralized, the GPO
Expand Down Expand Up @@ -604,7 +604,7 @@ def get_all_data(cls):
{'name':'uk-government-expenditure',
'title':'UK Government Expenditure',
'tags':'workshop-20081101,uk,gov,expenditure,finance,public,funding,penguin'.split(','),
'groups':'ukgov penguin',
'groups':'ukgov penguin',
'notes':'''Discussed at [Workshop on Public Information, 2008-11-02](http://okfn.org/wiki/PublicInformation).
Overview is available in Red Book, or Financial Statement and Budget Report (FSBR), [published by the Treasury](http://www.hm-treasury.gov.uk/budget.htm).''',
Expand All @@ -613,7 +613,7 @@ def get_all_data(cls):
{'name':'se-publications',
'title':'Sweden - Government Offices of Sweden - Publications',
'url':'http://www.sweden.gov.se/sb/d/574',
'groups':'penguin',
'groups':'penguin',
'tags':u'country-sweden,format-pdf,access-www,documents,publications,government,eutransparency,penguin,CAPITALS,surprise.,greek omega \u03a9,japanese katakana \u30a1'.split(','),
'license':'',
'notes':'''### About
Expand All @@ -627,7 +627,7 @@ def get_all_data(cls):
},
{'name':'se-opengov',
'title':'Opengov.se',
'groups':'penguin',
'groups':'penguin',
'url':'http://www.opengov.se/',
'download_url':'http://www.opengov.se/data/open/',
'tags':'country-sweden,government,data,penguin'.split(','),
Expand Down

0 comments on commit d1ba07f

Please sign in to comment.