ERP5Site_createModuleScribus.py 13.8 KB
Newer Older
Kevin Deldycke's avatar
Kevin Deldycke committed
1 2 3
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,
4
        import_pdf_file=None, option_html=None,desired_height=None, desired_width=None, import_image_1=None, import_image_2=None,
5
        import_image_3=None, **kw) :
Kevin Deldycke's avatar
Kevin Deldycke committed
6 7 8
  """ Creates a module, portal_type and ERP5Form from a scribus and
      PDFForm file"""
  context = self
9 10 11
  
  
  # IMPORTING MODULES
Kevin Deldycke's avatar
Kevin Deldycke committed
12 13
  from Products.Formulator.Errors import ValidationError, FormValidationError
  from Products.ERP5Form.ScribusUtils import ScribusParser
14 15 16
  from Products.ERP5Form.ScribusUtils import ManageModule
  from Products.ERP5Form.ScribusUtils import ManageFiles
  from Products.ERP5Form.ScribusUtils import ManageCSS
Kevin Deldycke's avatar
Kevin Deldycke committed
17
  from Products.ERP5Form.CreatePropertySheet import LocalGenerator
18 19 20 21 22
  # importing module to get an access to the 'searchFolder' method
  # needed to be able to list the objects in 'list_object_view' form
  from Products.ERP5.ERP5Site import ERP5Site
  from zLOG import LOG

23 24 25 26 27 28 29 30
  # CREATING MODULES INSTANCES
  ScribusParser = ScribusParser()
  ManageModule = ManageModule()
  ManageFiles = ManageFiles()
  ManageCSS = ManageCSS()
  generator = LocalGenerator()

  # DECLARING VARIABLES
31 32 33 34 35
  def_lineNumberInList = 20 # JPS-XXX - hardcoded
  def_colorRequired = 'rgb(192,192,255)' # JPS-XXX - hardcoded
  def_colorRequiredError = 'rgb(128,128,255)' # JPS-XXX - hardcoded
  def_color = '#F6FFFF' # JPS-XXX - hardcoded
  def_colorError = 'rgb(255,64,64)' # JPS-XXX - hardcoded
36 37

  # recovering objects
Kevin Deldycke's avatar
Kevin Deldycke committed
38 39 40
  portal = context.getPortalObject()
  portal_types = portal.portal_types
  object_portal_type_id = object_portal_type
41 42 43 44
  desired_height= desired_height
  desired_width= desired_width
  resolution= 300 # JPS-XXX - hardcoded
  option_html = option_html
45

46 47 48 49 50
  # DECLARING NAMES
  # declaring names for ERP5 objects, such as Form, DTML Document, etc.
  # these names are stored in a dict (object_names)
  object_names = ManageModule.setObjectNames(object_portal_type_id,
                                             object_title)
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  # CREATING NEW PORTAL TYPE FOR THE MODULE
  # Manage the creation of a ne portal_type for the module
  # (module is not saved for the moment, but properties can be
  # updated)
  ManageModule.setModulePortalType(portal_types,
                                   object_portal_type_id,
                                   module_portal_type,
                                   object_names
                                  )

  # PROCESSING SKIN FOLDER
  # Process and create if necessary the skins_folder defined by the user.
  # return the skin_folder object
  skin_folder = ManageModule.setSkinFolder(portal,
                                           portal_skins_folder)
      
68
     
69 70 71
  # ERP FORM LIST PROCESSING
  # Create ERP5 Form in order to view the module
  # set up the factory based on skin_folder
Kevin Deldycke's avatar
Kevin Deldycke committed
72 73
  factory = skin_folder.manage_addProduct['ERP5Form']
  
74 75 76 77
  # run the factory to create the new object (ERP5Form)
  ManageFiles.setERP5Form(factory,
                          str(object_names['view_list']),
                          str(object_title + ' Module View'))
Kevin Deldycke's avatar
Kevin Deldycke committed
78
  
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  # manage the module form and set up the list inside
  # update form properties with generic module values
  # and implement the objects' listing inside the form
  ManageModule.setModuleForm(object_title,
                             skin_folder,
                             object_names['view_list'],
                             module_title,
                             module_id,
                             def_lineNumberInList)
                             

  # INIT ATTRIBUTES DICT
  # global_properties is a special dict destinated to
  # keep all the field and page data safe during the
  # parsing, allowing them to be updated when needed
  # and used if necessary.
  global_properties = ScribusParser.initFieldDict()
96
  
97

Kevin Deldycke's avatar
Kevin Deldycke committed
98
  
99 100 101 102 103 104 105 106 107
  # PARSER VARIABLES DECLARATION
  # the following variable will recover the final CSS
  # file's content object (string) before saving it
  # onto the hard disk
  form_css_content = ""
  # properties_css_dict is used to store class informations
  properties_css_dict = {}
  # init page number
  page_number_int = 0
