Skip to content

Commit

Permalink
Separating out the group schemas into organization schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Sep 10, 2012
1 parent 8719a6a commit 222b78e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ckan/controllers/organization.py
Expand Up @@ -38,7 +38,7 @@ class OrganizationController(BaseController):
"""

def form_to_db_schema(self):
return schema.group_form_schema()
return schema.organization_form_schema()

def db_to_form_schema(self):
'''This is an interface to manipulate data from the database
Expand Down Expand Up @@ -490,7 +490,7 @@ def _add_users(self, group, parameters):

context = {
"group": group,
"schema": schema.default_group_schema(),
"schema": schema.default_organization_schema(),
"model": model,
"session": model.Session
}
Expand Down
63 changes: 63 additions & 0 deletions ckan/logic/schema.py
Expand Up @@ -15,6 +15,7 @@
package_name_validator,
package_version_validator,
group_name_validator,
organization_name_validator,
tag_length_validator,
tag_name_validator,
tag_string_convert,
Expand Down Expand Up @@ -202,6 +203,46 @@ def db_to_form_package_schema():
})
return schema

def default_organization_schema():

schema = {
'id': [ignore_missing, unicode],
'revision_id': [ignore],
'name': [not_empty, unicode, name_validator, organization_name_validator],
'title': [ignore_missing, unicode],
'description': [ignore_missing, unicode],
'image_url': [ignore_missing, unicode],
'type': [ignore_missing, unicode],
'state': [ignore_not_group_admin, ignore_missing],
'created': [ignore],
'approval_status': [ignore_missing, unicode],
'extras': default_extras_schema(),
'__extras': [ignore],
'packages': {
"id": [not_empty, unicode, package_id_or_name_exists],
"title":[ignore_missing, unicode],
"name":[ignore_missing, unicode],
"__extras": [ignore]
},
'groups': {
"name": [not_empty, unicode],
"capacity": [ignore_missing],
"__extras": [ignore]
},
'users': {
"name": [not_empty, unicode],
"capacity": [ignore_missing],
"__extras": [ignore]
},
'groups': {
"name": [not_empty, unicode],
"capacity": [ignore_missing],
"__extras": [ignore]
}
}
return schema


def default_group_schema():

schema = {
Expand Down Expand Up @@ -241,6 +282,22 @@ def default_group_schema():
}
return schema

def organization_form_schema():
schema = default_organization_schema()
schema['packages'] = {
"name": [not_empty, unicode],
"title": [ignore_missing],
"__extras": [ignore]
}
schema['users'] = {
"name": [not_empty, unicode],
"capacity": [ignore_missing],
"__extras": [ignore]
}
schema['display_name'] = [ignore_missing]
return schema


def group_form_schema():
schema = default_group_schema()
#schema['extras_validation'] = [duplicate_extras_key, ignore]
Expand All @@ -258,6 +315,12 @@ def group_form_schema():
return schema


def default_update_organization_schema():
schema = default_organization_schema()
schema["name"] = [ignore_missing, organiztion_name_validator, unicode]
return schema


def default_update_group_schema():
schema = default_group_schema()
schema["name"] = [ignore_missing, group_name_validator, unicode]
Expand Down
16 changes: 16 additions & 0 deletions ckan/logic/validators.py
Expand Up @@ -269,6 +269,22 @@ def duplicate_extras_key(key, data, errors, context):
if extras_keys:
errors[key].append(_('Duplicate key "%s"') % extras_keys[0])

def organization_name_validator(key, data, errors, context):
model = context['model']
session = context['session']
organization = context.get('organization')

query = session.query(model.Group.name).filter_by(name=data[key])
if group:
group_id = organization.id
else:
group_id = data.get(key[:-1] + ('id',))
if group_id and group_id is not missing:
query = query.filter(model.Group.id <> group_id)
result = query.first()
if result:
errors[key].append(_('Organization name already exists in database'))

def group_name_validator(key, data, errors, context):
model = context['model']
session = context['session']
Expand Down

0 comments on commit 222b78e

Please sign in to comment.