Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make available to import to dexterity blob types #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions collective/blueprint/jsonmigrator/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
from Products.Archetypes.interfaces import IBaseObject
from AccessControl.interfaces import IRoleManager

from plone.dexterity.utils import iterSchemata
from zope.schema import getFieldsInOrder

try:
from plone.dexterity.interfaces import IDexterityContent
dexterity_available = True
except:
dexterity_available = False


DATAFIELD = '_datafield_'
STATISTICSFIELD = '_statistics_field_prefix_'

Expand Down Expand Up @@ -111,7 +121,7 @@ class Statistics(object):
def __init__(self, transmogrifier, name, options, previous):
self.stats = {'START_TIME': int(time.time()),
'TIME_LAST_STEP': 0,
'STEP': options.get('log-step', 25),
'STEP': int(options.get('log-step', 25)),
'OBJ_COUNT': 0,
'EXISTED': 0,
'ADDED': 0,
Expand Down Expand Up @@ -201,7 +211,7 @@ def __iter__(self):
if obj is None: # path doesn't exist
yield item; continue

if IBaseObject.providedBy(obj):
if IBaseObject.providedBy(obj) or (dexterity_available and IDexterityContent.providedBy(obj)):
obj.setFormat(item[mimetypekey])

yield item
Expand Down Expand Up @@ -247,7 +257,7 @@ def __iter__(self):
if obj is None or not getattr(obj, 'workflow_history', False):
yield item; continue

if IBaseObject.providedBy(obj):
if IBaseObject.providedBy(obj) or (dexterity_available and IDexterityContent.providedBy(obj)):
item_tmp = item

# get back datetime stamp and set the workflow history
Expand Down Expand Up @@ -303,7 +313,7 @@ def __iter__(self):
if obj is None: # path doesn't exist
yield item; continue

if IBaseObject.providedBy(obj):
if IBaseObject.providedBy(obj) or (dexterity_available and IDexterityContent.providedBy(obj)):
if getattr(aq_base(obj), '_delProperty', False):
for prop in item[propertieskey]:
if getattr(aq_base(obj), prop[0], None) is not None:
Expand Down Expand Up @@ -362,7 +372,7 @@ def __iter__(self):
if obj is None: # path doesn't exist
yield item; continue

if IBaseObject.providedBy(obj):
if IBaseObject.providedBy(obj) or (dexterity_available and IDexterityContent.providedBy(obj)):

if item[ownerkey][0] and item[ownerkey][1]:
try:
Expand All @@ -380,7 +390,6 @@ def __iter__(self):
obj._owner = item[ownerkey][1]
except Exception, e:
raise Exception('ERROR: %s SETTING __OWNERSHIP TO %s' % (str(e), item[pathkey]))

yield item


Expand Down Expand Up @@ -512,6 +521,7 @@ def __iter__(self):
if obj is None: # path doesn't exist
yield item; continue

# AT
if IBaseObject.providedBy(obj):
for key in item.keys():
if not key.startswith(self.datafield_prefix):
Expand All @@ -527,4 +537,34 @@ def __iter__(self):
if len(value) != len(field.get(obj)):
field.set(obj, value)

# dexterity
if dexterity_available and IDexterityContent.providedBy(obj):
for key in item.keys():
if not key.startswith(self.datafield_prefix):
continue
if not os.path.exists(item[key]):
continue

fieldname = key[len(self.datafield_prefix):]
f = open(item[key])
value = f.read()
f.close()

filename = item['id'].decode('utf-8')
contenttype = ''

#get all fields for this obj
for schemata in iterSchemata(obj):
for name, field in getFieldsInOrder(schemata):
if field.__name__ == fieldname:
# create a blob instance
instance = field._type(
data=value,
filename=filename,
contentType=contenttype,
)
# set it
field.set(field.interface(obj), instance)
continue

yield item