Skip to content

Commit

Permalink
Fixing problem with publisher_form and moving setup_template_variable…
Browse files Browse the repository at this point in the history
…s out of plugin
  • Loading branch information
rossjones committed Feb 24, 2012
1 parent 93f72e3 commit 277bb76
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 78 deletions.
2 changes: 1 addition & 1 deletion ckan/controllers/group.py
Expand Up @@ -176,7 +176,7 @@ def _form_to_db_schema(self, group_type=None):
def _db_to_form_schema(self, group_type=None):
'''This is an interface to manipulate data from the database
into a format suitable for the form (optional)'''
return _lookup_plugin(group_type).form_to_db_schema()
return _lookup_plugin(group_type).db_to_form_schema()

def _setup_template_variables(self, context, data_dict, group_type=None):
return _lookup_plugin(group_type).setup_template_variables(context,data_dict)
Expand Down
8 changes: 6 additions & 2 deletions ckan/lib/alphabet_paginate.py
Expand Up @@ -45,11 +45,15 @@ 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 if isinstance( c, unicode ) else getattr(c, self.alpha_attribute)[0]
if isinstance(c, unicode):
x = c
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
8 changes: 4 additions & 4 deletions ckan/lib/dictization/__init__.py
Expand Up @@ -3,7 +3,7 @@
import sqlalchemy
from pylons import config

# NOTE
# NOTE
# The functions in this file contain very generic methods for dictizing objects
# and saving dictized objects. If a specialised use is needed please do NOT extend
# these functions. Copy code from here as needed.
Expand Down Expand Up @@ -68,7 +68,7 @@ def obj_list_dictize(obj_list, context, sort_key=lambda x:x):
return sorted(result_list, key=sort_key)

def obj_dict_dictize(obj_dict, context, sort_key=lambda x:x):
'''Get a dict whose values are model objects
'''Get a dict whose values are model objects
and represent it as a list of dicts'''

result_list = []
Expand All @@ -93,7 +93,7 @@ def get_unique_constraints(table, context):

def table_dict_save(table_dict, ModelClass, context):
'''Given a dict and a model class, update or create a sqlalchemy object.
This will use an existing object if "id" is supplied OR if any unique
This will use an existing object if "id" is supplied OR if any unique
constraints are met. e.g supplying just a tag name will get out that tag obj.
'''

Expand All @@ -107,7 +107,7 @@ def table_dict_save(table_dict, ModelClass, context):
unique_constriants = get_unique_constraints(table, context)

id = table_dict.get("id")

if id:
obj = session.query(ModelClass).get(id)

Expand Down
76 changes: 38 additions & 38 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -12,7 +12,7 @@

## package save

def group_list_dictize(obj_list, context,
def group_list_dictize(obj_list, context,
sort_key=lambda x:x['display_name'], reverse=False):

active = context.get('active', True)
Expand Down Expand Up @@ -93,10 +93,10 @@ def _execute_with_revision(q, rev_table, context):
But you can provide revision_id, revision_date or pending in the
context and it will filter to an earlier time or the latest unmoderated
object revision.
Raises NotFound if context['revision_id'] is provided, but the revision
ID does not exist.
Returns [] if there are no results.
'''
Expand All @@ -113,7 +113,7 @@ def _execute_with_revision(q, rev_table, context):
if not revision:
raise NotFound
revision_date = revision.timestamp

