Skip to content

Commit

Permalink
[2255] Support for membership capacity in the group sub-schema
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Mar 26, 2012
1 parent 4973812 commit 9e9a3f0
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 27 deletions.
4 changes: 2 additions & 2 deletions ckan/lib/dictization/model_dictize.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def package_dictize(pkg, context):
#groups
member_rev = model.member_revision_table
group = model.group_table
q = select([group],
q = select([group,member_rev.c.capacity],
from_obj=member_rev.join(group, group.c.id == member_rev.c.group_id)
).where(member_rev.c.table_id == pkg.id)\
.where(member_rev.c.state == 'active')
Expand Down Expand Up @@ -209,7 +209,7 @@ def package_dictize(pkg, context):
result_dict['metadata_modified'] = context.pop('metadata_modified')
result_dict['metadata_created'] = pkg.metadata_created.isoformat() \
if pkg.metadata_created else None

if context.get('for_view'):
for item in plugins.PluginImplementations(plugins.IPackageController):
result_dict = item.before_view(result_dict)
Expand Down
5 changes: 5 additions & 0 deletions ckan/lib/dictization/model_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def package_membership_list_save(group_dicts, package, context):
if not group_dicts and allow_partial_update:
return

capacity = 'member'
model = context["model"]
session = context["session"]
pending = context.get('pending')
Expand All @@ -212,6 +213,7 @@ def package_membership_list_save(group_dicts, package, context):
for group_dict in group_dicts:
id = group_dict.get("id")
name = group_dict.get("name")
capacity = group_dict.get("capacity", "member")
if id:
group = session.query(model.Group).get(id)
else:
Expand All @@ -224,18 +226,21 @@ def package_membership_list_save(group_dicts, package, context):
member_obj = model.Member(table_id = package.id,
table_name = 'package',
group = group,
capacity = capacity,
group_id=group.id,
state = 'active')
session.add(member_obj)


for group in set(group_member.keys()) - groups:
member_obj = group_member[group]
member_obj.capacity = capacity
member_obj.state = 'deleted'
session.add(member_obj)

for group in set(group_member.keys()) & groups:
member_obj = group_member[group]
member_obj.capacity = capacity
member_obj.state = 'active'
session.add(member_obj)

Expand Down
9 changes: 3 additions & 6 deletions ckan/lib/navl/dictization_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def make_full_schema(data, schema):

def augment_data(data, schema):
'''add missing, extras and junk data'''

flattented_schema = flatten_schema(schema)
key_combinations = get_all_key_combinations(data, flattented_schema)

Expand All @@ -119,12 +118,12 @@ def augment_data(data, schema):

## check if any thing naugthy is placed against subschemas
initial_tuple = key[::2]
if initial_tuple in [initial_key[:len(initial_tuple)]
if initial_tuple in [initial_key[:len(initial_tuple)]
for initial_key in flattented_schema]:
if data[key] <> []:
raise DataError('Only lists of dicts can be placed against '
'subschema %s' % key)
'subschema %s, not %s' % (key,type(data[key])))

if key[:-1] in key_combinations:
extras_key = key[:-1] + ('__extras',)
extras = new_data.get(extras_key, {})
Expand Down Expand Up @@ -208,7 +207,6 @@ def _remove_blank_keys(schema):

def validate(data, schema, context=None):
'''Validate an unflattened nested dict against a schema.'''

context = context or {}

assert isinstance(data, dict)
Expand Down Expand Up @@ -248,7 +246,6 @@ def validate_flattened(data, schema, context=None):

def _validate(data, schema, context):
'''validate a flattened dict against a schema'''

converted_data = augment_data(data, schema)
full_schema = make_full_schema(data, schema)

Expand Down
8 changes: 4 additions & 4 deletions ckan/logic/action/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ def term_translation_update(context, data_dict):
model.Session.rollback()
raise ValidationError(errors)

trans_table = model.term_translation_table
trans_table = model.term_translation_table

update = trans_table.update()
update = update.where(trans_table.c.term == data['term'])
Expand All @@ -484,14 +484,14 @@ def term_translation_update(context, data_dict):
model.Session.commit()

return data

def term_translation_update_many(context, data_dict):
model = context['model']


if not data_dict.get('data') and isinstance(data_dict, list):
raise ValidationError(
{'error':
{'error':
'term_translation_update_many needs to have a list of dicts in field data'}
)

Expand Down
15 changes: 6 additions & 9 deletions ckan/tests/functional/api/model/test_vocabulary.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,7 @@ def test_vocabulary_create_none_tags(self):
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=400)
assert response.body == ("Integrity Error: Only lists of dicts can "
"be placed against subschema tags - {u'name': u'foobar', "
"u'tags': None}")
assert "Integrity Error" in response.body

def test_vocabulary_create_empty_tags(self):
'''Test creating new vocabularies with [] for 'tags'.
Expand Down Expand Up @@ -418,7 +416,7 @@ def test_vocabulary_create_not_authorized(self):
def test_vocabulary_update_id_only(self):
self._update_vocabulary({'id': self.genre_vocab['id']},
self.sysadmin_user)

def test_vocabulary_update_id_and_same_name(self):
self._update_vocabulary({'id': self.genre_vocab['id'],
'name': self.genre_vocab['name']}, self.sysadmin_user)
Expand Down Expand Up @@ -536,7 +534,7 @@ def test_vocabulary_update_bad_tags(self):
'''
apikey = str(self.sysadmin_user.apikey)

for tags in (
[{'id': 'xxx'}, {'name': 'foo'}],
[{'name': 'foo'}, {'name': None}],
Expand All @@ -563,8 +561,7 @@ def test_vocabulary_update_none_tags(self):
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=400)
assert response.body.startswith("Integrity Error: Only lists of "
"dicts can be placed against subschema tags")
assert "Integrity Error" in response.body, response.body

def test_vocabulary_update_empty_tags(self):
'''Test updating vocabularies with [] for 'tags'.
Expand Down Expand Up @@ -652,7 +649,7 @@ def test_add_tag_to_vocab(self):
tags_before = self._list_tags(vocab)
tag_created = self._create_tag(self.sysadmin_user, 'noise', vocab)
tags_after = self._list_tags(vocab)
new_tag_names = [tag_name for tag_name in tags_after if tag_name not in
new_tag_names = [tag_name for tag_name in tags_after if tag_name not in
tags_before]
assert len(new_tag_names) == 1
assert tag_created['name'] in new_tag_names
Expand Down Expand Up @@ -689,7 +686,7 @@ def test_add_tag_vocab_not_exists(self):
'Tag vocabulary was not found.']

def test_add_tag_already_added(self):
'''Test the error response when a user tries to add a tag to a vocab
'''Test the error response when a user tries to add a tag to a vocab
that already has a tag with the same name.
'''
Expand Down
6 changes: 4 additions & 2 deletions ckan/tests/lib/test_dictization.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ def setup_class(cls):
'type': u'group',
'state': u'active',
'title': u"Dave's books",
"approval_status": u"approved"},
"approval_status": u"approved",
'capacity': u'member'},
{'description': u'Roger likes these books.',
'name': u'roger',
'type': u'group',
'state': u'active',
'title': u"Roger's books",
"approval_status": u"approved"}],
"approval_status": u"approved",
'capacity': u'member'}],
'isopen': True,
'license_id': u'other-open',
'license_title': u'Other (Open)',
Expand Down
8 changes: 6 additions & 2 deletions ckanext/organizations/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,14 @@ def package_form(self):
def db_to_form_schema(self):
'''This is an interface to manipulate data from the database
into a format suitable for the form (optional)'''
return default_package_schema()
schema = default_package_schema()
schema['groups']['capacity'] = [ ignore_missing, unicode ]
return schema

def form_to_db_schema(self):
return default_package_schema()
schema = default_package_schema()
schema['groups']['capacity'] = [ ignore_missing, unicode ]
return schema

def check_data_dict(self, data_dict, schema=None):
'''Check if the return data is correct, mostly for checking out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ <h2>Errors in form</h2>
</select>

<label for='cap_public'>
<input id='cap_public' type='radio' name='groups__0__capacity' value='public'/>
<input id='cap_public' type='radio' name='groups__0__capacity' value='public' py:attrs="{'checked':'checked'} if organization.get('capacity','') == 'public' else {}"/>
Public</label>

<label for='cap_private'>
<input id='cap_private' type='radio' name='groups__0__capacity' value='private'/>
<input id='cap_private' type='radio' name='groups__0__capacity' value='private' py:attrs="{'checked':'checked'} if organization.get('capacity','') == 'private' else {}"/>
Private</label>
</py:if>
<py:if test="not (c.is_sysadmin or c.auth_for_change_state)">
Expand Down

0 comments on commit 9e9a3f0

Please sign in to comment.