def example_random_forest_function(self, active_process_path): digits = load_digits() X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, random_state=0) # Create an active process active_process = self.portal_activities.unrestrictedTraverse( active_process_path) # Use CMFActivity as a backend for joblib with parallel_backend('CMFActivity', n_jobs=2, active_process=active_process): final_model = grow_ensemble(RandomForestClassifier(), X_train, y_train, n_estimators=10, n_jobs=2, random_state=42) score = final_model.score(X_test, y_test) # Set result value and an id to the active result and post it result = ActiveResult(result=score, signature=123) active_process.postResult(result) log('ok', len(final_model.estimators_)) return 'ok', len(final_model.estimators_), score
def getMovementTitle(movement): title = movement.getTitle() parent_value = movement.getParentValue() while parent_value.getPortalType() not in order_type_list: title = parent_value.getTitle() + ' / ' + title log('parent_value', parent_value) parent_value = parent_value.getParentValue() return title
def getFieldRawProperties(field, meta_type=None, key=None, key_prefix=None): """ Return the raw properties of the field """ if meta_type is None: meta_type = field.meta_type if key is None: key = field.generate_field_key(key_prefix=key_prefix) if meta_type == "ProxyField": meta_type = field.getRecursiveTemplateField().meta_type result = { "type": meta_type, "key": key, "values": {}, "overrides": field.overrides, "message_values": field.message_values } # these patchs change the field property names as are required by js rendering form_list_patch = False gadget_field_patch = False for key in field.values.keys(): # sometimes, field.values returns a key as string and also as a tuple if isinstance(key, str): result["values"][key] = field.values[key] if key == "columns": form_list_patch = True if key == "gadget_url": gadget_field_patch = True if form_list_patch: try: result["values"]["column_list"] = result["values"]["columns"] result["values"]["sort_column_list"] = result["values"][ "sort_columns"] result["values"]["search_column_list"] = result["values"][ "search_columns"] portal_type = result["values"]["portal_types"][0][ 0] if "portal_types" in result["values"] else False if not portal_type: portal_type = result["values"]["portal_type"][0][ 0] if "portal_type" in result["values"] else False query = "portal_type%3A%22" + portal_type + "%22" if portal_type else "" full_query = "urn:jio:allDocs?query=" + query result["values"]["query"] = full_query except KeyError: log("error while patching form list definition") if gadget_field_patch: try: result["values"]["url"] = result["values"]["gadget_url"] result["values"]["renderjs_extra"] = result["values"][ "renderjs_extra"][0][0] except (ValueError, KeyError, IndexError): log("error while patching form gadget list definition") return result
def example_simple_function(self, active_process_path): """ simple function to calculate sqrt """ active_process = self.portal_activities.unrestrictedTraverse( active_process_path) # Use CMFActivity as a backend for joblob with parallel_backend('CMFActivity', active_process=active_process): result = Parallel(n_jobs=2, pre_dispatch='all', timeout=30, verbose=30)(delayed(sqrt)(i**2) for i in range(5)) # Set result value and an id to the active result and post it result = ActiveResult(result=result) active_process.postResult(result) log("joblib activity result", result) return result
"""Compatibility script for old portal type Base_printPdf actions. """ from erp5.component.module.Log import log portal = context.getPortalObject() log('Deprecated Base_printPdf action called on a %s. ' 'Remove this action to use global print action.' % context.getPortalType()) portal.changeSkin('Print') return getattr(context, form_id)()
document = sci['object'] portal = document.getPortalObject() translateString = portal.Base_translateString # Get the owner owner = document.getViewPermissionOwner() owner_value = portal.Base_getUserValueByUserId(owner) # Get the authenticated user user_value = portal.portal_membership.getAuthenticatedMember().getUserValue() # If users are not defined, we need to log and return if not owner or owner_value is None: # We keep a trace because this is the best we # can do (preventing answers is even worse) log("ERP5 Query Workflow", "No owner defined") return if user_value is None: # We keep a trace because this is the best we # can do (preventing answers is even worse) log("ERP5 Query Workflow", "Current user is not defined") return # Build the message and translate it subject = translateString("Query was answered.") msg = """The Query ID ${id} which you posted has been answered by ${user} Question: ${question}
'trade': ('internal_packing_list_module', 'inventory_module', 'purchase_order_module', 'purchase_packing_list_module', 'purchase_trade_condition_module', 'returned_sale_packing_list_module', 'sale_order_module', 'sale_packing_list_module', 'sale_trade_condition_module'), 'pdm': ( 'component_module', 'product_module', 'purchase_supply_module', 'sale_supply_module', 'service_module', 'transformation_module', ), } for business_application_category_id, module_ids in module_business_application_map.items( ): for module_id in module_ids: module = getattr(portal, module_id, None) if module is not None: module.edit(business_application=business_application_category_id) print "Indexing translations" portal.ERP5Site_updateTranslationTable() # clear cache so user security is recalculated portal.portal_caches.clearAllCache() print "Clear cache." log("%s" % printed)
field_page_navigation_template = 'ListBox_viewSliderPageNavigationRenderer', field_list_action = 'list', field_enabled = 'checked', field_editable = '', field_page_template = '', field_external_validator = '', field_untranslatable_columns = '', field_hide_rows_on_no_search_criterion = '', field_style_columns = '', ) d.update(context.REQUEST) d.update(kw) # If the listbox is a proxy field, replace it with just a listbox # before it was always failing on "form_id was not transferred" if field.meta_type == "ProxyField": form = field.aq_parent field_id = field.getId() field_title = field.getTitle() log("Replacing ProxyField with a Listbox for {!s}.{!s}".format(form, context)) form.manage_delObjects(field_id) form.manage_addField(field_id, field_title, 'ListBox') field = getattr(form, 'listbox') #context.log('ListBox_setPropertyList', 'kw = %r, d = %r' % (kw, d,)) r = field.form.validate(d) field.manage_edit_xmlrpc(r) return 'Set Successfully.'
portal.portal_workflow.doActionFor(context, doaction_param_list['workflow_action'], **doaction_param_list) except ValidationFailed, error_message: if getattr(error_message, 'msg', None): # use of Message class to store message+mapping+domain message = error_message.msg if same_type(message, []): message = '. '.join('%s' % x for x in message) else: message = str(message) else: message = str(error_message) if len(message) > 2000: # too long message will generate a too long URI # that would become an error. log("Status message has been truncated") message = "%s ..." % message[:(2000 - 4)] except WorkflowException as error_message: if str(error_message) == "No workflow provides the '${action_id}' action.": message = translateString( "Workflow state may have been updated by other user. Please try again." ) return context.Base_redirect( form_id, keep_items={'portal_status_message': message}, **kw) else: raise else: message = request.get('portal_status_message') if message is None: message = translateString('Status changed.') kw.clear() # useful ?
from erp5.component.module.Log import log log('Depracated usage of Item_getQuantityUnit, please use Item_getQuantityUnitItemList instead' ) return context.Item_getQuantityUnitItemList()
translate = context.Base_translateString current_type = context.getPortalType() file_name = file.filename # we check for appropriate file type (by extension) # ContributionTool_getCandidateTypeListByExtension script returns a tuple of # one or more possible portal types for given extension # we accept or suggest appropriate portal type ext = file_name[file_name.rfind('.') + 1:] candidate_type_list = context.ContributionTool_getCandidateTypeListByExtension( ext) if not candidate_type_list and current_type != 'File': log("Document {!s} does not support extension {!s}. Use generic 'File' document." .format(current_type, ext), level=WARNING) return context.Base_redirect( dialog_id, keep_items={ 'portal_status_message': translate( "Current document does not support ${ext} file extension.", mapping={'ext': ext}), 'cancel_url': cancel_url }) if candidate_type_list and current_type not in candidate_type_list: log("File extension {!s} is supported only by {!s}, not by current document {!s}." .format(ext, candidate_type_list, current_type),
from erp5.component.module.Log import log after_script_id = context.getResourceValue().getConfigurationAfterScriptId() after_script = getattr(context, after_script_id, None) if after_script is not None: return after_script() log("After script not found for %s." % context.getRelativeUrl())
from erp5.component.module.Log import log log("Folder method received dialog_id, form_id, uids and {!s}".format( kwargs.keys())) message = "First submission." if kwargs.get("update_method", ""): return context.Base_renderForm(dialog_id, message="Updated. " + message) if donothing_confirmation == 0: # Here is an example of an adversary Script which hijacks `keep_items` # It should take keep_items from parameters, update it and pass it # through. But no programmer will ever comply therefor we are ready! return context.Base_renderForm(dialog_id, message="Submit again to confirm. " + message, level='warning', keep_items={'donothing_confirmation': 1}) return context.Base_redirect(form_id, keep_items={"portal_status_message": message})
from Products.PythonScripts.standard import newline_to_br from erp5.component.module.Log import log log("Event_getTextContentCorrespondToFormat is deprecated, use Event_getEditorFieldTextContent instead", level=100) # WARNING content_type = context.getContentType() if content_type == 'text/html' and context.hasFile(): return context.asStrippedHTML() else: value = context.getTextContent() if editable: return value else: return newline_to_br(value or "")
from Products.PythonScripts.standard import html_quote result = '' error_message = None try: result = context.asStrippedHTML() if result: return result if not context.hasBaseData(): error_message = context.Base_translateString( "This document is not converted yet.") except Exception, e: from erp5.component.module.Log import log log("asStrippedHTML", str(e)) error_message = "%s %s" % (context.Base_translateString("Preview Error:"), str(e)) if error_message is not None: return '<div class="error">%s</div>' % html_quote(error_message) return result
from erp5.component.module.Log import log from Products.ZSQLCatalog.SQLCatalog import Query # warn by logging (not possible use python's warn module in restricted environment) log("'quick_search_text' and 'advanced_search_text' scriptable keys are deprecated. Use 'search_text' instead." ) return Query(search_text=value)
from erp5.component.module.Log import log # this script is no longer needed. log('Category_getSortedCategoryChildValueList', 'use getCategoryChildValueList method') value_list = [ o for o in context.getCategoryChildValueList() if o.getRelativeUrl() != context.getRelativeUrl() ] sort_id = 'int_index' value_list.sort(key=lambda x: x.getProperty(sort_id)) return value_list
""" This script will setup the default ERP5's configuration as saved in erp5_scalability_test business template. As this modifies your site care must be taken! """ from erp5.component.module.Log import log portal = context.getPortalObject() configurator = getattr(portal.business_configuration_module, "default_standard_configuration", None) if configurator is None: log("Could not find the scalability business configuration object. Be sure to have erp5_scalability_test business template installed.") return if not portal.ERP5Site_isReady(): # nothing installed, thus do it log("START auto-configuration for ERP5's default configuration.") context.ERP5Site_bootstrapScalabilityTest(user_quantity=0, setup_activity_tool=False, create_test_data=False, set_id_generator=False) else: log("All configured. Nothing to do.")
if field_id.startswith("my_") or field_id.startswith("your_"): _, field_name = field_id.split('_', 1) if hasattr(field_value, 'as_dict'): # This is an encapsulated editor - convert it kw.update(field_value.as_dict()) else: kw[field_name] = request_form[field_name] = field_value else: kw[field_id] = request_form[field_id] = field_value if len(listbox_id_list): # Warn if there are more than one listbox in form ... if len(listbox_id_list) > 1: log('Base_callDialogMethod', 'There are %s listboxes in form %s.' % (len(listbox_id_list), form.id)) # ... but handle them anyway. for listbox_id in listbox_id_list: listbox_line_list = [] listbox = kw[listbox_id] listbox_keys = listbox.keys() listbox_keys.sort() for key in listbox_keys: listbox_line = listbox[key] listbox_line['listbox_key'] = key listbox_line_list.append(listbox_line) listbox_line_list = tuple(listbox_line_list) kw[listbox_id] = request_form[listbox_id] = listbox_line_list # Handle selection the new way # We expect selection-related parameters (query, uids...) in extra_param_json
""" from erp5.component.module.Log import log req = context.REQUEST # check if everything was filled fields = ('email', 'email_repeated', 'group', 'function', 'site', 'first_name', 'last_name') missing_string = '' kwargs = {} for f in fields: value = req.get('your_' + f) if not value: missing_string += '&missing:list=' + f else: kwargs[f] = value returned_params = '&'.join([f+'='+kwargs[f] for f in kwargs]) log(returned_params) if missing_string: params = returned_params + '&' + missing_string + '&portal_status_message=You did not fill all the required fields. See below to find out which fields still have to be filled.' log(params) return req.RESPONSE.redirect(context.absolute_url()+'?'+params) if req.get('your_email') != req.get('your_email_repeated'): missing_string += '&missing:list=email&missing:list=email_repeated' params = returned_params + '&' + missing_string + '&portal_status_message=You entered two different email addresses. Please make sure to enter the same email address in the fields Email Address and Repeat Email Address.' return req.RESPONSE.redirect(context.absolute_url()+'?'+params) # create a user log(kwargs) try: user = context.WebSite_createUser(**kwargs) log(user) msg = 'Thank you for registering. Your password will be sent to the email address that you provided once your account has been validated by the appropriate department.'
if field.meta_type == "ListBox": return True elif field.meta_type == "ProxyField": template_field = field.getRecursiveTemplateField() if template_field.meta_type == "ListBox": return True return False if form_or_id is None: form = context elif isinstance(form_or_id, str): try: form = getattr(context, form_or_id) except AttributeError: log("Form '{}' does not exist!", level=ERROR) return None else: form = form_or_id if form.meta_type not in ('ERP5 Form', 'Folder', 'ERP5 Folder'): raise RuntimeError( "Cannot get Listbox field from \"{!s}\"! Supported is only ERP5 Form and (ERP5) Folder" .format(form.meta_type)) listbox = None if "Form" in form.meta_type and form.has_field("listbox"): listbox = form.get_field("listbox") elif "Folder" in form.meta_type: listbox = getattr(form, "listbox", None)
""" Security categories assigned from the Person who is set as the destination of the document (e.g. in Memo type). """ from erp5.component.module.Log import log category_list = [] for ob in obj.getDestinationValueList(): category_dict = {} for base_category in base_category_list: if base_category == 'group': category_value = ob.Person_getPrincipalGroup() else: category_value = ob.getProperty(base_category) if category_value not in (None, ''): category_dict[base_category] = category_value else: raise RuntimeError, "Error: '%s' property is required in order to update person security group" % (base_category) category_list.append(category_dict) log(category_list) return category_list
from erp5.component.module.Log import log log('Launching activities to setup the demo configuration!') kw = {} installed_business_template_list = context.portal_templates.getInstalledBusinessTemplateTitleList() if 'erp5_configurator_standard' not in installed_business_template_list: kw = context.Alarm_installBusinessTemplateList() log('Finished to launch the activities to setup the demo configuration!') context.setEnabled(False)
for key in portal_actions_dict: path = "portal_types/%s" % key configuration_path_list.append(base64.b64encode(path)) for action in portal_actions_dict[key]: path = "portal_types/%s/%s" % (key, action) configuration_path_list.append(base64.b64encode(path)) try: action_object = context.restrictedTraverse(path) form = action_object.getActionText().split('/')[-1] path = "portal_skins/%s/%s" % (portal_skin, form) configuration_path_list.append(base64.b64encode(path)) except KeyError as e: raise KeyError("Error getting portal action info: " + str(e)) if new_dialog_form_list: for form in new_dialog_form_list: path = "portal_skins/%s/%s" % (portal_skin, form) configuration_path_list.append(base64.b64encode(path)) url_list = [] for path in configuration_path_list: url_list.append(hateoas_appcache + "/definition_view/" + path) return url_list except (ValueError, KeyError, SyntaxError, AttributeError) as e: if batch_mode == "0": raise e log('ERROR generating Base64 configuration url list: ' + str(e)) return []
# Script to call in action scripts before executig the actual action. from erp5.component.module.Log import log log("ERP5Site_prepare is deprecated, " "use Base_updateListboxSelection instead") # Update checked uids if None not in (selection_name, uids, listbox_uid): context.getPortalObject().portal_selections.updateSelectionCheckedUidList( selection_name, uids=uids, listbox_uid=listbox_uid, REQUEST=context.REQUEST)
from erp5.component.module.Log import log log('DepracationWarning: Please use ContributionTool_getPropertyDictFromFilename' ) return context.ContributionTool_getPropertyDictFromFilename( file_name, property_dict)
from erp5.component.module.Log import log log('Deprecated: use Base_getPreferredSectionItemList instead.') section_cat = context.portal_preferences.getPreferredSectionCategory() if section_cat in (None, '') : section_cat = context.getPortalDefaultSectionCategory() section_cat_obj = None result = [] if section_cat is not None: # get the organisations belonging to this group section_cat_obj = context.portal_categories.resolveCategory(section_cat) if section_cat_obj is not None: result = section_cat_obj.getGroupRelatedValueList(portal_type='Organisation', checked_permission='View') result = [r for r in result if r.getProperty('validation_state') not in ('invalidated', 'deleted')] current_source_section = context.getSourceSectionValue() if current_source_section is not None and current_source_section not in result: result.append(current_source_section) # convert to ListField format return [('', '')] + [(i.getTitle(), i.getRelativeUrl()) for i in result]
from erp5.component.module.Log import log log("ERP5Type_getSecurityCategoryFromArrow is deprecated, " "use ERP5Type_getSecurityCategoryFromContent instead") return context.ERP5Type_getSecurityCategoryFromContent( base_category_list, user_name, ob, portal_type)
from erp5.component.document.Document import ConversionError from erp5.component.module.Log import log from ZODB.POSException import ConflictError from xmlrpclib import Fault from socket import error as SocketError message = None if context.getExternalProcessingState() not in ('converted', 'empty'): # try to convert to base format only if not already done try: return context.convertToBaseFormat() except ConflictError: raise except ConversionError, e: message = 'Conversion Error: %s' % (str(e) or 'undefined.') except Fault, e: message = 'XMLFault: %s' % (repr(e) or 'undefined.') except SocketError, e: message = 'Socket Error: %s' % (repr(e) or 'undefined.') except Exception, e: message = 'Problem: %s' % (repr(e) or 'undefined.') # reach here, then exception was raised, message must be logged in workflow # do not simply raise but rather change external processing state # so user will see something is wrong context.conversionFailed(comment=message) log('%s %s' % (context.getRelativeUrl(), message)) return message
# We look for valid assignments of this user for assignment in person_object.contentValues( filter={'portal_type': 'Assignment'}): category_dict = {} if assignment.getValidationState() == 'open': try: for base_category in base_category_list: if base_category == 'follow_up': category_value = assignment.getDestinationProject() else: category_value = assignment.getProperty(base_category) if category_value not in (None, ''): if root: category_value = category_value.split('/')[0] category_dict[base_category] = category_value else: raise RuntimeError, "Error: '%s' property is required in order to update person security group" % ( base_category) category_list.append(category_dict) # if not strict, we go up the hierarchy (because if you work in group/a/b/c, chances are you # are working in group/a/b, too :) if not strict: grouplist = category_value.split('/') for i in range(1, len(grouplist)): cdict = category_dict.copy() cdict[base_category] = '/'.join(grouplist[:-i]) category_list.append(cdict) except RuntimeError, e: log(str(e)) return category_list