ERP5Site_createModuleScribus.py 13.3 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 5
        import_pdf_file=None, option_html=None, import_image_1=None, import_image_2=None,
        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 31
  # CREATING MODULES INSTANCES
  ScribusParser = ScribusParser()
  ManageModule = ManageModule()
  ManageFiles = ManageFiles()
  ManageCSS = ManageCSS()
  generator = LocalGenerator()

  import pdb
  pdb.set_trace()
Kevin Deldycke's avatar
Kevin Deldycke committed
32

33 34 35 36 37 38 39
  # DECLARING VARIABLES
  def_lineNumberInList = 20
  def_colorRequired = 'rgb(192,192,255)'
  def_colorRequiredError = 'rgb(128,128,255)'
  def_color = '#F6FFFF'
  def_colorError = 'rgb(255,64,64)'
  
Kevin Deldycke's avatar
Kevin Deldycke committed
40

41 42

  # recovering objects
Kevin Deldycke's avatar
Kevin Deldycke committed
43 44 45 46
  portal = context.getPortalObject()
  portal_types = portal.portal_types
  object_portal_type_id = object_portal_type

47

48 49 50 51 52
  # 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)
53 54
  
  
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  # 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)
      
      
  # 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
75 76
  factory = skin_folder.manage_addProduct['ERP5Form']
  
77 78 79 80
  # 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
81
  
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  # 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()

Kevin Deldycke's avatar
Kevin Deldycke committed
100
  
101 102 103 104 105 106 107 108 109
  # 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
Kevin Deldycke's avatar
Kevin Deldycke committed
110 111


112 113 114
  # import scribus file
  # take the input ScribusFile and read the content
  xml_string = ScribusParser.getContentFile(import_scribus_file)
115
  
Kevin Deldycke's avatar
Kevin Deldycke committed
116
  if xml_string == None:
117 118
    LOG("ScribusParser",1,"Scribus file is empty !")
    print "no field was defined in the Scribus file"
Kevin Deldycke's avatar
Kevin Deldycke committed
119 120
    pass
  else:
121 122 123
    
    # GETTING FULL SCRIBUS DOCUMENT PROPERTIES
    # get string from ScribusFile content
Kevin Deldycke's avatar
Kevin Deldycke committed
124
    output_string = str(xml_string)
125
    
Kevin Deldycke's avatar
Kevin Deldycke committed
126
    
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    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.
    widget_properties = ScribusParser.getPropertiesConversionDict(text_field_list)
    print " createmodule < ScribusParser.getPropertiesConversionDict\n"
    

    # 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
      image_size = ManageFiles.setBackgroundPictures(import_pdf_file,
                                        object_names,
                                        skin_folder)
      page_height, page_width = image_size
      print "   height = " + str(page_height)
      print "   width = " + str(page_width)
      print " createmodule < background generated\n"    
      
      
Kevin Deldycke's avatar
Kevin Deldycke committed
167
    # add field from OrderedWidgetProperties in ERP5 Module created
168
    # radiofield_widget_properties = {}
Kevin Deldycke's avatar
Kevin Deldycke committed
169
    position = {}
170
    # personal_properties_list is used to create PropertySheet
Kevin Deldycke's avatar
Kevin Deldycke committed
171
    personal_properties_list = []
172 173 174 175 176 177 178 179 180 181 182 183
    # recovering number of pages
    global_properties['page'] = len(widget_properties)  
    
    
    
    # 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()

       
184
    
185
    # BEGINING DATA INTERPRETATION
186 187 188
    LOG("ScribusParser",0,"begining interpretation of data")
    print " createmodule > begining data interpretation"
    #iterating pages
189 190
    #print "   %s" % int(global_properties['page'])
    for page_iterator in range(int(global_properties['page'])):
191 192 193 194
      page_number = str(page_iterator)
      page_content = widget_properties[page_number]
      page_id = "page_" + page_number
      
195 196
      
      
197
      if option_html == 1:
198 199 200 201 202 203 204 205 206 207 208
        # 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
        properties_css_dict = ManageCSS.setPageProperties(
                                   properties_css_dict,
                                   page_iterator,
                                   page_id,
                                   page_height)
        
       
Kevin Deldycke's avatar
Kevin Deldycke committed
209
      
210 211 212 213
          
          
      # RESUME DATA INTERPRETATION
      # iterating pageobjects in page
214 215
      for index in range(len(page_content)):
        (id, properties_field) = page_content[index]
216
        # testing each page_content              
217 218
        if properties_field.has_key('type'):
          
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
          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,
					   page_height,
					   page_iterator,
					   page_gap,
					   keep_page)

          # recover useful page_object attributes from scribus dict
          # to be able to create and update correctly the fields
          # composing the form
          ScribusParser.getFieldAttributes(page_content[index],
                                           option_html,
                                           page_id,
                                           global_properties)
240
          
241
    # CSS CLASS (generateOutputContent)
242 243
    if option_html ==1:

244 245 246
      # 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
247

248 249
      # generate output string from dict
      form_css_content = ManageCSS.generateOutputContent(properties_css_dict)
250

251 252
      # save CSS string content into ERP5
      ManageFiles.setCSSFile(factory,object_names['css'],form_css_content)
Kevin Deldycke's avatar
Kevin Deldycke committed
253

254 255 256



257 258 259 260 261 262 263
  # 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)
264

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  # 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
  ManageModule.setFieldsInObjectForm(skin_folder,
                                     object_names,
                                     default_groups,
                                     global_properties,
                                     option_html
                                     )
  print " createmodue < fields created in ERP5 Form\n"
282

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
                                     
                                     
  # 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"
  
298 299


300
  # PROPERTYSHEET AND DOCUMENT CREATION
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
Kevin Deldycke's avatar
Kevin Deldycke committed
351
  portal.REQUEST.RESPONSE.redirect( redirect_url )