108
  scale_factor=0
109 110 111
  # import scribus file
  # take the input ScribusFile and read the content
  xml_string = ScribusParser.getContentFile(import_scribus_file)
Kevin Deldycke's avatar
Kevin Deldycke committed
112
  if xml_string == None:
113
    print "no field was defined in the Scribus file"
Kevin Deldycke's avatar
Kevin Deldycke committed
114 115
    pass
  else:
116 117 118
    
    # GETTING FULL SCRIBUS DOCUMENT PROPERTIES
    # get string from ScribusFile content
Kevin Deldycke's avatar
Kevin Deldycke committed
119
    output_string = str(xml_string)
120
    
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    print " createmodule > ScribusParser.getXmlObjectPropertiesDict"
    # building a tree from the output string elaborated from the
    # original Scribus file.
    # create a list of pages containing a dict of all the page_objects
    # elements with their attributes 'as they are' (so without any check
    # for data integrity or unuseful values).
    # This procedure already makes a selection of parameters and take all the
    # unnecessary page_objects off.
    #import pdb
    #pdb.set_trace()
    (text_field_list,keep_page,page_gap) = ScribusParser.getXmlObjectsPropertiesDict(xml_string)
    print " createmodule < ScribusParser.getXmlObjectPropertiesDict\n"
    
    
    print " createmodule > ScribusParser.getPropertiesConversionDict"
    # parsing text_field_list created from the getXmlObjectsPropertiesDict
    # to extract all the usefull properties and organize elements. Then check
    # attributes to get properties values.
    # This represents the main process of the script.
140 141
    (widget_properties) = ScribusParser.getPropertiesConversionDict(text_field_list)

142 143
    print " createmodule < ScribusParser.getPropertiesConversionDict\n"
    
144 145
   
    
146 147 148 149 150 151 152 153 154

    # testing if final rendering is PDF-like
    if option_html ==1 :
      print " createmodule > generating background"
      ## BACKGROUND GENERATOR
      # extract background pictures from the PDF document, convert them in the right
      # format (JPG) and add them to the skin folder as Image objects.
      # used only with option_html == 1
      # recover image_size
155 156 157 158
      image_size=ManageFiles.setBackgroundPictures(import_pdf_file,object_names,skin_folder,desired_height,desired_width,resolution)
      
      page_height,page_width,original_page_height,original_page_width = image_size
      
159 160 161 162
      print "   height = " + str(page_height)
      print "   width = " + str(page_width)
      print " createmodule < background generated\n"    
      
Kevin Deldycke's avatar
Kevin Deldycke committed
163
    # add field from OrderedWidgetProperties in ERP5 Module created
164
    # radiofield_widget_properties = {}
Kevin Deldycke's avatar
Kevin Deldycke committed
165
    position = {}
166
    # personal_properties_list is used to create PropertySheet
Kevin Deldycke's avatar
Kevin Deldycke committed
167
    personal_properties_list = []
168

169 170
    # recovering number of pages
    global_properties['page'] = len(widget_properties)  
171

172 173 174 175 176 177 178
    # CSS FILE INIT
    # init the CSS dict by creating sub-dicts to store various information
    # i.e : 'head', 'standard' ,'error', etc.
    # these sub-dicts are stored in the properties_css_dict
    properties_css_dict = ManageCSS.setInit()

    # BEGINING DATA INTERPRETATION
179 180
    print " createmodule > begining data interpretation"
    #iterating pages
181 182
    #print "   %s" % int(global_properties['page'])
    for page_iterator in range(int(global_properties['page'])):
183 184 185
      page_number = str(page_iterator)
      page_content = widget_properties[page_number]
      page_id = "page_" + page_number
186

187
      if option_html == 1:
188 189 190 191
        # CSS PAGE DEFINITION (PAGE AND BACKGROUND IMAGE)
        # get CSS class properties relative to the actual page
        # (background picture position, picture size, etc.)
        # and add them to the css dict
192
        width_groups,height_groups = ManageFiles.getPageattributes(
193 194 195
                                              global_properties,
                                              import_pdf_file)
        properties_css_dict,properties_page,actual_width,actual_height = ManageCSS.setPageProperties(
196 197 198
                                   properties_css_dict,
                                   page_iterator,
                                   page_id,
199 200 201 202 203
                                   page_height,
           page_width,
           original_page_width,
           original_page_height,width_groups,height_groups)

204 205
      # RESUME DATA INTERPRETATION
      # iterating pageobjects in page
206 207
      for index in range(len(page_content)):
        (id, properties_field) = page_content[index]
208
        # testing each page_content
209
        if properties_field.has_key('type'):
210