if revision_date:
q = q.where(rev_table.c.revision_timestamp <= revision_date)
q = q.where(rev_table.c.expired_timestamp > revision_date)
Expand All @@ -133,7 +133,7 @@ def package_dictize(pkg, context):
but you can provide revision_id, revision_date or pending in the
context and it will filter to an earlier time or the latest unmoderated
object revision.
May raise NotFound. TODO: understand what the specific set of
circumstances are that cause this.
'''
Expand All @@ -148,15 +148,15 @@ def package_dictize(pkg, context):
#resources
res_rev = model.resource_revision_table
resource_group = model.resource_group_table
q = select([res_rev], from_obj = res_rev.join(resource_group,
q = select([res_rev], from_obj = res_rev.join(resource_group,
resource_group.c.id == res_rev.c.resource_group_id))
q = q.where(resource_group.c.package_id == pkg.id)
result = _execute_with_revision(q, res_rev, context)
result_dict["resources"] = resource_list_dictize(result, context)
#tags
tag_rev = model.package_tag_revision_table
tag = model.tag_table
q = select([tag, tag_rev.c.state, tag_rev.c.revision_timestamp],
q = select([tag, tag_rev.c.state, tag_rev.c.revision_timestamp],
from_obj=tag_rev.join(tag, tag.c.id == tag_rev.c.tag_id)
).where(tag_rev.c.package_id == pkg.id)
result = _execute_with_revision(q, tag_rev, context)
Expand All @@ -183,7 +183,7 @@ def package_dictize(pkg, context):
q = select([rel_rev]).where(rel_rev.c.object_package_id == pkg.id)
result = _execute_with_revision(q, rel_rev, context)
result_dict["relationships_as_object"] = obj_list_dictize(result, context)

# Extra properties from the domain object
# We need an actual Package object for this, not a PackageRevision
if isinstance(pkg,PackageRevision):
Expand Down Expand Up @@ -264,10 +264,10 @@ def tag_dictize(tag, context):

result_dict["packages"] = obj_list_dictize(
tag.packages_ordered, context)

return result_dict

def user_list_dictize(obj_list, context,
return result_dict

def user_list_dictize(obj_list, context,
sort_key=lambda x:x['name'], reverse=False):

result_list = []
Expand All @@ -288,37 +288,37 @@ def user_dictize(user, context):
result_dict = table_dictize(user, context)

del result_dict['password']

result_dict['display_name'] = user.display_name
result_dict['email_hash'] = user.email_hash
result_dict['number_of_edits'] = user.number_of_edits()
result_dict['number_administered_packages'] = user.number_administered_packages()

return result_dict
return result_dict

def task_status_dictize(task_status, context):
return table_dictize(task_status, context)

## conversion to api

def group_to_api1(group, context):

dictized = group_dictize(group, context)
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
for extra in dictized["extras"])
dictized["packages"] = sorted([package["name"] for package in dictized["packages"]])
return dictized

def group_to_api2(group, context):

dictized = group_dictize(group, context)
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
for extra in dictized["extras"])
dictized["packages"] = sorted([package["id"] for package in dictized["packages"]])
return dictized

def tag_to_api1(tag, context):

dictized = tag_dictize(tag, context)
return sorted([package["name"] for package in dictized["packages"]])

Expand All @@ -342,18 +342,18 @@ def package_to_api1(pkg, context):

dictized["groups"] = [group["name"] for group in dictized["groups"]]
dictized["tags"] = [tag["name"] for tag in dictized["tags"]]
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
for extra in dictized["extras"])
dictized['notes_rendered'] = ckan.misc.MarkdownFormat().to_html(pkg.notes)

resources = dictized["resources"]
resources = dictized["resources"]

for resource in resources:
resource_dict_to_api(resource, pkg.id, context)

if pkg.resources:
dictized['download_url'] = pkg.resources[0].url

dictized['license'] = pkg.license.title if pkg.license else None

dictized['ratings_average'] = pkg.get_average_rating()
Expand All @@ -368,9 +368,9 @@ def package_to_api1(pkg, context):
dictized['metadata_created'] = metadata_created.isoformat() \
if metadata_created else None

subjects = dictized.pop("relationships_as_subject")
objects = dictized.pop("relationships_as_object")
subjects = dictized.pop("relationships_as_subject")
objects = dictized.pop("relationships_as_object")

relationships = []
for relationship in objects:
model = context['model']
Expand All @@ -386,9 +386,9 @@ def package_to_api1(pkg, context):
'type': relationship['type'],
'object': pkg.get(relationship['object_package_id']).name,
'comment': relationship["comment"]})
dictized['relationships'] = relationships


dictized['relationships'] = relationships
return dictized

def package_to_api2(pkg, context):
Expand All @@ -397,16 +397,16 @@ def package_to_api2(pkg, context):

dictized["groups"] = [group["id"] for group in dictized["groups"]]
dictized.pop("revision_timestamp")

dictized["tags"] = [tag["name"] for tag in dictized["tags"]]
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
for extra in dictized["extras"])

resources = dictized["resources"]
resources = dictized["resources"]

for resource in resources:
resource_dict_to_api(resource,pkg.id, context)

dictized['license'] = pkg.license.title if pkg.license else None

dictized['ratings_average'] = pkg.get_average_rating()
Expand All @@ -420,9 +420,9 @@ def package_to_api2(pkg, context):
if pkg.metadata_created else None
dictized['notes_rendered'] = ckan.misc.MarkdownFormat().to_html(pkg.notes)

subjects = dictized.pop("relationships_as_subject")
objects = dictized.pop("relationships_as_object")
subjects = dictized.pop("relationships_as_subject")
objects = dictized.pop("relationships_as_object")

relationships = []
for relationship in objects:
model = context['model']
Expand All @@ -438,8 +438,8 @@ def package_to_api2(pkg, context):
'type': relationship['type'],
'object': relationship['object_package_id'],
'comment': relationship["comment"]})
dictized['relationships'] = relationships

dictized['relationships'] = relationships
return dictized

def activity_dictize(activity, context):
Expand Down
2 changes: 1 addition & 1 deletion ckan/logic/action/get.py
Expand Up @@ -882,7 +882,7 @@ def get_site_user(context, data_dict):
def roles_show(context, data_dict):
'''Returns the roles that users (and authorization groups) have on a
particular domain_object.
If you specify a user (or authorization group) then the resulting roles
will be filtered by those of that user (or authorization group).
Expand Down
6 changes: 4 additions & 2 deletions ckan/logic/schema.py
Expand Up @@ -170,6 +170,8 @@ def default_group_schema():
'__extras': [ignore],
'packages': {
"id": [not_empty, unicode, package_id_or_name_exists],
"name": [not_empty, unicode],
"title":[not_empty, unicode],
"__extras": [ignore]
}
}
Expand All @@ -184,9 +186,9 @@ def group_form_schema():
}
schema['users'] = {
"name": [not_empty, unicode],
"capacity": [ignore_missing],
"capacity": [ignore_missing],
"__extras": [ignore]
}
}
return schema


Expand Down

0 comments on commit 277bb76

Please sign in to comment.