[ckan-changes] [okfn/ckan] d9d991: [2306][model, schema, migration] Add 'created' fie...
GitHub
noreply at github.com
Tue Apr 24 13:46:40 UTC 2012
Branch: refs/heads/feature-2306-resource-created
Home: https://github.com/okfn/ckan
Commit: d9d9915b6853cfc0cd113f0a275cd523c8e9fcd8
https://github.com/okfn/ckan/commit/d9d9915b6853cfc0cd113f0a275cd523c8e9fcd8
Author: John Glover <j at johnglover.net>
Date: 2012-04-24 (Tue, 24 Apr 2012)
Changed paths:
M ckan/lib/dictization/model_save.py
M ckan/logic/schema.py
A ckan/migration/versions/054_add_resource_created_date.py
M ckan/model/package.py
M ckan/model/resource.py
M ckan/tests/lib/test_dictization.py
Log Message:
-----------
[2306][model, schema, migration] Add 'created' field to resource objects
diff --git a/ckan/lib/dictization/model_save.py b/ckan/lib/dictization/model_save.py
index aa9f1a5..0d45b57 100644
--- a/ckan/lib/dictization/model_save.py
+++ b/ckan/lib/dictization/model_save.py
@@ -1,3 +1,4 @@
+import datetime
import uuid
from sqlalchemy.orm import class_mapper
import ckan.lib.dictization as d
@@ -8,7 +9,6 @@
def resource_dict_save(res_dict, context):
model = context["model"]
session = context["session"]
- trigger_url_change = False
id = res_dict.get("id")
obj = None
@@ -29,6 +29,9 @@ def resource_dict_save(res_dict, context):
if key in ('extras', 'revision_timestamp'):
continue
if key in fields:
+ if isinstance(getattr(obj, key), datetime.datetime):
+ if getattr(obj, key).isoformat() == value:
+ continue
if key == 'url' and not new and obj.url <> value:
obj.url_changed = True
setattr(obj, key, value)
diff --git a/ckan/logic/schema.py b/ckan/logic/schema.py
index 0d41722..d756657 100644
--- a/ckan/logic/schema.py
+++ b/ckan/logic/schema.py
@@ -62,6 +62,7 @@ def default_resource_schema():
'webstore_url': [ignore_missing, unicode],
'cache_url': [ignore_missing, unicode],
'size': [ignore_missing, int_validator],
+ 'created': [ignore_missing, isodate],
'last_modified': [ignore_missing, isodate],
'cache_last_updated': [ignore_missing, isodate],
'webstore_last_updated': [ignore_missing, isodate],
diff --git a/ckan/migration/versions/054_add_resource_created_date.py b/ckan/migration/versions/054_add_resource_created_date.py
new file mode 100644
index 0000000..0364150
--- /dev/null
+++ b/ckan/migration/versions/054_add_resource_created_date.py
@@ -0,0 +1,9 @@
+def upgrade(migrate_engine):
+ migrate_engine.execute('''
+ ALTER TABLE resource
+ ADD COLUMN created timestamp without time zone;
+
+ ALTER TABLE resource_revision
+ ADD COLUMN created timestamp without time zone;
+ '''
+ )
diff --git a/ckan/model/package.py b/ckan/model/package.py
index 4365963..dd1e0d3 100644
--- a/ckan/model/package.py
+++ b/ckan/model/package.py
@@ -114,6 +114,7 @@ def get_resource_identity(resource_obj_or_dict):
else:
resource = resource_obj_or_dict
res_dict = resource.as_dict(core_columns_only=True)
+ del res_dict['created']
return res_dict
existing_res_identites = [get_resource_identity(res) \
for res in self.resources]
diff --git a/ckan/model/resource.py b/ckan/model/resource.py
index 3d08575..f370e02 100644
--- a/ckan/model/resource.py
+++ b/ckan/model/resource.py
@@ -3,6 +3,7 @@
from sqlalchemy import orm
from pylons import config
import vdm.sqlalchemy
+import datetime
from meta import *
from types import make_uuid, JsonDictType
@@ -11,7 +12,7 @@
from ckan.model import extension
from ckan.model.activity import ActivityDetail
-__all__ = ['Resource', 'resource_table',
+__all__ = ['Resource', 'resource_table',
'ResourceGroup', 'resource_group_table',
'ResourceRevision', 'resource_revision_table',
'ResourceGroupRevision', 'resource_group_revision_table',
@@ -19,9 +20,9 @@
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']
-
+ 'size', 'created', 'last_modified', 'cache_url',
+ 'cache_last_updated', 'webstore_url',
+ 'webstore_last_updated']
##formally package_resource
@@ -40,6 +41,7 @@
Column('mimetype', types.UnicodeText),
Column('mimetype_inner', types.UnicodeText),
Column('size', types.BigInteger),
+ Column('created', types.DateTime, default=datetime.datetime.now),
Column('last_modified', types.DateTime),
Column('cache_url', types.UnicodeText),
Column('cache_last_updated', types.DateTime),
diff --git a/ckan/tests/lib/test_dictization.py b/ckan/tests/lib/test_dictization.py
index 09342d2..d32fd89 100644
--- a/ckan/tests/lib/test_dictization.py
+++ b/ckan/tests/lib/test_dictization.py
@@ -353,7 +353,7 @@ def test_08_package_save(self):
def test_09_package_alter(self):
context = {"model": model,
- "session": model.Session}
+ "session": model.Session}
anna1 = model.Session.query(model.Package).filter_by(name='annakarenina').one()
================================================================
More information about the ckan-changes
mailing list