211 212 213 214 215 216 217 218 219
          if option_html ==1:
            # CSS FIELD PROPERTIES
            # get CSS class properties related to the actual page_object
            # in the page (position, size, color, etc.) and add them to
            # the css_dict
            properties_css_dict = ManageCSS.setFieldProperties(
                                           properties_css_dict,
                                           page_content[index],
                                           page_width,
220 221 222 223 224 225 226 227
             page_height,
             page_iterator,
             page_gap,
             keep_page,
             0,
             0,
             properties_page,actual_width,actual_height)

228 229 230 231

          # recover useful page_object attributes from scribus dict
          # to be able to create and update correctly the fields
          # composing the form
232

233 234 235 236
          ScribusParser.getFieldAttributes(page_content[index],
                                           option_html,
                                           page_id,
                                           global_properties)
237

238
    # CSS CLASS (generateOutputContent)
239 240
    if option_html ==1:

241 242 243
      # add last properties to css dict, including implementation
      # of a n+1 page to prevent bug when rendering under Konqueror
      ManageCSS.setFinalProperties(properties_css_dict,page_height)
Kevin Deldycke's avatar
Kevin Deldycke committed
244

245 246
      # generate output string from dict
      form_css_content = ManageCSS.generateOutputContent(properties_css_dict)
247

248 249
      # save CSS string content into ERP5
      ManageFiles.setCSSFile(factory,object_names['css'],form_css_content)
250
      
251 252 253



254 255 256 257 258 259 260
  # CREATING OBJECT FORM AND MANAGING GROUPS
  print " createmodule > generate fields in ERP Form"
  # CREATING ERP5 OBJECT FORM
  # create ERP5 Form to handle object view
  ManageFiles.setERP5Form(factory,
                          object_names['view_id'],
                          object_title)
261

262 263 264 265 266 267 268 269 270 271
  # update Form groups to have right number of groups with
  # corresponding names (depending on the kind of display)
  default_groups = ManageModule.setObjectForm(skin_folder,
                                              object_names,
                                              option_html,
                                              global_properties,
                                              object_portal_type)

  # create fields corresponding to the page_objects
  # recovered from the scribus file
272
  
273 274 275 276 277 278
  ManageModule.setFieldsInObjectForm(skin_folder,
                                     object_names,
                                     default_groups,
                                     global_properties,
                                     option_html
                                     )
279 280
  
  print " createmodule < fields created in ERP5 Form\n"
281

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
                                     
                                     
  # PDF IMPORTATION AND TALES GENERATION
  print " createmodule > managing PDF settings"
  # read all the content of the PDF document and save it in the skin_folder
  # as a PDFForm. then iterate the fields to get the corresponding TALES
  # expressions and save them in the PDFForm.
  ManageFiles.setPDFForm(factory,
                         skin_folder,
                         object_names,
                         object_title,
                         pdf_file = import_pdf_file
                         )
  print " createmodule < PDF settings managed\n"
  
297 298


299
  # PROPERTYSHEET AND DOCUMENT CREATION
300
  
301
  print " createmodule > PropertySheet and Document creation"
302 303 304 305 306 307 308 309 310
  # recover personal properties and save them in a PropertySheet
  # then create the Document related to the object
  ManageFiles.setPropertySheetAndDocument(global_properties,
                                          object_portal_type,
                                          generator,
                                          skin_folder,
                                          object_names)
  # as new document and PropertySheet are created, it is needed to reload the
  # registry.
Kevin Deldycke's avatar
Kevin Deldycke committed
311 312 313 314
  from Products.ERP5Type.Utils import initializeLocalPropertySheetRegistry
  initializeLocalPropertySheetRegistry()
  from Products.ERP5Type.Utils import initializeLocalDocumentRegistry
  initializeLocalDocumentRegistry()
315
  print " createmodule < PropertySheet and Document imported\n"
Kevin Deldycke's avatar
Kevin Deldycke committed
316 317 318
  
  
  
319 320 321 322 323 324 325 326 327 328

  # OBJECT PORTAL TYPE
  # create PortalType for the object
  ManageModule.setObjectPortalType(portal_types,
                                   object_portal_type_id,
                                   object_portal_type,
                                   object_names
                                   )

                                   
Kevin Deldycke's avatar
Kevin Deldycke committed
329 330
  
  # Finally add the module to the site
331 332 333 334
  ManageModule.registerModule(portal,
                              module_id,
                              module_portal_type,
                              object_portal_type)
Kevin Deldycke's avatar
Kevin Deldycke committed
335

336 337

  # manage redirection URL
Kevin Deldycke's avatar
Kevin Deldycke committed
338 339 340 341 342 343 344 345 346 347 348 349
  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.'
                            )

350
  # redirect
351
  portal.REQUEST.RESPONSE.redirect(redirect_url )