[ckan-changes] commit/ckan: kindly: [model] add fields to resource and add migration
Bitbucket
commits-noreply at bitbucket.org
Sun Sep 11 13:38:40 UTC 2011
1 new changeset in ckan:
http://bitbucket.org/okfn/ckan/changeset/26dd7e201cae/
changeset: 26dd7e201cae
branch: feature-1324-add-fields-to-resource
user: kindly
date: 2011-09-11 15:38:05
summary: [model] add fields to resource and add migration
affected #: 9 files (3.3 KB)
--- a/ckan/forms/common.py Fri Sep 09 15:56:20 2011 +0100
+++ b/ckan/forms/common.py Sun Sep 11 14:38:05 2011 +0100
@@ -417,13 +417,19 @@
# formalchemy form param format
# e.g. 'Dataset-1-resources-0-url': u'http://ww...'
row = 0
+ # The base columns historically defaulted to empty strings
+ # not None (Null). This is why they are seperate here.
+ base_columns = ['url', 'format', 'description', 'hash', 'id']
while True:
if not params.has_key('%s-%i-url' % (self.name, row)):
break
new_resource = {}
blank_row = True
for col in model.Resource.get_columns() + ['id']:
- value = params.get('%s-%i-%s' % (self.name, row, col), u'')
+ if col in base_columns:
+ value = params.get('%s-%i-%s' % (self.name, row, col), u'')
+ else:
+ value = params.get('%s-%i-%s' % (self.name, row, col))
new_resource[col] = value
if col != 'id' and value:
blank_row = False
--- a/ckan/lib/create_test_data.py Fri Sep 09 15:56:20 2011 +0100
+++ b/ckan/lib/create_test_data.py Sun Sep 11 14:38:05 2011 +0100
@@ -320,7 +320,7 @@
format=u'plain text',
description=u'Full text. Needs escaping: " Umlaut: \xfc',
hash=u'abc123',
- extras={'size': u'123'},
+ extras={'size_extra': u'123'},
**configured_extras[0]
)
pr2 = model.Resource(
@@ -328,7 +328,7 @@
format=u'json',
description=u'Index of the novel',
hash=u'def456',
- extras={'size': u'345'},
+ extras={'size_extra': u'345'},
**configured_extras[1]
)
model.Session.add(pr1)
--- a/ckan/logic/schema.py Fri Sep 09 15:56:20 2011 +0100
+++ b/ckan/logic/schema.py Sun Sep 11 14:38:05 2011 +0100
@@ -26,6 +26,7 @@
user_both_passwords_entered,
user_passwords_match,
user_password_not_empty,
+ isodate,
user_about_validator)
from formencode.validators import OneOf
import ckan.model
@@ -45,6 +46,16 @@
'state': [ignore],
'position': [ignore],
'revision_timestamp': [ignore],
+ 'name': [ignore_missing, unicode],
+ 'resource_type': [ignore_missing, unicode],
+ 'mimetype': [ignore_missing, unicode],
+ 'mimetype_inner': [ignore_missing, unicode],
+ 'webstore_url': [ignore_missing, unicode],
+ 'cache_url': [ignore_missing, unicode],
+ 'size': [ignore_missing, int],
+ 'last_modified': [ignore_missing, isodate],
+ 'cache_last_updated': [ignore_missing, isodate],
+ 'webstore_last_updated': [ignore_missing, isodate],
'__extras': [ignore_missing, extras_unicode_convert, keep_extras],
}
--- a/ckan/logic/validators.py Fri Sep 09 15:56:20 2011 +0100
+++ b/ckan/logic/validators.py Sun Sep 11 14:38:05 2011 +0100
@@ -1,8 +1,10 @@
import re
+import datetime
from pylons.i18n import _, ungettext, N_, gettext
from ckan.lib.navl.dictization_functions import Invalid, Missing, missing, unflatten
from ckan.authz import Authorizer
from ckan.logic import check_access, NotAuthorized
+from ckan.lib.helpers import date_str_to_datetime
def package_id_not_changed(value, context):
@@ -13,6 +15,17 @@
'This key is read-only') % (package.id, value))
return value
+def isodate(value, context):
+
+ if isinstance(value, datetime.datetime):
+ return value
+ try:
+ date = date_str_to_datetime(value)
+ context['revision_date'] = date
+ except (TypeError, ValueError), e:
+ raise Invalid(_('Date format incorrect'))
+ return date
+
def no_http(value, context):
model = context['model']
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ckan/migration/versions/041_resource_new_fields.py Sun Sep 11 14:38:05 2011 +0100
@@ -0,0 +1,32 @@
+from migrate import *
+
+def upgrade(migrate_engine):
+ migrate_engine.execute(
+ '''
+ begin;
+ ALTER TABLE resource
+ ADD COLUMN name text,
+ ADD COLUMN resource_type text,
+ ADD COLUMN mimetype text,
+ ADD COLUMN mimetype_inner text,
+ ADD COLUMN "size" bigint,
+ ADD COLUMN last_modified timestamp without time zone,
+ ADD COLUMN cache_url text,
+ ADD COLUMN cache_last_updated timestamp without time zone,
+ ADD COLUMN webstore_url text,
+ ADD COLUMN webstore_last_updated timestamp without time zone;
+
+ ALTER TABLE resource_revision
+ ADD COLUMN name text,
+ ADD COLUMN resource_type text,
+ ADD COLUMN mimetype text,
+ ADD COLUMN mimetype_inner text,
+ ADD COLUMN "size" bigint,
+ ADD COLUMN last_modified timestamp without time zone,
+ ADD COLUMN cache_url text,
+ ADD COLUMN cache_last_updated timestamp without time zone,
+ ADD COLUMN webstore_url text,
+ ADD COLUMN webstore_last_updated timestamp without time zone;
+ commit;
+ '''
+ )
--- a/ckan/model/resource.py Fri Sep 09 15:56:20 2011 +0100
+++ b/ckan/model/resource.py Sun Sep 11 14:38:05 2011 +0100
@@ -16,6 +16,13 @@
'ResourceGroupRevision', 'resource_group_revision_table',
]
+CORE_RESOURCE_COLUMNS = ['url', 'format', 'description', 'hash', 'name',
+ 'resource_type', 'mimetype', 'mimetype_inner',
+ 'size', 'last_modified', 'cache_url', 'cache_last_updated',
+ 'webstore_url', 'webstore_last_updated']
+
+
+
##formally package_resource
resource_table = Table(
'resource', metadata,
@@ -26,6 +33,18 @@
Column('description', types.UnicodeText),
Column('hash', types.UnicodeText),
Column('position', types.Integer),
+
+ Column('name', types.UnicodeText),
+ Column('resource_type', types.UnicodeText),
+ Column('mimetype', types.UnicodeText),
+ Column('mimetype_inner', types.UnicodeText),
+ Column('size', types.BigInteger),
+ Column('last_modified', types.DateTime),
+ Column('cache_url', types.UnicodeText),
+ Column('cache_last_updated', types.DateTime),
+ Column('webstore_url', types.UnicodeText),
+ Column('webstore_last_updated', types.DateTime),
+
Column('extras', JsonDictType),
)
@@ -59,8 +78,12 @@
self.format = format
self.description = description
self.hash = hash
+ # The base columns historically defaulted to empty strings
+ # not None (Null). This is why they are seperate here.
+ base_columns = ['url', 'format', 'description', 'hash']
+ for key in set(CORE_RESOURCE_COLUMNS) - set(base_columns):
+ setattr(self, key, kwargs.pop(key, None))
self.extras = extras or {}
-
extra_columns = self.get_extra_columns()
for field in extra_columns:
value = kwargs.pop(field, None)
@@ -95,9 +118,9 @@
def get_columns(cls, extra_columns=True):
'''Returns the core editable columns of the resource.'''
if extra_columns:
- return ['url', 'format', 'description', 'hash'] + cls.get_extra_columns()
+ return CORE_RESOURCE_COLUMNS + cls.get_extra_columns()
else:
- return ['url', 'format', 'description', 'hash']
+ return CORE_RESOURCE_COLUMNS
@classmethod
def get_extra_columns(cls):
--- a/ckan/tests/forms/test_package.py Fri Sep 09 15:56:20 2011 +0100
+++ b/ckan/tests/forms/test_package.py Sun Sep 11 14:38:05 2011 +0100
@@ -95,7 +95,7 @@
assert u'Full text. Needs escaping: " Umlaut: \xfc"' in out, out_printable
assert res.hash in out, out_printable
assert res.alt_url in out, out_printable
- assert res.extras['size'] in out, out_printable
+ assert res.extras['size_extra'] in out, out_printable
def test_2_fields(self):
fs = self._get_standard_fieldset()
@@ -131,6 +131,7 @@
indict['Package--resources-0-url'] = u'http:/1'
indict['Package--resources-0-format'] = u'xml'
indict['Package--resources-0-description'] = u'test desc'
+ indict['Package--resources-0-size'] = 10
indict['Package--resources-0-alt_url'] = u'http:/2'
fs = self._get_standard_fieldset().bind(model.Package, data=indict)
--- a/ckan/tests/functional/api/base.py Fri Sep 09 15:56:20 2011 +0100
+++ b/ckan/tests/functional/api/base.py Sun Sep 11 14:38:05 2011 +0100
@@ -317,14 +317,14 @@
u'description':u'Main file',
u'hash':u'abc123',
u'alt_url':u'alt_url',
- u'size':u'200',
+ u'size_extra':u'200',
}, {
u'url':u'http://blah.com/file2.xml',
u'format':u'xml',
u'description':u'Second file',
u'hash':u'def123',
u'alt_url':u'alt_url',
- u'size':u'200',
+ u'size_extra':u'200',
}],
'tags': [u'russion', u'novel'],
'license_id': testpackage_license_id,
--- a/ckan/tests/functional/api/model/test_package.py Fri Sep 09 15:56:20 2011 +0100
+++ b/ckan/tests/functional/api/model/test_package.py Sun Sep 11 14:38:05 2011 +0100
@@ -262,14 +262,14 @@
u'description':u'Appendix 1',
u'hash':u'def123',
u'alt_url':u'alt123',
- u'size':u'400',
+ u'size_extra':u'400',
},{
u'url':u'http://blah.com/file3.xml',
u'format':u'xml',
u'description':u'Appenddic 2',
u'hash':u'ghi123',
u'alt_url':u'alt123',
- u'size':u'400',
+ u'size_extra':u'400',
}],
'extras': {
u'key3': u'val3',
@@ -321,14 +321,14 @@
self.assert_equal(resource.description, u'Appendix 1')
self.assert_equal(resource.hash, u'def123')
self.assert_equal(resource.alt_url, u'alt123')
- self.assert_equal(resource.extras['size'], u'400')
+ self.assert_equal(resource.extras['size_extra'], u'400')
resource = package.resources[1]
self.assert_equal(resource.url, 'http://blah.com/file3.xml')
self.assert_equal(resource.format, u'xml')
self.assert_equal(resource.description, u'Appenddic 2')
self.assert_equal(resource.hash, u'ghi123')
self.assert_equal(resource.alt_url, u'alt123')
- self.assert_equal(resource.extras['size'], u'400')
+ self.assert_equal(resource.extras['size_extra'], u'400')
# Check unsubmitted fields have not changed.
# - url
Repository URL: https://bitbucket.org/okfn/ckan/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the ckan-changes
mailing list