Commit d0a7d08d authored by Kevin Deldycke's avatar Kevin Deldycke

First commit of Guy's work


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@4147 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 9ba17dbc
#############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
# Guy Oswald OBAMA <guy@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from Products.ERP5Type.Utils import writeLocalPropertySheet, writeLocalDocument
from Products.PythonScripts.Utility import allow_class
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions
from Globals import InitializeClass
class LocalGenerator:
""" Create Local PropertySheets and Documents
"""
copyright_text = """
#############################################################################
#
# Copyright (c) 2002-2005 Nexedi SARL and Contributors. All Rights Reserved.
# Jean-Paul Smets-Solanes <jp@nexedi.com>
# Kevin Deldycke <kevin@nexedi.com>
# Guy Oswald OBAMA <guy@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
"""
# Declarative security
security = ClassSecurityInfo()
security.declareProtected( Permissions.ManagePortal,
'generateLocalPropertySheet')
def generateLocalPropertySheet(self, property_sheet_name, properties = []):
""" Writes a Local PropertySheet.
`property_sheet_name` : name of this property sheet
`properties` : a list of dict representing properties
"""
class_name = property_sheet_name.replace(' ', '')
formating_dict = { 'class_name' : class_name,
'property_sheet_name': property_sheet_name}
string = self.copyright_text + """
class %(class_name)s :
\"\"\"%(property_sheet_name)s properties and categories.
\"\"\"
_properties = (
""" % formating_dict
for prop in properties:
string += """
{"""
for k, v in prop.items() :
string += """
'%s' : '%s',""" % (k, v)
string += """
}, """
string += """
)
_categories = ('source_section', 'destination_section')
"""
writeLocalPropertySheet(class_name, string)
security.declareProtected(Permissions.ManagePortal, 'generateLocalDocument')
def generateLocalDocument(self, class_name, portal_type_name):
"""Writes local Document.
`class_name` name of the class to be written
`portal_type_name` name of the portal type associated to this new class
"""
formating_dict = {
'class_name' : class_name,
'portal_type_name' : portal_type_name
}
string = self.copyright_text + """
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5Type.Utils import assertAttributePortalType
from Products.ERP5Type.XMLObject import XMLObject
from Products.ERP5.Core.Node import Node
from Products.ERP5.Document.Entity import Entity
class %(class_name)s(Node, XMLObject):
\"\"\"An %(portal_type_name)s object holds the information about
%(class_name)s objects can contain Coordinate objects
as well a documents of various types.
%(class_name)s objects can be synchronized accross multiple
sites
\"\"\"
meta_type = 'ERP5 %(portal_type_name)s'
portal_type = '%(portal_type_name)s'
add_permission = Permissions.AddPortalContent
isPortalContent = 1
isRADContent = 1
# Declarative Security
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.View)
# Declarative Properties
property_sheets = ( PropertySheet.Base
, PropertySheet.XMLObject
, PropertySheet.CategoryCore
, PropertySheet.DublinCore
, PropertySheet.Arrow
, PropertySheet.Task
, PropertySheet.%(class_name)s
)
""" % formating_dict
writeLocalDocument(class_name, string)
InitializeClass(LocalGenerator)
allow_class(LocalGenerator)
def ERP5Site_createModuleScribus(self, form_id=None, module_portal_type=None,
portal_skins_folder=None, object_portal_type=None, object_title=None, module_id=None,
module_title=None, selection_index=None, selection_name=None, import_scribus_file=None,
import_pdf_file=None, **kw) :
""" Creates a module, portal_type and ERP5Form from a scribus and
PDFForm file"""
context = self
from Products.Formulator.Errors import ValidationError, FormValidationError
from Products.ERP5Form.ScribusUtils import ScribusParser
ScribusParser = ScribusParser()
from Products.ERP5Form.CreatePropertySheet import LocalGenerator
generator = LocalGenerator()
# TODO
# - Allow in the module only the new document, we must activate the filter
# - handle an optionnal "description" parameter
# - set the form action to "Base_edit"
# - print : pdf
# - export : xml
# - report : last modified
# - security : 5A
portal = context.getPortalObject()
portal_types = portal.portal_types
object_portal_type_id = object_portal_type
# Create a new portal_type for the module
portal_types.manage_addTypeInformation( 'ERP5 Type Information'
, typeinfo_name = 'ERP5Type: ERP5 Folder'
, id = module_portal_type
)
module_portal_type_value = portal_types[module_portal_type]
# Set allowed content types
module_portal_type_value.allowed_content_types = (object_portal_type_id, )
module_portal_type_value.filter_content_types = 1
action_list = module_portal_type_value.listActions()
module_portal_type_value.deleteActions(selections=range(0, len(action_list)))
form_view_pdf = object_portal_type_id.replace(' ','') + '_view' +\
object_portal_type_id.replace(' ','') + 'asPDF'
form_view_list = object_title.replace(' ','') + 'Module_view' +\
object_portal_type_id.replace(' ','') + 'List'
# Parameters to addAction : id, name, action, condition, permission,
# category, visible=1, REQUEST=None
module_portal_type_value.addAction( "view"
, "View"
, "string:${object_url}/%s"%form_view_list
, ""
, "View"
, "object_view"
)
# Create the skin directory if does not exist yet
portal_skins_folder_name = portal_skins_folder
portal_skins = portal.portal_skins
if not portal_skins_folder_name in portal.portal_skins.objectIds():
portal_skins.manage_addFolder(portal_skins_folder_name)
skin_folder = portal.portal_skins[portal_skins_folder_name]
# Add new folders into skin paths.
for skin_name, selection in portal_skins.getSkinPaths():
selection = selection.split(',')
if portal_skins_folder_name not in selection:
new_selection = [portal_skins_folder_name,]
new_selection.extend(selection)
portal_skins.manage_skinLayers( skinpath = tuple(new_selection)
, skinname = skin_name
, add_skin = 1
)
# Create the default ERP5 Form in order to view the object
form_view_id = object_portal_type_id.replace(' ','') + '_view'
factory = skin_folder.manage_addProduct['ERP5Form']
factory.addERP5Form( form_view_id
, title = object_title
)
form_view_id_object = skin_folder[form_view_id]
form_view_id_object.rename_group('Default', 'left')
default_groups = ['right', 'center', 'bottom', 'hidden']
for group in default_groups:
form_view_id_object.add_group(group)
object_title_view = object_title + ' Module View'
factory = skin_folder.manage_addProduct['ERP5Form']
factory.addERP5Form( form_view_list
, title = object_title_view
)
form_view_list_object = skin_folder[form_view_list]
form_list_id = form_view_list_object.id
form_list = form_view_list_object.restrictedTraverse(form_list_id)
form_view_list_object.rename_group('Default', 'left')
#default_groups = ['right', 'center', 'bottom', 'hidden']
for group in default_groups:
form_view_list_object.add_group(group)
title_module = ''
for word in module_title.split():
title_module += str(word.capitalize() + ' ')
# Add ListBox Field
id = 'listbox'
title = title_module
field_type = 'ListBox'
form_view_list_object.manage_addField(id, title, field_type)
# manage ListBox settings
values_settings = {}
values_settings['pt'] = "form_list"
values_settings['action'] = "Base_doSelect"
# set the form settings
for key, value in values_settings.items():
setattr(form_view_list_object, key, value)
# manage edit property of ListBox
field_attributes = getattr(form_view_list_object, id)
field_attributes.values['lines'] = 20
field_attributes.values['columns'] = [('id', 'ID'), ('title', 'Title'), ('description', 'Description')]
field_attributes.values['list_action'] = 'list'
form_id = form_view_id_object.id
form = form_view_id_object.restrictedTraverse(form_id)
# import and manage Scribus File
xml_string = ScribusParser.getContentFile(import_scribus_file)
if xml_string == None:
#print "no field was defined in the Scribus file"
pass
else:
# get properties from Scribus File
output_string = str(xml_string)
text_field_list = ScribusParser.getXmlObjectsProperties(xml_string)
widget_properties = ScribusParser.getPropertiesConversion(text_field_list)
# add field from OrderedWidgetProperties in ERP5 Module created
radiofield_widget_properties = {}
position = {}
# personal_properties is used to create PropertySheet
personal_properties_list = []
for index in range(len(widget_properties)):
id = str(widget_properties[index][0])
properties_field = widget_properties[index][1]
if properties_field.has_key('type'):
field_type = str(properties_field['type'])
title = str(properties_field['title'])
form_view_id_object.manage_addField(id, title, field_type)
context = skin_folder[form_view_id]
form_id = context.id
# modify value of property
form = context.restrictedTraverse(form_id)
field_attributes = getattr(form, id)
type = 'string'
if field_type == 'DateTimeField':
type = 'date'
field_attributes.values['input_order'] = properties_field['input_order']
field_attributes.values['date_only'] = properties_field['date_only']
field_attributes.values['required'] = properties_field['required']
elif field_type == 'RelationStringField':
portal_type_item = properties_field['portal_type'].capitalize()
field_attributes.values['portal_type'] = [(portal_type_item, portal_type_item)]
field_attributes.values['base_category'] = properties_field['base_category']
field_attributes.values['catalog_index'] = properties_field['catalog_index']
field_attributes.values['default_module'] = properties_field['default_module']
personal_properties = { 'id' : id,
'description' : '',
'type' : type,
'mode': 'w'
}
personal_properties_list.append(personal_properties)
position[id] = properties_field['order']
if field_type == 'RadioField':
radiofield_widget_properties[id] = {'description' : ''}
items = []
for word_item in properties_field['items']:
items.append((word_item, word_item.capitalize()))
field_attributes.values['items'] = items
# Order field
for field in form.get_fields():
key = str(field.id)
if position.has_key(key) == 1 and position[key] == 'right':
field.move_field_group(key, 'left', 'right')
# manage_settings
values = {}
values['title'] = str(object_portal_type)
values['row_length'] = 4
values['name'] = str(form_view_id)
values['pt'] = "form_view"
values['action'] = "Base_edit"
values['update_action'] = ""
values['method'] = 'POST'
values['enctype'] = 'multipart/form-data'
values['encoding'] = "UTF-8"
values['stored_encoding'] = 'UTF-8'
values['unicode_mode'] = 0
# set the form settings
for key, value in values.items():
setattr(form, key, value)
# Import and manage PDF File before filling of default TALES expressions in cells
factory.addPDFForm(form_view_pdf, object_title, pdf_file = import_pdf_file)
for c in skin_folder.objectValues():
if c.getId() == form_view_pdf :
for cell_name in c.getCellNames():
if cell_name[0:3] == 'my_':
cellName = []
for word in cell_name[3:].split('_'):
word = word.capitalize()
cellName.append(word)
TALES = 'python: here.get' + "".join(cellName) + '()'
c.setCellTALES(cell_name, TALES)
# Create PropertySheet and Document
name_file = ''
title_module = ''
for word in module_title.split():
name_file += word.capitalize()
title_module += str(word.capitalize() + ' ')
generator.generateLocalPropertySheet(name_file, personal_properties_list)
generator.generateLocalDocument(name_file, object_portal_type)
# Reload register local property sheets
from Products.ERP5Type.Utils import initializeLocalPropertySheetRegistry
initializeLocalPropertySheetRegistry()
# Reload register local classes
from Products.ERP5Type.Utils import initializeLocalDocumentRegistry
initializeLocalDocumentRegistry()
# Then add the portal_type corresponding to the new object
typeinfo_name_ERP5Type = str('ERP5Type: ERP5 ' + object_portal_type)
#context.log("typeinfo_name_ERP5Type", typeinfo_name_ERP5Type)
#context.log("object_portal_type_id", object_portal_type_id)
portal_types.manage_addTypeInformation(
'ERP5 Type Information'
, typeinfo_name = typeinfo_name_ERP5Type
, id = object_portal_type_id)
object_portal_type_value = portal_types[object_portal_type_id]
# Set default actions
action_list = object_portal_type_value.listActions()
object_portal_type_value.deleteActions(
selections=range(0, len(action_list)))
# parameters to addAction : id, name, action, condition, permission,
# category, visible=1, REQUEST=None
object_portal_type_value.addAction( "view"
, "View"
, "string:${object_url}/%s" % form_view_id
, ""
, "View"
, "object_view"
)
object_portal_type_value.addAction( "print"
, "Print"
, "string:${object_url}/%s" % form_view_pdf
, ""
, "View"
, "object_print"
)
object_portal_type_value.addAction( "metadata"
, "Metadata"
, "string:${object_url}/Base_viewMetadata"
, ""
, "Manage properties"
, "object_view"
)
object_portal_type_value.addAction( "history"
, "History"
, "string:${object_url}/Base_viewHistory"
, ""
, "Manage properties"
, "object_view"
)
# Finally add the module to the site
portal.newContent( id = str(module_id)
, portal_type = str(module_portal_type)
, title = title_module
)
if not selection_index:
redirect_url = '%s/view?%s' % ( portal.absolute_url()
, 'portal_status_message=Module+Created.'
)
else:
redirect_url = '%s/view?selection_index=%s&selection_name=%s&%s' % (
portal.absolute_url()
, selection_index
, selection_name
, 'portal_status_message=Module+Created.'
)
portal.REQUEST.RESPONSE.redirect( redirect_url )
##############################################################################
#
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
# Guy Oswald OBAMA <guy@nexedi.com>
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from Products.PythonScripts.Utility import allow_class
from ZPublisher.HTTPRequest import FileUpload
from xml.dom.ext.reader import PyExpat
from xml.dom import Node, minidom
from AccessControl import ClassSecurityInfo
from Globals import InitializeClass, get_request
from zipfile import ZipFile, ZIP_DEFLATED
from StringIO import StringIO
from zLOG import LOG
import imghdr
import random
import getopt, sys, os, string
from urllib import quote
class ScribusParser:
"""
Parses a Scribus file
"""
# Declarative security
security = ClassSecurityInfo()
security.declarePublic('getXmlObjectsProperties')
def getXmlObjectsProperties(self, xml_string):
# Create the PyExpat reader
reader = PyExpat.Reader()
# Create DOM tree from the xml string
dom_tree = reader.fromString(xml_string)
text_field_list = {}
dom_root = dom_tree.documentElement
page_object_list = dom_root.getElementsByTagName("PAGEOBJECT")
# Take Xml objects properties
for page_object in page_object_list:
text_field_properties = {}
field_name = None
for attribute in page_object.attributes:
node_name = str(attribute.nodeName)
node_value = str(attribute.nodeValue)
if node_name == 'ANNAME':
if node_value != '':
field_name = node_value
else:
text_field_properties[node_name] = node_value
if field_name != None:
text_field_list[field_name] = text_field_properties
return text_field_list
security.declarePublic('getPropertiesConversion')
def getPropertiesConversion(self, text_field_list):
# Get Scribus field properties
field_scribus_properties_dict = {}
for field_name in text_field_list.keys():
text_field_properties = text_field_list[field_name]
field_scribus_properties_dict[field_name] = text_field_properties['ANTOOLTIP']
widget_properties_list = []
index = 1
while index < len(field_scribus_properties_dict):
for key, item in field_scribus_properties_dict.items():
if string.atoi(item[:3]) == index:
property_field_list = item[4:].split('#')
widget_properties_buffer = {}
for property_field in property_field_list:
property_field_split = property_field.split(':')
if property_field_split[0] == 'items':
property_field_split[1] = property_field_split[1].split('|')
widget_properties_buffer[property_field_split[0]] = property_field_split[1]
widget_properties_list.append((key, widget_properties_buffer))
break
index = index + 1
for key, item in field_scribus_properties_dict.items():
if string.atoi(item[:3]) == 999:
property_field_list = item[4:].split('#')
widget_properties_buffer = {}
for property_field in property_field_list:
property_field_split = property_field.split(':')
widget_properties_buffer[property_field_split[0]] = property_field_split[1]
widget_properties_list.append((key, widget_properties_buffer))
return widget_properties_list
security.declareProtected('Import/Export objects', 'getContentFile')
def getContentFile(self, file_descriptor):
""" Get file content """
return file_descriptor.read()
InitializeClass(ScribusParser)
allow_class(ScribusParser)
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment